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 Dynamic class to interpolate between a list
|
27
|
* of Static4Ds.
|
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
|
* Only unit quaternions represent valid rotations in 3D - and interpolating through rotations is the
|
32
|
* most common use case for this class. No effort is done to normalize the Points though.
|
33
|
*
|
34
|
* Rotation Quaternion is assumed to be in the form ( axisX*sinT, axisY*sinT, axisZ*sinT, cosT ).
|
35
|
*/
|
36
|
|
37
|
public class DynamicQuat extends Dynamic implements Data4D
|
38
|
{
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
// Here we implement our own Cache as we need something slightly different.
|
42
|
// omega, sinOmega, cosOmega - angle between pair of quaternions, its sinus and cosinus.
|
43
|
//
|
44
|
// (vx,vy,vz,vw) is the original vector from vv (copied here so when interpolating we can see if it is
|
45
|
// still valid and if not - rebuild the Cache.
|
46
|
|
47
|
private class VectorCacheQuat
|
48
|
{
|
49
|
float omega, sinOmega,cosOmega;
|
50
|
float vx,vy,vz,vw;
|
51
|
}
|
52
|
|
53
|
private final Vector<VectorCacheQuat> vc;
|
54
|
private VectorCacheQuat tmp1, tmp2;
|
55
|
|
56
|
private final Vector<Static4D> vv;
|
57
|
private Static4D curr, next;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
// Abramowitz / Stegun
|
61
|
|
62
|
private static float arcCos(float x)
|
63
|
{
|
64
|
if( x<0 )
|
65
|
return 3.14159265358979f - (float)Math.sqrt(1+x)*(1.5707288f + 0.2121144f*x + 0.074261f*x*x + 0.0187293f*x*x*x);
|
66
|
|
67
|
return (float)Math.sqrt(1-x)*(1.5707288f - 0.2121144f*x + 0.074261f*x*x - 0.0187293f*x*x*x);
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
private void recomputeCache()
|
73
|
{
|
74
|
if( numPoints>=2 )
|
75
|
{
|
76
|
int i, n;
|
77
|
Static4D cu,ne;
|
78
|
VectorCacheQuat vq;
|
79
|
|
80
|
for(i=0; i<numPoints; i++)
|
81
|
{
|
82
|
n = i<numPoints-1 ? i+1:0;
|
83
|
|
84
|
vq= vc.elementAt(i);
|
85
|
cu= vv.elementAt(i);
|
86
|
ne= vv.elementAt(n);
|
87
|
|
88
|
vq.vx = cu.x;
|
89
|
vq.vy = cu.y;
|
90
|
vq.vz = cu.z;
|
91
|
vq.vw = cu.w;
|
92
|
|
93
|
vq.cosOmega = cu.x*ne.x + cu.y*ne.y + cu.z*ne.z + cu.w*ne.w;
|
94
|
vq.sinOmega = (float)Math.sqrt(1-vq.cosOmega*vq.cosOmega);
|
95
|
vq.omega = arcCos(vq.cosOmega);
|
96
|
}
|
97
|
}
|
98
|
|
99
|
cacheDirty = false;
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
// PUBLIC API
|
104
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105
|
/**
|
106
|
* Default constructor.
|
107
|
*/
|
108
|
public DynamicQuat()
|
109
|
{
|
110
|
this(0,0.5f);
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
/**
|
115
|
* Constructor setting the speed of interpolation and the number of revolutions.
|
116
|
*
|
117
|
* What constitutes 'one revolution' depends on the MODE:
|
118
|
* {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
|
119
|
*
|
120
|
* @param duration number of milliseconds it takes to do one revolution.
|
121
|
* @param count number of revolutions we will do. Count<=0 means 'infinite'.
|
122
|
*/
|
123
|
public DynamicQuat(int duration, float count)
|
124
|
{
|
125
|
vv = new Vector<>();
|
126
|
vc = new Vector<>();
|
127
|
numPoints = 0;
|
128
|
cacheDirty = false;
|
129
|
mMode = MODE_LOOP;
|
130
|
mDuration = duration;
|
131
|
mCount = count;
|
132
|
mLastPos =-1;
|
133
|
mAccessType= ACCESS_TYPE_RANDOM;
|
134
|
mDimension = 4;
|
135
|
mSegment =-1;
|
136
|
|
137
|
initDynamic();
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
/**
|
142
|
* Returns the location'th Static4D.
|
143
|
*
|
144
|
* @param location the index of the Point we are interested in.
|
145
|
* @return The Static4D, if 0<=location<getNumPoints(), or null otherwise.
|
146
|
*/
|
147
|
public synchronized Static4D getPoint(int location)
|
148
|
{
|
149
|
return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
/**
|
154
|
* Resets the location'th Point.
|
155
|
* <p>
|
156
|
* Rotation Quaternion is assumed to be in the form ( axisX*sinT, axisY*sinT, axisZ*sinT, cosT ).
|
157
|
*
|
158
|
* @param location the index of the Point we are setting.
|
159
|
* @param x New value of its first float.
|
160
|
*/
|
161
|
public synchronized void setPoint(int location, float x, float y, float z, float w)
|
162
|
{
|
163
|
if( location>=0 && location<numPoints )
|
164
|
{
|
165
|
curr = vv.elementAt(location);
|
166
|
|
167
|
if( curr!=null )
|
168
|
{
|
169
|
curr.set(x,y,z,w);
|
170
|
cacheDirty=true;
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
/**
|
177
|
* Adds a new Static4D to the end of our list of Points to interpolate through.
|
178
|
* <p>
|
179
|
* Only a reference to the Point gets added to the List; this means that one can add a Point
|
180
|
* here, and later on {@link Static4D#set(float,float,float,float)} it to some new value and
|
181
|
* the change will be seamlessly reflected in the interpolated path.
|
182
|
* <p>
|
183
|
* A Point can be added multiple times.
|
184
|
*
|
185
|
* @param v The Point to add.
|
186
|
*/
|
187
|
public synchronized void add(Static4D v)
|
188
|
{
|
189
|
if( v!=null )
|
190
|
{
|
191
|
vv.add(v);
|
192
|
|
193
|
switch(numPoints)
|
194
|
{
|
195
|
case 0:
|
196
|
case 1: vc.add(new VectorCacheQuat());
|
197
|
vc.add(new VectorCacheQuat());
|
198
|
break;
|
199
|
default:vc.add(new VectorCacheQuat());
|
200
|
}
|
201
|
|
202
|
numPoints++;
|
203
|
cacheDirty = true;
|
204
|
}
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
/**
|
209
|
* Adds a new Static4D to the location'th place in our List of Points to interpolate through.
|
210
|
*
|
211
|
* @param location Index in our List to add the new Point at.
|
212
|
* @param v The Static4D to add.
|
213
|
*/
|
214
|
public synchronized void add(int location, Static4D v)
|
215
|
{
|
216
|
if( v!=null )
|
217
|
{
|
218
|
vv.add(location, v);
|
219
|
|
220
|
switch(numPoints)
|
221
|
{
|
222
|
case 0:
|
223
|
case 1: vc.add(new VectorCacheQuat());
|
224
|
vc.add(new VectorCacheQuat());
|
225
|
break;
|
226
|
default:vc.add(location,new VectorCacheQuat());
|
227
|
}
|
228
|
|
229
|
numPoints++;
|
230
|
cacheDirty = true;
|
231
|
}
|
232
|
}
|
233
|
|
234
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235
|
/**
|
236
|
* Removes all occurrences of Point v from the List of Points to interpolate through.
|
237
|
*
|
238
|
* @param v The Point to remove.
|
239
|
* @return <code>true</code> if we have removed at least one Point.
|
240
|
*/
|
241
|
public synchronized boolean remove(Static4D v)
|
242
|
{
|
243
|
int n = vv.indexOf(v);
|
244
|
boolean found = false;
|
245
|
|
246
|
while( n>=0 )
|
247
|
{
|
248
|
vv.remove(n);
|
249
|
|
250
|
switch(numPoints)
|
251
|
{
|
252
|
case 0:
|
253
|
case 1: break;
|
254
|
case 2: vc.removeAllElements();
|
255
|
break;
|
256
|
default:vc.remove(n);
|
257
|
}
|
258
|
|
259
|
numPoints--;
|
260
|
found = true;
|
261
|
n = vv.indexOf(v);
|
262
|
}
|
263
|
|
264
|
if( found )
|
265
|
{
|
266
|
cacheDirty=true;
|
267
|
}
|
268
|
|
269
|
return found;
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
/**
|
274
|
* Removes a location'th Point from the List of Points we interpolate through.
|
275
|
*
|
276
|
* @param location index of the Point we want to remove.
|
277
|
* @return <code>true</code> if location is valid, i.e. if 0<=location<getNumPoints().
|
278
|
*/
|
279
|
public synchronized boolean remove(int location)
|
280
|
{
|
281
|
if( location>=0 && location<numPoints )
|
282
|
{
|
283
|
vv.removeElementAt(location);
|
284
|
|
285
|
switch(numPoints)
|
286
|
{
|
287
|
case 0:
|
288
|
case 1: break;
|
289
|
case 2: vc.removeAllElements();
|
290
|
break;
|
291
|
default:vc.removeElementAt(location);
|
292
|
}
|
293
|
|
294
|
numPoints--;
|
295
|
cacheDirty = true;
|
296
|
return true;
|
297
|
}
|
298
|
|
299
|
return false;
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
/**
|
304
|
* Removes all Points.
|
305
|
*/
|
306
|
public synchronized void removeAll()
|
307
|
{
|
308
|
numPoints = 0;
|
309
|
vv.removeAllElements();
|
310
|
vc.removeAllElements();
|
311
|
cacheDirty = false;
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
/**
|
316
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
317
|
* Interpolation is done using the spherical linear algorithm, aka SLERP.
|
318
|
* <p>
|
319
|
* Since this is a 4-dimensional Dynamic, the resulting interpolated Static4D gets written
|
320
|
* to four locations in the buffer: buffer[offset], buffer[offset+1], buffer[offset+2] and buffer[offset+3].
|
321
|
*
|
322
|
* @param buffer Float buffer we will write the resulting Static4D to.
|
323
|
* @param offset Offset in the buffer where to write the result.
|
324
|
* @param time Time of interpolation. Time=0.0 is the beginning of the first revolution, time=1.0 - the end
|
325
|
* of the first revolution, time=2.5 - the middle of the third revolution.
|
326
|
* What constitutes 'one revolution' depends on the MODE:
|
327
|
* {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
|
328
|
**/
|
329
|
synchronized void interpolate(float[] buffer, int offset, float time)
|
330
|
{
|
331
|
switch(numPoints)
|
332
|
{
|
333
|
case 0: buffer[offset ] = 0.0f;
|
334
|
buffer[offset+1] = 0.0f;
|
335
|
buffer[offset+2] = 0.0f;
|
336
|
buffer[offset+3] = 0.0f;
|
337
|
break;
|
338
|
case 1: curr = vv.elementAt(0);
|
339
|
buffer[offset ] = curr.x;
|
340
|
buffer[offset+1] = curr.y;
|
341
|
buffer[offset+2] = curr.z;
|
342
|
buffer[offset+3] = curr.w;
|
343
|
break;
|
344
|
default:float t = time;
|
345
|
int vecCurr, segment;
|
346
|
float scale0, scale1;
|
347
|
|
348
|
switch(mMode)
|
349
|
{
|
350
|
case MODE_LOOP: time = time*numPoints;
|
351
|
segment = (int)time;
|
352
|
vecCurr = segment;
|
353
|
break;
|
354
|
case MODE_PATH: if( t>0.5f ) t = 1.0f-t;
|
355
|
time = 2*t*(numPoints-1);
|
356
|
segment = (int)(2*t*(numPoints-1));
|
357
|
vecCurr = segment;
|
358
|
break;
|
359
|
case MODE_JUMP: time = time*(numPoints-1);
|
360
|
segment = (int)time;
|
361
|
vecCurr = segment;
|
362
|
break;
|
363
|
default : vecCurr = 0;
|
364
|
segment = 0;
|
365
|
}
|
366
|
|
367
|
if( vecCurr>=0 && vecCurr<numPoints )
|
368
|
{
|
369
|
int vecNext = getNext(vecCurr,t);
|
370
|
|
371
|
curr = vv.elementAt(vecCurr);
|
372
|
tmp1 = vc.elementAt(vecCurr);
|
373
|
next = vv.elementAt(vecNext);
|
374
|
|
375
|
if( cacheDirty ) recomputeCache(); // recompute cache if we have added or remove vectors since last computation
|
376
|
else if( mSegment!= segment ) // ...or if we have just passed a vector and the vector we are currently flying to has changed
|
377
|
{
|
378
|
tmp2 = vc.elementAt(vecNext);
|
379
|
|
380
|
if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z || tmp2.vw!=next.w ) recomputeCache();
|
381
|
}
|
382
|
|
383
|
mSegment = segment;
|
384
|
|
385
|
time = time-vecCurr;
|
386
|
|
387
|
if( tmp1.sinOmega==0 )
|
388
|
{
|
389
|
scale0 = 1f;
|
390
|
scale1 = 0f;
|
391
|
}
|
392
|
else if( tmp1.cosOmega < 0.99 )
|
393
|
{
|
394
|
scale0 = (float)Math.sin( (1f-time)*tmp1.omega ) / tmp1.sinOmega;
|
395
|
scale1 = (float)Math.sin( time *tmp1.omega ) / tmp1.sinOmega;
|
396
|
}
|
397
|
else
|
398
|
{
|
399
|
scale0 = 1f-time;
|
400
|
scale1 = time;
|
401
|
}
|
402
|
|
403
|
buffer[offset ] = scale0*curr.x + scale1*next.x;
|
404
|
buffer[offset+1] = scale0*curr.y + scale1*next.y;
|
405
|
buffer[offset+2] = scale0*curr.z + scale1*next.z;
|
406
|
buffer[offset+3] = scale0*curr.w + scale1*next.w;
|
407
|
|
408
|
break;
|
409
|
}
|
410
|
}
|
411
|
}
|
412
|
|
413
|
}
|