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/Dynamic.java
20 20
package org.distorted.library.type;
21 21

  
22 22
import java.util.Random;
23
import java.util.Vector;
23 24

  
24 25
///////////////////////////////////////////////////////////////////////////////////////////////////
25 26
/** A class to interpolate between a List of Static{1,2,3,4}Ds.
......
64 65
   * We just jump back from the last point to the first.
65 66
   */
66 67
  public static final int MODE_JUMP = 2; 
67
 
68

  
68 69
  protected static Random mRnd = new Random();
69 70
  
70 71
  protected static final int NUM_NOISE = 5; // used iff mNoise>0.0. Number of intermediary points between each pair of adjacent vectors
71 72
                                            // where we randomize noise factors to make the way between the two vectors not so smooth.
73

  
74
  protected int mDimension;
72 75
  protected int numPoints;
73 76
  protected int mVecCurr;    
74 77
  protected boolean cacheDirty; // VectorCache not up to date
......
76 79
  protected long mDuration;     // number of milliseconds it takes to do a full loop/path from first vector to the last and back to the first
77 80
  protected float mCount;       // number of loops/paths we will do; mCount = 1.5 means we go from the first vector to the last, back to first, and to the last again. 
78 81
  protected float mNoise;       // how 'smooth' our path form each vector to the next is. mNoise = 0.0 (min) --> completely smooth; mNoise==1.0 (max) --> very uneven
79
  
82

  
83
  protected class VectorNoise
84
    {
85
    float[][] n;
86

  
87
    VectorNoise(int dim)
88
      {
89
      n = new float[dim][NUM_NOISE];
90

  
91
      n[0][0] = mRnd.nextFloat();
92
      for(int i=1; i<NUM_NOISE; i++) n[0][i] = n[0][i-1]+mRnd.nextFloat();
93
      float sum = n[0][NUM_NOISE-1] + mRnd.nextFloat();
94
      for(int i=0; i<NUM_NOISE; i++) n[0][i] /=sum;
95

  
96
      for(int j=1; j<dim; j++)
97
        {
98
        for(int i=0; i<NUM_NOISE; i++) n[j][i] = mRnd.nextFloat()-0.5f;
99
        }
100
      }
101
    }
102

  
103
  protected Vector<VectorNoise> vn;
104
  protected float[] mFactor;
105

  
80 106
///////////////////////////////////////////////////////////////////////////////////////////////////
81 107
// hide this from Javadoc
82 108
  
......
128 154
    
129 155
    return false;
130 156
    }
131
 
157

  
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

  
160
  protected float noise(float time,int vecNum)
161
    {
162
    float lower, upper, len;
163
    float d = time*(NUM_NOISE+1);
164
    int index = (int)d;
165
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
166
    VectorNoise tmpN = vn.elementAt(vecNum);
167

  
168
    float t = d-index;
169
    t = t*t*(3-2*t);
170

  
171
    switch(index)
172
      {
173
      case 0        : for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise*tmpN.n[i+1][0]*t;
174
                      return time + mNoise*(d*tmpN.n[0][0]-time);
175
      case NUM_NOISE: for(int i=0;i<mDimension-1;i++) mFactor[i] = mNoise*tmpN.n[i+1][NUM_NOISE-1]*(1-t);
176
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
177
                      lower = len + mNoise*(tmpN.n[0][NUM_NOISE-1]-len);
178
                      return (1.0f-lower)*(d-NUM_NOISE) + lower;
179
      default       : float ya,yb;
180

  
181
                      for(int i=0;i<mDimension-1;i++)
182
                        {
183
                        yb = tmpN.n[i+1][index  ];
184
                        ya = tmpN.n[i+1][index-1];
185
                        mFactor[i] = mNoise*((yb-ya)*t+ya);
186
                        }
187

  
188
                      len = ((float)index)/(NUM_NOISE+1);
189
                      lower = len + mNoise*(tmpN.n[0][index-1]-len);
190
                      len = ((float)index+1)/(NUM_NOISE+1);
191
                      upper = len + mNoise*(tmpN.n[0][index  ]-len);
192

  
193
                      return (upper-lower)*(d-index) + lower;
194
      }
195
    }
196

  
132 197
///////////////////////////////////////////////////////////////////////////////////////////////////
133 198
// internal debugging only!
134 199
  
......
136 201
    {
137 202
    return "duration="+mDuration+" count="+mCount+" Noise="+mNoise+" numVectors="+numPoints+" mMode="+mMode;
138 203
    }
139
  
204

  
140 205
///////////////////////////////////////////////////////////////////////////////////////////////////
141
  
206

  
142 207
  abstract void interpolate(float[] buffer, int offset, float time);
143
  abstract void createNoise();
144 208

  
145 209
///////////////////////////////////////////////////////////////////////////////////////////////////
146 210
// PUBLIC API
......
214 278
 * @param noise The noise level. Permitted range: 0 <= noise <= 1.
215 279
 */
216 280
  
217
  public void setNoise(float noise)
281
  public synchronized void setNoise(float noise)
218 282
    {
219
    if( mNoise==0.0f && noise != 0.0f )  
220
      createNoise();
283
    if( mNoise==0.0f && noise != 0.0f && vn==null )
284
      {
285
      vn = new Vector<>();
286
      for(int i=0; i<numPoints; i++) vn.add(new VectorNoise(mDimension));
287

  
288
      if( mDimension>=2 )
289
        {
290
        mFactor = new float[mDimension-1];
291
        }
292
      }
221 293
   
222 294
    if( mNoise<0.0f ) mNoise = 0.0f;
223 295
    if( mNoise>1.0f ) mNoise = 1.0f;

Also available in: Unified diff