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