Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic.java @ 78ff6ea9

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 return it.
29
* <li>if there are two Points, linearly bounce between them
30
* <li>if there are more, interpolate a path between them. Exact way we interpolate depends on the MODE.
31
* </ul>
32
*/
33

    
34
// The way Interpolation between more than 2 Points is done:
35
// 
36
// 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
//
39
// 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
//
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
// 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
//
48
// we have the solution:  X(t) = at^3 + bt^2 + ct + d where
49
// 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
//
54
// and similarly Y(t) and Z(t).
55

    
56
public abstract class Dynamic
57
  {
58
  /**
59
   * One revolution takes us from the first point to the last and back to first through the shortest path.
60
   */
61
  public static final int MODE_LOOP = 0; 
62
  /**
63
   * One revolution takes us from the first point to the last and back to first through the same path.
64
   */
65
  public static final int MODE_PATH = 1; 
66
  /**
67
   * One revolution takes us from the first point to the last and jumps straight back to the first point.
68
   */
69
  public static final int MODE_JUMP = 2; 
70

    
71
  /**
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
  public static final int ACCESS_TYPE_RANDOM     = 0;
78
  /**
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
   * Dynamic can only be used in one effect at a time.
83
   */
84
  public static final int ACCESS_TYPE_SEQUENTIAL = 1;
85

    
86
  protected int mDimension;
87
  protected int numPoints;
88
  protected int mSegment;       // between which pair of points are we currently? (in case of PATH this is a bit complicated!)
89
  protected boolean cacheDirty; // VectorCache not up to date
90
  protected int mMode;          // LOOP, PATH or JUMP
91
  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
  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
  protected double mLastPos;
94
  protected int mAccessType;
95

    
96
  protected class VectorNoise
97
    {
98
    float[][] n;
99

    
100
    VectorNoise()
101
      {
102
      n = new float[mDimension][NUM_NOISE];
103
      }
104

    
105
    void computeNoise()
106
      {
107
      n[0][0] = mRnd.nextFloat();
108
      for(int i=1; i<NUM_NOISE; i++) n[0][i] = n[0][i-1]+mRnd.nextFloat();
109

    
110
      float sum = n[0][NUM_NOISE-1] + mRnd.nextFloat();
111

    
112
      for(int i=0; i<NUM_NOISE; i++)
113
        {
114
        n[0][i] /=sum;
115
        for(int j=1; j<mDimension; j++) n[j][i] = mRnd.nextFloat()-0.5f;
116
        }
117
      }
118
    }
119

    
120
  protected Vector<VectorNoise> vn;
121
  protected float[] mFactor;
122
  protected float[] mNoise;
123
  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
    VectorCache()
141
      {
142
      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
      }
149
    }
150

    
151
  protected Vector<VectorCache> vc;
152
  protected VectorCache tmp1, tmp2;
153
  protected float mConvexity;
154

    
155
  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
  private long mStartTime;
161
  private long mCorrectedTime;
162
  private static long mPausedTime;
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// hide this from Javadoc
166
  
167
  protected Dynamic()
168
    {
169

    
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  protected Dynamic(int duration, float count, int dimension)
175
    {
176
    vc         = new Vector<>();
177
    vn         = null;
178
    numPoints  = 0;
179
    cacheDirty = false;
180
    mMode      = MODE_LOOP;
181
    mDuration  = duration;
182
    mCount     = count;
183
    mDimension = dimension;
184
    mSegment   = -1;
185
    mLastPos   = -1;
186
    mAccessType= ACCESS_TYPE_RANDOM;
187
    mConvexity = 1.0f;
188
    mStartTime = -1;
189
    mCorrectedTime = 0;
190

    
191
    baseV      = new float[mDimension][mDimension];
192
    buf        = new float[mDimension];
193
    old        = new float[mDimension];
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  public static void onPause()
199
    {
200
    mPausedTime = System.currentTimeMillis();
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  protected float noise(float time,int vecNum)
206
    {
207
    float lower, upper, len;
208
    float d = time*(NUM_NOISE+1);
209
    int index = (int)d;
210
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
211
    VectorNoise tmpN = vn.elementAt(vecNum);
212

    
213
    float t = d-index;
214
    t = t*t*(3-2*t);
215

    
216
    switch(index)
217
      {
218
      case 0        : for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise[i+1]*tmpN.n[i+1][0]*t;
219
                      return time + mNoise[0]*(d*tmpN.n[0][0]-time);
220
      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);
221
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
222
                      lower = len + mNoise[0]*(tmpN.n[0][NUM_NOISE-1]-len);
223
                      return (1.0f-lower)*(d-NUM_NOISE) + lower;
224
      default       : float ya,yb;
225

    
226
                      for(int i=0;i<mDimension-1;i++)
227
                        {
228
                        yb = tmpN.n[i+1][index  ];
229
                        ya = tmpN.n[i+1][index-1];
230
                        mFactor[i] = mNoise[i+1]*((yb-ya)*t+ya);
231
                        }
232

    
233
                      len = ((float)index)/(NUM_NOISE+1);
234
                      lower = len + mNoise[0]*(tmpN.n[0][index-1]-len);
235
                      len = ((float)index+1)/(NUM_NOISE+1);
236
                      upper = len + mNoise[0]*(tmpN.n[0][index  ]-len);
237

    
238
                      return (upper-lower)*(d-index) + lower;
239
      }
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
// debugging only
244

    
245
  private void printBase(String str)
246
    {
247
    String s;
248
    float t;
249

    
250
    for(int i=0; i<mDimension; i++)
251
      {
252
      s = "";
253

    
254
      for(int j=0; j<mDimension; j++)
255
        {
256
        t = ((int)(1000*baseV[i][j]))/(1000.0f);
257
        s+=(" "+t);
258
        }
259
      android.util.Log.e("dynamic", str+" base "+i+" : " + s);
260
      }
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
// debugging only
265

    
266
  @SuppressWarnings("unused")
267
  private void checkBase()
268
    {
269
    float tmp, cosA;
270
    float[] len= new float[mDimension];
271
    boolean error=false;
272

    
273
    for(int i=0; i<mDimension; i++)
274
      {
275
      len[i] = 0.0f;
276

    
277
      for(int k=0; k<mDimension; k++)
278
        {
279
        len[i] += baseV[i][k]*baseV[i][k];
280
        }
281

    
282
      if( len[i] == 0.0f || len[0]/len[i] < 0.95f || len[0]/len[i]>1.05f )
283
        {
284
        android.util.Log.e("dynamic", "length of vector "+i+" : "+Math.sqrt(len[i]));
285
        error = true;
286
        }
287
      }
288

    
289
    for(int i=0; i<mDimension; i++)
290
      for(int j=i+1; j<mDimension; j++)
291
        {
292
        tmp = 0.0f;
293

    
294
        for(int k=0; k<mDimension; k++)
295
          {
296
          tmp += baseV[i][k]*baseV[j][k];
297
          }
298

    
299
        cosA = ( (len[i]==0.0f || len[j]==0.0f) ? 0.0f : tmp/(float)Math.sqrt(len[i]*len[j]));
300

    
301
        if( cosA > 0.05f || cosA < -0.05f )
302
          {
303
          android.util.Log.e("dynamic", "cos angle between vectors "+i+" and "+j+" : "+cosA);
304
          error = true;
305
          }
306
        }
307

    
308
    if( error ) printBase("");
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  int getNext(int curr, float time)
314
    {
315
    switch(mMode)
316
      {
317
      case MODE_LOOP: return curr==numPoints-1 ? 0:curr+1;
318
      case MODE_PATH: return time<0.5f ? (curr+1) : (curr==0 ? 1 : curr-1);
319
      case MODE_JUMP: return curr==numPoints-1 ? 1:curr+1;
320
      default       : return 0;
321
      }
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  private void checkAngle(int index)
327
    {
328
    float cosA = 0.0f;
329

    
330
    for(int k=0;k<mDimension; k++)
331
      cosA += baseV[index][k]*old[k];
332

    
333
    if( cosA<0.0f )
334
      {
335
/*
336
      /// DEBUGGING ////
337
      String s = index+" (";
338
      float t;
339

    
340
      for(int j=0; j<mDimension; j++)
341
        {
342
        t = ((int)(100*baseV[index][j]))/(100.0f);
343
        s+=(" "+t);
344
        }
345
      s += ") (";
346

    
347
      for(int j=0; j<mDimension; j++)
348
        {
349
        t = ((int)(100*old[j]))/(100.0f);
350
        s+=(" "+t);
351
        }
352
      s+= ")";
353

    
354
      android.util.Log.e("dynamic", "kat: " + s);
355
      /// END DEBUGGING ///
356
*/
357
      for(int j=0; j<mDimension; j++)
358
        baseV[index][j] = -baseV[index][j];
359
      }
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363
// helper function in case we are interpolating through exactly 2 points
364

    
365
  protected void computeOrthonormalBase2(Static curr, Static next)
366
    {
367
    switch(mDimension)
368
      {
369
      case 1: Static1D curr1 = (Static1D)curr;
370
              Static1D next1 = (Static1D)next;
371
              baseV[0][0] = (next1.x-curr1.x);
372
              break;
373
      case 2: Static2D curr2 = (Static2D)curr;
374
              Static2D next2 = (Static2D)next;
375
              baseV[0][0] = (next2.x-curr2.x);
376
              baseV[0][1] = (next2.y-curr2.y);
377
              break;
378
      case 3: Static3D curr3 = (Static3D)curr;
379
              Static3D next3 = (Static3D)next;
380
              baseV[0][0] = (next3.x-curr3.x);
381
              baseV[0][1] = (next3.y-curr3.y);
382
              baseV[0][2] = (next3.z-curr3.z);
383
              break;
384
      case 4: Static4D curr4 = (Static4D)curr;
385
              Static4D next4 = (Static4D)next;
386
              baseV[0][0] = (next4.x-curr4.x);
387
              baseV[0][1] = (next4.y-curr4.y);
388
              baseV[0][2] = (next4.z-curr4.z);
389
              baseV[0][3] = (next4.w-curr4.w);
390
              break;
391
      case 5: Static5D curr5 = (Static5D)curr;
392
              Static5D next5 = (Static5D)next;
393
              baseV[0][0] = (next5.x-curr5.x);
394
              baseV[0][1] = (next5.y-curr5.y);
395
              baseV[0][2] = (next5.z-curr5.z);
396
              baseV[0][3] = (next5.w-curr5.w);
397
              baseV[0][4] = (next5.v-curr5.v);
398
              break;
399
      default: throw new RuntimeException("Unsupported dimension");
400
      }
401

    
402
    if( baseV[0][0] == 0.0f )
403
      {
404
      baseV[1][0] = 1.0f;
405
      baseV[1][1] = 0.0f;
406
      }
407
    else
408
      {
409
      baseV[1][0] = 0.0f;
410
      baseV[1][1] = 1.0f;
411
      }
412

    
413
    for(int i=2; i<mDimension; i++)
414
      {
415
      baseV[1][i] = 0.0f;
416
      }
417

    
418
    computeOrthonormalBase();
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422
// helper function in case we are interpolating through more than 2 points
423

    
424
  protected void computeOrthonormalBaseMore(float time,VectorCache vc)
425
    {
426
    for(int i=0; i<mDimension; i++)
427
      {
428
      baseV[0][i] = (3*vc.a[i]*time+2*vc.b[i])*time+vc.c[i];   // first derivative, i.e. velocity vector
429
      old[i]      = baseV[1][i];
430
      baseV[1][i] =  6*vc.a[i]*time+2*vc.b[i];                 // second derivative,i.e. acceleration vector
431
      }
432

    
433
    computeOrthonormalBase();
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
// When this function gets called, baseV[0] and baseV[1] should have been filled with two mDimension-al
438
// vectors. This function then fills the rest of the baseV array with a mDimension-al Orthonormal base.
439
// (mDimension-2 vectors, pairwise orthogonal to each other and to the original 2). The function always
440
// leaves base[0] alone but generally speaking must adjust base[1] to make it orthogonal to base[0]!
441
// The whole baseV is then used to compute Noise.
442
//
443
// When computing noise of a point travelling along a N-dimensional path, there are three cases:
444
// a) we may be interpolating through 1 point, i.e. standing in place - nothing to do in this case
445
// b) we may be interpolating through 2 points, i.e. travelling along a straight line between them -
446
//    then pass the velocity vector in baseV[0] and anything linearly independent in base[1].
447
//    The output will then be discontinuous in dimensions>2 (sad corollary from the Hairy Ball Theorem)
448
//    but we don't care - we are travelling along a straight line, so velocity (aka baseV[0]!) does
449
//    not change.
450
// c) we may be interpolating through more than 2 points. Then interpolation formulas ensure the path
451
//    will never be a straight line, even locally -> we can pass in baseV[0] and baseV[1] the velocity
452
//    and the acceleration (first and second derivatives of the path) which are then guaranteed to be
453
//    linearly independent. Then we can ensure this is continuous in dimensions <=4. This leaves
454
//    dimension 5 (ATM WAVE is 5-dimensional) discontinuous -> WAVE will suffer from chaotic noise.
455
//
456
// Bear in mind here the 'normal' in 'orthonormal' means 'length equal to the length of the original
457
// velocity vector' (rather than the standard 1)
458

    
459
  protected void computeOrthonormalBase()
460
    {
461
    int last_non_zero=-1;
462
    float tmp;
463

    
464
    for(int i=0; i<mDimension; i++)
465
      if( baseV[0][i] != 0.0f )
466
        last_non_zero=i;
467

    
468
    if( last_non_zero==-1 )                                               ///
469
      {                                                                   //  velocity is the 0 vector -> two
470
      for(int i=0; i<mDimension-1; i++)                                   //  consecutive points we are interpolating
471
        for(int j=0; j<mDimension; j++)                                   //  through are identical -> no noise,
472
          baseV[i+1][j]= 0.0f;                                            //  set the base to 0 vectors.
473
      }                                                                   ///
474
    else
475
      {
476
      for(int i=1; i<mDimension; i++)                                     /// One iteration computes baseV[i][*]
477
        {                                                                 //  (aka b[i]), the i-th orthonormal vector.
478
        buf[i-1]=0.0f;                                                    //
479
                                                                          //  We can use (modified!) Gram-Schmidt.
480
        for(int k=0; k<mDimension; k++)                                   //
481
          {                                                               //
482
          if( i>=2 )                                                      //  b[0] = b[0]
483
            {                                                             //  b[1] = b[1] - (<b[1],b[0]>/<b[0],b[0]>)*b[0]
484
            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]
485
            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]
486
            }                                                             //  (...)
487
                                                                          //  then b[i] = b[i] / |b[i]|  ( Here really b[i] = b[i] / (|b[0]|/|b[i]|)
488
          tmp = baseV[i-1][k];                                            //
489
          buf[i-1] += tmp*tmp;                                            //
490
          }                                                               //
491
                                                                          //
492
        for(int j=0; j<i; j++)                                            //
493
          {                                                               //
494
          tmp = 0.0f;                                                     //
495
          for(int k=0;k<mDimension; k++) tmp += baseV[i][k]*baseV[j][k];  //
496
          tmp /= buf[j];                                                  //
497
          for(int k=0;k<mDimension; k++) baseV[i][k] -= tmp*baseV[j][k];  //
498
          }                                                               //
499
                                                                          //
500
        checkAngle(i);                                                    //
501
        }                                                                 /// end compute baseV[i][*]
502

    
503
      buf[mDimension-1]=0.0f;                                             /// Normalize
504
      for(int k=0; k<mDimension; k++)                                     //
505
        {                                                                 //
506
        tmp = baseV[mDimension-1][k];                                     //
507
        buf[mDimension-1] += tmp*tmp;                                     //
508
        }                                                                 //
509
                                                                          //
510
      for(int i=1; i<mDimension; i++)                                     //
511
        {                                                                 //
512
        tmp = (float)Math.sqrt(buf[0]/buf[i]);                            //
513
        for(int k=0;k<mDimension; k++) baseV[i][k] *= tmp;                //
514
        }                                                                 /// End Normalize
515
      }
516
    }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

    
520
  abstract void interpolate(float[] buffer, int offset, float time);
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523
// PUBLIC API
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
/**
527
 * Sets the mode of the interpolation to Loop, Path or Jump.
528
 * <ul>
529
 * <li>Loop is when we go from the first point all the way to the last, and the back to the first through 
530
 * the shortest way.
531
 * <li>Path is when we come back from the last point back to the first the same way we got there.
532
 * <li>Jump is when we go from first to last and then jump straight back to the first.
533
 * </ul>
534
 * 
535
 * @param mode {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
536
 */
537
  public void setMode(int mode)
538
    {
539
    mMode = mode;  
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543
/**
544
 * Returns the number of Points this Dynamic has been fed with.
545
 *   
546
 * @return the number of Points we are currently interpolating through.
547
 */
548
  public synchronized int getNumPoints()
549
    {
550
    return numPoints;  
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554
/**
555
 * Sets how many revolutions we want to do.
556
 * <p>
557
 * Does not have to be an integer. What constitutes 'one revolution' depends on the MODE:
558
 * {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
559
 * Count<=0 means 'go on interpolating indefinitely'.
560
 * 
561
 * @param count the number of times we want to interpolate between our collection of Points.
562
 */
563
  public void setCount(float count)
564
    {
565
    mCount = count;  
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569
/**
570
 * Return the number of revolutions this Dynamic will make.
571
 * What constitutes 'one revolution' depends on the MODE:
572
 * {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
573
 *
574
 * @return the number revolutions this Dynamic will make.
575
 */
576
  public float getCount()
577
    {
578
    return mCount;
579
    }
580

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582
/**
583
 * Start running from the beginning again.
584
 *
585
 * If a Dynamic has been used already, and we want to use it again and start interpolating from the
586
 * first Point, first we need to reset it using this method.
587
 */
588
  public void resetToBeginning()
589
    {
590
    mStartTime = -1;
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594
/**
595
 * @param duration Number of milliseconds one revolution will take.
596
 *                 What constitutes 'one revolution' depends on the MODE:
597
 *                 {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
598
 */
599
  public void setDuration(long duration)
600
    {
601
    mDuration = duration;
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
/**
606
 * @return Number of milliseconds one revolution will take.
607
 */
608
  public long getDuration()
609
    {
610
    return mDuration;
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614
/**
615
 * @param convexity If set to the default (1.0f) then interpolation between 4 points
616
 *                  (1,0) (0,1) (-1,0) (0,-1) will be the natural circle centered at (0,0) with radius 1.
617
 *                  The less it is, the less convex the circle becomes, ultimately when convexity=0.0f
618
 *                  then the interpolation shape will be straight lines connecting the four points.
619
 *                  Further setting this to negative values will make the shape concave.
620
 *                  Valid values: all floats. (although probably only something around (0,2) actually
621
 *                  makes sense)
622
 */
623
  public void setConvexity(float convexity)
624
    {
625
    if( mConvexity!=convexity )
626
      {
627
      mConvexity = convexity;
628
      cacheDirty = true;
629
      }
630
    }
631

    
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633
/**
634
 * @return See {@link Dynamic#setConvexity(float)}
635
 */
636
  public float getConvexity()
637
    {
638
    return mConvexity;
639
    }
640

    
641
///////////////////////////////////////////////////////////////////////////////////////////////////
642
/**
643
 * Sets the access type this Dynamic will be working in.
644
 *
645
 * @param type {@link Dynamic#ACCESS_TYPE_RANDOM} or {@link Dynamic#ACCESS_TYPE_SEQUENTIAL}.
646
 */
647
  public void setAccessType(int type)
648
    {
649
    mAccessType = type;
650
    mLastPos = -1;
651
    }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654
/**
655
 * Return the Dimension, ie number of floats in a single Point this Dynamic interpolates through.
656
 *
657
 * @return number of floats in a single Point (ie its dimension) contained in the Dynamic.
658
 */
659
  public int getDimension()
660
    {
661
    return mDimension;
662
    }
663

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
/**
666
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
667
 * <p>
668
 * This version differs from the previous in that it returns a boolean value which indicates whether
669
 * the interpolation is finished.
670
 *
671
 * @param buffer Float buffer we will write the results to.
672
 * @param offset Offset in the buffer where to write the result.
673
 * @param time   Time of interpolation. Time=0.0 is the beginning of the first revolution, time=1.0 - the end
674
 *               of the first revolution, time=2.5 - the middle of the third revolution.
675
 *               What constitutes 'one revolution' depends on the MODE:
676
 *               {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
677
 * @param step   Time difference between now and the last time we called this function. Needed to figure
678
 *               out if the previous time we were called the effect wasn't finished yet, but now it is.
679
 * @return true if the interpolation reached its end.
680
 */
681
  public boolean get(float[] buffer, int offset, long time, long step)
682
    {
683
    if( mDuration<=0.0f )
684
      {
685
      interpolate(buffer,offset,mCount-(int)mCount);
686
      return false;
687
      }
688

    
689
    if( mStartTime==-1 )
690
      {
691
      mStartTime = time;
692
      mLastPos   = -1;
693
      }
694

    
695
    long diff = time-mPausedTime;
696

    
697
    if( mStartTime<mPausedTime && mCorrectedTime<mPausedTime && diff>=0 && diff<=step )
698
      {
699
      mCorrectedTime = mPausedTime;
700
      mStartTime += diff;
701
      step -= diff;
702
      }
703

    
704
    time -= mStartTime;
705

    
706
    if( time+step > mDuration*mCount && mCount>0.0f )
707
      {
708
      interpolate(buffer,offset,mCount-(int)mCount);
709
      return true;
710
      }
711

    
712
    double pos;
713

    
714
    if( mAccessType ==ACCESS_TYPE_SEQUENTIAL )
715
      {
716
      pos = mLastPos<0 ? (double)time/mDuration : (double)step/mDuration + mLastPos;
717
      mLastPos = pos;
718
      }
719
    else
720
      {
721
      pos = (double)time/mDuration;
722
      }
723

    
724
    interpolate(buffer,offset, (float)(pos-(int)pos) );
725
    return false;
726
    }
727

    
728
///////////////////////////////////////////////////////////////////////////////////////////////////
729
  }
(6-6/18)