Project

General

Profile

Download (28.7 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / type / Dynamic.java @ d99fcc9c

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