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