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