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/Dynamic5D.java
47 47
    float x,y,z,w,v;
48 48
    float vx,vy,vz,vw,vv;
49 49
    }
50
  
51
  private class VectorNoise
52
    {
53
    float[] nx;
54
    float[] ny;
55
    float[] nz;
56
    float[] nw;
57
    float[] nv;
58
   
59
    VectorNoise()
60
      {
61
      nx = new float[NUM_NOISE]; 
62
      nx[0] = mRnd.nextFloat();
63
      for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1] + mRnd.nextFloat();
64
      float sum = nx[NUM_NOISE-1] + mRnd.nextFloat();
65
      for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum;
66
     
67
      ny = new float[NUM_NOISE];
68
      for(int i=0; i<NUM_NOISE; i++) ny[i] = mRnd.nextFloat()-0.5f;
69
     
70
      nz = new float[NUM_NOISE];
71
      for(int i=0; i<NUM_NOISE; i++) nz[i] = mRnd.nextFloat()-0.5f;
72
     
73
      nw = new float[NUM_NOISE];
74
      for(int i=0; i<NUM_NOISE; i++) nw[i] = mRnd.nextFloat()-0.5f;  
75 50

  
76
      nv = new float[NUM_NOISE];
77
      for(int i=0; i<NUM_NOISE; i++) nv[i] = mRnd.nextFloat()-0.5f;  
78
      }
79
    }
80
  
81 51
  private Vector<VectorCache> vc;
82 52
  private VectorCache tmp1, tmp2;
83 53

  
84 54
  private Vector<Static5D> vv;
85 55
  private Static5D prev, curr, next;
86
  
87
  private Vector<VectorNoise> vn;
88
  private VectorNoise tmpN;
89
  
90
  private float mFactor1, mFactor2, mFactor3, mFactor4;  // used in Noise only. FactorN = noise factor of vector vecN.
56

  
91 57
  private float vec1X,vec1Y,vec1Z,vec1W,vec1V; //
92
  private float vec2X,vec2Y,vec2Z,vec2W,vec2V; // 4 noise vectors.
58
  private float vec2X,vec2Y,vec2Z,vec2W,vec2V; // 4 base noise vectors.
93 59
  private float vec3X,vec3Y,vec3Z,vec3W,vec3V; // 
94 60
  private float vec4X,vec4Y,vec4Z,vec4W,vec4V; // 
95
  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97 61

  
98
  synchronized void createNoise()
99
    {
100
    if( vn==null )
101
      {  
102
      vn = new Vector<>();
103
      for(int i=0; i<numPoints; i++) vn.add(new VectorNoise());
104
      }
105
    }
106
   
107 62
///////////////////////////////////////////////////////////////////////////////////////////////////
108 63
// no array bounds checking!
109 64
  
......
263 218
   
264 219
    cacheDirty = false;
265 220
    }
266
  
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268 221

  
269
  private float noise(float time,int vecNum)
270
    {
271
    float lower, upper, len;  
272
    float d = time*(NUM_NOISE+1);
273
    int index = (int)d;
274
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
275
    tmpN = vn.elementAt(vecNum);
276
   
277
    float t = d-index;
278
    t = t*t*(3-2*t);
279
   
280
    switch(index)
281
      {
282
      case 0        : mFactor1 = mNoise*tmpN.ny[0]*t;
283
                      mFactor2 = mNoise*tmpN.nz[0]*t;
284
                      mFactor3 = mNoise*tmpN.nw[0]*t;
285
                      mFactor4 = mNoise*tmpN.nv[0]*t;
286
                      return time + mNoise*(d*tmpN.nx[0]-time);
287
      case NUM_NOISE: mFactor1= mNoise*tmpN.ny[NUM_NOISE-1]*(1-t);
288
                      mFactor2= mNoise*tmpN.nz[NUM_NOISE-1]*(1-t);
289
                      mFactor3= mNoise*tmpN.nw[NUM_NOISE-1]*(1-t);
290
                      mFactor4= mNoise*tmpN.nv[NUM_NOISE-1]*(1-t);
291
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
292
                      lower = len + mNoise*(tmpN.nx[NUM_NOISE-1]-len);  
293
                      return (1.0f-lower)*(d-NUM_NOISE) + lower;
294
      default       : float ya,yb;
295
                      yb = tmpN.ny[index  ];
296
                      ya = tmpN.ny[index-1];
297
                      mFactor1 = mNoise*((yb-ya)*t+ya);
298
                      yb = tmpN.nz[index  ];
299
                      ya = tmpN.nz[index-1];
300
                      mFactor2 = mNoise*((yb-ya)*t+ya);
301
                      yb = tmpN.nw[index  ];
302
                      ya = tmpN.nw[index-1];
303
                      mFactor3 = mNoise*((yb-ya)*t+ya);
304
                      yb = tmpN.nv[index  ];
305
                      ya = tmpN.nv[index-1];
306
                      mFactor4 = mNoise*((yb-ya)*t+ya);
307
   
308
                      len = ((float)index)/(NUM_NOISE+1);
309
                      lower = len + mNoise*(tmpN.nx[index-1]-len);   
310
                      len = ((float)index+1)/(NUM_NOISE+1); 
311
                      upper = len + mNoise*(tmpN.nx[index  ]-len);
312
            
313
                      return (upper-lower)*(d-index) + lower; 
314
      }
315
    }
316
     
317 222
///////////////////////////////////////////////////////////////////////////////////////////////////
318 223
// v is the speed vector (i.e. position p(t) differentiated by time)
319 224
// a is the acceleration vector (differentiate once more)
......
450 355
        vec2Z = 1.0f - coeff21*vz - coeff22*vec1Z;
451 356
        vec2W = 0.0f - coeff21*vw - coeff22*vec1W;
452 357
        vec2V = 0.0f - coeff21*vv - coeff22*vec1V;
453
     
358

  
454 359
        float vec2_sq = vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z+vec2W*vec2W+vec2V*vec2V;
455 360
        float coeff31 = vw/v_sq;
456 361
        float coeff32 = vec1W/vec1_sq;
......
478 383
        float len2 = (float)Math.sqrt(v_sq/vec2_sq);    
479 384
        float len3 = (float)Math.sqrt(v_sq/vec3_sq);
480 385
        float len4 = (float)Math.sqrt(v_sq/vec4_sq);
481
     
386

  
482 387
        vec1X*=len1;
483 388
        vec1Y*=len1;
484 389
        vec1Z*=len1;
......
540 445
 */
541 446
  public Dynamic5D()
542 447
    {
543
    vv = new Vector<>();
544
    vc = new Vector<>();
545
    vn = null;
546
    numPoints = 0;
547
    cacheDirty = false;
548
    mMode = MODE_LOOP;
549
    mDuration = 0;
550
    mCount = 0.5f;
551
    mNoise = 0.0f;
448
    this(0,0.5f);
552 449
    }
553 450

  
554 451
///////////////////////////////////////////////////////////////////////////////////////////////////
......
571 468
    mMode = MODE_LOOP;
572 469
    mDuration = duration;
573 470
    mCount = count;
471
    mDimension = 5;
574 472
    }
575 473

  
576 474
///////////////////////////////////////////////////////////////////////////////////////////////////
......
624 522
      {
625 523
      vv.add(v);
626 524
        
627
      if( vn!=null ) vn.add(new VectorNoise());
525
      if( vn!=null ) vn.add(new VectorNoise(5));
628 526
       
629
       switch(numPoints)
630
         {
631
         case 0: break;
632
         case 1: setUpVectors(0.0f,null);
633
                 break;
634
         case 2: vc.add(new VectorCache());
635
                 vc.add(new VectorCache());
636
                 vc.add(new VectorCache());
637
                 break;
638
         default:vc.add(new VectorCache());
639
         }
527
      switch(numPoints)
528
        {
529
        case 0: break;
530
        case 1: setUpVectors(0.0f,null);
531
                break;
532
        case 2: vc.add(new VectorCache());
533
                vc.add(new VectorCache());
534
                vc.add(new VectorCache());
535
                break;
536
        default:vc.add(new VectorCache());
537
        }
640 538

  
641
       numPoints++;
642
       cacheDirty = true;
643
       }
539
      numPoints++;
540
      cacheDirty = true;
541
      }
644 542
    }
645 543

  
646 544
///////////////////////////////////////////////////////////////////////////////////////////////////
......
656 554
      {
657 555
      vv.add(location, v);
658 556
      
659
      if( vn!=null ) vn.add(new VectorNoise());
557
      if( vn!=null ) vn.add(new VectorNoise(5));
660 558
      
661 559
      switch(numPoints)
662 560
        {
......
803 701
                {
804 702
                time = noise(time,0);
805 703
            
806
                buffer[offset  ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor1 + vec2X*mFactor2 + vec3X*mFactor3 + vec4X*mFactor4);
807
                buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor1 + vec2Y*mFactor2 + vec3Y*mFactor3 + vec4Y*mFactor4);
808
                buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor1 + vec2Z*mFactor2 + vec3Z*mFactor3 + vec4Z*mFactor4);
809
                buffer[offset+3] = (next.w-curr.w)*time + curr.w + (vec1W*mFactor1 + vec2W*mFactor2 + vec3W*mFactor3 + vec4W*mFactor4); 
810
                buffer[offset+4] = (next.v-curr.v)*time + curr.v + (vec1V*mFactor1 + vec2V*mFactor2 + vec3V*mFactor3 + vec4V*mFactor4); 
704
                buffer[offset  ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor[0] + vec2X*mFactor[1] + vec3X*mFactor[2] + vec4X*mFactor[3]);
705
                buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor[0] + vec2Y*mFactor[1] + vec3Y*mFactor[2] + vec4Y*mFactor[3]);
706
                buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor[0] + vec2Z*mFactor[1] + vec3Z*mFactor[2] + vec4Z*mFactor[3]);
707
                buffer[offset+3] = (next.w-curr.w)*time + curr.w + (vec1W*mFactor[0] + vec2W*mFactor[1] + vec3W*mFactor[2] + vec4W*mFactor[3]);
708
                buffer[offset+4] = (next.v-curr.v)*time + curr.v + (vec1V*mFactor[0] + vec2V*mFactor[1] + vec3V*mFactor[2] + vec4V*mFactor[3]);
811 709
                }
812 710
              else
813 711
                {
......
868 766
              
869 767
                  setUpVectors(time,tmp1);
870 768
                 
871
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor1 + vec2X*mFactor2 + vec3X*mFactor3 + vec4X*mFactor4);
872
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor1 + vec2Y*mFactor2 + vec3Y*mFactor3 + vec4Y*mFactor4);
873
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor1 + vec2Z*mFactor2 + vec3Z*mFactor3 + vec4Z*mFactor4);
874
                  buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw + (vec1W*mFactor1 + vec2W*mFactor2 + vec3W*mFactor3 + vec4W*mFactor4);
875
                  buffer[offset+4]= ((tmp1.av*time+tmp1.bv)*time+tmp1.cv)*time+tmp1.dv + (vec1V*mFactor1 + vec2V*mFactor2 + vec3V*mFactor3 + vec4V*mFactor4);
769
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor[0] + vec2X*mFactor[1] + vec3X*mFactor[2] + vec4X*mFactor[3]);
770
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor[0] + vec2Y*mFactor[1] + vec3Y*mFactor[2] + vec4Y*mFactor[3]);
771
                  buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor[0] + vec2Z*mFactor[1] + vec3Z*mFactor[2] + vec4Z*mFactor[3]);
772
                  buffer[offset+3]= ((tmp1.aw*time+tmp1.bw)*time+tmp1.cw)*time+tmp1.dw + (vec1W*mFactor[0] + vec2W*mFactor[1] + vec3W*mFactor[2] + vec4W*mFactor[3]);
773
                  buffer[offset+4]= ((tmp1.av*time+tmp1.bv)*time+tmp1.cv)*time+tmp1.dv + (vec1V*mFactor[0] + vec2V*mFactor[1] + vec3V*mFactor[2] + vec4V*mFactor[3]);
876 774
                  }
877 775
                else
878 776
                  {

Also available in: Unified diff