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.Random;
|
23
|
import java.util.Vector;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
/** A class to interpolate between a list of Statics.
|
27
|
* <p><ul>
|
28
|
* <li>if there is only one Point, just jump to it.
|
29
|
* <li>if there are two Points, linearly bounce between them
|
30
|
* <li>if there are more, interpolate a loop (or a path!) between them.
|
31
|
* </ul>
|
32
|
*/
|
33
|
|
34
|
// The way Interpolation between more than 2 Points is done:
|
35
|
//
|
36
|
// Def: let w[i] = (w[i](x), w[i](y), w[i](z)) be the direction and speed we have to be flying at Point P[i]
|
37
|
//
|
38
|
// time it takes to fly though one segment v[i] --> v[i+1] : 0.0 --> 1.0
|
39
|
// w[i] should be parallel to v[i+1] - v[i-1] (cyclic notation)
|
40
|
// |w[i]| proportional to | P[i]-P[i+1] |
|
41
|
//
|
42
|
// Given that the flight route (X(t), Y(t), Z(t)) from P(i) to P(i+1) (0<=t<=1) has to satisfy
|
43
|
// X(0) = P[i ](x), Y(0)=P[i ](y), Z(0)=P[i ](z), X'(0) = w[i ](x), Y'(0) = w[i ](y), Z'(0) = w[i ](z)
|
44
|
// X(1) = P[i+1](x), Y(1)=P[i+1](y), Z(1)=P[i+1](z), X'(1) = w[i+1](x), Y'(1) = w[i+1](y), Z'(1) = w[i+1](z)
|
45
|
//
|
46
|
// we have the solution: X(t) = at^3 + bt^2 + ct + d where
|
47
|
// a = 2*P[i](x) + w[i](x) - 2*P[i+1](x) + w[i+1](x)
|
48
|
// b = -3*P[i](x) - 2*w[i](x) + 3*P[i+1](x) - w[i+1](x)
|
49
|
// c = w[i](x)
|
50
|
// d = P[i](x)
|
51
|
//
|
52
|
// and similarly Y(t) and Z(t).
|
53
|
|
54
|
public abstract class Dynamic
|
55
|
{
|
56
|
/**
|
57
|
* One revolution takes us from the first vector to the last and back to first through the shortest path.
|
58
|
*/
|
59
|
public static final int MODE_LOOP = 0;
|
60
|
/**
|
61
|
* We come back from the last to the first vector through the same way we got there.
|
62
|
*/
|
63
|
public static final int MODE_PATH = 1;
|
64
|
/**
|
65
|
* We just jump back from the last point to the first.
|
66
|
*/
|
67
|
public static final int MODE_JUMP = 2;
|
68
|
|
69
|
/**
|
70
|
* The default mode of access. When in this mode, we are able to call interpolate() with points in time
|
71
|
* in any random order. This means one single Dynamic can be used in many effects simultaneously.
|
72
|
* On the other hand, when in this mode, it is not possible to smoothly interpolate when mDuration suddenly
|
73
|
* changes.
|
74
|
*/
|
75
|
public static final int ACCESS_RANDOM = 0;
|
76
|
/**
|
77
|
* Set the mode to ACCESS_SEQUENTIAL if you need to change mDuration and you would rather have the Dynamic
|
78
|
* keep on smoothly interpolating.
|
79
|
* On the other hand, in this mode, a Dynamic can only be accessed in sequential manner, which means one
|
80
|
* Dynamic can only be used in one effect at a time.
|
81
|
*/
|
82
|
public static final int ACCESS_SEQUENTIAL = 1;
|
83
|
|
84
|
protected int mDimension;
|
85
|
protected int numPoints;
|
86
|
protected int mSegment; // between which pair of points are we currently? (in case of PATH this is a bit complicated!)
|
87
|
protected boolean cacheDirty; // VectorCache not up to date
|
88
|
protected int mMode; // LOOP, PATH or JUMP
|
89
|
protected long mDuration; // number of milliseconds it takes to do a full loop/path from first vector to the last and back to the first
|
90
|
protected float mCount; // number of loops/paths we will do; mCount = 1.5 means we go from the first vector to the last, back to first, and to the last again.
|
91
|
protected double mLastPos;
|
92
|
protected int mAccessMode;
|
93
|
|
94
|
protected class VectorNoise
|
95
|
{
|
96
|
float[][] n;
|
97
|
|
98
|
VectorNoise()
|
99
|
{
|
100
|
n = new float[mDimension][NUM_NOISE];
|
101
|
}
|
102
|
|
103
|
void computeNoise()
|
104
|
{
|
105
|
n[0][0] = mRnd.nextFloat();
|
106
|
for(int i=1; i<NUM_NOISE; i++) n[0][i] = n[0][i-1]+mRnd.nextFloat();
|
107
|
|
108
|
float sum = n[0][NUM_NOISE-1] + mRnd.nextFloat();
|
109
|
|
110
|
for(int i=0; i<NUM_NOISE; i++)
|
111
|
{
|
112
|
n[0][i] /=sum;
|
113
|
for(int j=1; j<mDimension; j++) n[j][i] = mRnd.nextFloat()-0.5f;
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
protected Vector<VectorNoise> vn;
|
119
|
protected float[] mFactor;
|
120
|
protected float[] mNoise;
|
121
|
protected float[][] baseV;
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
// the coefficients of the X(t), Y(t) and Z(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx etc.
|
125
|
// (tangent) is the vector tangent to the path.
|
126
|
// (cached) is the original vector from vv (copied here so when interpolating we can see if it is
|
127
|
// still valid and if not - rebuild the Cache
|
128
|
|
129
|
protected class VectorCache
|
130
|
{
|
131
|
float[] a;
|
132
|
float[] b;
|
133
|
float[] c;
|
134
|
float[] d;
|
135
|
float[] tangent;
|
136
|
float[] cached;
|
137
|
|
138
|
VectorCache()
|
139
|
{
|
140
|
a = new float[mDimension];
|
141
|
b = new float[mDimension];
|
142
|
c = new float[mDimension];
|
143
|
d = new float[mDimension];
|
144
|
tangent = new float[mDimension];
|
145
|
cached = new float[mDimension];
|
146
|
}
|
147
|
}
|
148
|
|
149
|
protected Vector<VectorCache> vc;
|
150
|
protected VectorCache tmp1, tmp2;
|
151
|
|
152
|
private float[] buf;
|
153
|
private float[] old;
|
154
|
private static Random mRnd = new Random();
|
155
|
private static final int NUM_NOISE = 5; // used iff mNoise>0.0. Number of intermediary points between each pair of adjacent vectors
|
156
|
// where we randomize noise factors to make the way between the two vectors not so smooth.
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
// hide this from Javadoc
|
160
|
|
161
|
protected Dynamic()
|
162
|
{
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
protected Dynamic(int duration, float count, int dimension)
|
168
|
{
|
169
|
vc = new Vector<>();
|
170
|
vn = null;
|
171
|
numPoints = 0;
|
172
|
cacheDirty = false;
|
173
|
mMode = MODE_LOOP;
|
174
|
mDuration = duration;
|
175
|
mCount = count;
|
176
|
mDimension = dimension;
|
177
|
mSegment = -1;
|
178
|
mLastPos = -1;
|
179
|
mAccessMode= ACCESS_RANDOM;
|
180
|
|
181
|
baseV = new float[mDimension][mDimension];
|
182
|
buf = new float[mDimension];
|
183
|
old = new float[mDimension];
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
protected float noise(float time,int vecNum)
|
189
|
{
|
190
|
float lower, upper, len;
|
191
|
float d = time*(NUM_NOISE+1);
|
192
|
int index = (int)d;
|
193
|
if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
|
194
|
VectorNoise tmpN = vn.elementAt(vecNum);
|
195
|
|
196
|
float t = d-index;
|
197
|
t = t*t*(3-2*t);
|
198
|
|
199
|
switch(index)
|
200
|
{
|
201
|
case 0 : for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise[i+1]*tmpN.n[i+1][0]*t;
|
202
|
return time + mNoise[0]*(d*tmpN.n[0][0]-time);
|
203
|
case NUM_NOISE: for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise[i+1]*tmpN.n[i+1][NUM_NOISE-1]*(1-t);
|
204
|
len = ((float)NUM_NOISE)/(NUM_NOISE+1);
|
205
|
lower = len + mNoise[0]*(tmpN.n[0][NUM_NOISE-1]-len);
|
206
|
return (1.0f-lower)*(d-NUM_NOISE) + lower;
|
207
|
default : float ya,yb;
|
208
|
|
209
|
for(int i=0;i<mDimension-1;i++)
|
210
|
{
|
211
|
yb = tmpN.n[i+1][index ];
|
212
|
ya = tmpN.n[i+1][index-1];
|
213
|
mFactor[i] = mNoise[i+1]*((yb-ya)*t+ya);
|
214
|
}
|
215
|
|
216
|
len = ((float)index)/(NUM_NOISE+1);
|
217
|
lower = len + mNoise[0]*(tmpN.n[0][index-1]-len);
|
218
|
len = ((float)index+1)/(NUM_NOISE+1);
|
219
|
upper = len + mNoise[0]*(tmpN.n[0][index ]-len);
|
220
|
|
221
|
return (upper-lower)*(d-index) + lower;
|
222
|
}
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
// debugging only
|
227
|
|
228
|
private void printBase(String str)
|
229
|
{
|
230
|
String s;
|
231
|
float t;
|
232
|
|
233
|
for(int i=0; i<mDimension; i++)
|
234
|
{
|
235
|
s = "";
|
236
|
|
237
|
for(int j=0; j<mDimension; j++)
|
238
|
{
|
239
|
t = ((int)(1000*baseV[i][j]))/(1000.0f);
|
240
|
s+=(" "+t);
|
241
|
}
|
242
|
android.util.Log.e("dynamic", str+" base "+i+" : " + s);
|
243
|
}
|
244
|
}
|
245
|
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
// debugging only
|
248
|
|
249
|
@SuppressWarnings("unused")
|
250
|
private void checkBase()
|
251
|
{
|
252
|
float tmp, cosA;
|
253
|
float[] len= new float[mDimension];
|
254
|
boolean error=false;
|
255
|
|
256
|
for(int i=0; i<mDimension; i++)
|
257
|
{
|
258
|
len[i] = 0.0f;
|
259
|
|
260
|
for(int k=0; k<mDimension; k++)
|
261
|
{
|
262
|
len[i] += baseV[i][k]*baseV[i][k];
|
263
|
}
|
264
|
|
265
|
if( len[i] == 0.0f || len[0]/len[i] < 0.95f || len[0]/len[i]>1.05f )
|
266
|
{
|
267
|
android.util.Log.e("dynamic", "length of vector "+i+" : "+Math.sqrt(len[i]));
|
268
|
error = true;
|
269
|
}
|
270
|
}
|
271
|
|
272
|
for(int i=0; i<mDimension; i++)
|
273
|
for(int j=i+1; j<mDimension; j++)
|
274
|
{
|
275
|
tmp = 0.0f;
|
276
|
|
277
|
for(int k=0; k<mDimension; k++)
|
278
|
{
|
279
|
tmp += baseV[i][k]*baseV[j][k];
|
280
|
}
|
281
|
|
282
|
cosA = ( (len[i]==0.0f || len[j]==0.0f) ? 0.0f : tmp/(float)Math.sqrt(len[i]*len[j]));
|
283
|
|
284
|
if( cosA > 0.05f || cosA < -0.05f )
|
285
|
{
|
286
|
android.util.Log.e("dynamic", "cos angle between vectors "+i+" and "+j+" : "+cosA);
|
287
|
error = true;
|
288
|
}
|
289
|
}
|
290
|
|
291
|
if( error ) printBase("");
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
private void checkAngle(int index)
|
297
|
{
|
298
|
float cosA = 0.0f;
|
299
|
|
300
|
for(int k=0;k<mDimension; k++)
|
301
|
cosA += baseV[index][k]*old[k];
|
302
|
|
303
|
if( cosA<0.0f )
|
304
|
{
|
305
|
/*
|
306
|
/// DEBUGGING ////
|
307
|
String s = index+" (";
|
308
|
float t;
|
309
|
|
310
|
for(int j=0; j<mDimension; j++)
|
311
|
{
|
312
|
t = ((int)(100*baseV[index][j]))/(100.0f);
|
313
|
s+=(" "+t);
|
314
|
}
|
315
|
s += ") (";
|
316
|
|
317
|
for(int j=0; j<mDimension; j++)
|
318
|
{
|
319
|
t = ((int)(100*old[j]))/(100.0f);
|
320
|
s+=(" "+t);
|
321
|
}
|
322
|
s+= ")";
|
323
|
|
324
|
android.util.Log.e("dynamic", "kat: " + s);
|
325
|
/// END DEBUGGING ///
|
326
|
*/
|
327
|
for(int j=0; j<mDimension; j++)
|
328
|
baseV[index][j] = -baseV[index][j];
|
329
|
}
|
330
|
}
|
331
|
|
332
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
333
|
// helper function in case we are interpolating through exactly 2 points
|
334
|
|
335
|
protected void computeOrthonormalBase2(Static1D curr, Static1D next)
|
336
|
{
|
337
|
switch(mDimension)
|
338
|
{
|
339
|
case 1: baseV[0][0] = (next.x-curr.x);
|
340
|
break;
|
341
|
case 2: Static2D curr2 = (Static2D)curr;
|
342
|
Static2D next2 = (Static2D)next;
|
343
|
baseV[0][0] = (next2.x-curr2.x);
|
344
|
baseV[0][1] = (next2.y-curr2.y);
|
345
|
break;
|
346
|
case 3: Static3D curr3 = (Static3D)curr;
|
347
|
Static3D next3 = (Static3D)next;
|
348
|
baseV[0][0] = (next3.x-curr3.x);
|
349
|
baseV[0][1] = (next3.y-curr3.y);
|
350
|
baseV[0][2] = (next3.z-curr3.z);
|
351
|
break;
|
352
|
case 4: Static4D curr4 = (Static4D)curr;
|
353
|
Static4D next4 = (Static4D)next;
|
354
|
baseV[0][0] = (next4.x-curr4.x);
|
355
|
baseV[0][1] = (next4.y-curr4.y);
|
356
|
baseV[0][2] = (next4.z-curr4.z);
|
357
|
baseV[0][3] = (next4.w-curr4.w);
|
358
|
break;
|
359
|
case 5: Static5D curr5 = (Static5D)curr;
|
360
|
Static5D next5 = (Static5D)next;
|
361
|
baseV[0][0] = (next5.x-curr5.x);
|
362
|
baseV[0][1] = (next5.y-curr5.y);
|
363
|
baseV[0][2] = (next5.z-curr5.z);
|
364
|
baseV[0][3] = (next5.w-curr5.w);
|
365
|
baseV[0][4] = (next5.v-curr5.v);
|
366
|
break;
|
367
|
default: throw new RuntimeException("Unsupported dimension");
|
368
|
}
|
369
|
|
370
|
if( baseV[0][0] == 0.0f )
|
371
|
{
|
372
|
baseV[1][0] = 1.0f;
|
373
|
baseV[1][1] = 0.0f;
|
374
|
}
|
375
|
else
|
376
|
{
|
377
|
baseV[1][0] = 0.0f;
|
378
|
baseV[1][1] = 1.0f;
|
379
|
}
|
380
|
|
381
|
for(int i=2; i<mDimension; i++)
|
382
|
{
|
383
|
baseV[1][i] = 0.0f;
|
384
|
}
|
385
|
|
386
|
computeOrthonormalBase();
|
387
|
}
|
388
|
|
389
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
390
|
// helper function in case we are interpolating through more than 2 points
|
391
|
|
392
|
protected void computeOrthonormalBaseMore(float time,VectorCache vc)
|
393
|
{
|
394
|
for(int i=0; i<mDimension; i++)
|
395
|
{
|
396
|
baseV[0][i] = (3*vc.a[i]*time+2*vc.b[i])*time+vc.c[i]; // first derivative, i.e. velocity vector
|
397
|
old[i] = baseV[1][i];
|
398
|
baseV[1][i] = 6*vc.a[i]*time+2*vc.b[i]; // second derivative,i.e. acceleration vector
|
399
|
}
|
400
|
|
401
|
computeOrthonormalBase();
|
402
|
}
|
403
|
|
404
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
405
|
// When this function gets called, baseV[0] and baseV[1] should have been filled with two mDimension-al
|
406
|
// vectors. This function then fills the rest of the baseV array with a mDimension-al Orthonormal base.
|
407
|
// (mDimension-2 vectors, pairwise orthogonal to each other and to the original 2). The function always
|
408
|
// leaves base[0] alone but generally speaking must adjust base[1] to make it orthogonal to base[0]!
|
409
|
// The whole baseV is then used to compute Noise.
|
410
|
//
|
411
|
// When computing noise of a point travelling along a N-dimensional path, there are three cases:
|
412
|
// a) we may be interpolating through 1 point, i.e. standing in place - nothing to do in this case
|
413
|
// b) we may be interpolating through 2 points, i.e. travelling along a straight line between them -
|
414
|
// then pass the velocity vector in baseV[0] and anything linearly independent in base[1].
|
415
|
// The output will then be discontinuous in dimensions>2 (sad corollary from the Hairy Ball Theorem)
|
416
|
// but we don't care - we are travelling along a straight line, so velocity (aka baseV[0]!) does
|
417
|
// not change.
|
418
|
// c) we may be interpolating through more than 2 points. Then interpolation formulas ensure the path
|
419
|
// will never be a straight line, even locally -> we can pass in baseV[0] and baseV[1] the velocity
|
420
|
// and the acceleration (first and second derivatives of the path) which are then guaranteed to be
|
421
|
// linearly independent. Then we can ensure this is continuous in dimensions <=4. This leaves
|
422
|
// dimension 5 (ATM WAVE is 5-dimensional) discontinuous -> WAVE will suffer from chaotic noise.
|
423
|
//
|
424
|
// Bear in mind here the 'normal' in 'orthonormal' means 'length equal to the length of the original
|
425
|
// velocity vector' (rather than the standard 1)
|
426
|
|
427
|
protected void computeOrthonormalBase()
|
428
|
{
|
429
|
int last_non_zero=-1;
|
430
|
float tmp;
|
431
|
|
432
|
for(int i=0; i<mDimension; i++)
|
433
|
if( baseV[0][i] != 0.0f )
|
434
|
last_non_zero=i;
|
435
|
|
436
|
if( last_non_zero==-1 ) ///
|
437
|
{ // velocity is the 0 vector -> two
|
438
|
for(int i=0; i<mDimension-1; i++) // consecutive points we are interpolating
|
439
|
for(int j=0; j<mDimension; j++) // through are identical -> no noise,
|
440
|
baseV[i+1][j]= 0.0f; // set the base to 0 vectors.
|
441
|
} ///
|
442
|
else
|
443
|
{
|
444
|
for(int i=1; i<mDimension; i++) /// One iteration computes baseV[i][*]
|
445
|
{ // (aka b[i]), the i-th orthonormal vector.
|
446
|
buf[i-1]=0.0f; //
|
447
|
// We can use (modified!) Gram-Schmidt.
|
448
|
for(int k=0; k<mDimension; k++) //
|
449
|
{ //
|
450
|
if( i>=2 ) // b[0] = b[0]
|
451
|
{ // b[1] = b[1] - (<b[1],b[0]>/<b[0],b[0]>)*b[0]
|
452
|
old[k] = baseV[i][k]; // b[2] = b[2] - (<b[2],b[0]>/<b[0],b[0]>)*b[0] - (<b[2],b[1]>/<b[1],b[1]>)*b[1]
|
453
|
baseV[i][k]= (k==i-(last_non_zero>=i?1:0)) ? 1.0f : 0.0f; // b[3] = b[3] - (<b[3],b[0]>/<b[0],b[0]>)*b[0] - (<b[3],b[1]>/<b[1],b[1]>)*b[1] - (<b[3],b[2]>/<b[2],b[2]>)*b[2]
|
454
|
} // (...)
|
455
|
// then b[i] = b[i] / |b[i]| ( Here really b[i] = b[i] / (|b[0]|/|b[i]|)
|
456
|
tmp = baseV[i-1][k]; //
|
457
|
buf[i-1] += tmp*tmp; //
|
458
|
} //
|
459
|
//
|
460
|
for(int j=0; j<i; j++) //
|
461
|
{ //
|
462
|
tmp = 0.0f; //
|
463
|
for(int k=0;k<mDimension; k++) tmp += baseV[i][k]*baseV[j][k]; //
|
464
|
tmp /= buf[j]; //
|
465
|
for(int k=0;k<mDimension; k++) baseV[i][k] -= tmp*baseV[j][k]; //
|
466
|
} //
|
467
|
//
|
468
|
checkAngle(i); //
|
469
|
} /// end compute baseV[i][*]
|
470
|
|
471
|
buf[mDimension-1]=0.0f; /// Normalize
|
472
|
for(int k=0; k<mDimension; k++) //
|
473
|
{ //
|
474
|
tmp = baseV[mDimension-1][k]; //
|
475
|
buf[mDimension-1] += tmp*tmp; //
|
476
|
} //
|
477
|
//
|
478
|
for(int i=1; i<mDimension; i++) //
|
479
|
{ //
|
480
|
tmp = (float)Math.sqrt(buf[0]/buf[i]); //
|
481
|
for(int k=0;k<mDimension; k++) baseV[i][k] *= tmp; //
|
482
|
} /// End Normalize
|
483
|
}
|
484
|
|
485
|
//printBase("end");
|
486
|
//checkBase();
|
487
|
}
|
488
|
|
489
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
490
|
// internal debugging only!
|
491
|
|
492
|
public String print()
|
493
|
{
|
494
|
return "duration="+mDuration+" count="+mCount+" Noise[0]="+mNoise[0]+" numVectors="+numPoints+" mMode="+mMode;
|
495
|
}
|
496
|
|
497
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
498
|
|
499
|
abstract void interpolate(float[] buffer, int offset, float time);
|
500
|
|
501
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
502
|
// PUBLIC API
|
503
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
504
|
|
505
|
/**
|
506
|
* Sets the mode of the interpolation to Loop, Path or Jump.
|
507
|
* <ul>
|
508
|
* <li>Loop is when we go from the first point all the way to the last, and the back to the first through
|
509
|
* the shortest way.
|
510
|
* <li>Path is when we come back from the last point back to the first the same way we got there.
|
511
|
* <li>Jump is when we go from first to last and then jump back to the first.
|
512
|
* </ul>
|
513
|
*
|
514
|
* @param mode {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
|
515
|
*/
|
516
|
public void setMode(int mode)
|
517
|
{
|
518
|
mMode = mode;
|
519
|
}
|
520
|
|
521
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
522
|
/**
|
523
|
* Returns the number of Statics this Dynamic has been fed with.
|
524
|
*
|
525
|
* @return the number of Statics we are currently interpolating through.
|
526
|
*/
|
527
|
public synchronized int getNumPoints()
|
528
|
{
|
529
|
return numPoints;
|
530
|
}
|
531
|
|
532
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
533
|
/**
|
534
|
* Controls how many times we want to interpolate.
|
535
|
* <p>
|
536
|
* Count equal to 1 means 'go from the first Static to the last and back'. Does not have to be an
|
537
|
* integer - i.e. count=1.5 would mean 'start at the first Point, go to the last, come back to the first,
|
538
|
* go to the last again and stop'.
|
539
|
* Count<=0 means 'go on interpolating indefinitely'.
|
540
|
*
|
541
|
* @param count the number of times we want to interpolate between our collection of Statics.
|
542
|
*/
|
543
|
public void setCount(float count)
|
544
|
{
|
545
|
mCount = count;
|
546
|
}
|
547
|
|
548
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
549
|
/**
|
550
|
* Sets the time it takes to do one full interpolation.
|
551
|
*
|
552
|
* @param duration Time, in milliseconds, it takes to do one full interpolation, i.e. go from the first
|
553
|
* Point to the last and back.
|
554
|
*/
|
555
|
public void setDuration(long duration)
|
556
|
{
|
557
|
mDuration = duration;
|
558
|
}
|
559
|
|
560
|
|
561
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
562
|
/**
|
563
|
* Sets the access mode this Dynamic will be working in.
|
564
|
*
|
565
|
* @param mode {@link Dynamic#ACCESS_RANDOM} or {@link Dynamic#ACCESS_SEQUENTIAL}.
|
566
|
*/
|
567
|
public void setAccessMode(int mode)
|
568
|
{
|
569
|
mAccessMode = mode;
|
570
|
mLastPos = -1;
|
571
|
}
|
572
|
|
573
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
574
|
/**
|
575
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
576
|
*
|
577
|
* @param buffer Float buffer we will write the results to.
|
578
|
* @param offset Offset in the buffer where to write the result.
|
579
|
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
|
580
|
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
|
581
|
*/
|
582
|
public void interpolateMain(float[] buffer, int offset, long time)
|
583
|
{
|
584
|
if( mDuration<=0.0f )
|
585
|
{
|
586
|
interpolate(buffer,offset,mCount-(int)mCount);
|
587
|
}
|
588
|
else
|
589
|
{
|
590
|
double pos = (double)time/mDuration;
|
591
|
|
592
|
if( pos<=mCount || mCount<=0.0f )
|
593
|
{
|
594
|
interpolate(buffer,offset, (float)(pos-(int)pos) );
|
595
|
}
|
596
|
}
|
597
|
}
|
598
|
|
599
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
600
|
/**
|
601
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
602
|
* <p>
|
603
|
* This version differs from the previous in that it returns a boolean value which indicates whether
|
604
|
* the interpolation is finished.
|
605
|
*
|
606
|
* @param buffer Float buffer we will write the results to.
|
607
|
* @param offset Offset in the buffer where to write the result.
|
608
|
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
|
609
|
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
|
610
|
* @param step Time difference between now and the last time we called this function. Needed to figure out
|
611
|
* if the previous time we were called the effect wasn't finished yet, but now it is.
|
612
|
* @return true if the interpolation reached its end.
|
613
|
*/
|
614
|
public boolean interpolateMain(float[] buffer, int offset, long time, long step)
|
615
|
{
|
616
|
if( mDuration<=0.0f )
|
617
|
{
|
618
|
interpolate(buffer,offset,mCount-(int)mCount);
|
619
|
return false;
|
620
|
}
|
621
|
|
622
|
double pos;
|
623
|
|
624
|
if( mAccessMode==ACCESS_SEQUENTIAL )
|
625
|
{
|
626
|
pos = mLastPos<0 ? (double)time/mDuration : (double)step/mDuration + mLastPos;
|
627
|
mLastPos = pos;
|
628
|
}
|
629
|
else
|
630
|
{
|
631
|
pos = (double)time/mDuration;
|
632
|
}
|
633
|
|
634
|
if( pos<=mCount || mCount<=0.0f )
|
635
|
{
|
636
|
interpolate(buffer,offset, (float)(pos-(int)pos) );
|
637
|
|
638
|
if( time+step > mDuration*mCount && mCount>0.0f )
|
639
|
{
|
640
|
interpolate(buffer,offset,mCount-(int)mCount);
|
641
|
return true;
|
642
|
}
|
643
|
}
|
644
|
|
645
|
return false;
|
646
|
}
|
647
|
|
648
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
649
|
}
|