Project

General

Profile

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

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

1 e0a16874 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a4835695 Leszek Koltunski
package org.distorted.library.type;
21 6a06a912 Leszek Koltunski
22
import java.util.Random;
23 3002bef3 Leszek Koltunski
import java.util.Vector;
24 6a06a912 Leszek Koltunski
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26 ea16dc89 Leszek Koltunski
/** A class to interpolate between a list of Statics.
27 6a06a912 Leszek Koltunski
* <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 6a2ebb18 Leszek Koltunski
// c = w[i](x)
50 6a06a912 Leszek Koltunski
// d = P[i](x)
51
//
52
// and similarly Y(t) and Z(t).
53
54 568b29d8 Leszek Koltunski
public abstract class Dynamic
55 6a06a912 Leszek Koltunski
  {
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 3002bef3 Leszek Koltunski
69 bdb341bc Leszek Koltunski
  /**
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 24d22f93 Leszek Koltunski
   * Dynamic can only be used in one effect at a time.
81 bdb341bc Leszek Koltunski
   */
82
  public static final int ACCESS_SEQUENTIAL = 1;
83
84 310e14fb leszek
  protected int mDimension;
85 6a06a912 Leszek Koltunski
  protected int numPoints;
86 291705f6 Leszek Koltunski
  protected int mSegment;       // between which pair of points are we currently? (in case of PATH this is a bit complicated!)
87 6a06a912 Leszek Koltunski
  protected boolean cacheDirty; // VectorCache not up to date
88
  protected int mMode;          // LOOP, PATH or JUMP
89 8c893ffc Leszek Koltunski
  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 6a06a912 Leszek Koltunski
  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 bdb341bc Leszek Koltunski
  protected double mLastPos;
92
  protected int mAccessMode;
93 3002bef3 Leszek Koltunski
94
  protected class VectorNoise
95
    {
96
    float[][] n;
97
98 291705f6 Leszek Koltunski
    VectorNoise()
99 3002bef3 Leszek Koltunski
      {
100 291705f6 Leszek Koltunski
      n = new float[mDimension][NUM_NOISE];
101
      }
102 3002bef3 Leszek Koltunski
103 291705f6 Leszek Koltunski
    void computeNoise()
104
      {
105 3002bef3 Leszek Koltunski
      n[0][0] = mRnd.nextFloat();
106
      for(int i=1; i<NUM_NOISE; i++) n[0][i] = n[0][i-1]+mRnd.nextFloat();
107 291705f6 Leszek Koltunski
108 3002bef3 Leszek Koltunski
      float sum = n[0][NUM_NOISE-1] + mRnd.nextFloat();
109
110 291705f6 Leszek Koltunski
      for(int i=0; i<NUM_NOISE; i++)
111 3002bef3 Leszek Koltunski
        {
112 291705f6 Leszek Koltunski
        n[0][i] /=sum;
113
        for(int j=1; j<mDimension; j++) n[j][i] = mRnd.nextFloat()-0.5f;
114 3002bef3 Leszek Koltunski
        }
115
      }
116
    }
117
118
  protected Vector<VectorNoise> vn;
119
  protected float[] mFactor;
120 1e22c248 Leszek Koltunski
  protected float[] mNoise;
121 649544b8 Leszek Koltunski
  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 291705f6 Leszek Koltunski
    VectorCache()
139 649544b8 Leszek Koltunski
      {
140 291705f6 Leszek Koltunski
      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 649544b8 Leszek Koltunski
      }
147
    }
148
149
  protected Vector<VectorCache> vc;
150
  protected VectorCache tmp1, tmp2;
151 3002bef3 Leszek Koltunski
152 6a2ebb18 Leszek Koltunski
  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 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
// hide this from Javadoc
160
  
161 649544b8 Leszek Koltunski
  protected Dynamic()
162 6a06a912 Leszek Koltunski
    {
163 310e14fb leszek
164 6a06a912 Leszek Koltunski
    }
165 8c893ffc Leszek Koltunski
166 649544b8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
  protected Dynamic(int duration, float count, int dimension)
169
    {
170 291705f6 Leszek Koltunski
    vc         = new Vector<>();
171
    vn         = null;
172
    numPoints  = 0;
173 649544b8 Leszek Koltunski
    cacheDirty = false;
174 291705f6 Leszek Koltunski
    mMode      = MODE_LOOP;
175
    mDuration  = duration;
176
    mCount     = count;
177 649544b8 Leszek Koltunski
    mDimension = dimension;
178 291705f6 Leszek Koltunski
    mSegment   = -1;
179 bdb341bc Leszek Koltunski
    mLastPos   = -1;
180
    mAccessMode= ACCESS_RANDOM;
181 649544b8 Leszek Koltunski
182 291705f6 Leszek Koltunski
    baseV      = new float[mDimension][mDimension];
183
    buf        = new float[mDimension];
184
    old        = new float[mDimension];
185 649544b8 Leszek Koltunski
    }
186
187 3002bef3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189
  protected float noise(float time,int vecNum)
190
    {
191
    float lower, upper, len;
192
    float d = time*(NUM_NOISE+1);
193
    int index = (int)d;
194
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
195
    VectorNoise tmpN = vn.elementAt(vecNum);
196
197
    float t = d-index;
198
    t = t*t*(3-2*t);
199
200
    switch(index)
201
      {
202 1e22c248 Leszek Koltunski
      case 0        : for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise[i+1]*tmpN.n[i+1][0]*t;
203
                      return time + mNoise[0]*(d*tmpN.n[0][0]-time);
204
      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);
205 3002bef3 Leszek Koltunski
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
206 1e22c248 Leszek Koltunski
                      lower = len + mNoise[0]*(tmpN.n[0][NUM_NOISE-1]-len);
207 3002bef3 Leszek Koltunski
                      return (1.0f-lower)*(d-NUM_NOISE) + lower;
208
      default       : float ya,yb;
209
210
                      for(int i=0;i<mDimension-1;i++)
211
                        {
212
                        yb = tmpN.n[i+1][index  ];
213
                        ya = tmpN.n[i+1][index-1];
214 1e22c248 Leszek Koltunski
                        mFactor[i] = mNoise[i+1]*((yb-ya)*t+ya);
215 3002bef3 Leszek Koltunski
                        }
216
217
                      len = ((float)index)/(NUM_NOISE+1);
218 1e22c248 Leszek Koltunski
                      lower = len + mNoise[0]*(tmpN.n[0][index-1]-len);
219 3002bef3 Leszek Koltunski
                      len = ((float)index+1)/(NUM_NOISE+1);
220 1e22c248 Leszek Koltunski
                      upper = len + mNoise[0]*(tmpN.n[0][index  ]-len);
221 3002bef3 Leszek Koltunski
222
                      return (upper-lower)*(d-index) + lower;
223
      }
224
    }
225
226 649544b8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
// debugging only
228
229
  private void printBase(String str)
230
    {
231
    String s;
232
    float t;
233
234
    for(int i=0; i<mDimension; i++)
235
      {
236
      s = "";
237
238
      for(int j=0; j<mDimension; j++)
239
        {
240
        t = ((int)(1000*baseV[i][j]))/(1000.0f);
241
        s+=(" "+t);
242
        }
243
      android.util.Log.e("dynamic", str+" base "+i+" : " + s);
244
      }
245
    }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
// debugging only
249
250 24d22f93 Leszek Koltunski
  @SuppressWarnings("unused")
251 649544b8 Leszek Koltunski
  private void checkBase()
252
    {
253 6a2ebb18 Leszek Koltunski
    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 649544b8 Leszek Koltunski
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 6a2ebb18 Leszek Koltunski
        cosA = ( (len[i]==0.0f || len[j]==0.0f) ? 0.0f : tmp/(float)Math.sqrt(len[i]*len[j]));
284 649544b8 Leszek Koltunski
285 6a2ebb18 Leszek Koltunski
        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 649544b8 Leszek Koltunski
        }
291
292 6a2ebb18 Leszek Koltunski
    if( error ) printBase("");
293 649544b8 Leszek Koltunski
    }
294
295 c6dec65b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 649544b8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
334
// helper function in case we are interpolating through exactly 2 points
335
336 a20f274f Leszek Koltunski
  protected void computeOrthonormalBase2(Static curr, Static next)
337 649544b8 Leszek Koltunski
    {
338
    switch(mDimension)
339
      {
340 a20f274f Leszek Koltunski
      case 1: Static1D curr1 = (Static1D)curr;
341
              Static1D next1 = (Static1D)next;
342
              baseV[0][0] = (next1.x-curr1.x);
343 649544b8 Leszek Koltunski
              break;
344
      case 2: Static2D curr2 = (Static2D)curr;
345
              Static2D next2 = (Static2D)next;
346
              baseV[0][0] = (next2.x-curr2.x);
347
              baseV[0][1] = (next2.y-curr2.y);
348
              break;
349
      case 3: Static3D curr3 = (Static3D)curr;
350
              Static3D next3 = (Static3D)next;
351
              baseV[0][0] = (next3.x-curr3.x);
352
              baseV[0][1] = (next3.y-curr3.y);
353
              baseV[0][2] = (next3.z-curr3.z);
354
              break;
355
      case 4: Static4D curr4 = (Static4D)curr;
356
              Static4D next4 = (Static4D)next;
357
              baseV[0][0] = (next4.x-curr4.x);
358
              baseV[0][1] = (next4.y-curr4.y);
359
              baseV[0][2] = (next4.z-curr4.z);
360
              baseV[0][3] = (next4.w-curr4.w);
361
              break;
362
      case 5: Static5D curr5 = (Static5D)curr;
363
              Static5D next5 = (Static5D)next;
364
              baseV[0][0] = (next5.x-curr5.x);
365
              baseV[0][1] = (next5.y-curr5.y);
366
              baseV[0][2] = (next5.z-curr5.z);
367
              baseV[0][3] = (next5.w-curr5.w);
368
              baseV[0][4] = (next5.v-curr5.v);
369
              break;
370
      default: throw new RuntimeException("Unsupported dimension");
371
      }
372
373
    if( baseV[0][0] == 0.0f )
374
      {
375
      baseV[1][0] = 1.0f;
376
      baseV[1][1] = 0.0f;
377
      }
378
    else
379
      {
380
      baseV[1][0] = 0.0f;
381
      baseV[1][1] = 1.0f;
382
      }
383
384
    for(int i=2; i<mDimension; i++)
385
      {
386
      baseV[1][i] = 0.0f;
387
      }
388
389
    computeOrthonormalBase();
390
    }
391
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
// helper function in case we are interpolating through more than 2 points
394
395
  protected void computeOrthonormalBaseMore(float time,VectorCache vc)
396
    {
397
    for(int i=0; i<mDimension; i++)
398
      {
399 6a2ebb18 Leszek Koltunski
      baseV[0][i] = (3*vc.a[i]*time+2*vc.b[i])*time+vc.c[i];   // first derivative, i.e. velocity vector
400
      old[i]      = baseV[1][i];
401
      baseV[1][i] =  6*vc.a[i]*time+2*vc.b[i];                 // second derivative,i.e. acceleration vector
402 649544b8 Leszek Koltunski
      }
403
404
    computeOrthonormalBase();
405
    }
406
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
// When this function gets called, baseV[0] and baseV[1] should have been filled with two mDimension-al
409
// vectors. This function then fills the rest of the baseV array with a mDimension-al Orthonormal base.
410
// (mDimension-2 vectors, pairwise orthogonal to each other and to the original 2). The function always
411
// leaves base[0] alone but generally speaking must adjust base[1] to make it orthogonal to base[0]!
412
// The whole baseV is then used to compute Noise.
413
//
414
// When computing noise of a point travelling along a N-dimensional path, there are three cases:
415
// a) we may be interpolating through 1 point, i.e. standing in place - nothing to do in this case
416
// b) we may be interpolating through 2 points, i.e. travelling along a straight line between them -
417
//    then pass the velocity vector in baseV[0] and anything linearly independent in base[1].
418
//    The output will then be discontinuous in dimensions>2 (sad corollary from the Hairy Ball Theorem)
419
//    but we don't care - we are travelling along a straight line, so velocity (aka baseV[0]!) does
420
//    not change.
421
// c) we may be interpolating through more than 2 points. Then interpolation formulas ensure the path
422
//    will never be a straight line, even locally -> we can pass in baseV[0] and baseV[1] the velocity
423
//    and the acceleration (first and second derivatives of the path) which are then guaranteed to be
424
//    linearly independent. Then we can ensure this is continuous in dimensions <=4. This leaves
425
//    dimension 5 (ATM WAVE is 5-dimensional) discontinuous -> WAVE will suffer from chaotic noise.
426
//
427
// Bear in mind here the 'normal' in 'orthonormal' means 'length equal to the length of the original
428
// velocity vector' (rather than the standard 1)
429
430
  protected void computeOrthonormalBase()
431
    {
432
    int last_non_zero=-1;
433 6a2ebb18 Leszek Koltunski
    float tmp;
434 649544b8 Leszek Koltunski
435 6a2ebb18 Leszek Koltunski
    for(int i=0; i<mDimension; i++)
436
      if( baseV[0][i] != 0.0f )
437 649544b8 Leszek Koltunski
        last_non_zero=i;
438 6a2ebb18 Leszek Koltunski
439 a36b0cbb Leszek Koltunski
    if( last_non_zero==-1 )                                               ///
440 6a2ebb18 Leszek Koltunski
      {                                                                   //  velocity is the 0 vector -> two
441
      for(int i=0; i<mDimension-1; i++)                                   //  consecutive points we are interpolating
442
        for(int j=0; j<mDimension; j++)                                   //  through are identical -> no noise,
443
          baseV[i+1][j]= 0.0f;                                            //  set the base to 0 vectors.
444
      }                                                                   ///
445 649544b8 Leszek Koltunski
    else
446
      {
447 6a2ebb18 Leszek Koltunski
      for(int i=1; i<mDimension; i++)                                     /// One iteration computes baseV[i][*]
448
        {                                                                 //  (aka b[i]), the i-th orthonormal vector.
449
        buf[i-1]=0.0f;                                                    //
450
                                                                          //  We can use (modified!) Gram-Schmidt.
451
        for(int k=0; k<mDimension; k++)                                   //
452
          {                                                               //
453
          if( i>=2 )                                                      //  b[0] = b[0]
454
            {                                                             //  b[1] = b[1] - (<b[1],b[0]>/<b[0],b[0]>)*b[0]
455
            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]
456
            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]
457
            }                                                             //  (...)
458
                                                                          //  then b[i] = b[i] / |b[i]|  ( Here really b[i] = b[i] / (|b[0]|/|b[i]|)
459
          tmp = baseV[i-1][k];                                            //
460
          buf[i-1] += tmp*tmp;                                            //
461
          }                                                               //
462
                                                                          //
463
        for(int j=0; j<i; j++)                                            //
464
          {                                                               //
465
          tmp = 0.0f;                                                     //
466
          for(int k=0;k<mDimension; k++) tmp += baseV[i][k]*baseV[j][k];  //
467
          tmp /= buf[j];                                                  //
468
          for(int k=0;k<mDimension; k++) baseV[i][k] -= tmp*baseV[j][k];  //
469
          }                                                               //
470
                                                                          //
471
        checkAngle(i);                                                    //
472
        }                                                                 /// end compute baseV[i][*]
473
474
      buf[mDimension-1]=0.0f;                                             /// Normalize
475
      for(int k=0; k<mDimension; k++)                                     //
476
        {                                                                 //
477
        tmp = baseV[mDimension-1][k];                                     //
478
        buf[mDimension-1] += tmp*tmp;                                     //
479
        }                                                                 //
480
                                                                          //
481
      for(int i=1; i<mDimension; i++)                                     //
482
        {                                                                 //
483
        tmp = (float)Math.sqrt(buf[0]/buf[i]);                            //
484
        for(int k=0;k<mDimension; k++) baseV[i][k] *= tmp;                //
485
        }                                                                 /// End Normalize
486 649544b8 Leszek Koltunski
      }
487
488
    //printBase("end");
489
    //checkBase();
490
    }
491
492 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
493
// internal debugging only!
494 24d22f93 Leszek Koltunski
495 a4835695 Leszek Koltunski
  public String print()
496 6a06a912 Leszek Koltunski
    {
497 24d22f93 Leszek Koltunski
    return "duration="+mDuration+" count="+mCount+" Noise[0]="+mNoise[0]+" numVectors="+numPoints+" mMode="+mMode;
498 6a06a912 Leszek Koltunski
    }
499 3002bef3 Leszek Koltunski
500 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
501 3002bef3 Leszek Koltunski
502 6a06a912 Leszek Koltunski
  abstract void interpolate(float[] buffer, int offset, float time);
503
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505
// PUBLIC API
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507 8c893ffc Leszek Koltunski
508 6a06a912 Leszek Koltunski
/**
509
 * Sets the mode of the interpolation to Loop, Path or Jump.
510
 * <ul>
511
 * <li>Loop is when we go from the first point all the way to the last, and the back to the first through 
512
 * the shortest way.
513
 * <li>Path is when we come back from the last point back to the first the same way we got there.
514
 * <li>Jump is when we go from first to last and then jump back to the first.
515
 * </ul>
516
 * 
517 568b29d8 Leszek Koltunski
 * @param mode {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
518 6a06a912 Leszek Koltunski
 */
519
  public void setMode(int mode)
520
    {
521
    mMode = mode;  
522
    }
523
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
/**
526 ea16dc89 Leszek Koltunski
 * Returns the number of Statics this Dynamic has been fed with.
527 6a06a912 Leszek Koltunski
 *   
528 ea16dc89 Leszek Koltunski
 * @return the number of Statics we are currently interpolating through.
529 6a06a912 Leszek Koltunski
 */
530
  public synchronized int getNumPoints()
531
    {
532
    return numPoints;  
533
    }
534
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536
/**
537
 * Controls how many times we want to interpolate.
538
 * <p>
539 ea16dc89 Leszek Koltunski
 * Count equal to 1 means 'go from the first Static to the last and back'. Does not have to be an
540 6a06a912 Leszek Koltunski
 * integer - i.e. count=1.5 would mean 'start at the first Point, go to the last, come back to the first, 
541
 * go to the last again and stop'.
542
 * Count<=0 means 'go on interpolating indefinitely'.
543
 * 
544 ea16dc89 Leszek Koltunski
 * @param count the number of times we want to interpolate between our collection of Statics.
545 6a06a912 Leszek Koltunski
 */
546
  public void setCount(float count)
547
    {
548
    mCount = count;  
549
    }
550
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
/**
553
 * Sets the time it takes to do one full interpolation.
554
 * 
555
 * @param duration Time, in milliseconds, it takes to do one full interpolation, i.e. go from the first 
556
 *                 Point to the last and back. 
557
 */
558
  public void setDuration(long duration)
559
    {
560
    mDuration = duration;
561
    }
562
563 bdb341bc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
564
/**
565
 * Sets the access mode this Dynamic will be working in.
566
 *
567 24d22f93 Leszek Koltunski
 * @param mode {@link Dynamic#ACCESS_RANDOM} or {@link Dynamic#ACCESS_SEQUENTIAL}.
568 bdb341bc Leszek Koltunski
 */
569
  public void setAccessMode(int mode)
570
    {
571
    mAccessMode = mode;
572
    mLastPos = -1;
573
    }
574
575 6d62a900 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
576
/**
577
 * Return the Dimension, ie number of floats in a single Point this Dynamic interpolates through.
578
 */
579
  public int getDimension()
580
    {
581
    return mDimension;
582
    }
583
584 bdb341bc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
585
/**
586
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
587
 *
588 24d22f93 Leszek Koltunski
 * @param buffer Float buffer we will write the results to.
589 bdb341bc Leszek Koltunski
 * @param offset Offset in the buffer where to write the result.
590
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
591
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
592
 */
593 a1d92a36 leszek
  public void get(float[] buffer, int offset, long time)
594 bdb341bc Leszek Koltunski
    {
595
    if( mDuration<=0.0f )
596
      {
597
      interpolate(buffer,offset,mCount-(int)mCount);
598
      }
599
    else
600
      {
601
      double pos = (double)time/mDuration;
602
603
      if( pos<=mCount || mCount<=0.0f )
604
        {
605
        interpolate(buffer,offset, (float)(pos-(int)pos) );
606
        }
607
      }
608
    }
609
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611
/**
612
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
613
 * <p>
614
 * This version differs from the previous in that it returns a boolean value which indicates whether
615
 * the interpolation is finished.
616
 *
617 24d22f93 Leszek Koltunski
 * @param buffer Float buffer we will write the results to.
618 bdb341bc Leszek Koltunski
 * @param offset Offset in the buffer where to write the result.
619
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
620
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
621
 * @param step Time difference between now and the last time we called this function. Needed to figure out
622
 *             if the previous time we were called the effect wasn't finished yet, but now it is.
623
 * @return true if the interpolation reached its end.
624
 */
625 a1d92a36 leszek
  public boolean get(float[] buffer, int offset, long time, long step)
626 bdb341bc Leszek Koltunski
    {
627
    if( mDuration<=0.0f )
628
      {
629
      interpolate(buffer,offset,mCount-(int)mCount);
630
      return false;
631
      }
632 48d0867a leszek
    if( time+step > mDuration*mCount && mCount>0.0f )
633
      {
634
      interpolate(buffer,offset,mCount-(int)mCount);
635
      return true;
636
      }
637 bdb341bc Leszek Koltunski
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 48d0867a leszek
    interpolate(buffer,offset, (float)(pos-(int)pos) );
651 bdb341bc Leszek Koltunski
    return false;
652
    }
653
654 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
655
  }