Project

General

Profile

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

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

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 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
   * one 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

    
158
//private int lastNon;
159

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

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

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

    
183
    baseV      = new float[mDimension][mDimension];
184
    buf        = new float[mDimension];
185
    old        = new float[mDimension];
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

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

    
198
    float t = d-index;
199
    t = t*t*(3-2*t);
200

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

    
211
                      for(int i=0;i<mDimension-1;i++)
212
                        {
213
                        yb = tmpN.n[i+1][index  ];
214
                        ya = tmpN.n[i+1][index-1];
215
                        mFactor[i] = mNoise[i+1]*((yb-ya)*t+ya);
216
                        }
217

    
218
                      len = ((float)index)/(NUM_NOISE+1);
219
                      lower = len + mNoise[0]*(tmpN.n[0][index-1]-len);
220
                      len = ((float)index+1)/(NUM_NOISE+1);
221
                      upper = len + mNoise[0]*(tmpN.n[0][index  ]-len);
222

    
223
                      return (upper-lower)*(d-index) + lower;
224
      }
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
// debugging only
229

    
230
  private void printBase(String str)
231
    {
232
    String s;
233
    float t;
234

    
235
    for(int i=0; i<mDimension; i++)
236
      {
237
      s = "";
238

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

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
// debugging only
250

    
251
  private void checkBase()
252
    {
253
    float tmp, cosA;
254
    float[] len= new float[mDimension];
255
    boolean error=false;
256

    
257
    for(int i=0; i<mDimension; i++)
258
      {
259
      len[i] = 0.0f;
260

    
261
      for(int k=0; k<mDimension; k++)
262
        {
263
        len[i] += baseV[i][k]*baseV[i][k];
264
        }
265

    
266
      if( len[i] == 0.0f || len[0]/len[i] < 0.95f || len[0]/len[i]>1.05f )
267
        {
268
        android.util.Log.e("dynamic", "length of vector "+i+" : "+Math.sqrt(len[i]));
269
        error = true;
270
        }
271
      }
272

    
273
    for(int i=0; i<mDimension; i++)
274
      for(int j=i+1; j<mDimension; j++)
275
        {
276
        tmp = 0.0f;
277

    
278
        for(int k=0; k<mDimension; k++)
279
          {
280
          tmp += baseV[i][k]*baseV[j][k];
281
          }
282

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

    
285
        if( cosA > 0.05f || cosA < -0.05f )
286
          {
287
          android.util.Log.e("dynamic", "cos angle between vectors "+i+" and "+j+" : "+cosA);
288
          error = true;
289
          }
290
        }
291

    
292
    if( error ) printBase("");
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
  private void checkAngle(int index)
298
    {
299
    float cosA = 0.0f;
300

    
301
    for(int k=0;k<mDimension; k++)
302
      cosA += baseV[index][k]*old[k];
303

    
304
    if( cosA<0.0f )
305
      {
306
/*
307
      /// DEBUGGING ////
308
      String s = index+" (";
309
      float t;
310

    
311
      for(int j=0; j<mDimension; j++)
312
        {
313
        t = ((int)(100*baseV[index][j]))/(100.0f);
314
        s+=(" "+t);
315
        }
316
      s += ") (";
317

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

    
325
      android.util.Log.e("dynamic", "kat: " + s);
326
      /// END DEBUGGING ///
327
*/
328
      for(int j=0; j<mDimension; j++)
329
        baseV[index][j] = -baseV[index][j];
330
      }
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
// helper function in case we are interpolating through exactly 2 points
335

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

    
371
    if( baseV[0][0] == 0.0f )
372
      {
373
      baseV[1][0] = 1.0f;
374
      baseV[1][1] = 0.0f;
375
      }
376
    else
377
      {
378
      baseV[1][0] = 0.0f;
379
      baseV[1][1] = 1.0f;
380
      }
381

    
382
    for(int i=2; i<mDimension; i++)
383
      {
384
      baseV[1][i] = 0.0f;
385
      }
386

    
387
    computeOrthonormalBase();
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
// debugging
392
/*
393
  protected void computeOrthonormalBaseMoreDebug(float time,VectorCache vc)
394
    {
395
    for(int i=0; i<mDimension; i++)
396
      {
397
      baseV[0][i] = (3*vc.a[i]*time+2*vc.b[i])*time+vc.c[i];   // first derivative, i.e. velocity vector
398
      baseV[1][i] =  6*vc.a[i]*time+2*vc.b[i];                 // second derivative,i.e. acceleration vector
399
      }
400

    
401
    float av=0.0f, vv=0.0f;
402

    
403
    android.util.Log.e("dyn3D", " ==>  velocity     ("+baseV[0][0]+","+baseV[0][1]+","+baseV[0][2]+")");
404
    android.util.Log.e("dyn3D", " ==>  acceleration ("+baseV[1][0]+","+baseV[1][1]+","+baseV[1][2]+")");
405

    
406
    for(int k=0; k<mDimension; k++)
407
      {
408
      vv += baseV[0][k]*baseV[0][k];
409
      av += baseV[1][k]*baseV[0][k];
410
      }
411

    
412
    android.util.Log.e("dyn3D", " ==>  av: "+av+" vv="+vv);
413

    
414
    av /= vv;
415

    
416
    for(int k=0;k<mDimension; k++)
417
      {
418
      baseV[1][k] -= av*baseV[0][k];
419
      }
420

    
421
    android.util.Log.e("dyn3D", " ==>  second base ("+baseV[1][0]+","+baseV[1][1]+","+baseV[1][2]+")");
422
    }
423
*/
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
// helper function in case we are interpolating through more than 2 points
426

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

    
436
    computeOrthonormalBase();
437
    }
438

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

    
462
  protected void computeOrthonormalBase()
463
    {
464
    int last_non_zero=-1;
465
    float tmp;
466

    
467
    for(int i=0; i<mDimension; i++)
468
      if( baseV[0][i] != 0.0f )
469
        last_non_zero=i;
470
/*
471
if( last_non_zero != lastNon )
472
  android.util.Log.e("dynamic", "lastNon="+lastNon+" last_non_zero="+last_non_zero);
473
*/
474

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

    
510
      buf[mDimension-1]=0.0f;                                             /// Normalize
511
      for(int k=0; k<mDimension; k++)                                     //
512
        {                                                                 //
513
        tmp = baseV[mDimension-1][k];                                     //
514
        buf[mDimension-1] += tmp*tmp;                                     //
515
        }                                                                 //
516
                                                                          //
517
      for(int i=1; i<mDimension; i++)                                     //
518
        {                                                                 //
519
        tmp = (float)Math.sqrt(buf[0]/buf[i]);                            //
520
        for(int k=0;k<mDimension; k++) baseV[i][k] *= tmp;                //
521
        }                                                                 /// End Normalize
522
      }
523

    
524
//lastNon = last_non_zero;
525

    
526
    //printBase("end");
527
    //checkBase();
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
// internal debugging only!
532
  
533
  public String print()
534
    {
535
    return "duration="+mDuration+" count="+mCount+" Noise="+mNoise+" numVectors="+numPoints+" mMode="+mMode;
536
    }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

    
540
  abstract void interpolate(float[] buffer, int offset, float time);
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543
// PUBLIC API
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
/**
547
 * Sets the mode of the interpolation to Loop, Path or Jump.
548
 * <ul>
549
 * <li>Loop is when we go from the first point all the way to the last, and the back to the first through 
550
 * the shortest way.
551
 * <li>Path is when we come back from the last point back to the first the same way we got there.
552
 * <li>Jump is when we go from first to last and then jump back to the first.
553
 * </ul>
554
 * 
555
 * @param mode {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
556
 */
557

    
558
  public void setMode(int mode)
559
    {
560
    mMode = mode;  
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
/**
565
 * Returns the number of Statics this Dynamic has been fed with.
566
 *   
567
 * @return the number of Statics we are currently interpolating through.
568
 */
569
  public synchronized int getNumPoints()
570
    {
571
    return numPoints;  
572
    }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575
/**
576
 * Controls how many times we want to interpolate.
577
 * <p>
578
 * Count equal to 1 means 'go from the first Static to the last and back'. Does not have to be an
579
 * integer - i.e. count=1.5 would mean 'start at the first Point, go to the last, come back to the first, 
580
 * go to the last again and stop'.
581
 * Count<=0 means 'go on interpolating indefinitely'.
582
 * 
583
 * @param count the number of times we want to interpolate between our collection of Statics.
584
 */
585
  public void setCount(float count)
586
    {
587
    mCount = count;  
588
    }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591
/**
592
 * Sets the time it takes to do one full interpolation.
593
 * 
594
 * @param duration Time, in milliseconds, it takes to do one full interpolation, i.e. go from the first 
595
 *                 Point to the last and back. 
596
 */
597
  
598
  public void setDuration(long duration)
599
    {
600
    mDuration = duration;
601
    }
602

    
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
/**
606
 * Sets the access mode this Dynamic will be working in.
607
 *
608
 * @param mode ACCESS_RANDOM or ACCESS_SEQUENTIAL.
609
 *             see {@link Dynamic#ACCESS_RANDOM}.
610
 *             see {@link Dynamic#ACCESS_SEQUENTIAL}.
611
 */
612
  public void setAccessMode(int mode)
613
    {
614
    mAccessMode = mode;
615
    mLastPos = -1;
616
    }
617

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619
/**
620
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
621
 *
622
 * @param buffer Float buffer we will write the resulting Static1D to.
623
 * @param offset Offset in the buffer where to write the result.
624
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
625
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
626
 */
627

    
628
  public void interpolateMain(float[] buffer, int offset, long time)
629
    {
630
    if( mDuration<=0.0f )
631
      {
632
      interpolate(buffer,offset,mCount-(int)mCount);
633
      }
634
    else
635
      {
636
      double pos = (double)time/mDuration;
637

    
638
      if( pos<=mCount || mCount<=0.0f )
639
        {
640
        interpolate(buffer,offset, (float)(pos-(int)pos) );
641
        }
642
      }
643
    }
644

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646
/**
647
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
648
 * <p>
649
 * This version differs from the previous in that it returns a boolean value which indicates whether
650
 * the interpolation is finished.
651
 *
652
 * @param buffer Float buffer we will write the resulting Static1D to.
653
 * @param offset Offset in the buffer where to write the result.
654
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
655
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
656
 * @param step Time difference between now and the last time we called this function. Needed to figure out
657
 *             if the previous time we were called the effect wasn't finished yet, but now it is.
658
 * @return true if the interpolation reached its end.
659
 */
660
  public boolean interpolateMain(float[] buffer, int offset, long time, long step)
661
    {
662
    if( mDuration<=0.0f )
663
      {
664
      interpolate(buffer,offset,mCount-(int)mCount);
665
      return false;
666
      }
667

    
668
    double pos;
669

    
670
    if( mAccessMode==ACCESS_SEQUENTIAL )
671
      {
672
      pos = mLastPos<0 ? (double)time/mDuration : (double)step/mDuration + mLastPos;
673
      mLastPos = pos;
674
      }
675
    else
676
      {
677
      pos = (double)time/mDuration;
678
      }
679

    
680
    if( pos<=mCount || mCount<=0.0f )
681
      {
682
      interpolate(buffer,offset, (float)(pos-(int)pos) );
683

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

    
691
    return false;
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695
  }
(6-6/17)