Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic.java @ 24b1f190

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