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