Project

General

Profile

« Previous | Next » 

Revision 3002bef3

Added by Leszek Koltunski over 7 years ago

Move most of the NOISE complications from DynamicND classes to the parent Dynamic class.

View differences:

src/main/java/org/distorted/library/type/Dynamic4D.java
46 46
    float x,y,z,w;
47 47
    float vx,vy,vz,vw;
48 48
    }
49
  
50
  private class VectorNoise
51
    {
52
    float[] nx;
53
    float[] ny;
54
    float[] nz;
55
    float[] nw;
56
   
57
    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
  
49

  
76 50
  private Vector<VectorCache> vc;
77 51
  private VectorCache tmp1, tmp2;
78 52

  
79 53
  private Vector<Static4D> vv;
80 54
  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. FactorN = noise factor of vecN.
55

  
86 56
  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 57
  private float vec2X,vec2Y,vec2Z,vec2W;      // vector perpendicular to v(t) and to vec1.
88 58
  private float vec3X,vec3Y,vec3Z,vec3W;      // vector perpendicular to v(t) and to vec1.
89
  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91 59

  
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 60
///////////////////////////////////////////////////////////////////////////////////////////////////
102 61
// no array bounds checking!
103 62
  
......
241 200
   
242 201
    cacheDirty = false;
243 202
    }
244
  
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246 203

  
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 204
///////////////////////////////////////////////////////////////////////////////////////////////////
291 205
// v is the speed vector (i.e. position p(t) differentiated by time)
292 206
// a is the acceleration vector (differentiate once more)
......
446 360
 */
447 361
  public Dynamic4D()
448 362
    {
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;
363
    this(0,0.5f);
458 364
    }
459 365

  
460 366
///////////////////////////////////////////////////////////////////////////////////////////////////
......
477 383
    mMode = MODE_LOOP;
478 384
    mDuration = duration;
479 385
    mCount = count;
386
    mDimension = 4;
480 387
    }
481 388

  
482 389
///////////////////////////////////////////////////////////////////////////////////////////////////
......
530 437
      {
531 438
      vv.add(v);
532 439
        
533
      if( vn!=null ) vn.add(new VectorNoise());
440
      if( vn!=null ) vn.add(new VectorNoise(4));
534 441
       
535 442
       switch(numPoints)
536 443
         {
......
562 469
      {
563 470
      vv.add(location, v);
564 471
      
565
      if( vn!=null ) vn.add(new VectorNoise());
472
      if( vn!=null ) vn.add(new VectorNoise(4));
566 473
      
567 474
      switch(numPoints)
568 475
        {
......
707 614
                {
708 615
                time = noise(time,0);
709 616
            
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); 
617
                buffer[offset  ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor[0] + vec2X*mFactor[1] + vec3X*mFactor[2]);
618
                buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor[0] + vec2Y*mFactor[1] + vec3Y*mFactor[2]);
619
                buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor[0] + vec2Z*mFactor[1] + vec3Z*mFactor[2]);
620
                buffer[offset+3] = (next.w-curr.w)*time + curr.w + (vec1W*mFactor[0] + vec2W*mFactor[1] + vec3W*mFactor[2]);
714 621
                }
715 622
              else
716 623
                {
......
770 677
              
771 678
                  setUpVectors(time,tmp1);
772 679
                 
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);
680
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor[0] + vec2X*mFactor[1] + vec3X*mFactor[2]);
681
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor[0] + vec2Y*mFactor[1] + vec3Y*mFactor[2]);
682
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor[0] + vec2Z*mFactor[1] + vec3Z*mFactor[2]);
683
                  buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw + (vec1W*mFactor[0] + vec2W*mFactor[1] + vec3W*mFactor[2]);
777 684
                  }
778 685
                else
779 686
                  {

Also available in: Unified diff