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 2-dimensional implementation of the Dynamic class to interpolate between a list
|
27
|
* of Float2Ds.
|
28
|
*/
|
29
|
|
30
|
public class Dynamic2D extends Dynamic implements Data2D
|
31
|
{
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
// the coefficients of the X(t), Y(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx etc.
|
35
|
// (x,y) is the vector tangent to the path.
|
36
|
// (vx,vy) 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
|
|
44
|
float x,y;
|
45
|
float vx,vy;
|
46
|
}
|
47
|
|
48
|
private class VectorNoise
|
49
|
{
|
50
|
float[] nx;
|
51
|
float[] ny;
|
52
|
|
53
|
VectorNoise()
|
54
|
{
|
55
|
nx = new float[NUM_NOISE];
|
56
|
nx[0] = mRnd.nextFloat();
|
57
|
for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1]+mRnd.nextFloat();
|
58
|
float sum = nx[NUM_NOISE-1] + mRnd.nextFloat();
|
59
|
for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum;
|
60
|
|
61
|
ny = new float[NUM_NOISE];
|
62
|
for(int i=0; i<NUM_NOISE; i++) ny[i] = mRnd.nextFloat()-0.5f;
|
63
|
}
|
64
|
}
|
65
|
|
66
|
private Vector<VectorCache> vc;
|
67
|
private VectorCache tmp1, tmp2;
|
68
|
|
69
|
private Vector<Static2D> vv;
|
70
|
private Static2D prev, curr, next;
|
71
|
|
72
|
private Vector<VectorNoise> vn;
|
73
|
private VectorNoise tmpN;
|
74
|
|
75
|
private float mFactor;
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
synchronized void createNoise()
|
80
|
{
|
81
|
if( vn==null )
|
82
|
{
|
83
|
vn = new Vector<>();
|
84
|
for(int i=0; i<numPoints; i++) vn.add(new VectorNoise());
|
85
|
}
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
// no array bounds checking!
|
90
|
|
91
|
private void vec(int c)
|
92
|
{
|
93
|
int p = c>0 ? c-1: numPoints-1;
|
94
|
int n = c<numPoints-1 ? c+1: 0;
|
95
|
|
96
|
prev = vv.elementAt(p);
|
97
|
curr = vv.elementAt(c);
|
98
|
next = vv.elementAt(n);
|
99
|
|
100
|
tmp1 = vc.elementAt(c);
|
101
|
|
102
|
float px = curr.x - prev.x;
|
103
|
float py = curr.y - prev.y;
|
104
|
float nx = next.x - curr.x;
|
105
|
float ny = next.y - curr.y;
|
106
|
|
107
|
float d = nx*nx+ny*ny;
|
108
|
|
109
|
if( d>0 )
|
110
|
{
|
111
|
float q = (float)Math.sqrt((px*px+py*py)/d);
|
112
|
|
113
|
if( q>1 )
|
114
|
{
|
115
|
tmp1.x = nx+px/q;
|
116
|
tmp1.y = ny+py/q;
|
117
|
}
|
118
|
else
|
119
|
{
|
120
|
tmp1.x = px+nx*q;
|
121
|
tmp1.y = py+ny*q;
|
122
|
}
|
123
|
}
|
124
|
else
|
125
|
{
|
126
|
tmp1.x = 0.0f;
|
127
|
tmp1.y = 0.0f;
|
128
|
}
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
|
133
|
private void recomputeCache()
|
134
|
{
|
135
|
if( numPoints==1 )
|
136
|
{
|
137
|
tmp1= vc.elementAt(0);
|
138
|
curr= vv.elementAt(0);
|
139
|
|
140
|
tmp1.ax = tmp1.ay = 0.0f;
|
141
|
tmp1.bx = tmp1.by = 0.0f;
|
142
|
tmp1.cx = curr.x;
|
143
|
tmp1.cy = curr.y;
|
144
|
tmp1.dx = tmp1.dy = 0.0f;
|
145
|
}
|
146
|
else if( numPoints==2 )
|
147
|
{
|
148
|
tmp1= vc.elementAt(0);
|
149
|
tmp2= vc.elementAt(1);
|
150
|
curr= vv.elementAt(0);
|
151
|
next= vv.elementAt(1);
|
152
|
|
153
|
tmp1.ax = tmp1.ay = 0.0f;
|
154
|
tmp1.bx = tmp1.by = 0.0f;
|
155
|
tmp1.cx = next.x - curr.x;
|
156
|
tmp1.cy = next.y - curr.y;
|
157
|
tmp1.dx = curr.x;
|
158
|
tmp1.dy = curr.y;
|
159
|
|
160
|
tmp2.ax = tmp2.ay = 0.0f;
|
161
|
tmp2.bx = tmp2.by = 0.0f;
|
162
|
tmp2.cx = curr.x - next.x;
|
163
|
tmp2.cy = curr.y - next.y;
|
164
|
tmp2.dx = next.x;
|
165
|
tmp2.dy = next.y;
|
166
|
}
|
167
|
else
|
168
|
{
|
169
|
int i, n;
|
170
|
|
171
|
for(i=0; i<numPoints; i++) vec(i);
|
172
|
|
173
|
for(i=0; i<numPoints; i++)
|
174
|
{
|
175
|
n = i<numPoints-1 ? i+1:0;
|
176
|
|
177
|
tmp1= vc.elementAt(i);
|
178
|
tmp2= vc.elementAt(n);
|
179
|
curr= vv.elementAt(i);
|
180
|
next= vv.elementAt(n);
|
181
|
|
182
|
tmp1.vx = curr.x;
|
183
|
tmp1.vy = curr.y;
|
184
|
|
185
|
tmp1.ax = 2*curr.x + tmp1.x - 2*next.x + tmp2.x;
|
186
|
tmp1.bx = -3*curr.x - 2*tmp1.x + 3*next.x - tmp2.x;
|
187
|
tmp1.cx = tmp1.x;
|
188
|
tmp1.dx = curr.x;
|
189
|
|
190
|
tmp1.ay = 2*curr.y + tmp1.y - 2*next.y + tmp2.y;
|
191
|
tmp1.by = -3*curr.y - 2*tmp1.y + 3*next.y - tmp2.y;
|
192
|
tmp1.cy = tmp1.y;
|
193
|
tmp1.dy = curr.y;
|
194
|
}
|
195
|
}
|
196
|
|
197
|
cacheDirty = false;
|
198
|
}
|
199
|
|
200
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201
|
|
202
|
private float noise(float time,int vecNum)
|
203
|
{
|
204
|
float lower, upper, len;
|
205
|
float d = time*(NUM_NOISE+1);
|
206
|
int index = (int)d;
|
207
|
if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
|
208
|
tmpN = vn.elementAt(vecNum);
|
209
|
|
210
|
float x = d-index;
|
211
|
x = x*x*(3-2*x);
|
212
|
|
213
|
switch(index)
|
214
|
{
|
215
|
case 0 : mFactor = mNoise*tmpN.ny[0]*x;
|
216
|
return time + mNoise*(d*tmpN.nx[0]-time);
|
217
|
case NUM_NOISE: mFactor= mNoise*tmpN.ny[NUM_NOISE-1]*(1-x);
|
218
|
len = ((float)NUM_NOISE)/(NUM_NOISE+1);
|
219
|
lower = len + mNoise*(tmpN.nx[NUM_NOISE-1]-len);
|
220
|
return (1.0f-lower)*(d-NUM_NOISE) + lower;
|
221
|
default : float yb = tmpN.ny[index ];
|
222
|
float ya = tmpN.ny[index-1];
|
223
|
mFactor = mNoise*((yb-ya)*x+ya);
|
224
|
|
225
|
len = ((float)index)/(NUM_NOISE+1);
|
226
|
lower = len + mNoise*(tmpN.nx[index-1]-len);
|
227
|
len = ((float)index+1)/(NUM_NOISE+1);
|
228
|
upper = len + mNoise*(tmpN.nx[index ]-len);
|
229
|
|
230
|
return (upper-lower)*(d-index) + lower;
|
231
|
}
|
232
|
}
|
233
|
|
234
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235
|
// PUBLIC API
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
/**
|
238
|
* Default constructor.
|
239
|
*/
|
240
|
public Dynamic2D()
|
241
|
{
|
242
|
vv = new Vector<>();
|
243
|
vc = new Vector<>();
|
244
|
vn = null;
|
245
|
numPoints = 0;
|
246
|
cacheDirty = false;
|
247
|
mMode = MODE_LOOP;
|
248
|
mDuration = 0;
|
249
|
mCount = 0.5f;
|
250
|
mNoise = 0.0f;
|
251
|
}
|
252
|
|
253
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254
|
|
255
|
/**
|
256
|
* Default constructor.
|
257
|
*
|
258
|
* @param duration number of milliseconds it takes to do a full loop/path from first vector to the
|
259
|
* last and back to the first
|
260
|
* @param count number of loops/paths we will do; mCount = 1.5 means we go from the first vector
|
261
|
* to the last, back to first, and to the last again.
|
262
|
*/
|
263
|
public Dynamic2D(int duration, float count)
|
264
|
{
|
265
|
vv = new Vector<>();
|
266
|
vc = new Vector<>();
|
267
|
vn = null;
|
268
|
numPoints = 0;
|
269
|
cacheDirty = false;
|
270
|
mMode = MODE_LOOP;
|
271
|
mDuration = duration;
|
272
|
mCount = count;
|
273
|
}
|
274
|
|
275
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276
|
/**
|
277
|
* Returns the location'th Static2D.
|
278
|
*
|
279
|
* @param location the index of the Point we are interested in.
|
280
|
* @return The Static2D, if 0<=location<getNumPoints(), or null otherwise.
|
281
|
*/
|
282
|
public synchronized Static2D getPoint(int location)
|
283
|
{
|
284
|
return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
/**
|
289
|
* Resets the location'th Point.
|
290
|
*
|
291
|
* @param location the index of the Point we are setting.
|
292
|
* @param x New value of its first float.
|
293
|
*/
|
294
|
public synchronized void setPoint(int location, float x, float y)
|
295
|
{
|
296
|
if( location>=0 && location<numPoints )
|
297
|
{
|
298
|
curr = vv.elementAt(location);
|
299
|
|
300
|
if( curr!=null )
|
301
|
{
|
302
|
curr.set(x,y);
|
303
|
cacheDirty=true;
|
304
|
}
|
305
|
}
|
306
|
}
|
307
|
|
308
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
309
|
/**
|
310
|
* Adds a new Static2D to the end of our list of Points to interpolate through.
|
311
|
* <p>
|
312
|
* Only a reference to the Point gets added to the List; this means that one can add a Point
|
313
|
* here, and later on {@link Static2D#set(float,float)} it to some new value and the change
|
314
|
* will be seamlessly reflected in the interpolated path.
|
315
|
* <p>
|
316
|
* A Point can be added multiple times.
|
317
|
*
|
318
|
* @param v The Point to add.
|
319
|
*/
|
320
|
public synchronized void add(Static2D v)
|
321
|
{
|
322
|
if( v!=null )
|
323
|
{
|
324
|
vv.add(v);
|
325
|
|
326
|
if( vn!=null ) vn.add(new VectorNoise());
|
327
|
|
328
|
switch(numPoints)
|
329
|
{
|
330
|
case 0:
|
331
|
case 1: break;
|
332
|
case 2: vc.add(new VectorCache());
|
333
|
vc.add(new VectorCache());
|
334
|
vc.add(new VectorCache());
|
335
|
break;
|
336
|
default:vc.add(new VectorCache());
|
337
|
}
|
338
|
|
339
|
numPoints++;
|
340
|
cacheDirty = true;
|
341
|
}
|
342
|
}
|
343
|
|
344
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
345
|
/**
|
346
|
* Adds a new Static2D to the location'th place in our List of Points to interpolate through.
|
347
|
*
|
348
|
* @param location Index in our List to add the new Point at.
|
349
|
* @param v The Point to add.
|
350
|
*/
|
351
|
public synchronized void add(int location, Static2D v)
|
352
|
{
|
353
|
if( v!=null )
|
354
|
{
|
355
|
vv.add(location, v);
|
356
|
|
357
|
if( vn!=null ) vn.add(new VectorNoise());
|
358
|
|
359
|
switch(numPoints)
|
360
|
{
|
361
|
case 0:
|
362
|
case 1: break;
|
363
|
case 2: vc.add(new VectorCache());
|
364
|
vc.add(new VectorCache());
|
365
|
vc.add(new VectorCache());
|
366
|
break;
|
367
|
default:vc.add(location,new VectorCache());
|
368
|
}
|
369
|
|
370
|
numPoints++;
|
371
|
cacheDirty = true;
|
372
|
}
|
373
|
}
|
374
|
|
375
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
376
|
/**
|
377
|
* Removes all occurrences of Point v from the List of Points to interpolate through.
|
378
|
*
|
379
|
* @param v The Point to remove.
|
380
|
* @return <code>true</code> if we have removed at least one Point.
|
381
|
*/
|
382
|
public synchronized boolean remove(Static2D v)
|
383
|
{
|
384
|
int n = vv.indexOf(v);
|
385
|
boolean found = false;
|
386
|
|
387
|
while( n>=0 )
|
388
|
{
|
389
|
vv.remove(n);
|
390
|
|
391
|
if( vn!=null ) vn.remove(0);
|
392
|
|
393
|
switch(numPoints)
|
394
|
{
|
395
|
case 0:
|
396
|
case 1:
|
397
|
case 2: break;
|
398
|
case 3: vc.removeAllElements();
|
399
|
break;
|
400
|
default:vc.remove(n);
|
401
|
}
|
402
|
|
403
|
numPoints--;
|
404
|
found = true;
|
405
|
n = vv.indexOf(v);
|
406
|
}
|
407
|
|
408
|
if( found )
|
409
|
{
|
410
|
cacheDirty=true;
|
411
|
}
|
412
|
|
413
|
return found;
|
414
|
}
|
415
|
|
416
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
417
|
/**
|
418
|
* Removes a location'th Point from the List of Points we interpolate through.
|
419
|
*
|
420
|
* @param location index of the Point we want to remove.
|
421
|
* @return <code>true</code> if location is valid, i.e. if 0<=location<getNumPoints().
|
422
|
*/
|
423
|
public synchronized boolean remove(int location)
|
424
|
{
|
425
|
if( location>=0 && location<numPoints )
|
426
|
{
|
427
|
vv.removeElementAt(location);
|
428
|
|
429
|
if( vn!=null ) vn.remove(0);
|
430
|
|
431
|
switch(numPoints)
|
432
|
{
|
433
|
case 0:
|
434
|
case 1:
|
435
|
case 2: break;
|
436
|
case 3: vc.removeAllElements();
|
437
|
break;
|
438
|
default:vc.removeElementAt(location);
|
439
|
}
|
440
|
|
441
|
numPoints--;
|
442
|
cacheDirty = true;
|
443
|
return true;
|
444
|
}
|
445
|
|
446
|
return false;
|
447
|
}
|
448
|
|
449
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
450
|
/**
|
451
|
* Removes all Points.
|
452
|
*/
|
453
|
public synchronized void removeAll()
|
454
|
{
|
455
|
numPoints = 0;
|
456
|
vv.removeAllElements();
|
457
|
vc.removeAllElements();
|
458
|
cacheDirty = false;
|
459
|
|
460
|
if( vn!=null ) vn.removeAllElements();
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
/**
|
465
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
466
|
* <p>
|
467
|
* Since this is a 2-dimensional Dynamic, the resulting interpolated Static2D gets written
|
468
|
* to two locations in the buffer: buffer[offset] and buffer[offset+1].
|
469
|
*
|
470
|
* @param buffer Float buffer we will write the resulting Static2D to.
|
471
|
* @param offset Offset in the buffer where to write the result.
|
472
|
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
|
473
|
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
|
474
|
*/
|
475
|
public synchronized void interpolate(float[] buffer, int offset, float time)
|
476
|
{
|
477
|
switch(numPoints)
|
478
|
{
|
479
|
case 0: buffer[offset ] = 0.0f;
|
480
|
buffer[offset+1] = 0.0f;
|
481
|
break;
|
482
|
case 1: curr = vv.elementAt(0);
|
483
|
buffer[offset ] = curr.x;
|
484
|
buffer[offset+1] = curr.y;
|
485
|
break;
|
486
|
case 2: curr = vv.elementAt(0);
|
487
|
next = vv.elementAt(1);
|
488
|
|
489
|
if( mMode==MODE_LOOP || mMode==MODE_PATH ) time = (time>0.5f ? 2-2*time : 2*time);
|
490
|
|
491
|
if( vn!=null )
|
492
|
{
|
493
|
time = noise(time,0);
|
494
|
|
495
|
float dx2 = next.x-curr.x;
|
496
|
float dy2 = next.y-curr.y;
|
497
|
|
498
|
buffer[offset ] = dx2*time + curr.x +dy2*mFactor;
|
499
|
buffer[offset+1] = dy2*time + curr.y -dx2*mFactor;
|
500
|
}
|
501
|
else
|
502
|
{
|
503
|
buffer[offset ] = (next.x-curr.x)*time + curr.x;
|
504
|
buffer[offset+1] = (next.y-curr.y)*time + curr.y;
|
505
|
}
|
506
|
|
507
|
break;
|
508
|
default:float t = time;
|
509
|
|
510
|
switch(mMode)
|
511
|
{
|
512
|
case MODE_LOOP: time = time*numPoints;
|
513
|
break;
|
514
|
case MODE_PATH: time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
|
515
|
break;
|
516
|
case MODE_JUMP: time = time*(numPoints-1);
|
517
|
break;
|
518
|
}
|
519
|
|
520
|
int vecCurr = (int)time;
|
521
|
time = time-vecCurr;
|
522
|
|
523
|
if( vecCurr>=0 && vecCurr<numPoints )
|
524
|
{
|
525
|
if( cacheDirty ) recomputeCache(); // recompute cache if we have added or remove vectors since last computation
|
526
|
else if( mVecCurr!= vecCurr ) // ...or if we have just passed a vector and the vector we are currently flying to has changed
|
527
|
{
|
528
|
int vecNext;
|
529
|
mVecCurr = vecCurr;
|
530
|
|
531
|
switch(mMode)
|
532
|
{
|
533
|
case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1;
|
534
|
break;
|
535
|
case MODE_PATH: if( t<0.5f ) vecNext = vecCurr==numPoints-1 ? numPoints-2: vecCurr+1;
|
536
|
else vecNext = vecCurr==0 ? 1 : vecCurr-1;
|
537
|
break;
|
538
|
case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
|
539
|
break;
|
540
|
default : vecNext = 0;
|
541
|
}
|
542
|
|
543
|
next = vv.elementAt(vecNext);
|
544
|
tmp2 = vc.elementAt(vecNext);
|
545
|
|
546
|
if( tmp2.vx!=next.x || tmp2.vy!=next.y ) recomputeCache();
|
547
|
}
|
548
|
|
549
|
if( vn!=null )
|
550
|
{
|
551
|
time = noise(time,vecCurr);
|
552
|
tmp1 = vc.elementAt(vecCurr);
|
553
|
|
554
|
float dx2 = (3*tmp1.ax*time+2*tmp1.bx)*time + tmp1.cx;
|
555
|
float dy2 = (3*tmp1.ay*time+2*tmp1.by)*time + tmp1.cy;
|
556
|
|
557
|
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx +dy2*mFactor;
|
558
|
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy -dx2*mFactor;
|
559
|
}
|
560
|
else
|
561
|
{
|
562
|
tmp1 = vc.elementAt(vecCurr);
|
563
|
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx;
|
564
|
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy;
|
565
|
}
|
566
|
|
567
|
break;
|
568
|
}
|
569
|
}
|
570
|
}
|
571
|
}
|
572
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
573
|
//
|