Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic.java @ 7602a827

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of DistortedLibrary.                                                               //
5
//                                                                                               //
6
// DistortedLibrary 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
// DistortedLibrary 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 DistortedLibrary.  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 jump to it.
29
* <li>if there are two Points, linearly bounce between them
30
* <li>if there are more, interpolate a loop (or a path!) between them.
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 vector to the last and back to first through the shortest path. 
58
   */
59
  public static final int MODE_LOOP = 0; 
60
  /**
61
   * We come back from the last to the first vector through the same way we got there.
62
   */
63
  public static final int MODE_PATH = 1; 
64
  /**
65
   * We just jump back from the last point to the first.
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_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_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 mAccessMode;
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 mTimeLastInterpolated;
158
  private long mTimeWhenSetDuration;
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
    mAccessMode= ACCESS_RANDOM;
183

    
184
    mTimeLastInterpolated = 0;
185
    mTimeWhenSetDuration  = 0;
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 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 Statics this Dynamic has been fed with.
521
 *   
522
 * @return the number of Statics we are currently interpolating through.
523
 */
524
  public synchronized int getNumPoints()
525
    {
526
    return numPoints;  
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
/**
531
 * Controls how many times we want to interpolate.
532
 * <p>
533
 * Count equal to 1 means 'go from the first Static to the last and back'. Does not have to be an
534
 * integer - i.e. count=1.5 would mean 'start at the first Point, go to the last, come back to the first, 
535
 * go to the last again and stop'.
536
 * Count<=0 means 'go on interpolating indefinitely'.
537
 * 
538
 * @param count the number of times we want to interpolate between our collection of Statics.
539
 */
540
  public void setCount(float count)
541
    {
542
    mCount = count;  
543
    }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546
/**
547
 * Sets the time it takes to do one full interpolation.
548
 * 
549
 * @param durationInMilliseconds time it takes to do one full interpolation, i.e. go from the first
550
 *                               Point to the last and back.
551
 */
552
  public void makeRunNowFor(long durationInMilliseconds)
553
    {
554
    mDuration = durationInMilliseconds;
555
    mTimeWhenSetDuration = mTimeLastInterpolated;
556
    }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559
/**
560
 * Sets the access mode this Dynamic will be working in.
561
 *
562
 * @param mode {@link Dynamic#ACCESS_RANDOM} or {@link Dynamic#ACCESS_SEQUENTIAL}.
563
 */
564
  public void setAccessMode(int mode)
565
    {
566
    mAccessMode = mode;
567
    mLastPos = -1;
568
    }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571
/**
572
 * Return the Dimension, ie number of floats in a single Point this Dynamic interpolates through.
573
 */
574
  public int getDimension()
575
    {
576
    return mDimension;
577
    }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580
/**
581
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
582
 *
583
 * @param buffer Float buffer we will write the results to.
584
 * @param offset Offset in the buffer where to write the result.
585
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
586
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
587
 */
588
  public void get(float[] buffer, int offset, long time)
589
    {
590
    mTimeLastInterpolated = time;
591

    
592
    if( mDuration<=0.0f )
593
      {
594
      interpolate(buffer,offset,mCount-(int)mCount);
595
      }
596
    else
597
      {
598
      double pos = (double)(time-mTimeWhenSetDuration)/mDuration;
599

    
600
      if( pos<=mCount || mCount<=0.0f )
601
        {
602
        interpolate(buffer,offset, (float)(pos-(int)pos) );
603
        }
604
      }
605
    }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608
/**
609
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
610
 * <p>
611
 * This version differs from the previous in that it returns a boolean value which indicates whether
612
 * the interpolation is finished.
613
 *
614
 * @param buffer Float buffer we will write the results to.
615
 * @param offset Offset in the buffer where to write the result.
616
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
617
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
618
 * @param step Time difference between now and the last time we called this function. Needed to figure out
619
 *             if the previous time we were called the effect wasn't finished yet, but now it is.
620
 * @return true if the interpolation reached its end.
621
 */
622
  public boolean get(float[] buffer, int offset, long time, long step)
623
    {
624
    mTimeLastInterpolated = time;
625
    time -= mTimeWhenSetDuration;
626

    
627
    if( mDuration<=0.0f )
628
      {
629
      interpolate(buffer,offset,mCount-(int)mCount);
630
      return false;
631
      }
632
    if( time+step > mDuration*mCount && mCount>0.0f )
633
      {
634
      interpolate(buffer,offset,mCount-(int)mCount);
635
      return true;
636
      }
637

    
638
    double pos;
639

    
640
    if( mAccessMode==ACCESS_SEQUENTIAL )
641
      {
642
      pos = mLastPos<0 ? (double)time/mDuration : (double)step/mDuration + mLastPos;
643
      mLastPos = pos;
644
      }
645
    else
646
      {
647
      pos = (double)time/mDuration;
648
      }
649

    
650
    interpolate(buffer,offset, (float)(pos-(int)pos) );
651
    return false;
652
    }
653

    
654
///////////////////////////////////////////////////////////////////////////////////////////////////
655
  }
(6-6/18)