Project

General

Profile

« Previous | Next » 

Revision 649544b8

Added by Leszek Koltunski over 7 years ago

Completely redesign Noise in the Dynamics and move all the complexity to the parent class.

something does not work with it now :)

View differences:

src/main/java/org/distorted/library/type/Dynamic2D.java
29 29

  
30 30
public class Dynamic2D extends Dynamic implements Data2D
31 31
  {
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// the coefficients of the X(t), Y(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx  etc.
35
// (x,y) is the vector tangent to the path.
36
// (vx,vy) 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
   
44
    float x,y;
45
    float vx,vy;
46
    }
47

  
48
  private Vector<VectorCache> vc;
49
  private VectorCache tmp1, tmp2;
50
   
51 32
  private Vector<Static2D> vv;
52 33
  private Static2D prev, curr, next;
53 34

  
......
78 59
      
79 60
      if( q>1 )
80 61
        {
81
        tmp1.x = nx+px/q;
82
        tmp1.y = ny+py/q;
62
        tmp1.tangent[0] = nx+px/q;
63
        tmp1.tangent[1] = ny+py/q;
83 64
        }
84 65
      else
85 66
        {
86
        tmp1.x = px+nx*q;
87
        tmp1.y = py+ny*q;
67
        tmp1.tangent[0] = px+nx*q;
68
        tmp1.tangent[1] = py+ny*q;
88 69
        }
89 70
      }
90 71
    else
91 72
      {
92
      tmp1.x = 0.0f;
93
      tmp1.y = 0.0f;
73
      tmp1.tangent[0] = 0.0f;
74
      tmp1.tangent[1] = 0.0f;
94 75
      }
95 76
    }
96 77
   
......
103 84
      tmp1= vc.elementAt(0);
104 85
      curr= vv.elementAt(0);
105 86
              
106
      tmp1.ax = tmp1.ay = 0.0f;
107
      tmp1.bx = tmp1.by = 0.0f;
108
      tmp1.cx = curr.x;
109
      tmp1.cy = curr.y;
110
      tmp1.dx = tmp1.dy = 0.0f;
87
      tmp1.a[0] = tmp1.a[1] = 0.0f;
88
      tmp1.b[0] = tmp1.b[1] = 0.0f;
89
      tmp1.c[0] = curr.x;
90
      tmp1.c[1] = curr.y;
91
      tmp1.d[0] = tmp1.d[1] = 0.0f;
111 92
      }
112 93
    else if( numPoints==2 )
113 94
      {
......
116 97
      curr= vv.elementAt(0);
117 98
      next= vv.elementAt(1);
118 99
          
119
      tmp1.ax = tmp1.ay = 0.0f;
120
      tmp1.bx = tmp1.by = 0.0f;
121
      tmp1.cx = next.x - curr.x;
122
      tmp1.cy = next.y - curr.y;
123
      tmp1.dx = curr.x;
124
      tmp1.dy = curr.y;
100
      tmp1.a[0] = tmp1.a[1] = 0.0f;
101
      tmp1.b[0] = tmp1.b[1] = 0.0f;
102
      tmp1.c[0] = next.x - curr.x;
103
      tmp1.c[1] = next.y - curr.y;
104
      tmp1.d[0] = curr.x;
105
      tmp1.d[1] = curr.y;
125 106
      
126
      tmp2.ax = tmp2.ay = 0.0f;
127
      tmp2.bx = tmp2.by = 0.0f;
128
      tmp2.cx = curr.x - next.x;
129
      tmp2.cy = curr.y - next.y;
130
      tmp2.dx = next.x;
131
      tmp2.dy = next.y;
107
      tmp2.a[0] = tmp2.a[1] = 0.0f;
108
      tmp2.b[0] = tmp2.b[1] = 0.0f;
109
      tmp2.c[0] = curr.x - next.x;
110
      tmp2.c[1] = curr.y - next.y;
111
      tmp2.d[0] = next.x;
112
      tmp2.d[1] = next.y;
132 113
      }
133 114
    else
134 115
      {
......
145 126
        curr= vv.elementAt(i);
146 127
        next= vv.elementAt(n);
147 128
      
148
        tmp1.vx = curr.x;
149
        tmp1.vy = curr.y;
150
        
151
        tmp1.ax =  2*curr.x +   tmp1.x - 2*next.x + tmp2.x;
152
        tmp1.bx = -3*curr.x - 2*tmp1.x + 3*next.x - tmp2.x;
153
        tmp1.cx = tmp1.x;
154
        tmp1.dx = curr.x;
155
      
156
        tmp1.ay =  2*curr.y +   tmp1.y - 2*next.y + tmp2.y;
157
        tmp1.by = -3*curr.y - 2*tmp1.y + 3*next.y - tmp2.y;
158
        tmp1.cy = tmp1.y;
159
        tmp1.dy = curr.y;
129
        tmp1.cached[0] = curr.x;
130
        tmp1.cached[1] = curr.y;
131

  
132
        tmp1.a[0] =  2*curr.x +   tmp1.tangent[0] - 2*next.x + tmp2.tangent[0];
133
        tmp1.b[0] = -3*curr.x - 2*tmp1.tangent[0] + 3*next.x - tmp2.tangent[0];
134
        tmp1.c[0] = tmp1.tangent[0];
135
        tmp1.d[0] = curr.x;
136

  
137
        tmp1.a[1] =  2*curr.y +   tmp1.tangent[1] - 2*next.y + tmp2.tangent[1];
138
        tmp1.b[1] = -3*curr.y - 2*tmp1.tangent[1] + 3*next.y - tmp2.tangent[1];
139
        tmp1.c[1] = tmp1.tangent[1];
140
        tmp1.d[1] = curr.y;
160 141
        }
161 142
      }
162 143
    
......
171 152
 */
172 153
  public Dynamic2D()
173 154
    {
174
    this(0,0.5f);
155
    super(0,0.5f,2);
156
    vv = new Vector<>();
175 157
    }
176 158

  
177 159
///////////////////////////////////////////////////////////////////////////////////////////////////
......
186 168
 */
187 169
  public Dynamic2D(int duration, float count)
188 170
    {
171
    super(duration,count,2);
189 172
    vv = new Vector<>();
190
    vc = new Vector<>();
191
    vn = null;
192
    numPoints = 0;
193
    cacheDirty = false;
194
    mMode = MODE_LOOP;
195
    mDuration = duration;
196
    mCount = count;
197
    mDimension = 2;
198 173
    }
199 174

  
200 175
///////////////////////////////////////////////////////////////////////////////////////////////////
......
254 229
        {
255 230
        case 0:
256 231
        case 1: break;
257
        case 2: vc.add(new VectorCache());
258
                vc.add(new VectorCache());
259
                vc.add(new VectorCache());
232
        case 2: vc.add(new VectorCache(2));
233
                vc.add(new VectorCache(2));
234
                vc.add(new VectorCache(2));
260 235
                break;
261
        default:vc.add(new VectorCache());
236
        default:vc.add(new VectorCache(2));
262 237
        }
263 238
     
264 239
      numPoints++;
......
285 260
        {
286 261
        case 0:
287 262
        case 1: break;
288
        case 2: vc.add(new VectorCache());
289
                vc.add(new VectorCache());
290
                vc.add(new VectorCache());
263
        case 2: vc.add(new VectorCache(2));
264
                vc.add(new VectorCache(2));
265
                vc.add(new VectorCache(2));
291 266
                break;
292
        default:vc.add(location,new VectorCache());
267
        default:vc.add(location,new VectorCache(2));
293 268
        }
294 269
      
295 270
      numPoints++;
......
417 392
                {
418 393
                time = noise(time,0);
419 394
              
420
                float dx2 = next.x-curr.x;
421
                float dy2 = next.y-curr.y;
395
                baseV[0][0] = next.x-curr.x;
396
                baseV[0][1] = next.y-curr.y;
422 397
   
423
                buffer[offset  ] = dx2*time + curr.x +dy2*mFactor[0];
424
                buffer[offset+1] = dy2*time + curr.y -dx2*mFactor[0];
398
                buffer[offset  ] = baseV[0][0]*time + curr.x +baseV[0][0]*mFactor[0];
399
                buffer[offset+1] = baseV[0][1]*time + curr.y -baseV[0][1]*mFactor[0];
425 400
                }
426 401
              else
427 402
                {
......
468 443
                  next = vv.elementAt(vecNext);
469 444
                  tmp2 = vc.elementAt(vecNext);
470 445
              
471
                  if( tmp2.vx!=next.x || tmp2.vy!=next.y ) recomputeCache();
446
                  if( tmp2.cached[0]!=next.x || tmp2.cached[1]!=next.y ) recomputeCache();
472 447
                  }
473
              
448

  
449
                tmp1 = vc.elementAt(vecCurr);
450

  
474 451
                if( vn!=null )
475 452
                  {
476 453
                  time = noise(time,vecCurr);
477
                  tmp1 = vc.elementAt(vecCurr);
478
               
479
                  float dx2 = (3*tmp1.ax*time+2*tmp1.bx)*time + tmp1.cx;
480
                  float dy2 = (3*tmp1.ay*time+2*tmp1.by)*time + tmp1.cy;
454

  
455
                  baseV[0][0] = (3*tmp1.a[0]*time+2*tmp1.b[0])*time + tmp1.c[0];
456
                  baseV[0][1] = (3*tmp1.a[1]*time+2*tmp1.b[1])*time + tmp1.c[1];
481 457
                 
482
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx +dy2*mFactor[0];
483
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy -dx2*mFactor[0];
458
                  buffer[offset  ]= ((tmp1.a[0]*time+tmp1.b[0])*time+tmp1.c[0])*time+tmp1.d[0] +baseV[0][0]*mFactor[0];
459
                  buffer[offset+1]= ((tmp1.a[1]*time+tmp1.b[1])*time+tmp1.c[1])*time+tmp1.d[1] -baseV[0][1]*mFactor[0];
484 460
                  } 
485 461
                else
486 462
                  {
487
                  tmp1 = vc.elementAt(vecCurr);
488
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx;
489
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy;
463
                  buffer[offset  ]= ((tmp1.a[0]*time+tmp1.b[0])*time+tmp1.c[0])*time+tmp1.d[0];
464
                  buffer[offset+1]= ((tmp1.a[1]*time+tmp1.b[1])*time+tmp1.c[1])*time+tmp1.d[1];
490 465
                  }
491 466
                
492 467
                break;

Also available in: Unified diff