Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic.java @ f871c455

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

    
154
  private float[] buf;
155
  private float[] old;
156
  private static Random mRnd = new Random();
157
  private static final int NUM_NOISE = 5; // used iff mNoise>0.0. Number of intermediary points between each pair of adjacent vectors
158
                                          // where we randomize noise factors to make the way between the two vectors not so smooth.
159
  private long mTimeOffset;
160
  private boolean mSetOffset;
161

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

    
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

    
186
    mTimeOffset = 0;
187
    mSetOffset  = true;
188

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

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

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

    
204
    float t = d-index;
205
    t = t*t*(3-2*t);
206

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

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

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

    
229
                      return (upper-lower)*(d-index) + lower;
230
      }
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// debugging only
235

    
236
  private void printBase(String str)
237
    {
238
    String s;
239
    float t;
240

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

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
// debugging only
256

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

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

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

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

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

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

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

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

    
299
    if( error ) printBase("");
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

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

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

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

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

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

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

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
// helper function in case we are interpolating through exactly 2 points
342

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

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

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

    
396
    computeOrthonormalBase();
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
// helper function in case we are interpolating through more than 2 points
401

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

    
411
    computeOrthonormalBase();
412
    }
413

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

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

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

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

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

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  abstract void interpolate(float[] buffer, int offset, float time);
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501
// PUBLIC API
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

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

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

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

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

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

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

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

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592
/**
593
 * Sets the access type this Dynamic will be working in.
594
 *
595
 * @param type {@link Dynamic#ACCESS_TYPE_RANDOM} or {@link Dynamic#ACCESS_TYPE_SEQUENTIAL}.
596
 */
597
  public void setAccessType(int type)
598
    {
599
    mAccessType = type;
600
    mLastPos = -1;
601
    }
602

    
603
///////////////////////////////////////////////////////////////////////////////////////////////////
604
/**
605
 * Return the Dimension, ie number of floats in a single Point this Dynamic interpolates through.
606
 *
607
 * @return number of floats in a single Point (ie its dimension) contained in the Dynamic.
608
 */
609
  public int getDimension()
610
    {
611
    return mDimension;
612
    }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615
/**
616
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
617
 *
618
 * @param buffer Float buffer we will write the results to.
619
 * @param offset Offset in the buffer where to write the result.
620
 * @param time   Time of interpolation. Time=0.0 is the beginning of the first revolution, time=1.0 - the end
621
 *               of the first revolution, time=2.5 - the middle of the third revolution.
622
 *               What constitutes 'one revolution' depends on the MODE:
623
 *               {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
624
 */
625
  public void get(float[] buffer, int offset, long time)
626
    {
627
    if( mDuration<=0.0f )
628
      {
629
      interpolate(buffer,offset,mCount-(int)mCount);
630
      }
631
    else
632
      {
633
      if( mSetOffset )
634
        {
635
        mSetOffset = false;
636
        mTimeOffset= time;
637
        mLastPos   = -1;
638
        }
639

    
640
      time -= mTimeOffset;
641

    
642
      double pos = (double)time/mDuration;
643

    
644
      if( pos<=mCount || mCount<=0.0f )
645
        {
646
        interpolate(buffer,offset, (float)(pos-(int)pos) );
647
        }
648
      }
649
    }
650

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

    
676
    if( mSetOffset )
677
      {
678
      mSetOffset = false;
679
      mTimeOffset= time;
680
      mLastPos   = -1;
681
      }
682

    
683
    time -= mTimeOffset;
684

    
685
    if( time+step > mDuration*mCount && mCount>0.0f )
686
      {
687
      interpolate(buffer,offset,mCount-(int)mCount);
688
      return true;
689
      }
690

    
691
    double pos;
692

    
693
    if( mAccessType ==ACCESS_TYPE_SEQUENTIAL )
694
      {
695
      pos = mLastPos<0 ? (double)time/mDuration : (double)step/mDuration + mLastPos;
696
      mLastPos = pos;
697
      }
698
    else
699
      {
700
      pos = (double)time/mDuration;
701
      }
702

    
703
    interpolate(buffer,offset, (float)(pos-(int)pos) );
704
    return false;
705
    }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708
  }
(6-6/18)