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