Project

General

Profile

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

library / src / main / java / org / distorted / library / type / Dynamic3D.java @ 3002bef3

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 3-dimensional implementation of the Dynamic class to interpolate between a list
27
* of Static3Ds.
28
*/
29

    
30
public class Dynamic3D extends Dynamic implements Data3D
31
  {
32
 
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// the coefficients of the X(t), Y(t) and Z(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx  etc.
35
// (x,y,z) is the vector tangent to the path.
36
// (vx,vy,vz) 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
   
45
    float x,y,z;
46
    float vx,vy,vz;
47
    }
48

    
49
  private Vector<VectorCache> vc;
50
  private VectorCache tmp1, tmp2;
51

    
52
  private Vector<Static3D> vv;
53
  private Static3D prev, curr, next;
54

    
55
  private float vec1X,vec1Y,vec1Z;   // 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)
56
  private float vec2X,vec2Y,vec2Z;   // vector perpendicular to v(t0 and to vec1.
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// no array bounds checking!
60
  
61
  private void vec(int c)
62
    {
63
    int p = c>0 ? c-1: numPoints-1;
64
    int n = c<numPoints-1 ? c+1: 0;
65
    
66
    prev = vv.elementAt(p);
67
    curr = vv.elementAt(c);
68
    next = vv.elementAt(n);
69

    
70
    tmp1 = vc.elementAt(c);
71
    
72
    float px = curr.x - prev.x;
73
    float py = curr.y - prev.y;
74
    float pz = curr.z - prev.z;
75
    float nx = next.x - curr.x;
76
    float ny = next.y - curr.y;
77
    float nz = next.z - curr.z;
78
     
79
    float d = nx*nx+ny*ny+nz*nz;
80
    
81
    if( d>0 )
82
      {
83
      float q = (float)Math.sqrt((px*px+py*py+pz*pz)/d);
84
      
85
      if( q>1 )
86
        {
87
        tmp1.x = nx+px/q;
88
        tmp1.y = ny+py/q;
89
        tmp1.z = nz+pz/q;
90
        }
91
      else
92
        {
93
        tmp1.x = px+nx*q;
94
        tmp1.y = py+ny*q;
95
        tmp1.z = pz+nz*q;
96
        }
97
      }
98
    else
99
      {
100
      tmp1.x = 0.0f;
101
      tmp1.y = 0.0f;
102
      tmp1.z = 0.0f;  
103
      }
104
    }
105
    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
  
108
  private void recomputeCache()
109
    {  
110
    if( numPoints==1 )
111
      {
112
      tmp1= vc.elementAt(0);
113
      curr= vv.elementAt(0);
114
        
115
      tmp1.ax = tmp1.ay = tmp1.az = 0.0f;
116
      tmp1.bx = tmp1.by = tmp1.bz = 0.0f;
117
      tmp1.cx = curr.x;
118
      tmp1.cy = curr.y;
119
      tmp1.cz = curr.z;
120
      tmp1.dx = tmp1.dy = tmp1.dz = 0.0f;
121
      }
122
    else if( numPoints==2 )
123
      {
124
      tmp1= vc.elementAt(0);
125
      tmp2= vc.elementAt(1);
126
      curr= vv.elementAt(0);
127
      next= vv.elementAt(1);
128
          
129
      tmp1.ax = tmp1.ay = tmp1.az = 0.0f;
130
      tmp1.bx = tmp1.by = tmp1.bz = 0.0f;
131
      tmp1.cx = next.x - curr.x;
132
      tmp1.cy = next.y - curr.y;
133
      tmp1.cz = next.z - curr.z;
134
      tmp1.dx = curr.x;
135
      tmp1.dy = curr.y;
136
      tmp1.dz = curr.z;
137
      
138
      tmp2.ax = tmp2.ay = tmp2.az = 0.0f;
139
      tmp2.bx = tmp2.by = tmp2.bz = 0.0f;
140
      tmp2.cx = curr.x - next.x;
141
      tmp2.cy = curr.y - next.y;
142
      tmp2.cz = curr.z - next.z;
143
      tmp2.dx = next.x;
144
      tmp2.dy = next.y;
145
      tmp2.dz = next.z;
146
      }
147
    else
148
      {
149
      int i, n;  
150
         
151
      for(i=0; i<numPoints; i++) vec(i);
152
   
153
      for(i=0; i<numPoints; i++)
154
        {
155
        n = i<numPoints-1 ? i+1:0;  
156
      
157
        tmp1= vc.elementAt(i);
158
        tmp2= vc.elementAt(n);
159
        curr= vv.elementAt(i);
160
        next= vv.elementAt(n);
161
      
162
        tmp1.vx = curr.x;
163
        tmp1.vy = curr.y;
164
        tmp1.vz = curr.z;
165
        
166
        tmp1.ax =  2*curr.x +   tmp1.x - 2*next.x + tmp2.x;
167
        tmp1.bx = -3*curr.x - 2*tmp1.x + 3*next.x - tmp2.x;
168
        tmp1.cx = tmp1.x;
169
        tmp1.dx = curr.x;
170
      
171
        tmp1.ay =  2*curr.y +   tmp1.y - 2*next.y + tmp2.y;
172
        tmp1.by = -3*curr.y - 2*tmp1.y + 3*next.y - tmp2.y;
173
        tmp1.cy = tmp1.y;
174
        tmp1.dy = curr.y;
175
      
176
        tmp1.az =  2*curr.z +   tmp1.z - 2*next.z + tmp2.z;
177
        tmp1.bz = -3*curr.z - 2*tmp1.z + 3*next.z - tmp2.z;
178
        tmp1.cz = tmp1.z;
179
        tmp1.dz = curr.z;
180
        }
181
      }
182
   
183
    cacheDirty = false;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// v is the speed vector (i.e. position p(t) differentiated by time)
188
// a is the acceleration vector (differentiate once more)
189
// now what we are doing is compute vec1{X,Y,Z} to be a vector perpendicular to v and in the same plane as both v and a.
190
// vec2{X,Y,Z} would be (v)x(vec1).
191
//  
192
// vec1 = a-delta*v where delta = (v*a)/|v|^2   (see Gram-Schmidt)
193
  
194
  private void setUpVectors(float time,VectorCache vc)
195
    {
196
    if( vc!=null )
197
      {
198
      float vx = (3*vc.ax*time+2*vc.bx)*time+vc.cx;
199
      float vy = (3*vc.ay*time+2*vc.by)*time+vc.cy;
200
      float vz = (3*vc.az*time+2*vc.bz)*time+vc.cz;
201
     
202
      float ax = 6*vc.ax*time+2*vc.bx;
203
      float ay = 6*vc.ay*time+2*vc.by;
204
      float az = 6*vc.az*time+2*vc.bz;
205
     
206
      float v_sq = vx*vx+vy*vy+vz*vz;
207
      float delta = (vx*ax+vy*ay+vz*az)/v_sq;
208
     
209
      vec1X = ax-delta*vx;
210
      vec1Y = ay-delta*vy;
211
      vec1Z = az-delta*vz;
212
     
213
      vec2X = vy*vec1Z-vz*vec1Y;
214
      vec2Y = vz*vec1X-vx*vec1Z;
215
      vec2Z = vx*vec1Y-vy*vec1X;
216
     
217
      float len1 = (float)Math.sqrt(v_sq/(vec1X*vec1X+vec1Y*vec1Y+vec1Z*vec1Z));
218
      float len2 = (float)Math.sqrt(v_sq/(vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z));   
219
     
220
      vec1X*=len1;
221
      vec1Y*=len1;
222
      vec1Z*=len1;
223
     
224
      vec2X*=len2;
225
      vec2Y*=len2;
226
      vec2Z*=len2;
227
      }
228
    else
229
      {
230
      curr = vv.elementAt(0);
231
      next = vv.elementAt(1); 
232
     
233
      float vx = (next.x-curr.x);
234
      float vy = (next.y-curr.y);
235
      float vz = (next.z-curr.z);
236
     
237
      float b = (float)Math.sqrt(vx*vx+vy*vy);
238
     
239
      if( b>0.0f )
240
        {
241
        vec1X = vx*vz/b;
242
        vec1Y = vy*vz/b;
243
        vec1Z = -b;
244
      
245
        vec2X = vy*vec1Z-vz*vec1Y;
246
        vec2Y = vz*vec1X-vx*vec1Z;
247
        vec2Z = vx*vec1Y-vy*vec1X;
248
       
249
        float len2 = (float)Math.sqrt((vx*vx+vy*vy+vz*vz)/(vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z));
250
       
251
        vec2X*=len2;
252
        vec2Y*=len2;
253
        vec2Z*=len2;
254
        }
255
      else
256
        {
257
        vec1X = vz;
258
        vec1Y = 0.0f;
259
        vec1Z = 0.0f;
260
      
261
        vec2X = 0.0f;
262
        vec2Y = vz;
263
        vec2Z = 0.0f;
264
        }
265
      }
266
    }
267
  
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
// PUBLIC API
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
/**
272
 * Default constructor.
273
 */
274
  public Dynamic3D()
275
    {
276
    this(0,0.5f);
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
/**
282
 * Default constructor.
283
 *
284
 * @param duration number of milliseconds it takes to do a full loop/path from first vector to the
285
 *                 last and back to the first
286
 * @param count    number of loops/paths we will do; mCount = 1.5 means we go from the first vector
287
 *                 to the last, back to first, and to the last again.
288
 */
289
  public Dynamic3D(int duration, float count)
290
    {
291
    vv = new Vector<>();
292
    vc = new Vector<>();
293
    vn = null;
294
    numPoints = 0;
295
    cacheDirty = false;
296
    mMode = MODE_LOOP;
297
    mDuration = duration;
298
    mCount = count;
299
    mDimension = 3;
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303
/**
304
 * Returns the location'th Static3D.
305
 *   
306
 * @param location the index of the Point we are interested in.
307
 * @return The Static3D, if 0<=location&lt;getNumPoints(), or null otherwise.
308
 */  
309
  public synchronized Static3D getPoint(int location)
310
    {
311
    return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;  
312
    }
313
  
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
/**
316
 * Resets the location'th Point.
317
 * 
318
 * @param location the index of the Point we are setting.
319
 * @param x New value of its first float.
320
 */
321
  public synchronized void setPoint(int location, float x, float y, float z)
322
    {
323
    if( location>=0 && location<numPoints )
324
      {
325
      curr = vv.elementAt(location);
326
   
327
      if( curr!=null )
328
        {
329
        curr.set(x,y,z);
330
        cacheDirty=true;
331
        }
332
      }
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336
/**
337
 * Adds a new Static3D to the end of our list of Points to interpolate through.
338
 * <p>   
339
 * Only a reference to the Point gets added to the List; this means that one can add a Point 
340
 * here, and later on {@link Static3D#set(float,float,float)} it to some new value and the
341
 * change will be seamlessly reflected in the interpolated path.  
342
 * <p>
343
 * A Point can be added multiple times.
344
 *   
345
 * @param v The Point to add.
346
 */    
347
  public synchronized void add(Static3D v)
348
    {
349
    if( v!=null )
350
      {
351
      vv.add(v);
352
        
353
      if( vn!=null ) vn.add(new VectorNoise(3));
354
       
355
      switch(numPoints)
356
        {
357
        case 0: break;
358
        case 1: setUpVectors(0.0f,null);
359
                break;
360
        case 2: vc.add(new VectorCache());
361
                vc.add(new VectorCache());
362
                vc.add(new VectorCache());
363
                break;
364
        default:vc.add(new VectorCache());
365
        }
366

    
367
      numPoints++;
368
      cacheDirty = true;
369
      }
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373
/**
374
 * Adds a new Static3D to the location'th place in our List of Points to interpolate through.
375
 *   
376
 * @param location Index in our List to add the new Point at.
377
 * @param v The Point to add.
378
 */  
379
  public synchronized void add(int location, Static3D v)
380
    {
381
    if( v!=null )
382
      {
383
      vv.add(location, v);
384
      
385
      if( vn!=null ) vn.add(new VectorNoise(3));
386
      
387
      switch(numPoints)
388
        {
389
        case 0: break;
390
        case 1: setUpVectors(0.0f,null);
391
                break;
392
        case 2: vc.add(new VectorCache());
393
                vc.add(new VectorCache());
394
                vc.add(new VectorCache());
395
                break;
396
        default:vc.add(location,new VectorCache());
397
        }
398

    
399
      numPoints++;
400
      cacheDirty = true;
401
      }
402
    }
403
  
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405
/**
406
 * Removes all occurrences of Point v from the List of Points to interpolate through.  
407
 * 
408
 * @param v The Point to remove.
409
 * @return <code>true</code> if we have removed at least one Point.
410
 */
411
  public synchronized boolean remove(Static3D v)
412
    {
413
    int n = vv.indexOf(v);
414
    boolean found = false;
415
   
416
    while( n>=0 ) 
417
      {
418
      vv.remove(n);
419
     
420
      if( vn!=null ) vn.remove(0);
421
     
422
      switch(numPoints)
423
        {
424
        case 0:
425
        case 1: 
426
        case 2: break;
427
        case 3: vc.removeAllElements();
428
                setUpVectors(0.0f,null);
429
                break;
430
        default:vc.remove(n);
431
        }
432

    
433
      numPoints--;
434
      found = true;
435
      n = vv.indexOf(v);
436
      }
437
   
438
    if( found ) 
439
      {
440
      cacheDirty=true;
441
      }
442
   
443
    return found;
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
/**
448
 * Removes a location'th Point from the List of Points we interpolate through.
449
 * 
450
 * @param location index of the Point we want to remove. 
451
 * @return <code>true</code> if location is valid, i.e. if 0<=location&lt;getNumPoints().
452
 */
453
  public synchronized boolean remove(int location)
454
    {
455
    if( location>=0 && location<numPoints ) 
456
      {
457
      vv.removeElementAt(location);
458
       
459
      if( vn!=null ) vn.remove(0);
460
      
461
      switch(numPoints)
462
        {
463
        case 0:
464
        case 1: 
465
        case 2: break;
466
        case 3: vc.removeAllElements();
467
                setUpVectors(0.0f,null);
468
                break;
469
        default:vc.removeElementAt(location);
470
        }
471

    
472
      numPoints--;
473
      cacheDirty = true; 
474
      return true;
475
      }
476

    
477
   return false;
478
   }
479
  
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481
/**
482
 * Removes all Points.
483
 */
484
  public synchronized void removeAll()
485
    {
486
    numPoints = 0;
487
    vv.removeAllElements();
488
    vc.removeAllElements();
489
    cacheDirty = false;
490
   
491
    if( vn!=null ) vn.removeAllElements();
492
    }
493
  
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
/**
496
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
497
 * <p>
498
 * Since this is a 3-dimensional Dynamic, the resulting interpolated Static3D gets written
499
 * to three locations in the buffer: buffer[offset], buffer[offset+1] and buffer[offset+2]. 
500
 * 
501
 * @param buffer Float buffer we will write the resulting Static3D to.
502
 * @param offset Offset in the buffer where to write the result.
503
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
504
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
505
 */    
506
  public synchronized void interpolate(float[] buffer, int offset, float time)
507
    {  
508
    switch(numPoints)
509
      {
510
      case 0: buffer[offset  ] = 0.0f;
511
              buffer[offset+1] = 0.0f;
512
              buffer[offset+2] = 0.0f;
513
              break;
514
      case 1: curr = vv.elementAt(0);
515
              buffer[offset  ] = curr.x;
516
              buffer[offset+1] = curr.y;
517
              buffer[offset+2] = curr.z;
518
              break;
519
      case 2: curr = vv.elementAt(0);
520
              next = vv.elementAt(1);
521
             
522
              if( mMode==MODE_LOOP || mMode==MODE_PATH ) time = (time>0.5f ? 2-2*time : 2*time);
523
             
524
              if( vn!=null )
525
                {
526
                time = noise(time,0);
527
            
528
                buffer[offset  ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor[0] + vec2X*mFactor[1]);
529
                buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor[0] + vec2Y*mFactor[1]);
530
                buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor[0] + vec2Z*mFactor[1]);
531
                }
532
              else
533
                {
534
                buffer[offset  ] = (next.x-curr.x)*time + curr.x;
535
                buffer[offset+1] = (next.y-curr.y)*time + curr.y;
536
                buffer[offset+2] = (next.z-curr.z)*time + curr.z;
537
                }
538
             
539
              break;
540
      default:float t = time;
541
        
542
              switch(mMode)
543
                {
544
                case MODE_LOOP: time = time*numPoints;
545
                                break;
546
                case MODE_PATH: time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
547
                                break;
548
                case MODE_JUMP: time = time*(numPoints-1);
549
                                break;
550
                }
551
           
552
              int vecCurr = (int)time;
553
              time = time-vecCurr;
554
      
555
              if( vecCurr>=0 && vecCurr<numPoints )
556
                {
557
                if( cacheDirty ) recomputeCache();    // recompute cache if we have added or remove vectors since last computation
558
                else if( mVecCurr!= vecCurr )         // ...or if we have just passed a vector and the vector we are currently flying to has changed
559
                  {
560
                  int vecNext;   
561
                  mVecCurr = vecCurr;
562
                       
563
                  switch(mMode)
564
                    {
565
                    case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1; 
566
                                    break;
567
                    case MODE_PATH: if( t<0.5f ) vecNext = vecCurr==numPoints-1 ? numPoints-2: vecCurr+1;  
568
                                    else         vecNext = vecCurr==0 ? 1 : vecCurr-1;  
569
                                    break;
570
                    case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
571
                                    break;
572
                    default       : vecNext = 0;                
573
                    }
574
              
575
                  next = vv.elementAt(vecNext);
576
                  tmp2 = vc.elementAt(vecNext);
577
              
578
                  if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z ) recomputeCache();
579
                  }
580
            
581
                tmp1 = vc.elementAt(vecCurr);
582
               
583
                if( vn!=null )
584
                  {
585
                  time = noise(time,vecCurr);
586
              
587
                  setUpVectors(time,tmp1);
588
                 
589
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor[0] + vec2X*mFactor[1]);
590
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor[0] + vec2Y*mFactor[1]);
591
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor[0] + vec2Z*mFactor[1]);
592
                  }
593
                else
594
                  {
595
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx;
596
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy;
597
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz;
598
                  }
599
               
600
                break;
601
                }
602
       }
603
     }  
604

    
605
  }
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607
//
(9-9/17)