Project

General

Profile

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

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

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 w[i] = (w[i](x), w[i](y), w[i](z)) be the direction and speed we have to be flying at Point P[i]
37
//
38
// time it takes to fly though one segment v[i] --> v[i+1] : 0.0 --> 1.0
39
// w[i] should be parallel to v[i+1] - v[i-1]   (cyclic notation)
40
// |w[i]| proportional to | P[i]-P[i+1] |
41
//
42
// Given that the flight route (X(t), Y(t), Z(t)) from P(i) to P(i+1)  (0<=t<=1) has to satisfy
43
// X(0) = P[i  ](x), Y(0)=P[i  ](y), Z(0)=P[i  ](z), X'(0) = w[i  ](x), Y'(0) = w[i  ](y), Z'(0) = w[i  ](z)
44
// X(1) = P[i+1](x), Y(1)=P[i+1](y), Z(1)=P[i+1](z), X'(1) = w[i+1](x), Y'(1) = w[i+1](y), Z'(1) = w[i+1](z)
45
//
46
// we have the solution:  X(t) = at^3 + bt^2 + ct + d where
47
// a =  2*P[i](x) +   w[i](x) - 2*P[i+1](x) + w[i+1](x)
48
// b = -3*P[i](x) - 2*w[i](x) + 3*P[i+1](x) - w[i+1](x)
49
// c = w[i](x)
50
// d = P[i](x)
51
//
52
// and similarly Y(t) and Z(t).
53

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

    
69
  /**
70
   * The default mode of access. When in this mode, we are able to call interpolate() with points in time
71
   * in any random order. This means one single Dynamic can be used in many effects simultaneously.
72
   * On the other hand, when in this mode, it is not possible to smoothly interpolate when mDuration suddenly
73
   * changes.
74
   */
75
  public static final int ACCESS_TYPE_RANDOM     = 0;
76
  /**
77
   * Set the mode to ACCESS_SEQUENTIAL if you need to change mDuration and you would rather have the Dynamic
78
   * keep on smoothly interpolating.
79
   * On the other hand, in this mode, a Dynamic can only be accessed in sequential manner, which means one
80
   * Dynamic can only be used in one effect at a time.
81
   */
82
  public static final int ACCESS_TYPE_SEQUENTIAL = 1;
83

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

    
94
  protected class VectorNoise
95
    {
96
    float[][] n;
97

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

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

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

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

    
118
  protected Vector<VectorNoise> vn;
119
  protected float[] mFactor;
120
  protected float[] mNoise;
121
  protected float[][] baseV;
122

    
123
  ///////////////////////////////////////////////////////////////////////////////////////////////////
124
  // the coefficients of the X(t), Y(t) and Z(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx  etc.
125
  // (tangent) is the vector tangent to the path.
126
  // (cached) is the original vector from vv (copied here so when interpolating we can see if it is
127
  // still valid and if not - rebuild the Cache
128

    
129
  protected class VectorCache
130
    {
131
    float[] a;
132
    float[] b;
133
    float[] c;
134
    float[] d;
135
    float[] tangent;
136
    float[] cached;
137

    
138
    VectorCache()
139
      {
140
      a = new float[mDimension];
141
      b = new float[mDimension];
142
      c = new float[mDimension];
143
      d = new float[mDimension];
144
      tangent = new float[mDimension];
145
      cached = new float[mDimension];
146
      }
147
    }
148

    
149
  protected Vector<VectorCache> vc;
150
  protected VectorCache tmp1, tmp2;
151

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

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// hide this from Javadoc
162
  
163
  protected Dynamic()
164
    {
165

    
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
184
    mTimeOffset = 0;
185
    mSetOffset  = true;
186

    
187
    baseV      = new float[mDimension][mDimension];
188
    buf        = new float[mDimension];
189
    old        = new float[mDimension];
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

    
202
    float t = d-index;
203
    t = t*t*(3-2*t);
204

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

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

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

    
227
                      return (upper-lower)*(d-index) + lower;
228
      }
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
// debugging only
233

    
234
  private void printBase(String str)
235
    {
236
    String s;
237
    float t;
238

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

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

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
// debugging only
254

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

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

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

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

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

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

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

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

    
297
    if( error ) printBase("");
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

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

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

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

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

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

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

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
// helper function in case we are interpolating through exactly 2 points
340

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

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

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

    
394
    computeOrthonormalBase();
395
    }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
// helper function in case we are interpolating through more than 2 points
399

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

    
409
    computeOrthonormalBase();
410
    }
411

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

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

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

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

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

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
  abstract void interpolate(float[] buffer, int offset, float time);
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
// PUBLIC API
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

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

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

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

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

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

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

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

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

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

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

    
638
      time -= mTimeOffset;
639

    
640
      double pos = (double)time/mDuration;
641

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

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

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

    
681
    time -= mTimeOffset;
682

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

    
689
    double pos;
690

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

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

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706
  }
(6-6/18)