Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic.java @ 12ecac18

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 mTimeOffset;
161
  private boolean mSetOffset;
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

    
188
    mTimeOffset= 0;
189
    mSetOffset = true;
190

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

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

    
198
  protected float noise(float time,int vecNum)
199
    {
200
    float lower, upper, len;
201
    float d = time*(NUM_NOISE+1);
202
    int index = (int)d;
203
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
204
    VectorNoise tmpN = vn.elementAt(vecNum);
205

    
206
    float t = d-index;
207
    t = t*t*(3-2*t);
208

    
209
    switch(index)
210
      {
211
      case 0        : for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise[i+1]*tmpN.n[i+1][0]*t;
212
                      return time + mNoise[0]*(d*tmpN.n[0][0]-time);
213
      case NUM_NOISE: for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise[i+1]*tmpN.n[i+1][NUM_NOISE-1]*(1-t);
214
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
215
                      lower = len + mNoise[0]*(tmpN.n[0][NUM_NOISE-1]-len);
216
                      return (1.0f-lower)*(d-NUM_NOISE) + lower;
217
      default       : float ya,yb;
218

    
219
                      for(int i=0;i<mDimension-1;i++)
220
                        {
221
                        yb = tmpN.n[i+1][index  ];
222
                        ya = tmpN.n[i+1][index-1];
223
                        mFactor[i] = mNoise[i+1]*((yb-ya)*t+ya);
224
                        }
225

    
226
                      len = ((float)index)/(NUM_NOISE+1);
227
                      lower = len + mNoise[0]*(tmpN.n[0][index-1]-len);
228
                      len = ((float)index+1)/(NUM_NOISE+1);
229
                      upper = len + mNoise[0]*(tmpN.n[0][index  ]-len);
230

    
231
                      return (upper-lower)*(d-index) + lower;
232
      }
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
// debugging only
237

    
238
  private void printBase(String str)
239
    {
240
    String s;
241
    float t;
242

    
243
    for(int i=0; i<mDimension; i++)
244
      {
245
      s = "";
246

    
247
      for(int j=0; j<mDimension; j++)
248
        {
249
        t = ((int)(1000*baseV[i][j]))/(1000.0f);
250
        s+=(" "+t);
251
        }
252
      android.util.Log.e("dynamic", str+" base "+i+" : " + s);
253
      }
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
// debugging only
258

    
259
  @SuppressWarnings("unused")
260
  private void checkBase()
261
    {
262
    float tmp, cosA;
263
    float[] len= new float[mDimension];
264
    boolean error=false;
265

    
266
    for(int i=0; i<mDimension; i++)
267
      {
268
      len[i] = 0.0f;
269

    
270
      for(int k=0; k<mDimension; k++)
271
        {
272
        len[i] += baseV[i][k]*baseV[i][k];
273
        }
274

    
275
      if( len[i] == 0.0f || len[0]/len[i] < 0.95f || len[0]/len[i]>1.05f )
276
        {
277
        android.util.Log.e("dynamic", "length of vector "+i+" : "+Math.sqrt(len[i]));
278
        error = true;
279
        }
280
      }
281

    
282
    for(int i=0; i<mDimension; i++)
283
      for(int j=i+1; j<mDimension; j++)
284
        {
285
        tmp = 0.0f;
286

    
287
        for(int k=0; k<mDimension; k++)
288
          {
289
          tmp += baseV[i][k]*baseV[j][k];
290
          }
291

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

    
294
        if( cosA > 0.05f || cosA < -0.05f )
295
          {
296
          android.util.Log.e("dynamic", "cos angle between vectors "+i+" and "+j+" : "+cosA);
297
          error = true;
298
          }
299
        }
300

    
301
    if( error ) printBase("");
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  private void checkAngle(int index)
307
    {
308
    float cosA = 0.0f;
309

    
310
    for(int k=0;k<mDimension; k++)
311
      cosA += baseV[index][k]*old[k];
312

    
313
    if( cosA<0.0f )
314
      {
315
/*
316
      /// DEBUGGING ////
317
      String s = index+" (";
318
      float t;
319

    
320
      for(int j=0; j<mDimension; j++)
321
        {
322
        t = ((int)(100*baseV[index][j]))/(100.0f);
323
        s+=(" "+t);
324
        }
325
      s += ") (";
326

    
327
      for(int j=0; j<mDimension; j++)
328
        {
329
        t = ((int)(100*old[j]))/(100.0f);
330
        s+=(" "+t);
331
        }
332
      s+= ")";
333

    
334
      android.util.Log.e("dynamic", "kat: " + s);
335
      /// END DEBUGGING ///
336
*/
337
      for(int j=0; j<mDimension; j++)
338
        baseV[index][j] = -baseV[index][j];
339
      }
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
// helper function in case we are interpolating through exactly 2 points
344

    
345
  protected void computeOrthonormalBase2(Static curr, Static next)
346
    {
347
    switch(mDimension)
348
      {
349
      case 1: Static1D curr1 = (Static1D)curr;
350
              Static1D next1 = (Static1D)next;
351
              baseV[0][0] = (next1.x-curr1.x);
352
              break;
353
      case 2: Static2D curr2 = (Static2D)curr;
354
              Static2D next2 = (Static2D)next;
355
              baseV[0][0] = (next2.x-curr2.x);
356
              baseV[0][1] = (next2.y-curr2.y);
357
              break;
358
      case 3: Static3D curr3 = (Static3D)curr;
359
              Static3D next3 = (Static3D)next;
360
              baseV[0][0] = (next3.x-curr3.x);
361
              baseV[0][1] = (next3.y-curr3.y);
362
              baseV[0][2] = (next3.z-curr3.z);
363
              break;
364
      case 4: Static4D curr4 = (Static4D)curr;
365
              Static4D next4 = (Static4D)next;
366
              baseV[0][0] = (next4.x-curr4.x);
367
              baseV[0][1] = (next4.y-curr4.y);
368
              baseV[0][2] = (next4.z-curr4.z);
369
              baseV[0][3] = (next4.w-curr4.w);
370
              break;
371
      case 5: Static5D curr5 = (Static5D)curr;
372
              Static5D next5 = (Static5D)next;
373
              baseV[0][0] = (next5.x-curr5.x);
374
              baseV[0][1] = (next5.y-curr5.y);
375
              baseV[0][2] = (next5.z-curr5.z);
376
              baseV[0][3] = (next5.w-curr5.w);
377
              baseV[0][4] = (next5.v-curr5.v);
378
              break;
379
      default: throw new RuntimeException("Unsupported dimension");
380
      }
381

    
382
    if( baseV[0][0] == 0.0f )
383
      {
384
      baseV[1][0] = 1.0f;
385
      baseV[1][1] = 0.0f;
386
      }
387
    else
388
      {
389
      baseV[1][0] = 0.0f;
390
      baseV[1][1] = 1.0f;
391
      }
392

    
393
    for(int i=2; i<mDimension; i++)
394
      {
395
      baseV[1][i] = 0.0f;
396
      }
397

    
398
    computeOrthonormalBase();
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402
// helper function in case we are interpolating through more than 2 points
403

    
404
  protected void computeOrthonormalBaseMore(float time,VectorCache vc)
405
    {
406
    for(int i=0; i<mDimension; i++)
407
      {
408
      baseV[0][i] = (3*vc.a[i]*time+2*vc.b[i])*time+vc.c[i];   // first derivative, i.e. velocity vector
409
      old[i]      = baseV[1][i];
410
      baseV[1][i] =  6*vc.a[i]*time+2*vc.b[i];                 // second derivative,i.e. acceleration vector
411
      }
412

    
413
    computeOrthonormalBase();
414
    }
415

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

    
439
  protected void computeOrthonormalBase()
440
    {
441
    int last_non_zero=-1;
442
    float tmp;
443

    
444
    for(int i=0; i<mDimension; i++)
445
      if( baseV[0][i] != 0.0f )
446
        last_non_zero=i;
447

    
448
    if( last_non_zero==-1 )                                               ///
449
      {                                                                   //  velocity is the 0 vector -> two
450
      for(int i=0; i<mDimension-1; i++)                                   //  consecutive points we are interpolating
451
        for(int j=0; j<mDimension; j++)                                   //  through are identical -> no noise,
452
          baseV[i+1][j]= 0.0f;                                            //  set the base to 0 vectors.
453
      }                                                                   ///
454
    else
455
      {
456
      for(int i=1; i<mDimension; i++)                                     /// One iteration computes baseV[i][*]
457
        {                                                                 //  (aka b[i]), the i-th orthonormal vector.
458
        buf[i-1]=0.0f;                                                    //
459
                                                                          //  We can use (modified!) Gram-Schmidt.
460
        for(int k=0; k<mDimension; k++)                                   //
461
          {                                                               //
462
          if( i>=2 )                                                      //  b[0] = b[0]
463
            {                                                             //  b[1] = b[1] - (<b[1],b[0]>/<b[0],b[0]>)*b[0]
464
            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]
465
            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]
466
            }                                                             //  (...)
467
                                                                          //  then b[i] = b[i] / |b[i]|  ( Here really b[i] = b[i] / (|b[0]|/|b[i]|)
468
          tmp = baseV[i-1][k];                                            //
469
          buf[i-1] += tmp*tmp;                                            //
470
          }                                                               //
471
                                                                          //
472
        for(int j=0; j<i; j++)                                            //
473
          {                                                               //
474
          tmp = 0.0f;                                                     //
475
          for(int k=0;k<mDimension; k++) tmp += baseV[i][k]*baseV[j][k];  //
476
          tmp /= buf[j];                                                  //
477
          for(int k=0;k<mDimension; k++) baseV[i][k] -= tmp*baseV[j][k];  //
478
          }                                                               //
479
                                                                          //
480
        checkAngle(i);                                                    //
481
        }                                                                 /// end compute baseV[i][*]
482

    
483
      buf[mDimension-1]=0.0f;                                             /// Normalize
484
      for(int k=0; k<mDimension; k++)                                     //
485
        {                                                                 //
486
        tmp = baseV[mDimension-1][k];                                     //
487
        buf[mDimension-1] += tmp*tmp;                                     //
488
        }                                                                 //
489
                                                                          //
490
      for(int i=1; i<mDimension; i++)                                     //
491
        {                                                                 //
492
        tmp = (float)Math.sqrt(buf[0]/buf[i]);                            //
493
        for(int k=0;k<mDimension; k++) baseV[i][k] *= tmp;                //
494
        }                                                                 /// End Normalize
495
      }
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
  abstract void interpolate(float[] buffer, int offset, float time);
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
// PUBLIC API
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
/**
507
 * Sets the mode of the interpolation to Loop, Path or Jump.
508
 * <ul>
509
 * <li>Loop is when we go from the first point all the way to the last, and the back to the first through 
510
 * the shortest way.
511
 * <li>Path is when we come back from the last point back to the first the same way we got there.
512
 * <li>Jump is when we go from first to last and then jump straight back to the first.
513
 * </ul>
514
 * 
515
 * @param mode {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
516
 */
517
  public void setMode(int mode)
518
    {
519
    mMode = mode;  
520
    }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523
/**
524
 * Returns the number of Points this Dynamic has been fed with.
525
 *   
526
 * @return the number of Points we are currently interpolating through.
527
 */
528
  public synchronized int getNumPoints()
529
    {
530
    return numPoints;  
531
    }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534
/**
535
 * Sets how many revolutions we want to do.
536
 * <p>
537
 * Does not have to be an integer. What constitutes 'one revolution' depends on the MODE:
538
 * {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
539
 * Count<=0 means 'go on interpolating indefinitely'.
540
 * 
541
 * @param count the number of times we want to interpolate between our collection of Points.
542
 */
543
  public void setCount(float count)
544
    {
545
    mCount = count;  
546
    }
547

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549
/**
550
 * Return the number of revolutions this Dynamic will make.
551
 * What constitutes 'one revolution' depends on the MODE:
552
 * {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
553
 *
554
 * @return the number revolutions this Dynamic will make.
555
 */
556
  public float getCount()
557
    {
558
    return mCount;
559
    }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562
/**
563
 * Start running from the beginning again.
564
 *
565
 * If a Dynamic has been used already, and we want to use it again and start interpolating from the
566
 * first Point, first we need to reset it using this method.
567
 */
568
  public void resetToBeginning()
569
    {
570
    mSetOffset = true;
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574
/**
575
 * @param duration Number of milliseconds one revolution will take.
576
 *                 What constitutes 'one revolution' depends on the MODE:
577
 *                 {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
578
 */
579
  public void setDuration(long duration)
580
    {
581
    mDuration = duration;
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585
/**
586
 * @return Number of milliseconds one revolution will take.
587
 */
588
  public long getDuration()
589
    {
590
    return mDuration;
591
    }
592

    
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595
/**
596
 * @param convexity If set to the default (1.0f) then interpolation between 4 points
597
 *                  (1,0) (0,1) (-1,0) (0,-1) will be the natural circle centered at (0,0) with radius 1.
598
 *                  The less it is, the less convex the circle becomes, ultimately when convexity=0.0f
599
 *                  then the interpolation shape will be straight lines connecting the four points.
600
 *                  Further setting this to negative values will make the shape concave.
601
 *                  Valid values: all floats. (although probably only something around (0,2) actually
602
 *                  makes sense)
603
 */
604
  public void setConvexity(float convexity)
605
    {
606
    if( mConvexity!=convexity )
607
      {
608
      mConvexity = convexity;
609
      cacheDirty = true;
610
      }
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614
/**
615
 * @return See {@link Dynamic#setConvexity(float)}
616
 */
617
  public float getConvexity()
618
    {
619
    return mConvexity;
620
    }
621

    
622
///////////////////////////////////////////////////////////////////////////////////////////////////
623
/**
624
 * Sets the access type this Dynamic will be working in.
625
 *
626
 * @param type {@link Dynamic#ACCESS_TYPE_RANDOM} or {@link Dynamic#ACCESS_TYPE_SEQUENTIAL}.
627
 */
628
  public void setAccessType(int type)
629
    {
630
    mAccessType = type;
631
    mLastPos = -1;
632
    }
633

    
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635
/**
636
 * Return the Dimension, ie number of floats in a single Point this Dynamic interpolates through.
637
 *
638
 * @return number of floats in a single Point (ie its dimension) contained in the Dynamic.
639
 */
640
  public int getDimension()
641
    {
642
    return mDimension;
643
    }
644

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646
/**
647
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
648
 *
649
 * @param buffer Float buffer we will write the results to.
650
 * @param offset Offset in the buffer where to write the result.
651
 * @param time   Time of interpolation. Time=0.0 is the beginning of the first revolution, time=1.0 - the end
652
 *               of the first revolution, time=2.5 - the middle of the third revolution.
653
 *               What constitutes 'one revolution' depends on the MODE:
654
 *               {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
655
 */
656
  public void get(float[] buffer, int offset, long time)
657
    {
658
    if( mDuration<=0.0f )
659
      {
660
      interpolate(buffer,offset,mCount-(int)mCount);
661
      }
662
    else
663
      {
664
      if( mSetOffset )
665
        {
666
        mSetOffset = false;
667
        mTimeOffset= time;
668
        mLastPos   = -1;
669
        }
670

    
671
      time -= mTimeOffset;
672

    
673
      double pos = (double)time/mDuration;
674

    
675
      if( pos<=mCount || mCount<=0.0f )
676
        {
677
        interpolate(buffer,offset, (float)(pos-(int)pos) );
678
        }
679
      }
680
    }
681

    
682
///////////////////////////////////////////////////////////////////////////////////////////////////
683
/**
684
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
685
 * <p>
686
 * This version differs from the previous in that it returns a boolean value which indicates whether
687
 * the interpolation is finished.
688
 *
689
 * @param buffer Float buffer we will write the results to.
690
 * @param offset Offset in the buffer where to write the result.
691
 * @param time   Time of interpolation. Time=0.0 is the beginning of the first revolution, time=1.0 - the end
692
 *               of the first revolution, time=2.5 - the middle of the third revolution.
693
 *               What constitutes 'one revolution' depends on the MODE:
694
 *               {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
695
 * @param step   Time difference between now and the last time we called this function. Needed to figure
696
 *               out if the previous time we were called the effect wasn't finished yet, but now it is.
697
 * @return true if the interpolation reached its end.
698
 */
699
  public boolean get(float[] buffer, int offset, long time, long step)
700
    {
701
    if( mDuration<=0.0f )
702
      {
703
      interpolate(buffer,offset,mCount-(int)mCount);
704
      return false;
705
      }
706

    
707
    if( mSetOffset )
708
      {
709
      mSetOffset = false;
710
      mTimeOffset= time;
711
      mLastPos   = -1;
712
      }
713

    
714
    time -= mTimeOffset;
715

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

    
722
    double pos;
723

    
724
    if( mAccessType ==ACCESS_TYPE_SEQUENTIAL )
725
      {
726
      pos = mLastPos<0 ? (double)time/mDuration : (double)step/mDuration + mLastPos;
727
      mLastPos = pos;
728
      }
729
    else
730
      {
731
      pos = (double)time/mDuration;
732
      }
733

    
734
    interpolate(buffer,offset, (float)(pos-(int)pos) );
735
    return false;
736
    }
737

    
738
///////////////////////////////////////////////////////////////////////////////////////////////////
739
  }
(6-6/18)