Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic.java @ 0bff397a

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 static long mPausedTime;
162

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

    
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

    
189
    baseV      = new float[mDimension][mDimension];
190
    buf        = new float[mDimension];
191
    old        = new float[mDimension];
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

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

    
211
    float t = d-index;
212
    t = t*t*(3-2*t);
213

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

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

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

    
236
                      return (upper-lower)*(d-index) + lower;
237
      }
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// debugging only
242

    
243
  private void printBase(String str)
244
    {
245
    String s;
246
    float t;
247

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

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
// debugging only
263

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

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

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

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

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

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

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

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

    
306
    if( error ) printBase("");
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

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

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

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

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

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

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

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

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

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
// helper function in case we are interpolating through exactly 2 points
362

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

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

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

    
416
    computeOrthonormalBase();
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420
// helper function in case we are interpolating through more than 2 points
421

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

    
431
    computeOrthonormalBase();
432
    }
433

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

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

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

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

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

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  abstract void interpolate(float[] buffer, int offset, float time);
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521
// PUBLIC API
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

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

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

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

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

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

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

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

    
611

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

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

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

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

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

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

    
694
    long diff = time-mPausedTime;
695

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

    
702
    time -= mStartTime;
703

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

    
710
    double pos;
711

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

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

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727
  }
(6-6/18)