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
|
*/
|
29
|
|
30
|
public class Dynamic4D extends Dynamic implements Data4D
|
31
|
{
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
// the coefficients of the X(t), Y(t), Z(t), W(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx etc.
|
35
|
// (x,y,z,w) is the vector tangent to the path.
|
36
|
// (vx,vy,vz,vw) is the original vector from vv (copied here so when interpolating we can see if it is
|
37
|
// still valid and if not - rebuild the Cache
|
38
|
|
39
|
private class VectorCache
|
40
|
{
|
41
|
float ax, bx, cx, dx;
|
42
|
float ay, by, cy, dy;
|
43
|
float az, bz, cz, dz;
|
44
|
float aw, bw, cw, dw;
|
45
|
|
46
|
float x,y,z,w;
|
47
|
float vx,vy,vz,vw;
|
48
|
}
|
49
|
|
50
|
private Vector<VectorCache> vc;
|
51
|
private VectorCache tmp1, tmp2;
|
52
|
|
53
|
private Vector<Static4D> vv;
|
54
|
private Static4D prev, curr, next;
|
55
|
|
56
|
private float vec1X,vec1Y,vec1Z,vec1W; // vector perpendicular to v(t) and in the same plane as v(t) and a(t) (for >2 points only, in case of 2 points this is calculated differently)
|
57
|
private float vec2X,vec2Y,vec2Z,vec2W; // vector perpendicular to v(t) and to vec1.
|
58
|
private float vec3X,vec3Y,vec3Z,vec3W; // vector perpendicular to v(t) and to vec1.
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
// no array bounds checking!
|
62
|
|
63
|
private void vec(int c)
|
64
|
{
|
65
|
int p = c>0 ? c-1: numPoints-1;
|
66
|
int n = c<numPoints-1 ? c+1: 0;
|
67
|
|
68
|
prev = vv.elementAt(p);
|
69
|
curr = vv.elementAt(c);
|
70
|
next = vv.elementAt(n);
|
71
|
|
72
|
tmp1 = vc.elementAt(c);
|
73
|
|
74
|
float px = curr.x - prev.x;
|
75
|
float py = curr.y - prev.y;
|
76
|
float pz = curr.z - prev.z;
|
77
|
float pw = curr.w - prev.w;
|
78
|
float nx = next.x - curr.x;
|
79
|
float ny = next.y - curr.y;
|
80
|
float nz = next.z - curr.z;
|
81
|
float nw = next.w - curr.w;
|
82
|
|
83
|
float d = nx*nx+ny*ny+nz*nz+nw*nw;
|
84
|
|
85
|
if( d>0 )
|
86
|
{
|
87
|
float q = (float)Math.sqrt((px*px+py*py+pz*pz+pw*pw)/d);
|
88
|
|
89
|
if( q>1 )
|
90
|
{
|
91
|
tmp1.x = nx+px/q;
|
92
|
tmp1.y = ny+py/q;
|
93
|
tmp1.z = nz+pz/q;
|
94
|
tmp1.w = nw+pw/q;
|
95
|
}
|
96
|
else
|
97
|
{
|
98
|
tmp1.x = px+nx*q;
|
99
|
tmp1.y = py+ny*q;
|
100
|
tmp1.z = pz+nz*q;
|
101
|
tmp1.w = pw+nw*q;
|
102
|
}
|
103
|
}
|
104
|
else
|
105
|
{
|
106
|
tmp1.x = 0.0f;
|
107
|
tmp1.y = 0.0f;
|
108
|
tmp1.z = 0.0f;
|
109
|
tmp1.w = 0.0f;
|
110
|
}
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
|
115
|
private void recomputeCache()
|
116
|
{
|
117
|
if( numPoints==1 )
|
118
|
{
|
119
|
tmp1= vc.elementAt(0);
|
120
|
curr= vv.elementAt(0);
|
121
|
|
122
|
tmp1.ax = tmp1.ay = tmp1.az = tmp1.aw = 0.0f;
|
123
|
tmp1.bx = tmp1.by = tmp1.bz = tmp1.bw = 0.0f;
|
124
|
tmp1.cx = curr.x;
|
125
|
tmp1.cy = curr.y;
|
126
|
tmp1.cz = curr.z;
|
127
|
tmp1.cw = curr.w;
|
128
|
tmp1.dx = tmp1.dy = tmp1.dz = tmp1.dw = 0.0f;
|
129
|
}
|
130
|
else if( numPoints==2 )
|
131
|
{
|
132
|
tmp1= vc.elementAt(0);
|
133
|
tmp2= vc.elementAt(1);
|
134
|
curr= vv.elementAt(0);
|
135
|
next= vv.elementAt(1);
|
136
|
|
137
|
tmp1.ax = tmp1.ay = tmp1.az = tmp1.aw = 0.0f;
|
138
|
tmp1.bx = tmp1.by = tmp1.bz = tmp1.bw = 0.0f;
|
139
|
tmp1.cx = next.x - curr.x;
|
140
|
tmp1.cy = next.y - curr.y;
|
141
|
tmp1.cz = next.z - curr.z;
|
142
|
tmp1.cw = next.w - curr.w;
|
143
|
tmp1.dx = curr.x;
|
144
|
tmp1.dy = curr.y;
|
145
|
tmp1.dz = curr.z;
|
146
|
tmp1.dw = curr.w;
|
147
|
|
148
|
tmp2.ax = tmp2.ay = tmp2.az = tmp2.aw = 0.0f;
|
149
|
tmp2.bx = tmp2.by = tmp2.bz = tmp2.bw = 0.0f;
|
150
|
tmp2.cx = curr.x - next.x;
|
151
|
tmp2.cy = curr.y - next.y;
|
152
|
tmp2.cz = curr.z - next.z;
|
153
|
tmp2.cw = curr.w - next.w;
|
154
|
tmp2.dx = next.x;
|
155
|
tmp2.dy = next.y;
|
156
|
tmp2.dz = next.z;
|
157
|
tmp2.dw = next.w;
|
158
|
}
|
159
|
else
|
160
|
{
|
161
|
int i, n;
|
162
|
|
163
|
for(i=0; i<numPoints; i++) vec(i);
|
164
|
|
165
|
for(i=0; i<numPoints; i++)
|
166
|
{
|
167
|
n = i<numPoints-1 ? i+1:0;
|
168
|
|
169
|
tmp1= vc.elementAt(i);
|
170
|
tmp2= vc.elementAt(n);
|
171
|
curr= vv.elementAt(i);
|
172
|
next= vv.elementAt(n);
|
173
|
|
174
|
tmp1.vx = curr.x;
|
175
|
tmp1.vy = curr.y;
|
176
|
tmp1.vz = curr.z;
|
177
|
tmp1.vw = curr.w;
|
178
|
|
179
|
tmp1.ax = 2*curr.x + tmp1.x - 2*next.x + tmp2.x;
|
180
|
tmp1.bx = -3*curr.x - 2*tmp1.x + 3*next.x - tmp2.x;
|
181
|
tmp1.cx = tmp1.x;
|
182
|
tmp1.dx = curr.x;
|
183
|
|
184
|
tmp1.ay = 2*curr.y + tmp1.y - 2*next.y + tmp2.y;
|
185
|
tmp1.by = -3*curr.y - 2*tmp1.y + 3*next.y - tmp2.y;
|
186
|
tmp1.cy = tmp1.y;
|
187
|
tmp1.dy = curr.y;
|
188
|
|
189
|
tmp1.az = 2*curr.z + tmp1.z - 2*next.z + tmp2.z;
|
190
|
tmp1.bz = -3*curr.z - 2*tmp1.z + 3*next.z - tmp2.z;
|
191
|
tmp1.cz = tmp1.z;
|
192
|
tmp1.dz = curr.z;
|
193
|
|
194
|
tmp1.aw = 2*curr.w + tmp1.w - 2*next.w + tmp2.w;
|
195
|
tmp1.bw = -3*curr.w - 2*tmp1.w + 3*next.w - tmp2.w;
|
196
|
tmp1.cw = tmp1.w;
|
197
|
tmp1.dw = curr.w;
|
198
|
}
|
199
|
}
|
200
|
|
201
|
cacheDirty = false;
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
// v is the speed vector (i.e. position p(t) differentiated by time)
|
206
|
// a is the acceleration vector (differentiate once more)
|
207
|
//
|
208
|
// Now we construct orthogonal basis with Gram-Schmidt:
|
209
|
// vec1 = a-delta*v where delta = (v*a)/|v|^2
|
210
|
// vec2 = (0,0,1,0) - coeff1*(vx,vy,vz,vw) - coeff2*(vec1x,vec1y,vec1z,vec1w) where coeff1 = vz/|v|^2, coeff2 = vec1Z/|vec1|^2
|
211
|
// vec3 = (0,0,0,1) - coeff1*(vx,vy,vz,vw) - coeff2*(vec1x,vec1y,vec1z,vec1w) - coeff3*(vec2x,vec2y,vec2z,vec2w) where coeff1 = vw/|v|^2, coeff2 = vec1W/|vec1|^2, coeff3 = vec2W/|vec2|^2
|
212
|
|
213
|
private void setUpVectors(float time,VectorCache vc)
|
214
|
{
|
215
|
if( vc!=null )
|
216
|
{
|
217
|
float vx = (3*vc.ax*time+2*vc.bx)*time+vc.cx;
|
218
|
float vy = (3*vc.ay*time+2*vc.by)*time+vc.cy;
|
219
|
float vz = (3*vc.az*time+2*vc.bz)*time+vc.cz;
|
220
|
float vw = (3*vc.aw*time+2*vc.bw)*time+vc.cw;
|
221
|
|
222
|
float ax = 6*vc.ax*time+2*vc.bx;
|
223
|
float ay = 6*vc.ay*time+2*vc.by;
|
224
|
float az = 6*vc.az*time+2*vc.bz;
|
225
|
float aw = 6*vc.aw*time+2*vc.bw;
|
226
|
|
227
|
float v_sq = vx*vx+vy*vy+vz*vz+vw*vw;
|
228
|
float delta = (vx*ax+vy*ay+vz*az+vw*aw)/v_sq;
|
229
|
|
230
|
vec1X = ax-delta*vx;
|
231
|
vec1Y = ay-delta*vy;
|
232
|
vec1Z = az-delta*vz;
|
233
|
vec1W = aw-delta*vw;
|
234
|
|
235
|
float vec1_sq = vec1X*vec1X+vec1Y*vec1Y+vec1Z*vec1Z+vec1W*vec1W;
|
236
|
|
237
|
// construct vec2 and vec3. Cross product does not work in 4th dimension!
|
238
|
float coeff21 = vz/v_sq;
|
239
|
float coeff22 = vec1Z/vec1_sq;
|
240
|
vec2X = 0.0f - coeff21*vx - coeff22*vec1X;
|
241
|
vec2Y = 0.0f - coeff21*vy - coeff22*vec1Y;
|
242
|
vec2Z = 1.0f - coeff21*vz - coeff22*vec1Z;
|
243
|
vec2W = 0.0f - coeff21*vw - coeff22*vec1W;
|
244
|
|
245
|
float vec2_sq = vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z+vec2W*vec2W;
|
246
|
float coeff31 = vw/v_sq;
|
247
|
float coeff32 = vec1W/vec1_sq;
|
248
|
float coeff33 = vec2W/vec2_sq;
|
249
|
vec3X = 0.0f - coeff31*vx - coeff32*vec1X - coeff33*vec2X;
|
250
|
vec3Y = 0.0f - coeff31*vy - coeff32*vec1Y - coeff33*vec2Y;
|
251
|
vec3Z = 0.0f - coeff31*vz - coeff32*vec1Z - coeff33*vec2Z;
|
252
|
vec3W = 1.0f - coeff31*vw - coeff32*vec1W - coeff33*vec2W;
|
253
|
|
254
|
float vec3_sq = vec3X*vec3X+vec3Y*vec3Y+vec3Z*vec3Z+vec3W*vec3W;
|
255
|
|
256
|
float len1 = (float)Math.sqrt(v_sq/vec1_sq);
|
257
|
float len2 = (float)Math.sqrt(v_sq/vec2_sq);
|
258
|
float len3 = (float)Math.sqrt(v_sq/vec3_sq);
|
259
|
|
260
|
vec1X*=len1;
|
261
|
vec1Y*=len1;
|
262
|
vec1Z*=len1;
|
263
|
vec1W*=len1;
|
264
|
|
265
|
vec2X*=len2;
|
266
|
vec2Y*=len2;
|
267
|
vec2Z*=len2;
|
268
|
vec2W*=len2;
|
269
|
|
270
|
vec3X*=len3;
|
271
|
vec3Y*=len3;
|
272
|
vec3Z*=len3;
|
273
|
vec3W*=len3;
|
274
|
}
|
275
|
else
|
276
|
{
|
277
|
curr = vv.elementAt(0);
|
278
|
next = vv.elementAt(1);
|
279
|
|
280
|
float vx = (next.x-curr.x);
|
281
|
float vy = (next.y-curr.y);
|
282
|
float vz = (next.z-curr.z);
|
283
|
float vw = (next.w-curr.w);
|
284
|
|
285
|
float b = (float)Math.sqrt(vx*vx+vy*vy+vz*vz);
|
286
|
|
287
|
if( b>0.0f )
|
288
|
{
|
289
|
vec1X = vx*vw/b;
|
290
|
vec1Y = vy*vw/b;
|
291
|
vec1Z = vz*vw/b;
|
292
|
vec1W = -b;
|
293
|
|
294
|
float v_sq = vx*vx+vy*vy+vz*vz+vw*vw;
|
295
|
float vec1_sq = vec1X*vec1X+vec1Y*vec1Y+vec1Z*vec1Z+vec1W*vec1W;
|
296
|
|
297
|
// construct vec2 and vec3. Cross product does not work in 4th dimension!
|
298
|
float coeff21 = vz/v_sq;
|
299
|
float coeff22 = vec1Z/vec1_sq;
|
300
|
vec2X = 0.0f - coeff21*vx - coeff22*vec1X;
|
301
|
vec2Y = 0.0f - coeff21*vy - coeff22*vec1Y;
|
302
|
vec2Z = 1.0f - coeff21*vz - coeff22*vec1Z;
|
303
|
vec2W = 0.0f - coeff21*vw - coeff22*vec1W;
|
304
|
|
305
|
float vec2_sq = vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z+vec2W*vec2W;
|
306
|
float coeff31 = vw/v_sq;
|
307
|
float coeff32 = vec1W/vec1_sq;
|
308
|
float coeff33 = vec2W/vec2_sq;
|
309
|
vec3X = 0.0f - coeff31*vx - coeff32*vec1X - coeff33*vec2X;
|
310
|
vec3Y = 0.0f - coeff31*vy - coeff32*vec1Y - coeff33*vec2Y;
|
311
|
vec3Z = 0.0f - coeff31*vz - coeff32*vec1Z - coeff33*vec2Z;
|
312
|
vec3W = 1.0f - coeff31*vw - coeff32*vec1W - coeff33*vec2W;
|
313
|
|
314
|
float vec3_sq = vec3X*vec3X+vec3Y*vec3Y+vec3Z*vec3Z+vec3W*vec3W;
|
315
|
|
316
|
float len1 = (float)Math.sqrt(v_sq/vec1_sq);
|
317
|
float len2 = (float)Math.sqrt(v_sq/vec2_sq);
|
318
|
float len3 = (float)Math.sqrt(v_sq/vec3_sq);
|
319
|
|
320
|
vec1X*=len1;
|
321
|
vec1Y*=len1;
|
322
|
vec1Z*=len1;
|
323
|
vec1W*=len1;
|
324
|
|
325
|
vec2X*=len2;
|
326
|
vec2Y*=len2;
|
327
|
vec2Z*=len2;
|
328
|
vec2W*=len2;
|
329
|
|
330
|
vec3X*=len3;
|
331
|
vec3Y*=len3;
|
332
|
vec3Z*=len3;
|
333
|
vec3W*=len3;
|
334
|
}
|
335
|
else
|
336
|
{
|
337
|
vec1X = vw;
|
338
|
vec1Y = 0.0f;
|
339
|
vec1Z = 0.0f;
|
340
|
vec1W = 0.0f;
|
341
|
|
342
|
vec2X = 0.0f;
|
343
|
vec2Y = vw;
|
344
|
vec2Z = 0.0f;
|
345
|
vec2W = 0.0f;
|
346
|
|
347
|
vec3X = 0.0f;
|
348
|
vec3Y = 0.0f;
|
349
|
vec3Z = vw;
|
350
|
vec3W = 0.0f;
|
351
|
}
|
352
|
}
|
353
|
}
|
354
|
|
355
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
356
|
// PUBLIC API
|
357
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
358
|
/**
|
359
|
* Default constructor.
|
360
|
*/
|
361
|
public Dynamic4D()
|
362
|
{
|
363
|
this(0,0.5f);
|
364
|
}
|
365
|
|
366
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
367
|
|
368
|
/**
|
369
|
* Default constructor.
|
370
|
*
|
371
|
* @param duration number of milliseconds it takes to do a full loop/path from first vector to the
|
372
|
* last and back to the first
|
373
|
* @param count number of loops/paths we will do; mCount = 1.5 means we go from the first vector
|
374
|
* to the last, back to first, and to the last again.
|
375
|
*/
|
376
|
public Dynamic4D(int duration, float count)
|
377
|
{
|
378
|
vv = new Vector<>();
|
379
|
vc = new Vector<>();
|
380
|
vn = null;
|
381
|
numPoints = 0;
|
382
|
cacheDirty = false;
|
383
|
mMode = MODE_LOOP;
|
384
|
mDuration = duration;
|
385
|
mCount = count;
|
386
|
mDimension = 4;
|
387
|
}
|
388
|
|
389
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
390
|
/**
|
391
|
* Returns the location'th Static4D.
|
392
|
*
|
393
|
* @param location the index of the Point we are interested in.
|
394
|
* @return The Static4D, if 0<=location<getNumPoints(), or null otherwise.
|
395
|
*/
|
396
|
public synchronized Static4D getPoint(int location)
|
397
|
{
|
398
|
return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;
|
399
|
}
|
400
|
|
401
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
402
|
/**
|
403
|
* Resets the location'th Point.
|
404
|
*
|
405
|
* @param location the index of the Point we are setting.
|
406
|
* @param x New value of its first float.
|
407
|
*/
|
408
|
public synchronized void setPoint(int location, float x, float y, float z, float w)
|
409
|
{
|
410
|
if( location>=0 && location<numPoints )
|
411
|
{
|
412
|
curr = vv.elementAt(location);
|
413
|
|
414
|
if( curr!=null )
|
415
|
{
|
416
|
curr.set(x,y,z,w);
|
417
|
cacheDirty=true;
|
418
|
}
|
419
|
}
|
420
|
}
|
421
|
|
422
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
423
|
/**
|
424
|
* Adds a new Static4D to the end of our list of Points to interpolate through.
|
425
|
* <p>
|
426
|
* Only a reference to the Point gets added to the List; this means that one can add a Point
|
427
|
* here, and later on {@link Static4D#set(float,float,float,float)} it to some new value and
|
428
|
* the change will be seamlessly reflected in the interpolated path.
|
429
|
* <p>
|
430
|
* A Point can be added multiple times.
|
431
|
*
|
432
|
* @param v The Point to add.
|
433
|
*/
|
434
|
public synchronized void add(Static4D v)
|
435
|
{
|
436
|
if( v!=null )
|
437
|
{
|
438
|
vv.add(v);
|
439
|
|
440
|
if( vn!=null ) vn.add(new VectorNoise(4));
|
441
|
|
442
|
switch(numPoints)
|
443
|
{
|
444
|
case 0: break;
|
445
|
case 1: setUpVectors(0.0f,null);
|
446
|
break;
|
447
|
case 2: vc.add(new VectorCache());
|
448
|
vc.add(new VectorCache());
|
449
|
vc.add(new VectorCache());
|
450
|
break;
|
451
|
default:vc.add(new VectorCache());
|
452
|
}
|
453
|
|
454
|
numPoints++;
|
455
|
cacheDirty = true;
|
456
|
}
|
457
|
}
|
458
|
|
459
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
460
|
/**
|
461
|
* Adds a new Static4D to the location'th place in our List of Points to interpolate through.
|
462
|
*
|
463
|
* @param location Index in our List to add the new Point at.
|
464
|
* @param v The Static4D to add.
|
465
|
*/
|
466
|
public synchronized void add(int location, Static4D v)
|
467
|
{
|
468
|
if( v!=null )
|
469
|
{
|
470
|
vv.add(location, v);
|
471
|
|
472
|
if( vn!=null ) vn.add(new VectorNoise(4));
|
473
|
|
474
|
switch(numPoints)
|
475
|
{
|
476
|
case 0: break;
|
477
|
case 1: setUpVectors(0.0f,null);
|
478
|
break;
|
479
|
case 2: vc.add(new VectorCache());
|
480
|
vc.add(new VectorCache());
|
481
|
vc.add(new VectorCache());
|
482
|
break;
|
483
|
default:vc.add(location,new VectorCache());
|
484
|
}
|
485
|
|
486
|
numPoints++;
|
487
|
cacheDirty = true;
|
488
|
}
|
489
|
}
|
490
|
|
491
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
492
|
/**
|
493
|
* Removes all occurrences of Point v from the List of Points to interpolate through.
|
494
|
*
|
495
|
* @param v The Point to remove.
|
496
|
* @return <code>true</code> if we have removed at least one Point.
|
497
|
*/
|
498
|
public synchronized boolean remove(Static4D v)
|
499
|
{
|
500
|
int n = vv.indexOf(v);
|
501
|
boolean found = false;
|
502
|
|
503
|
while( n>=0 )
|
504
|
{
|
505
|
vv.remove(n);
|
506
|
|
507
|
if( vn!=null ) vn.remove(0);
|
508
|
|
509
|
switch(numPoints)
|
510
|
{
|
511
|
case 0:
|
512
|
case 1:
|
513
|
case 2: break;
|
514
|
case 3: vc.removeAllElements();
|
515
|
setUpVectors(0.0f,null);
|
516
|
break;
|
517
|
default:vc.remove(n);
|
518
|
}
|
519
|
|
520
|
numPoints--;
|
521
|
found = true;
|
522
|
n = vv.indexOf(v);
|
523
|
}
|
524
|
|
525
|
if( found )
|
526
|
{
|
527
|
cacheDirty=true;
|
528
|
}
|
529
|
|
530
|
return found;
|
531
|
}
|
532
|
|
533
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
534
|
/**
|
535
|
* Removes a location'th Point from the List of Points we interpolate through.
|
536
|
*
|
537
|
* @param location index of the Point we want to remove.
|
538
|
* @return <code>true</code> if location is valid, i.e. if 0<=location<getNumPoints().
|
539
|
*/
|
540
|
public synchronized boolean remove(int location)
|
541
|
{
|
542
|
if( location>=0 && location<numPoints )
|
543
|
{
|
544
|
vv.removeElementAt(location);
|
545
|
|
546
|
if( vn!=null ) vn.remove(0);
|
547
|
|
548
|
switch(numPoints)
|
549
|
{
|
550
|
case 0:
|
551
|
case 1:
|
552
|
case 2: break;
|
553
|
case 3: vc.removeAllElements();
|
554
|
setUpVectors(0.0f,null);
|
555
|
break;
|
556
|
default:vc.removeElementAt(location);
|
557
|
}
|
558
|
|
559
|
numPoints--;
|
560
|
cacheDirty = true;
|
561
|
return true;
|
562
|
}
|
563
|
|
564
|
return false;
|
565
|
}
|
566
|
|
567
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
568
|
/**
|
569
|
* Removes all Points.
|
570
|
*/
|
571
|
public synchronized void removeAll()
|
572
|
{
|
573
|
numPoints = 0;
|
574
|
vv.removeAllElements();
|
575
|
vc.removeAllElements();
|
576
|
cacheDirty = false;
|
577
|
|
578
|
if( vn!=null ) vn.removeAllElements();
|
579
|
}
|
580
|
|
581
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
582
|
/**
|
583
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
584
|
* <p>
|
585
|
* Since this is a 4-dimensional Dynamic, the resulting interpolated Static4D gets written
|
586
|
* to four locations in the buffer: buffer[offset], buffer[offset+1], buffer[offset+2] and buffer[offset+3].
|
587
|
*
|
588
|
* @param buffer Float buffer we will write the resulting Static4D to.
|
589
|
* @param offset Offset in the buffer where to write the result.
|
590
|
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
|
591
|
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
|
592
|
*/
|
593
|
public synchronized void interpolate(float[] buffer, int offset, float time)
|
594
|
{
|
595
|
switch(numPoints)
|
596
|
{
|
597
|
case 0: buffer[offset ] = 0.0f;
|
598
|
buffer[offset+1] = 0.0f;
|
599
|
buffer[offset+2] = 0.0f;
|
600
|
buffer[offset+3] = 0.0f;
|
601
|
break;
|
602
|
case 1: curr = vv.elementAt(0);
|
603
|
buffer[offset ] = curr.x;
|
604
|
buffer[offset+1] = curr.y;
|
605
|
buffer[offset+2] = curr.z;
|
606
|
buffer[offset+3] = curr.w;
|
607
|
break;
|
608
|
case 2: curr = vv.elementAt(0);
|
609
|
next = vv.elementAt(1);
|
610
|
|
611
|
if( mMode==MODE_LOOP || mMode==MODE_PATH ) time = (time>0.5f ? 2-2*time : 2*time);
|
612
|
|
613
|
if( vn!=null )
|
614
|
{
|
615
|
time = noise(time,0);
|
616
|
|
617
|
buffer[offset ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor[0] + vec2X*mFactor[1] + vec3X*mFactor[2]);
|
618
|
buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor[0] + vec2Y*mFactor[1] + vec3Y*mFactor[2]);
|
619
|
buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor[0] + vec2Z*mFactor[1] + vec3Z*mFactor[2]);
|
620
|
buffer[offset+3] = (next.w-curr.w)*time + curr.w + (vec1W*mFactor[0] + vec2W*mFactor[1] + vec3W*mFactor[2]);
|
621
|
}
|
622
|
else
|
623
|
{
|
624
|
buffer[offset ] = (next.x-curr.x)*time + curr.x;
|
625
|
buffer[offset+1] = (next.y-curr.y)*time + curr.y;
|
626
|
buffer[offset+2] = (next.z-curr.z)*time + curr.z;
|
627
|
buffer[offset+3] = (next.w-curr.w)*time + curr.w;
|
628
|
}
|
629
|
|
630
|
break;
|
631
|
default:float t = time;
|
632
|
|
633
|
switch(mMode)
|
634
|
{
|
635
|
case MODE_LOOP: time = time*numPoints;
|
636
|
break;
|
637
|
case MODE_PATH: time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
|
638
|
break;
|
639
|
case MODE_JUMP: time = time*(numPoints-1);
|
640
|
break;
|
641
|
}
|
642
|
|
643
|
int vecCurr = (int)time;
|
644
|
time = time-vecCurr;
|
645
|
|
646
|
if( vecCurr>=0 && vecCurr<numPoints )
|
647
|
{
|
648
|
if( cacheDirty ) recomputeCache(); // recompute cache if we have added or remove vectors since last computation
|
649
|
else if( mVecCurr!= vecCurr ) // ...or if we have just passed a vector and the vector we are currently flying to has changed
|
650
|
{
|
651
|
int vecNext;
|
652
|
mVecCurr = vecCurr;
|
653
|
|
654
|
switch(mMode)
|
655
|
{
|
656
|
case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1;
|
657
|
break;
|
658
|
case MODE_PATH: if( t<0.5f ) vecNext = vecCurr==numPoints-1 ? numPoints-2: vecCurr+1;
|
659
|
else vecNext = vecCurr==0 ? 1 : vecCurr-1;
|
660
|
break;
|
661
|
case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
|
662
|
break;
|
663
|
default : vecNext = 0;
|
664
|
}
|
665
|
|
666
|
next = vv.elementAt(vecNext);
|
667
|
tmp2 = vc.elementAt(vecNext);
|
668
|
|
669
|
if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z || tmp2.vw!=next.w ) recomputeCache();
|
670
|
}
|
671
|
|
672
|
tmp1 = vc.elementAt(vecCurr);
|
673
|
|
674
|
if( vn!=null )
|
675
|
{
|
676
|
time = noise(time,vecCurr);
|
677
|
|
678
|
setUpVectors(time,tmp1);
|
679
|
|
680
|
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor[0] + vec2X*mFactor[1] + vec3X*mFactor[2]);
|
681
|
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor[0] + vec2Y*mFactor[1] + vec3Y*mFactor[2]);
|
682
|
buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor[0] + vec2Z*mFactor[1] + vec3Z*mFactor[2]);
|
683
|
buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw + (vec1W*mFactor[0] + vec2W*mFactor[1] + vec3W*mFactor[2]);
|
684
|
}
|
685
|
else
|
686
|
{
|
687
|
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx;
|
688
|
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy;
|
689
|
buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz;
|
690
|
buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw;
|
691
|
}
|
692
|
|
693
|
break;
|
694
|
}
|
695
|
}
|
696
|
}
|
697
|
|
698
|
}
|
699
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
700
|
//
|