1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.type;
|
21
|
|
22
|
import java.util.Vector;
|
23
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
/**
|
26
|
* A 4-dimensional implementation of the Interpolator class to interpolate between a list
|
27
|
* of Float4Ds.
|
28
|
* Here, the Points are assumed to be Quaternions - thus we do the Spherical Linear Interpolation, aka
|
29
|
* SLERP. Noise not supported (yet?).
|
30
|
*/
|
31
|
|
32
|
public class InterpolatorQuat extends Interpolator
|
33
|
{
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
// omega, sinOmega, cosOmega - angle between pair of quaternions, its sinus and cosinus.
|
37
|
//
|
38
|
// (vx,vy,vz,vw) is the original vector from vv (copied here so when interpolating we can see if it is
|
39
|
// still valid and if not - rebuild the Cache
|
40
|
|
41
|
private class VectorCache
|
42
|
{
|
43
|
float omega, sinOmega,cosOmega;
|
44
|
float vx,vy,vz,vw;
|
45
|
}
|
46
|
|
47
|
private Vector<VectorCache> vc;
|
48
|
private VectorCache tmp1, tmp2;
|
49
|
|
50
|
private Vector<Float4D> vv;
|
51
|
private Float4D curr, next;
|
52
|
|
53
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
//Abramowitz / Stegun
|
55
|
|
56
|
private static float arcCos(float x)
|
57
|
{
|
58
|
if( x<0 )
|
59
|
return 3.14159265358979f - (float)Math.sqrt(1+x)*(1.5707288f + 0.2121144f*x + 0.074261f*x*x + 0.0187293f*x*x*x);
|
60
|
|
61
|
return (float)Math.sqrt(1-x)*(1.5707288f - 0.2121144f*x + 0.074261f*x*x - 0.0187293f*x*x*x);
|
62
|
}
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
// Quaternion Interpolator doesn't support noise
|
66
|
|
67
|
synchronized void createNoise()
|
68
|
{
|
69
|
|
70
|
}
|
71
|
|
72
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73
|
|
74
|
private void recomputeCache()
|
75
|
{
|
76
|
if( numPoints>=2 )
|
77
|
{
|
78
|
int i, n;
|
79
|
|
80
|
for(i=0; i<numPoints; i++)
|
81
|
{
|
82
|
n = i<numPoints-1 ? i+1:0;
|
83
|
|
84
|
tmp1= vc.elementAt(i);
|
85
|
tmp2= vc.elementAt(n);
|
86
|
curr= vv.elementAt(i);
|
87
|
next= vv.elementAt(n);
|
88
|
|
89
|
tmp1.vx = curr.x;
|
90
|
tmp1.vy = curr.y;
|
91
|
tmp1.vz = curr.z;
|
92
|
tmp1.vw = curr.w;
|
93
|
|
94
|
tmp1.cosOmega = curr.x*next.x + curr.y*next.y + curr.z*next.z + curr.w*next.w;
|
95
|
|
96
|
if( tmp1.cosOmega<0 && n!=0 ) // do not invert the last quaternion even if we'd have to go the long way around!
|
97
|
{
|
98
|
tmp1.cosOmega = -tmp1.cosOmega;
|
99
|
next.x = -next.x;
|
100
|
next.y = -next.y;
|
101
|
next.z = -next.z;
|
102
|
next.w = -next.w;
|
103
|
}
|
104
|
|
105
|
tmp1.sinOmega = (float)Math.sqrt(1-tmp1.cosOmega*tmp1.cosOmega);
|
106
|
tmp1.omega = arcCos(tmp1.cosOmega);
|
107
|
}
|
108
|
}
|
109
|
|
110
|
cacheDirty = false;
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
// PUBLIC API
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
/**
|
117
|
* Default constructor.
|
118
|
*/
|
119
|
public InterpolatorQuat()
|
120
|
{
|
121
|
vv = new Vector<Float4D>();
|
122
|
vc = new Vector<VectorCache>();
|
123
|
numPoints = 0;
|
124
|
cacheDirty = false;
|
125
|
mMode = MODE_LOOP;
|
126
|
mDuration = 0;
|
127
|
mCount = 0.5f;
|
128
|
mNoise = 0.0f;
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
/**
|
133
|
* Returns the location'th Float4D.
|
134
|
*
|
135
|
* @param location the index of the Point we are interested in.
|
136
|
* @return The Float4D, if 0<=location<getNumPoints(), or null otherwise.
|
137
|
*/
|
138
|
public synchronized Float4D getPoint(int location)
|
139
|
{
|
140
|
return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
/**
|
145
|
* Resets the location'th Point.
|
146
|
*
|
147
|
* @param location the index of the Point we are setting.
|
148
|
* @param x New value of its first float.
|
149
|
*/
|
150
|
public synchronized void setPoint(int location, float x, float y, float z, float w)
|
151
|
{
|
152
|
if( location>=0 && location<numPoints )
|
153
|
{
|
154
|
curr = vv.elementAt(location);
|
155
|
|
156
|
if( curr!=null )
|
157
|
{
|
158
|
curr.set(x,y,z,w);
|
159
|
cacheDirty=true;
|
160
|
}
|
161
|
}
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
/**
|
166
|
* Adds a new Float4D to the end of our list of Points to interpolate through.
|
167
|
* <p>
|
168
|
* Only a reference to the Point gets added to the List; this means that one can add a Point
|
169
|
* here, and later on {@link Float4D#set(float,float,float,float)} it to some new value and
|
170
|
* the change will be seamlessly reflected in the interpolated path.
|
171
|
* <p>
|
172
|
* A Point can be added multiple times.
|
173
|
*
|
174
|
* @param v The Point to add.
|
175
|
*/
|
176
|
public synchronized void add(Float4D v)
|
177
|
{
|
178
|
if( v!=null )
|
179
|
{
|
180
|
vv.add(v);
|
181
|
|
182
|
switch(numPoints)
|
183
|
{
|
184
|
case 0:
|
185
|
case 1: vc.add(new VectorCache());
|
186
|
vc.add(new VectorCache());
|
187
|
break;
|
188
|
default:vc.add(new VectorCache());
|
189
|
}
|
190
|
|
191
|
numPoints++;
|
192
|
cacheDirty = true;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
/**
|
198
|
* Adds a new Float4D to the location'th place in our List of Points to interpolate through.
|
199
|
*
|
200
|
* @param location Index in our List to add the new Point at.
|
201
|
* @param v The Float4D to add.
|
202
|
*/
|
203
|
public synchronized void add(int location, Float4D v)
|
204
|
{
|
205
|
if( v!=null )
|
206
|
{
|
207
|
vv.add(location, v);
|
208
|
|
209
|
switch(numPoints)
|
210
|
{
|
211
|
case 0:
|
212
|
case 1: vc.add(new VectorCache());
|
213
|
vc.add(new VectorCache());
|
214
|
break;
|
215
|
default:vc.add(location,new VectorCache());
|
216
|
}
|
217
|
|
218
|
numPoints++;
|
219
|
cacheDirty = true;
|
220
|
}
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
/**
|
225
|
* Removes all occurrences of Point v from the List of Points to interpolate through.
|
226
|
*
|
227
|
* @param v The Point to remove.
|
228
|
* @return <code>true</code> if we have removed at least one Point.
|
229
|
*/
|
230
|
public synchronized boolean remove(Float4D v)
|
231
|
{
|
232
|
int n = vv.indexOf(v);
|
233
|
boolean found = false;
|
234
|
|
235
|
while( n>=0 )
|
236
|
{
|
237
|
vv.remove(n);
|
238
|
|
239
|
switch(numPoints)
|
240
|
{
|
241
|
case 0:
|
242
|
case 1: break;
|
243
|
case 2: vc.removeAllElements();
|
244
|
break;
|
245
|
default:vc.remove(n);
|
246
|
}
|
247
|
|
248
|
numPoints--;
|
249
|
found = true;
|
250
|
n = vv.indexOf(v);
|
251
|
}
|
252
|
|
253
|
if( found )
|
254
|
{
|
255
|
cacheDirty=true;
|
256
|
}
|
257
|
|
258
|
return found;
|
259
|
}
|
260
|
|
261
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
262
|
/**
|
263
|
* Removes a location'th Point from the List of Points we interpolate through.
|
264
|
*
|
265
|
* @param location index of the Point we want to remove.
|
266
|
* @return <code>true</code> if location is valid, i.e. if 0<=location<getNumPoints().
|
267
|
*/
|
268
|
public synchronized boolean remove(int location)
|
269
|
{
|
270
|
if( location>=0 && location<numPoints )
|
271
|
{
|
272
|
vv.removeElementAt(location);
|
273
|
|
274
|
switch(numPoints)
|
275
|
{
|
276
|
case 0:
|
277
|
case 1: break;
|
278
|
case 2: vc.removeAllElements();
|
279
|
break;
|
280
|
default:vc.removeElementAt(location);
|
281
|
}
|
282
|
|
283
|
numPoints--;
|
284
|
cacheDirty = true;
|
285
|
return true;
|
286
|
}
|
287
|
|
288
|
return false;
|
289
|
}
|
290
|
|
291
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
292
|
/**
|
293
|
* Removes all Points.
|
294
|
*/
|
295
|
public synchronized void removeAll()
|
296
|
{
|
297
|
numPoints = 0;
|
298
|
vv.removeAllElements();
|
299
|
vc.removeAllElements();
|
300
|
cacheDirty = false;
|
301
|
}
|
302
|
|
303
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304
|
/**
|
305
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
306
|
* Interpolation is done using the spherical linear algorithm, aka SLERP.
|
307
|
* <p>
|
308
|
* Since this is a 4-dimensional Interpolator, the resulting interpolated Float4D gets written
|
309
|
* to four locations in the buffer: buffer[offset], buffer[offset+1], buffer[offset+2] and buffer[offset+3].
|
310
|
*
|
311
|
* @param buffer Float buffer we will write the resulting Float4D to.
|
312
|
* @param offset Offset in the buffer where to write the result.
|
313
|
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
|
314
|
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
|
315
|
*/
|
316
|
public synchronized void interpolate(float[] buffer, int offset, float time)
|
317
|
{
|
318
|
switch(numPoints)
|
319
|
{
|
320
|
case 0: buffer[offset ] = 0.0f;
|
321
|
buffer[offset+1] = 0.0f;
|
322
|
buffer[offset+2] = 0.0f;
|
323
|
buffer[offset+3] = 0.0f;
|
324
|
break;
|
325
|
case 1: curr = vv.elementAt(0);
|
326
|
buffer[offset ] = curr.x;
|
327
|
buffer[offset+1] = curr.y;
|
328
|
buffer[offset+2] = curr.z;
|
329
|
buffer[offset+3] = curr.w;
|
330
|
break;
|
331
|
default:float t = time;
|
332
|
float scale0, scale1;
|
333
|
|
334
|
if( mMode==MODE_JUMP ) time = time*(numPoints-1);
|
335
|
else if( mMode==MODE_PATH || numPoints==2 ) time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
|
336
|
else time = time*numPoints;
|
337
|
|
338
|
int vecNext, vecCurr = (int)time;
|
339
|
time = time-vecCurr;
|
340
|
|
341
|
if( vecCurr>=0 && vecCurr<numPoints )
|
342
|
{
|
343
|
if( cacheDirty ) recomputeCache(); // recompute cache if we have added or remove vectors since last computation
|
344
|
|
345
|
switch(mMode)
|
346
|
{
|
347
|
case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1;
|
348
|
break;
|
349
|
case MODE_PATH: if( t<0.5f ) vecNext = vecCurr+1;
|
350
|
else vecNext = vecCurr==0 ? 1 : vecCurr-1;
|
351
|
break;
|
352
|
case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
|
353
|
break;
|
354
|
default : vecNext = 0;
|
355
|
}
|
356
|
|
357
|
curr = vv.elementAt(vecCurr);
|
358
|
next = vv.elementAt(vecNext);
|
359
|
tmp1 = vc.elementAt(vecCurr);
|
360
|
tmp2 = vc.elementAt(vecNext);
|
361
|
|
362
|
if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z || tmp2.vw!=next.w ) recomputeCache();
|
363
|
|
364
|
if( tmp1.sinOmega==0 )
|
365
|
{
|
366
|
scale0 = 0f;
|
367
|
scale1 = 1f;
|
368
|
}
|
369
|
else if( tmp1.cosOmega < 0.99 )
|
370
|
{
|
371
|
scale0 = (float)Math.sin( (1f-time)*tmp1.omega ) / tmp1.sinOmega;
|
372
|
scale1 = (float)Math.sin( time *tmp1.omega ) / tmp1.sinOmega;
|
373
|
}
|
374
|
else
|
375
|
{
|
376
|
scale0 = 1f-time;
|
377
|
scale1 = time;
|
378
|
}
|
379
|
|
380
|
buffer[offset ] = scale0*curr.x + scale1*next.x;
|
381
|
buffer[offset+1] = scale0*curr.y + scale1*next.y;
|
382
|
buffer[offset+2] = scale0*curr.z + scale1*next.z;
|
383
|
buffer[offset+3] = scale0*curr.w + scale1*next.w;
|
384
|
|
385
|
break;
|
386
|
}
|
387
|
}
|
388
|
}
|
389
|
|
390
|
}
|
391
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
392
|
//
|