Project

General

Profile

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

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

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 org.distorted.library.main.InternalMaster;
23

    
24
import java.util.ArrayList;
25
import java.util.Random;
26
import java.util.Vector;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/** A class to interpolate between a list of Statics.
30
* <p><ul>
31
* <li>if there is only one Point, just return it.
32
* <li>if there are two Points, linearly bounce between them
33
* <li>if there are more, interpolate a path between them. Exact way we interpolate depends on the MODE.
34
* </ul>
35
*/
36

    
37
// The way Interpolation between more than 2 Points is done:
38
// 
39
// 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
//
42
// 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
//
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
// 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
//
51
// we have the solution:  X(t) = at^3 + bt^2 + ct + d where
52
// 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
//
57
// and similarly Y(t) and Z(t).
58

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

    
74
  /**
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
  public static final int ACCESS_TYPE_RANDOM     = 0;
81
  /**
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
   * Dynamic can only be used in one effect at a time.
86
   */
87
  public static final int ACCESS_TYPE_SEQUENTIAL = 1;
88

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

    
99
  protected class VectorNoise
100
    {
101
    float[][] n;
102

    
103
    VectorNoise()
104
      {
105
      n = new float[mDimension][NUM_NOISE];
106
      }
107

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

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

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

    
123
  protected Vector<VectorNoise> vn;
124
  protected float[] mFactor;
125
  protected float[] mNoise;
126
  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
    VectorCache()
144
      {
145
      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
      }
152
    }
153

    
154
  protected Vector<VectorCache> vc;
155
  protected VectorCache tmp1, tmp2;
156
  protected float mConvexity;
157

    
158
  private float[] buf;
159
  private float[] old;
160
  private static final Random mRnd = new Random();
161
  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
  private long mStartTime;
164
  private long mCorrectedTime;
165
  private static long mPausedTime;
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// hide this from Javadoc
169
  
170
  protected Dynamic()
171
    {
172

    
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

    
194
    baseV      = new float[mDimension][mDimension];
195
    buf        = new float[mDimension];
196
    old        = new float[mDimension];
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  void initDynamic()
202
    {
203
    mStartTime = -1;
204
    mCorrectedTime = 0;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public static void onPause()
210
    {
211
    mPausedTime = System.currentTimeMillis();
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      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
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
233
                      lower = len + mNoise[0]*(tmpN.n[0][NUM_NOISE-1]-len);
234
                      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
                        mFactor[i] = mNoise[i+1]*((yb-ya)*t+ya);
242
                        }
243

    
244
                      len = ((float)index)/(NUM_NOISE+1);
245
                      lower = len + mNoise[0]*(tmpN.n[0][index-1]-len);
246
                      len = ((float)index+1)/(NUM_NOISE+1);
247
                      upper = len + mNoise[0]*(tmpN.n[0][index  ]-len);
248

    
249
                      return (upper-lower)*(d-index) + lower;
250
      }
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  @SuppressWarnings("unused")
278
  private void checkBase()
279
    {
280
    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

    
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
        cosA = ( (len[i]==0.0f || len[j]==0.0f) ? 0.0f : tmp/(float)Math.sqrt(len[i]*len[j]));
311

    
312
        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
        }
318

    
319
    if( error ) printBase("");
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
374
// helper function in case we are interpolating through exactly 2 points
375

    
376
  protected void computeOrthonormalBase2(Static curr, Static next)
377
    {
378
    switch(mDimension)
379
      {
380
      case 1: Static1D curr1 = (Static1D)curr;
381
              Static1D next1 = (Static1D)next;
382
              baseV[0][0] = (next1.x-curr1.x);
383
              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
      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
      }
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
    float tmp;
474

    
475
    for(int i=0; i<mDimension; i++)
476
      if( baseV[0][i] != 0.0f )
477
        last_non_zero=i;
478

    
479
    if( last_non_zero==-1 )                                               ///
480
      {                                                                   //  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
    else
486
      {
487
      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
      }
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
  abstract void interpolate(float[] buffer, int offset, float time);
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534
// PUBLIC API
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
/**
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
 * <li>Jump is when we go from first to last and then jump straight back to the first.
544
 * </ul>
545
 * 
546
 * @param mode {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
547
 */
548
  public void setMode(int mode)
549
    {
550
    mMode = mode;  
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554
/**
555
 * Returns the number of Points this Dynamic has been fed with.
556
 *   
557
 * @return the number of Points we are currently interpolating through.
558
 */
559
  public synchronized int getNumPoints()
560
    {
561
    return numPoints;  
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
/**
566
 * Sets how many revolutions we want to do.
567
 * <p>
568
 * 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
 * Count<=0 means 'go on interpolating indefinitely'.
571
 * 
572
 * @param count the number of times we want to interpolate between our collection of Points.
573
 */
574
  public void setCount(float count)
575
    {
576
    mCount = count;  
577
    }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580
/**
581
 * 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
 *
585
 * @return the number revolutions this Dynamic will make.
586
 */
587
  public float getCount()
588
    {
589
    return mCount;
590
    }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593
/**
594
 * Start running from the beginning again.
595
 *
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
 */
599
  public void resetToBeginning()
600
    {
601
    mStartTime = -1;
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
/**
606
 * @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
 */
610
  public void setDuration(long duration)
611
    {
612
    mDuration = duration;
613
    }
614

    
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616
/**
617
 * @return Number of milliseconds one revolution will take.
618
 */
619
  public long getDuration()
620
    {
621
    return mDuration;
622
    }
623

    
624
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
653
/**
654
 * Sets the access type this Dynamic will be working in.
655
 *
656
 * @param type {@link Dynamic#ACCESS_TYPE_RANDOM} or {@link Dynamic#ACCESS_TYPE_SEQUENTIAL}.
657
 */
658
  public void setAccessType(int type)
659
    {
660
    mAccessType = type;
661
    mLastPos = -1;
662
    }
663

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
/**
666
 * Return the Dimension, ie number of floats in a single Point this Dynamic interpolates through.
667
 *
668
 * @return number of floats in a single Point (ie its dimension) contained in the Dynamic.
669
 */
670
  public int getDimension()
671
    {
672
    return mDimension;
673
    }
674

    
675
///////////////////////////////////////////////////////////////////////////////////////////////////
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
 * @param buffer Float buffer we will write the results to.
683
 * @param offset Offset in the buffer where to write the result.
684
 * @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
 * @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
 * @return true if the interpolation reached its end.
691
 */
692
  public boolean get(float[] buffer, int offset, long time, long step)
693
    {
694
    if( mDuration<=0.0f )
695
      {
696
      interpolate(buffer,offset,mCount-(int)mCount);
697
      return false;
698
      }
699

    
700
    if( mStartTime==-1 )
701
      {
702
      mStartTime = time;
703
      mLastPos   = -1;
704
      }
705

    
706
    long diff = time-mPausedTime;
707

    
708
    if( mStartTime<mPausedTime && mCorrectedTime<mPausedTime && diff>=0 && diff<=step )
709
      {
710
      mCorrectedTime = mPausedTime;
711
      mStartTime += diff;
712
      step -= diff;
713
      }
714

    
715
    time -= mStartTime;
716

    
717
    if( time+step > mDuration*mCount && mCount>0.0f )
718
      {
719
      interpolate(buffer,offset,mCount-(int)mCount);
720
      return true;
721
      }
722

    
723
    double pos;
724

    
725
    if( mAccessType ==ACCESS_TYPE_SEQUENTIAL )
726
      {
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
    interpolate(buffer,offset, (float)(pos-(int)pos) );
736
    return false;
737
    }
738
  }
(6-6/18)