Project

General

Profile

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

library / src / main / java / org / distorted / library / Interpolator4D.java @ 1e438fc7

1
package org.distorted.library;
2

    
3
import java.util.Vector;
4

    
5
///////////////////////////////////////////////////////////////////////////////////////////////////
6
/** 
7
* A 4-dimensional implementation of the Interpolator class to interpolate between a list 
8
* of Float4Ds.
9
*/
10

    
11
public class Interpolator4D extends Interpolator 
12
  {
13
 
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15
// the coefficients of the X(t), Y(t), Z(t), W(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx  etc.
16
// (x,y,z,w) is the vector tangent to the path.
17
// (vx,vy,vz,vw) is the original vector from vv (copied here so when interpolating we can see if it is 
18
// still valid and if not - rebuild the Cache
19
  
20
  private class VectorCache
21
    {
22
    float ax, bx, cx, dx;
23
    float ay, by, cy, dy;
24
    float az, bz, cz, dz;
25
    float aw, bw, cw, dw;
26
   
27
    float x,y,z,w;
28
    float vx,vy,vz,vw;
29
    }
30
  
31
  private class VectorNoise
32
    {
33
    float[] nx;
34
    float[] ny;
35
    float[] nz;
36
    float[] nw;
37
   
38
    public VectorNoise()
39
      {
40
      nx = new float[NUM_NOISE]; 
41
      nx[0] = mRnd.nextFloat();
42
      for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1] + mRnd.nextFloat();
43
      float sum = nx[NUM_NOISE-1] + mRnd.nextFloat();
44
      for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum;
45
     
46
      ny = new float[NUM_NOISE];
47
      for(int i=0; i<NUM_NOISE; i++) ny[i] = mRnd.nextFloat()-0.5f;
48
     
49
      nz = new float[NUM_NOISE];
50
      for(int i=0; i<NUM_NOISE; i++) nz[i] = mRnd.nextFloat()-0.5f;
51
     
52
      nw = new float[NUM_NOISE];
53
      for(int i=0; i<NUM_NOISE; i++) nw[i] = mRnd.nextFloat()-0.5f;  
54
      }
55
    }
56
  
57
  private Vector<VectorCache> vc;
58
  private VectorCache tmp1, tmp2;
59

    
60
  private Vector<Float4D> vv;
61
  private Float4D prev, curr, next;
62
  
63
  private Vector<VectorNoise> vn;
64
  private VectorNoise tmpN;
65
  
66
  private float mFactor1, mFactor2, mFactor3; // used in Noise only. Those are noise factors; 1=noise of the (vec1X,vec1Y,vec1Z,vec1W) vector; 2=noise of (vec2X,vec2Y,vec2Z,vec2W) and same for vec3.
67
  private float vec1X,vec1Y,vec1Z,vec1W;      // vector perpendicular to v(t) and in the same plane as v(t) and a(t) (for >2 points only, in case of 2 points this is calculated differently)
68
  private float vec2X,vec2Y,vec2Z,vec2W;      // vector perpendicular to v(t) and to vec1.
69
  private float vec3X,vec3Y,vec3Z,vec3W;      // vector perpendicular to v(t) and to vec1.
70
  
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  synchronized void createNoise()
74
    {
75
    if( vn==null )
76
      {  
77
      vn = new Vector<VectorNoise>();
78
      for(int i=0; i<numPoints; i++) vn.add(new VectorNoise());
79
      }
80
    }
81
   
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
// no array bounds checking!
84
  
85
  private void vec(int c)
86
    {
87
    int p = c>0 ? c-1: numPoints-1;
88
    int n = c<numPoints-1 ? c+1: 0;
89
    
90
    prev = vv.elementAt(p);
91
    curr = vv.elementAt(c);
92
    next = vv.elementAt(n);
93

    
94
    tmp1 = vc.elementAt(c);
95
    
96
    float px = curr.x - prev.x;
97
    float py = curr.y - prev.y;
98
    float pz = curr.z - prev.z;
99
    float pw = curr.w - prev.w;
100
    float nx = next.x - curr.x;
101
    float ny = next.y - curr.y;
102
    float nz = next.z - curr.z;
103
    float nw = next.w - curr.w;
104
     
105
    float d = nx*nx+ny*ny+nz*nz+nw*nw;
106
    
107
    if( d>0 )
108
      {
109
      float q = (float)Math.sqrt((px*px+py*py+pz*pz+pw*pw)/d);
110
      
111
      if( q>1 )
112
        {
113
        tmp1.x = nx+px/q;
114
        tmp1.y = ny+py/q;
115
        tmp1.z = nz+pz/q;
116
        tmp1.w = nw+pw/q;
117
        }
118
      else
119
        {
120
        tmp1.x = px+nx*q;
121
        tmp1.y = py+ny*q;
122
        tmp1.z = pz+nz*q;
123
        tmp1.w = pw+nw*q;
124
        }
125
      }
126
    else
127
      {
128
      tmp1.x = 0.0f;
129
      tmp1.y = 0.0f;
130
      tmp1.z = 0.0f;  
131
      tmp1.w = 0.0f;
132
      }
133
    }
134
    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
  
137
  private void recomputeCache()
138
    {  
139
    if( numPoints==1 )
140
      {
141
      tmp1= vc.elementAt(0);
142
      curr= vv.elementAt(0);
143
        
144
      tmp1.ax = tmp1.ay = tmp1.az = tmp1.aw = 0.0f;
145
      tmp1.bx = tmp1.by = tmp1.bz = tmp1.bw = 0.0f;
146
      tmp1.cx = curr.x;
147
      tmp1.cy = curr.y;
148
      tmp1.cz = curr.z;
149
      tmp1.cw = curr.w;
150
      tmp1.dx = tmp1.dy = tmp1.dz = tmp1.dw = 0.0f;
151
      }
152
    else if( numPoints==2 )
153
      {
154
      tmp1= vc.elementAt(0);
155
      tmp2= vc.elementAt(1);
156
      curr= vv.elementAt(0);
157
      next= vv.elementAt(1);
158
      
159
      tmp1.ax = tmp1.ay = tmp1.az = tmp1.aw = 0.0f;
160
      tmp1.bx = tmp1.by = tmp1.bz = tmp1.bw = 0.0f;
161
      tmp1.cx = next.x - curr.x;
162
      tmp1.cy = next.y - curr.y;
163
      tmp1.cz = next.z - curr.z;
164
      tmp1.cw = next.w - curr.w;
165
      tmp1.dx = curr.x;
166
      tmp1.dy = curr.y;
167
      tmp1.dz = curr.z;
168
      tmp1.dw = curr.w;
169
      
170
      tmp2.ax = tmp2.ay = tmp2.az = tmp2.aw = 0.0f;
171
      tmp2.bx = tmp2.by = tmp2.bz = tmp2.bw = 0.0f;
172
      tmp2.cx = curr.x - next.x;
173
      tmp2.cy = curr.y - next.y;
174
      tmp2.cz = curr.z - next.z;
175
      tmp2.cw = curr.w - next.w;
176
      tmp2.dx = next.x;
177
      tmp2.dy = next.y;
178
      tmp2.dz = next.z;
179
      tmp2.dw = next.w;
180
      }
181
    else
182
      {
183
      int i, n;  
184
      
185
      for(i=0; i<numPoints; i++) vec(i);
186
   
187
      for(i=0; i<numPoints; i++)
188
        {
189
        n = i<numPoints-1 ? i+1:0;  
190
      
191
        tmp1= vc.elementAt(i);
192
        tmp2= vc.elementAt(n);
193
        curr= vv.elementAt(i);
194
        next= vv.elementAt(n);
195
      
196
        tmp1.vx = curr.x;
197
        tmp1.vy = curr.y;
198
        tmp1.vz = curr.z;
199
        tmp1.vw = curr.w;
200
        
201
        tmp1.ax =  2*curr.x +   tmp1.x - 2*next.x + tmp2.x;
202
        tmp1.bx = -3*curr.x - 2*tmp1.x + 3*next.x - tmp2.x;
203
        tmp1.cx = tmp1.x;
204
        tmp1.dx = curr.x;
205
      
206
        tmp1.ay =  2*curr.y +   tmp1.y - 2*next.y + tmp2.y;
207
        tmp1.by = -3*curr.y - 2*tmp1.y + 3*next.y - tmp2.y;
208
        tmp1.cy = tmp1.y;
209
        tmp1.dy = curr.y;
210
      
211
        tmp1.az =  2*curr.z +   tmp1.z - 2*next.z + tmp2.z;
212
        tmp1.bz = -3*curr.z - 2*tmp1.z + 3*next.z - tmp2.z;
213
        tmp1.cz = tmp1.z;
214
        tmp1.dz = curr.z;
215
        
216
        tmp1.aw =  2*curr.w +   tmp1.w - 2*next.w + tmp2.w;
217
        tmp1.bw = -3*curr.w - 2*tmp1.w + 3*next.w - tmp2.w;
218
        tmp1.cw = tmp1.w;
219
        tmp1.dw = curr.w;
220
        }
221
      }
222
   
223
    cacheDirty = false;
224
    }
225
  
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  private float noise(float time,int vecNum)
229
    {
230
    float lower, upper, len;  
231
    float d = time*(NUM_NOISE+1);
232
    int index = (int)d;
233
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
234
    tmpN = vn.elementAt(vecNum);
235
   
236
    float t = d-index;
237
    t = t*t*(3-2*t);
238
   
239
    switch(index)
240
      {
241
      case 0        : mFactor1 = mNoise*tmpN.ny[0]*t;
242
                      mFactor2 = mNoise*tmpN.nz[0]*t;
243
                      mFactor3 = mNoise*tmpN.nw[0]*t;
244
                      return time + mNoise*(d*tmpN.nx[0]-time);
245
      case NUM_NOISE: mFactor1= mNoise*tmpN.ny[NUM_NOISE-1]*(1-t);
246
                      mFactor2= mNoise*tmpN.nz[NUM_NOISE-1]*(1-t);
247
                      mFactor3= mNoise*tmpN.nw[NUM_NOISE-1]*(1-t);
248
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
249
                      lower = len + mNoise*(tmpN.nx[NUM_NOISE-1]-len);  
250
                      return (1.0f-lower)*(d-NUM_NOISE) + lower;
251
      default       : float ya,yb;
252
                      yb = tmpN.ny[index  ];
253
                      ya = tmpN.ny[index-1];
254
                      mFactor1 = mNoise*((yb-ya)*t+ya);
255
                      yb = tmpN.nz[index  ];
256
                      ya = tmpN.nz[index-1];
257
                      mFactor2 = mNoise*((yb-ya)*t+ya);
258
                      yb = tmpN.nw[index  ];
259
                      ya = tmpN.nw[index-1];
260
                      mFactor3 = mNoise*((yb-ya)*t+ya);
261
   
262
                      len = ((float)index)/(NUM_NOISE+1);
263
                      lower = len + mNoise*(tmpN.nx[index-1]-len);   
264
                      len = ((float)index+1)/(NUM_NOISE+1); 
265
                      upper = len + mNoise*(tmpN.nx[index  ]-len);
266
            
267
                      return (upper-lower)*(d-index) + lower; 
268
      }
269
    }
270
     
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
// v is the speed vector (i.e. position p(t) differentiated by time)
273
// a is the acceleration vector (differentiate once more)
274
//
275
// Now we construct orthogonal basis with Gram-Schmidt:  
276
// vec1 = a-delta*v where delta = (v*a)/|v|^2
277
// vec2 = (0,0,1,0) - coeff1*(vx,vy,vz,vw) - coeff2*(vec1x,vec1y,vec1z,vec1w)                                     where coeff1 = vz/|v|^2, coeff2 = vec1Z/|vec1|^2
278
// vec3 = (0,0,0,1) - coeff1*(vx,vy,vz,vw) - coeff2*(vec1x,vec1y,vec1z,vec1w) - coeff3*(vec2x,vec2y,vec2z,vec2w)  where coeff1 = vw/|v|^2, coeff2 = vec1W/|vec1|^2, coeff3 = vec2W/|vec2|^2
279
    
280
  private void setUpVectors(float time,VectorCache vc)
281
    {
282
    if( vc!=null )
283
      {
284
      float vx = (3*vc.ax*time+2*vc.bx)*time+vc.cx;
285
      float vy = (3*vc.ay*time+2*vc.by)*time+vc.cy;
286
      float vz = (3*vc.az*time+2*vc.bz)*time+vc.cz;
287
      float vw = (3*vc.aw*time+2*vc.bw)*time+vc.cw;
288
     
289
      float ax = 6*vc.ax*time+2*vc.bx;
290
      float ay = 6*vc.ay*time+2*vc.by;
291
      float az = 6*vc.az*time+2*vc.bz;
292
      float aw = 6*vc.aw*time+2*vc.bw;
293
     
294
      float v_sq = vx*vx+vy*vy+vz*vz+vw*vw;
295
      float delta = (vx*ax+vy*ay+vz*az*vw*vw)/v_sq;
296
     
297
      vec1X = ax-delta*vx;
298
      vec1Y = ay-delta*vy;
299
      vec1Z = az-delta*vz;
300
      vec1W = aw-delta*vw;
301
     
302
      float vec1_sq = vec1X*vec1X+vec1Y*vec1Y+vec1Z*vec1Z+vec1W*vec1W;
303
     
304
      // construct vec2 and vec3. Cross product does not work in 4th dimension!
305
      float coeff21 = vz/v_sq;
306
      float coeff22 = vec1Z/vec1_sq;
307
      vec2X = 0.0f - coeff21*vx - coeff22*vec1X;
308
      vec2Y = 0.0f - coeff21*vy - coeff22*vec1Y;
309
      vec2Z = 1.0f - coeff21*vz - coeff22*vec1Z;
310
      vec2W = 0.0f - coeff21*vw - coeff22*vec1W;
311
     
312
      float vec2_sq = vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z+vec2W*vec2W;
313
      float coeff31 = vw/v_sq;
314
      float coeff32 = vec1W/vec1_sq;
315
      float coeff33 = vec2W/vec2_sq;
316
      vec2X = 0.0f - coeff31*vx - coeff32*vec1X - coeff33*vec2X;
317
      vec2Y = 0.0f - coeff31*vy - coeff32*vec1Y - coeff33*vec2Y;
318
      vec2Z = 0.0f - coeff31*vz - coeff32*vec1Z - coeff33*vec2Z;
319
      vec2W = 1.0f - coeff31*vw - coeff32*vec1W - coeff33*vec2W;
320
     
321
      float vec3_sq = vec3X*vec3X+vec3Y*vec3Y+vec3Z*vec3Z+vec3W*vec3W;
322
     
323
      float len1 = (float)Math.sqrt(v_sq/vec1_sq);   
324
      float len2 = (float)Math.sqrt(v_sq/vec2_sq);   
325
      float len3 = (float)Math.sqrt(v_sq/vec3_sq);
326
     
327
      vec1X*=len1;
328
      vec1Y*=len1;
329
      vec1Z*=len1;
330
      vec1W*=len1;
331
     
332
      vec2X*=len2;
333
      vec2Y*=len2;
334
      vec2Z*=len2;
335
      vec2W*=len2;
336
     
337
      vec3X*=len3;
338
      vec3Y*=len3;
339
      vec3Z*=len3;
340
      vec3W*=len3;
341
      }
342
    else
343
      {
344
      curr = vv.elementAt(0);
345
      next = vv.elementAt(1); 
346
     
347
      float vx = (next.x-curr.x);
348
      float vy = (next.y-curr.y);
349
      float vz = (next.z-curr.z);
350
      float vw = (next.w-curr.w);
351
     
352
      float b = (float)Math.sqrt(vx*vx+vy*vy+vz*vz);
353
     
354
      if( b>0.0f )
355
        {
356
        vec1X = vx*vw/b;
357
        vec1Y = vy*vw/b;
358
        vec1Z = vz*vw/b;
359
        vec1W = -b;
360
      
361
        float v_sq = vx*vx+vy*vy+vz*vz+vw*vw;
362
        float vec1_sq = vec1X*vec1X+vec1Y*vec1Y+vec1Z*vec1Z+vec1W*vec1W;
363
     
364
        // construct vec2 and vec3. Cross product does not work in 4th dimension!
365
        float coeff21 = vz/v_sq;
366
        float coeff22 = vec1Z/vec1_sq;
367
        vec2X = 0.0f - coeff21*vx - coeff22*vec1X;
368
        vec2Y = 0.0f - coeff21*vy - coeff22*vec1Y;
369
        vec2Z = 1.0f - coeff21*vz - coeff22*vec1Z;
370
        vec2W = 0.0f - coeff21*vw - coeff22*vec1W;
371
     
372
        float vec2_sq = vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z+vec2W*vec2W;
373
        float coeff31 = vw/v_sq;
374
        float coeff32 = vec1W/vec1_sq;
375
        float coeff33 = vec2W/vec2_sq;
376
        vec2X = 0.0f - coeff31*vx - coeff32*vec1X - coeff33*vec2X;
377
        vec2Y = 0.0f - coeff31*vy - coeff32*vec1Y - coeff33*vec2Y;
378
        vec2Z = 0.0f - coeff31*vz - coeff32*vec1Z - coeff33*vec2Z;
379
        vec2W = 1.0f - coeff31*vw - coeff32*vec1W - coeff33*vec2W;
380
     
381
        float vec3_sq = vec3X*vec3X+vec3Y*vec3Y+vec3Z*vec3Z+vec3W*vec3W;
382
     
383
        float len1 = (float)Math.sqrt(v_sq/vec1_sq);    
384
        float len2 = (float)Math.sqrt(v_sq/vec2_sq);    
385
        float len3 = (float)Math.sqrt(v_sq/vec3_sq);
386
     
387
        vec1X*=len1;
388
        vec1Y*=len1;
389
        vec1Z*=len1;
390
        vec1W*=len1;
391
     
392
        vec2X*=len2;
393
        vec2Y*=len2;
394
        vec2Z*=len2;
395
        vec2W*=len2;
396
     
397
        vec3X*=len3;
398
        vec3Y*=len3;
399
        vec3Z*=len3;
400
        vec3W*=len3;
401
        }
402
      else
403
        {
404
        vec1X = vw;
405
        vec1Y = 0.0f;
406
        vec1Z = 0.0f;
407
        vec1W = 0.0f;
408
      
409
        vec2X = 0.0f;
410
        vec2Y = vw;
411
        vec2Z = 0.0f;
412
        vec2W = 0.0f;
413
      
414
        vec3X = 0.0f;
415
        vec3Y = 0.0f;
416
        vec3Z = vw;
417
        vec3W = 0.0f;
418
        }
419
      }
420
    }
421
  
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423
// PUBLIC API
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
/**
426
 * Default constructor.
427
 */
428
  public Interpolator4D()
429
    {
430
    vv = new Vector<Float4D>();
431
    vc = new Vector<VectorCache>();
432
    vn = null;
433
    numPoints = 0;
434
    cacheDirty = false;
435
    mMode = MODE_LOOP;
436
    mDuration = 0;
437
    mCount = 0.5f;
438
    mNoise = 0.0f;
439
    }
440
  
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442
/**
443
 * Returns the location'th Float4D. 
444
 *   
445
 * @param location the index of the Point we are interested in.
446
 * @return The Float4D, if 0<=location&lt;getNumPoints(), or null otherwise. 
447
 */  
448
  public synchronized Float4D getPoint(int location)
449
    {
450
    return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;  
451
    }
452
  
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454
/**
455
 * Resets the location'th Point.
456
 * 
457
 * @param location the index of the Point we are setting.
458
 * @param x New value of its first float.
459
 */
460
  public synchronized void setPoint(int location, float x, float y, float z, float w)
461
    {
462
    if( location>=0 && location<numPoints )
463
      {
464
      curr = vv.elementAt(location);
465
   
466
      if( curr!=null )
467
        {
468
        curr.set(x,y,z,w);
469
        cacheDirty=true;
470
        }
471
      }
472
    }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475
/**
476
 * Adds a new Float4D to the end of our list of Points to interpolate through.
477
 * <p>   
478
 * Only a reference to the Point gets added to the List; this means that one can add a Point 
479
 * here, and later on {@link Float4D#set(float,float,float,float)} it to some new value and 
480
 * the change will be seamlessly reflected in the interpolated path.  
481
 * <p>
482
 * A Point can be added multiple times.
483
 *   
484
 * @param v The Point to add.
485
 */    
486
  public synchronized void add(Float4D v)
487
    {
488
    if( v!=null )
489
      {
490
      vv.add(v);
491
        
492
      if( vn!=null ) vn.add(new VectorNoise());
493
       
494
       switch(numPoints)
495
         {
496
         case 0: break;
497
         case 1: setUpVectors(0.0f,null);
498
                 break;
499
         case 2: vc.add(new VectorCache());
500
                 vc.add(new VectorCache());
501
                 vc.add(new VectorCache());
502
                 break;
503
         default:vc.add(new VectorCache());
504
         }
505

    
506
       numPoints++;
507
       cacheDirty = true;
508
       }
509
    }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512
/**
513
 * Adds a new Float4D to the location'th place in our List of Points to interpolate through.  
514
 *   
515
 * @param location Index in our List to add the new Point at.
516
 * @param v The Float4D to add.
517
 */  
518
  public synchronized void add(int location, Float4D v)
519
    {
520
    if( v!=null )
521
      {
522
      vv.add(location, v);
523
      
524
      if( vn!=null ) vn.add(new VectorNoise());
525
      
526
      switch(numPoints)
527
        {
528
        case 0: break;
529
        case 1: setUpVectors(0.0f,null);
530
                break;
531
        case 2: vc.add(new VectorCache());
532
                vc.add(new VectorCache());
533
                vc.add(new VectorCache());
534
                break;
535
        default:vc.add(location,new VectorCache());
536
        }
537

    
538
      numPoints++;
539
      cacheDirty = true;
540
      }
541
    }
542
  
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544
/**
545
 * Removes all occurrences of Point v from the List of Points to interpolate through.  
546
 * 
547
 * @param v The Point to remove.
548
 * @return <code>true</code> if we have removed at least one Point.
549
 */
550
  public synchronized boolean remove(Float4D v)
551
    {
552
    int n = vv.indexOf(v);
553
    boolean found = false;
554
   
555
    while( n>=0 ) 
556
      {
557
      vv.remove(n);
558
     
559
      if( vn!=null ) vn.remove(0);
560
     
561
      switch(numPoints)
562
        {
563
        case 0:
564
        case 1: 
565
        case 2: break;
566
        case 3: vc.removeAllElements();
567
                setUpVectors(0.0f,null);
568
                break;
569
        default:vc.remove(n);
570
        }
571

    
572
      numPoints--;
573
      found = true;
574
      n = vv.indexOf(v);
575
      }
576
   
577
    if( found ) 
578
      {
579
      cacheDirty=true;
580
      }
581
   
582
    return found;
583
    }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586
/**
587
 * Removes a location'th Point from the List of Points we interpolate through.
588
 * 
589
 * @param location index of the Point we want to remove. 
590
 * @return <code>true</code> if location is valid, i.e. if 0<=location&lt;getNumPoints().
591
 */
592
  public synchronized boolean remove(int location)
593
    {
594
    if( location>=0 && location<numPoints ) 
595
      {
596
      vv.removeElementAt(location);
597
       
598
      if( vn!=null ) vn.remove(0);
599
      
600
      switch(numPoints)
601
        {
602
        case 0:
603
        case 1: 
604
        case 2: break;
605
        case 3: vc.removeAllElements();
606
                setUpVectors(0.0f,null);
607
                break;
608
        default:vc.removeElementAt(location);
609
        }
610

    
611
      numPoints--;
612
      cacheDirty = true; 
613
      return true;
614
      }
615

    
616
    return false;
617
    }
618
  
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620
/**
621
 * Removes all Points.
622
 */
623
  public synchronized void removeAll()
624
    {
625
    numPoints = 0;
626
    vv.removeAllElements();
627
    vc.removeAllElements();
628
    cacheDirty = false;
629
   
630
    if( vn!=null ) vn.removeAllElements();
631
    }
632
  
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634
/**
635
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
636
 * <p>
637
 * Since this is a 4-dimensional Interpolator, the resulting interpolated Float4D gets written
638
 * to four locations in the buffer: buffer[offset], buffer[offset+1], buffer[offset+2] and buffer[offset+3]. 
639
 * 
640
 * @param buffer Float buffer we will write the resulting Float4D to.
641
 * @param offset Offset in the buffer where to write the result.
642
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
643
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
644
 */    
645
  public synchronized void interpolate(float[] buffer, int offset, float time)
646
    {  
647
    switch(numPoints)
648
      {
649
      case 0: buffer[offset  ] = 0.0f;
650
              buffer[offset+1] = 0.0f;
651
              buffer[offset+2] = 0.0f;
652
              buffer[offset+3] = 0.0f;
653
              break;
654
      case 1: curr = vv.elementAt(0);
655
              buffer[offset  ] = curr.x;
656
              buffer[offset+1] = curr.y;
657
              buffer[offset+2] = curr.z;
658
              buffer[offset+3] = curr.w;
659
              break;
660
      case 2: curr = vv.elementAt(0);
661
              next = vv.elementAt(1);
662
            
663
              if( mMode==MODE_LOOP || mMode==MODE_PATH ) time = (time>0.5f ? 2-2*time : 2*time);
664
             
665
              if( vn!=null )
666
                {
667
                time = noise(time,0);
668
            
669
                buffer[offset  ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor1 + vec2X*mFactor2 + vec3X*mFactor3);
670
                buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor1 + vec2Y*mFactor2 + vec3Y*mFactor3);
671
                buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor1 + vec2Z*mFactor2 + vec3Z*mFactor3);
672
                buffer[offset+3] = (next.w-curr.w)*time + curr.w + (vec1W*mFactor1 + vec2W*mFactor2 + vec3W*mFactor3); 
673
                }
674
              else
675
                {
676
                buffer[offset  ] = (next.x-curr.x)*time + curr.x;
677
                buffer[offset+1] = (next.y-curr.y)*time + curr.y;
678
                buffer[offset+2] = (next.z-curr.z)*time + curr.z;
679
                buffer[offset+3] = (next.w-curr.w)*time + curr.w;
680
                }
681
                
682
              break;
683
      default:float t = time;
684
        
685
              switch(mMode)
686
                {
687
                case MODE_LOOP: time = time*numPoints;
688
                                break;
689
                case MODE_PATH: time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
690
                                break;
691
                case MODE_JUMP: time = time*(numPoints-1);
692
                                break;
693
                }
694
     
695
              int vecCurr = (int)time;
696
              time = time-vecCurr;
697
      
698
              if( vecCurr>=0 && vecCurr<numPoints )
699
                {
700
                if( cacheDirty ) recomputeCache();    // recompute cache if we have added or remove vectors since last computation
701
                else if( mVecCurr!= vecCurr )         // ...or if we have just passed a vector and the vector we are currently flying to has changed
702
                  {
703
                  int vecNext;   
704
                  mVecCurr = vecCurr;
705
                       
706
                  switch(mMode)
707
                    {
708
                    case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1; 
709
                                    break;
710
                    case MODE_PATH: if( t<0.5f ) vecNext = vecCurr==numPoints-1 ? numPoints-2: vecCurr+1;  
711
                                    else         vecNext = vecCurr==0 ? 1 : vecCurr-1;  
712
                                    break;
713
                    case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
714
                                    break;
715
                    default       : vecNext = 0;                
716
                    }
717
     
718
                  next = vv.elementAt(vecNext);
719
                  tmp2 = vc.elementAt(vecNext);
720
              
721
                  if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z || tmp2.vw!=next.w ) recomputeCache();
722
                  }
723
            
724
                tmp1 = vc.elementAt(vecCurr);
725
               
726
                if( vn!=null )
727
                  {
728
                  time = noise(time,vecCurr);
729
              
730
                  setUpVectors(time,tmp1);
731
                 
732
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor1 + vec2X*mFactor2 + vec3X*mFactor3);
733
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor1 + vec2Y*mFactor2 + vec3Y*mFactor3);
734
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor1 + vec2Z*mFactor2 + vec3Z*mFactor3);
735
                  buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw + (vec1W*mFactor1 + vec2W*mFactor2 + vec3W*mFactor3);
736
                  }
737
                else
738
                  {
739
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx;
740
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy;
741
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz;
742
                  buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw;
743
                  }
744
 
745
                break;
746
                }
747
      }
748
    }  
749

    
750
  }
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752
//
(29-29/30)