Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic4D.java @ bb7d8484

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.Vector;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/** 
26
* A 4-dimensional implementation of the Dynamic class to interpolate between a list
27
* of Float4Ds.
28
*/
29

    
30
public class Dynamic4D extends Dynamic implements Data4D
31
  {
32
 
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// 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.
35
// (x,y,z,w) is the vector tangent to the path.
36
// (vx,vy,vz,vw) is the original vector from vv (copied here so when interpolating we can see if it is 
37
// still valid and if not - rebuild the Cache
38
  
39
  private class VectorCache
40
    {
41
    float ax, bx, cx, dx;
42
    float ay, by, cy, dy;
43
    float az, bz, cz, dz;
44
    float aw, bw, cw, dw;
45
   
46
    float x,y,z,w;
47
    float vx,vy,vz,vw;
48
    }
49
  
50
  private class VectorNoise
51
    {
52
    float[] nx;
53
    float[] ny;
54
    float[] nz;
55
    float[] nw;
56
   
57
    public VectorNoise()
58
      {
59
      nx = new float[NUM_NOISE]; 
60
      nx[0] = mRnd.nextFloat();
61
      for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1] + mRnd.nextFloat();
62
      float sum = nx[NUM_NOISE-1] + mRnd.nextFloat();
63
      for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum;
64
     
65
      ny = new float[NUM_NOISE];
66
      for(int i=0; i<NUM_NOISE; i++) ny[i] = mRnd.nextFloat()-0.5f;
67
     
68
      nz = new float[NUM_NOISE];
69
      for(int i=0; i<NUM_NOISE; i++) nz[i] = mRnd.nextFloat()-0.5f;
70
     
71
      nw = new float[NUM_NOISE];
72
      for(int i=0; i<NUM_NOISE; i++) nw[i] = mRnd.nextFloat()-0.5f;  
73
      }
74
    }
75
  
76
  private Vector<VectorCache> vc;
77
  private VectorCache tmp1, tmp2;
78

    
79
  private Vector<Static4D> vv;
80
  private Static4D prev, curr, next;
81
  
82
  private Vector<VectorNoise> vn;
83
  private VectorNoise tmpN;
84
  
85
  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.
86
  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)
87
  private float vec2X,vec2Y,vec2Z,vec2W;      // vector perpendicular to v(t) and to vec1.
88
  private float vec3X,vec3Y,vec3Z,vec3W;      // vector perpendicular to v(t) and to vec1.
89
  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  synchronized void createNoise()
93
    {
94
    if( vn==null )
95
      {  
96
      vn = new Vector<>();
97
      for(int i=0; i<numPoints; i++) vn.add(new VectorNoise());
98
      }
99
    }
100
   
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// no array bounds checking!
103
  
104
  private void vec(int c)
105
    {
106
    int p = c>0 ? c-1: numPoints-1;
107
    int n = c<numPoints-1 ? c+1: 0;
108
    
109
    prev = vv.elementAt(p);
110
    curr = vv.elementAt(c);
111
    next = vv.elementAt(n);
112

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

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

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
/**
463
 * Default constructor.
464
 *
465
 * @param duration number of milliseconds it takes to do a full loop/path from first vector to the
466
 *                 last and back to the first
467
 * @param count    number of loops/paths we will do; mCount = 1.5 means we go from the first vector
468
 *                 to the last, back to first, and to the last again.
469
 */
470
  public Dynamic4D(int duration, float count)
471
    {
472
    vv = new Vector<>();
473
    vc = new Vector<>();
474
    vn = null;
475
    numPoints = 0;
476
    cacheDirty = false;
477
    mMode = MODE_LOOP;
478
    mDuration = duration;
479
    mCount = count;
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483
/**
484
 * Returns the location'th Static4D.
485
 *   
486
 * @param location the index of the Point we are interested in.
487
 * @return The Static4D, if 0<=location&lt;getNumPoints(), or null otherwise.
488
 */  
489
  public synchronized Static4D getPoint(int location)
490
    {
491
    return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;  
492
    }
493
  
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
/**
496
 * Resets the location'th Point.
497
 * 
498
 * @param location the index of the Point we are setting.
499
 * @param x New value of its first float.
500
 */
501
  public synchronized void setPoint(int location, float x, float y, float z, float w)
502
    {
503
    if( location>=0 && location<numPoints )
504
      {
505
      curr = vv.elementAt(location);
506
   
507
      if( curr!=null )
508
        {
509
        curr.set(x,y,z,w);
510
        cacheDirty=true;
511
        }
512
      }
513
    }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516
/**
517
 * Adds a new Static4D to the end of our list of Points to interpolate through.
518
 * <p>   
519
 * Only a reference to the Point gets added to the List; this means that one can add a Point 
520
 * here, and later on {@link Static4D#set(float,float,float,float)} it to some new value and
521
 * the change will be seamlessly reflected in the interpolated path.  
522
 * <p>
523
 * A Point can be added multiple times.
524
 *   
525
 * @param v The Point to add.
526
 */    
527
  public synchronized void add(Static4D v)
528
    {
529
    if( v!=null )
530
      {
531
      vv.add(v);
532
        
533
      if( vn!=null ) vn.add(new VectorNoise());
534
       
535
       switch(numPoints)
536
         {
537
         case 0: break;
538
         case 1: setUpVectors(0.0f,null);
539
                 break;
540
         case 2: vc.add(new VectorCache());
541
                 vc.add(new VectorCache());
542
                 vc.add(new VectorCache());
543
                 break;
544
         default:vc.add(new VectorCache());
545
         }
546

    
547
       numPoints++;
548
       cacheDirty = true;
549
       }
550
    }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553
/**
554
 * Adds a new Static4D to the location'th place in our List of Points to interpolate through.
555
 *   
556
 * @param location Index in our List to add the new Point at.
557
 * @param v The Static4D to add.
558
 */  
559
  public synchronized void add(int location, Static4D v)
560
    {
561
    if( v!=null )
562
      {
563
      vv.add(location, v);
564
      
565
      if( vn!=null ) vn.add(new VectorNoise());
566
      
567
      switch(numPoints)
568
        {
569
        case 0: break;
570
        case 1: setUpVectors(0.0f,null);
571
                break;
572
        case 2: vc.add(new VectorCache());
573
                vc.add(new VectorCache());
574
                vc.add(new VectorCache());
575
                break;
576
        default:vc.add(location,new VectorCache());
577
        }
578

    
579
      numPoints++;
580
      cacheDirty = true;
581
      }
582
    }
583
  
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585
/**
586
 * Removes all occurrences of Point v from the List of Points to interpolate through.  
587
 * 
588
 * @param v The Point to remove.
589
 * @return <code>true</code> if we have removed at least one Point.
590
 */
591
  public synchronized boolean remove(Static4D v)
592
    {
593
    int n = vv.indexOf(v);
594
    boolean found = false;
595
   
596
    while( n>=0 ) 
597
      {
598
      vv.remove(n);
599
     
600
      if( vn!=null ) vn.remove(0);
601
     
602
      switch(numPoints)
603
        {
604
        case 0:
605
        case 1: 
606
        case 2: break;
607
        case 3: vc.removeAllElements();
608
                setUpVectors(0.0f,null);
609
                break;
610
        default:vc.remove(n);
611
        }
612

    
613
      numPoints--;
614
      found = true;
615
      n = vv.indexOf(v);
616
      }
617
   
618
    if( found ) 
619
      {
620
      cacheDirty=true;
621
      }
622
   
623
    return found;
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627
/**
628
 * Removes a location'th Point from the List of Points we interpolate through.
629
 * 
630
 * @param location index of the Point we want to remove. 
631
 * @return <code>true</code> if location is valid, i.e. if 0<=location&lt;getNumPoints().
632
 */
633
  public synchronized boolean remove(int location)
634
    {
635
    if( location>=0 && location<numPoints ) 
636
      {
637
      vv.removeElementAt(location);
638
       
639
      if( vn!=null ) vn.remove(0);
640
      
641
      switch(numPoints)
642
        {
643
        case 0:
644
        case 1: 
645
        case 2: break;
646
        case 3: vc.removeAllElements();
647
                setUpVectors(0.0f,null);
648
                break;
649
        default:vc.removeElementAt(location);
650
        }
651

    
652
      numPoints--;
653
      cacheDirty = true; 
654
      return true;
655
      }
656

    
657
    return false;
658
    }
659
  
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661
/**
662
 * Removes all Points.
663
 */
664
  public synchronized void removeAll()
665
    {
666
    numPoints = 0;
667
    vv.removeAllElements();
668
    vc.removeAllElements();
669
    cacheDirty = false;
670
   
671
    if( vn!=null ) vn.removeAllElements();
672
    }
673
  
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
/**
676
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
677
 * <p>
678
 * Since this is a 4-dimensional Dynamic, the resulting interpolated Static4D gets written
679
 * to four locations in the buffer: buffer[offset], buffer[offset+1], buffer[offset+2] and buffer[offset+3]. 
680
 * 
681
 * @param buffer Float buffer we will write the resulting Static4D to.
682
 * @param offset Offset in the buffer where to write the result.
683
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
684
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
685
 */    
686
  public synchronized void interpolate(float[] buffer, int offset, float time)
687
    {  
688
    switch(numPoints)
689
      {
690
      case 0: buffer[offset  ] = 0.0f;
691
              buffer[offset+1] = 0.0f;
692
              buffer[offset+2] = 0.0f;
693
              buffer[offset+3] = 0.0f;
694
              break;
695
      case 1: curr = vv.elementAt(0);
696
              buffer[offset  ] = curr.x;
697
              buffer[offset+1] = curr.y;
698
              buffer[offset+2] = curr.z;
699
              buffer[offset+3] = curr.w;
700
              break;
701
      case 2: curr = vv.elementAt(0);
702
              next = vv.elementAt(1);
703
            
704
              if( mMode==MODE_LOOP || mMode==MODE_PATH ) time = (time>0.5f ? 2-2*time : 2*time);
705
             
706
              if( vn!=null )
707
                {
708
                time = noise(time,0);
709
            
710
                buffer[offset  ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor1 + vec2X*mFactor2 + vec3X*mFactor3);
711
                buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor1 + vec2Y*mFactor2 + vec3Y*mFactor3);
712
                buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor1 + vec2Z*mFactor2 + vec3Z*mFactor3);
713
                buffer[offset+3] = (next.w-curr.w)*time + curr.w + (vec1W*mFactor1 + vec2W*mFactor2 + vec3W*mFactor3); 
714
                }
715
              else
716
                {
717
                buffer[offset  ] = (next.x-curr.x)*time + curr.x;
718
                buffer[offset+1] = (next.y-curr.y)*time + curr.y;
719
                buffer[offset+2] = (next.z-curr.z)*time + curr.z;
720
                buffer[offset+3] = (next.w-curr.w)*time + curr.w;
721
                }
722
                
723
              break;
724
      default:float t = time;
725
        
726
              switch(mMode)
727
                {
728
                case MODE_LOOP: time = time*numPoints;
729
                                break;
730
                case MODE_PATH: time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
731
                                break;
732
                case MODE_JUMP: time = time*(numPoints-1);
733
                                break;
734
                }
735
     
736
              int vecCurr = (int)time;
737
              time = time-vecCurr;
738
      
739
              if( vecCurr>=0 && vecCurr<numPoints )
740
                {
741
                if( cacheDirty ) recomputeCache();    // recompute cache if we have added or remove vectors since last computation
742
                else if( mVecCurr!= vecCurr )         // ...or if we have just passed a vector and the vector we are currently flying to has changed
743
                  {
744
                  int vecNext;   
745
                  mVecCurr = vecCurr;
746
                       
747
                  switch(mMode)
748
                    {
749
                    case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1; 
750
                                    break;
751
                    case MODE_PATH: if( t<0.5f ) vecNext = vecCurr==numPoints-1 ? numPoints-2: vecCurr+1;  
752
                                    else         vecNext = vecCurr==0 ? 1 : vecCurr-1;  
753
                                    break;
754
                    case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
755
                                    break;
756
                    default       : vecNext = 0;                
757
                    }
758
     
759
                  next = vv.elementAt(vecNext);
760
                  tmp2 = vc.elementAt(vecNext);
761
              
762
                  if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z || tmp2.vw!=next.w ) recomputeCache();
763
                  }
764
            
765
                tmp1 = vc.elementAt(vecCurr);
766
               
767
                if( vn!=null )
768
                  {
769
                  time = noise(time,vecCurr);
770
              
771
                  setUpVectors(time,tmp1);
772
                 
773
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor1 + vec2X*mFactor2 + vec3X*mFactor3);
774
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor1 + vec2Y*mFactor2 + vec3Y*mFactor3);
775
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor1 + vec2Z*mFactor2 + vec3Z*mFactor3);
776
                  buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw + (vec1W*mFactor1 + vec2W*mFactor2 + vec3W*mFactor3);
777
                  }
778
                else
779
                  {
780
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx;
781
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy;
782
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz;
783
                  buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw;
784
                  }
785
 
786
                break;
787
                }
788
      }
789
    }  
790

    
791
  }
792
///////////////////////////////////////////////////////////////////////////////////////////////////
793
//
(9-9/14)