Project

General

Profile

« Previous | Next » 

Revision a4835695

Added by Leszek Koltunski over 8 years ago

move data types, like FlatND and InterpolatorND, to a separate package.

View differences:

src/main/java/org/distorted/library/DistortedObject.java
23 23
import android.opengl.GLES20;
24 24
import android.opengl.GLUtils;
25 25

  
26
import org.distorted.library.type.Float1D;
27
import org.distorted.library.type.Float2D;
28
import org.distorted.library.type.Float3D;
29
import org.distorted.library.type.Float4D;
30
import org.distorted.library.type.Interpolator;
31
import org.distorted.library.type.Interpolator1D;
32
import org.distorted.library.type.Interpolator2D;
33
import org.distorted.library.type.Interpolator3D;
34
import org.distorted.library.type.Interpolator4D;
35
import org.distorted.library.type.InterpolatorQuat;
36

  
26 37
///////////////////////////////////////////////////////////////////////////////////////////////////
27 38
/**
28 39
 * All Objects to which Distorted Graphics effects can be applied need to be extended from here.
......
658 669
    Interpolator1D di = new Interpolator1D();  
659 670
    di.setCount(0.5f);
660 671
    di.setDuration(duration);
661
    di.add(new Float1D(    0));                             
672
    di.add(new Float1D(    0));
662 673
    di.add(new Float1D(angle));                        
663 674

  
664 675
    return mM.add(EffectNames.ROTATE, center, di, 0.0f,0.0f,1.0f);
src/main/java/org/distorted/library/EffectMessage.java
20 20
package org.distorted.library;
21 21

  
22 22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/** 
23

  
24
import org.distorted.library.type.Interpolator;
25

  
26
/**
24 27
* Defines all possible events a class implementing the {@link EffectListener} interface can receive.
25 28
*/
26 29

  
src/main/java/org/distorted/library/EffectQueue.java
19 19

  
20 20
package org.distorted.library;
21 21

  
22
import org.distorted.library.type.Interpolator;
23
import org.distorted.library.type.Interpolator2D;
24

  
22 25
import java.util.Vector;
23 26

  
24 27
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectQueueFragment.java
21 21

  
22 22
import android.opengl.GLES20;
23 23

  
24
import org.distorted.library.type.Float2D;
25
import org.distorted.library.type.Float3D;
26
import org.distorted.library.type.Float4D;
27
import org.distorted.library.type.Interpolator;
28
import org.distorted.library.type.Interpolator1D;
29
import org.distorted.library.type.Interpolator2D;
30

  
24 31
///////////////////////////////////////////////////////////////////////////////////////////////////
25 32

  
26 33
class EffectQueueFragment extends EffectQueue
......
168 175
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
169 176
      mInterI[mNumEffects] = inter;
170 177
      mInterP[mNumEffects] = point;
171
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
172
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
173
   
178

  
179
      if( region==null )
180
        {
181
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
182
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
183
        }
184
      else
185
        {
186
        float z = region.getZ();
187
        float w = region.getW();
188

  
189
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
190
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
191
        }
192

  
174 193
      return addBase(eln); 
175 194
      }
176 195
      
......
186 205
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
187 206
      mInterI[mNumEffects] = inter;
188 207
      mInterP[mNumEffects] = null;
189
      mBuf[4*mNumEffects  ] = point.x;
190
      mBuf[4*mNumEffects+1] = point.y;
191
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
192
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
193
   
208
      mBuf[4*mNumEffects  ] = point.getX();
209
      mBuf[4*mNumEffects+1] = point.getY();
210

  
211
      if( region==null )
212
        {
213
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
214
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
215
        }
216
      else
217
        {
218
        float z = region.getZ();
219
        float w = region.getW();
220

  
221
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
222
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
223
        }
224

  
194 225
      return addBase(eln);
195 226
      }
196 227
      
......
205 236
      {
206 237
      mInterI[mNumEffects] = inter;
207 238
      mInterP[mNumEffects] = point;
208
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
209
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
210
   
211
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
212
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
213
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
239

  
240
      if( region==null )
241
        {
242
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
243
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
244
        }
245
      else
246
        {
247
        float z = region.getZ();
248
        float w = region.getW();
249

  
250
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
251
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
252
        }
253

  
254
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
255
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
256
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
214 257
     
215 258
      return addBase(eln); 
216 259
      }
......
226 269
      {
227 270
      mInterI[mNumEffects] = inter;
228 271
      mInterP[mNumEffects] = null;
229
      mBuf[4*mNumEffects  ] = point.x;
230
      mBuf[4*mNumEffects+1] = point.y;
231
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
232
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
233
      
234
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
235
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
236
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
272
      mBuf[4*mNumEffects  ] = point.getX();
273
      mBuf[4*mNumEffects+1] = point.getY();
274

  
275
      if( region==null )
276
        {
277
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
278
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
279
        }
280
      else
281
        {
282
        float z = region.getZ();
283
        float w = region.getW();
284

  
285
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
286
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
287
        }
288

  
289
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
290
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
291
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
237 292
   
238 293
      return addBase(eln);
239 294
      }
......
249 304
      {
250 305
      mInterI[mNumEffects] = null;
251 306
      mInterP[mNumEffects] = point;
252
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
253
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
254
   
307

  
308
      if( region==null )
309
        {
310
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
311
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
312
        }
313
      else
314
        {
315
        float z = region.getZ();
316
        float w = region.getW();
317

  
318
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
319
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
320
        }
321

  
255 322
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
256
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
257
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
258
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
323
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
324
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
325
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
259 326
     
260 327
      return addBase(eln); 
261 328
      }
......
271 338
      {
272 339
      mInterI[mNumEffects] = null;
273 340
      mInterP[mNumEffects] = null;
274
      mBuf[4*mNumEffects  ] = point.x;
275
      mBuf[4*mNumEffects+1] = point.y;
276
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
277
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
278
      
341
      mBuf[4*mNumEffects  ] = point.getX();
342
      mBuf[4*mNumEffects+1] = point.getY();
343

  
344
      if( region==null )
345
        {
346
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
347
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
348
        }
349
      else
350
        {
351
        float z = region.getZ();
352
        float w = region.getW();
353

  
354
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
355
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
356
        }
357

  
279 358
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
280
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
281
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
282
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
359
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
360
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
361
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
283 362
   
284 363
      return addBase(eln);
285 364
      }
src/main/java/org/distorted/library/EffectQueueMatrix.java
22 22
import android.opengl.GLES20;
23 23
import android.opengl.Matrix;
24 24

  
25
import org.distorted.library.type.Float3D;
26
import org.distorted.library.type.Interpolator;
27
import org.distorted.library.type.Interpolator1D;
28
import org.distorted.library.type.Interpolator3D;
29

  
25 30
///////////////////////////////////////////////////////////////////////////////////////////////////
26 31

  
27 32
class EffectQueueMatrix extends EffectQueue
......
276 281
      mInterP[mNumEffects] = null;
277 282
      mInterI[mNumEffects] = i;
278 283
      
279
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = p.x;
280
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = p.y;
281
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = p.z;
284
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = p.getX();
285
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = p.getY();
286
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = p.getZ();
282 287
            
283 288
      return addBase(eln);
284 289
      }
......
295 300
      mInterP[mNumEffects] = null;
296 301
      mInterI[mNumEffects] = i;
297 302
      
298
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = p.x;
299
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = p.y;
300
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = p.z;
303
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = p.getX();
304
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = p.getY();
305
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = p.getZ();
301 306
      
302 307
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = aX;
303 308
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = aY;  
......
337 342
      mInterP[mNumEffects] = null; 
338 343
      mInterI[mNumEffects] = null;
339 344
      
340
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = p.x;
341
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = p.y;
342
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = p.z;
345
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = p.getX();
346
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = p.getY();
347
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = p.getZ();
343 348
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = aA;  
344 349
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = aX;
345 350
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = aY;  
src/main/java/org/distorted/library/EffectQueueVertex.java
21 21

  
22 22
import android.opengl.GLES20;
23 23

  
24
import org.distorted.library.type.Float2D;
25
import org.distorted.library.type.Float4D;
26
import org.distorted.library.type.Interpolator;
27
import org.distorted.library.type.Interpolator2D;
28

  
24 29
///////////////////////////////////////////////////////////////////////////////////////////////////
25 30

  
26 31
class EffectQueueVertex extends EffectQueue
......
173 178
      
174 179
      mInterI[mNumEffects] = inter;
175 180
      mInterP[mNumEffects] = null;
176
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.x-mObjHalfX;
177
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.y+mObjHalfY;
181
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.getX()-mObjHalfX;
182
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.getY()+mObjHalfY;
178 183
     
179 184
      return addPriv(eln,region);
180 185
      }
......
195 200
     
196 201
      mInterI[mNumEffects] = null;
197 202
      mInterP[mNumEffects] = null;
198
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.x-mObjHalfX;
199
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.y+mObjHalfY;
203
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.getX()-mObjHalfX;
204
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.getY()+mObjHalfY;
200 205
      
201 206
      return addPriv(eln,region);    
202 207
      }
......
210 215
    {    
211 216
    if( region!=null )
212 217
      {
213
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = region.x;
214
      mUniforms[NUM_UNIFORMS*mNumEffects+4] =-region.y;   // invert y already
215
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = region.z<=0.0f ? 1000*mObjHalfX : region.z;
216
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = region.w;
218
      float z = region.getZ();
219

  
220
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = region.getX();
221
      mUniforms[NUM_UNIFORMS*mNumEffects+4] =-region.getY();   // invert y already
222
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = z<=0.0f ? 1000*mObjHalfX : z;
223
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = region.getW();
217 224
      }
218 225
    else
219 226
      {
src/main/java/org/distorted/library/Float1D.java
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;
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * A 1-dimensional data structure containing a single float. The float has no particular meaning; 
25
 * when this data structure is used in Interpolators, we can think of it as a 1-dimensional Point 
26
 * a few of which the Interpolator interpolates between.
27
 */
28

  
29
public class Float1D 
30
  {
31
  float x;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Constructor that initialises the value of the single float to ox.   
36
 *   
37
 * @param ox value of the single float.
38
 */
39
  public Float1D(int ox)
40
    {
41
    x = ox;
42
    }
43

  
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
/**
46
 * Constructor that initialises the value of the single float to ox.   
47
 *   
48
 * @param ox value of the single float.
49
 */  
50
  public Float1D(float ox)
51
    {
52
    x = ox;
53
    }
54
  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
/**
57
 * Resets the value of the single float.
58
 * 
59
 * @param ox new value of the single float.
60
 */
61
  public void set(int ox)
62
    {
63
    x = ox;
64
    }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
/**
68
 * Resets the value of the single float.
69
 * 
70
 * @param ox new value of the single float.
71
 */
72
  public void set(float ox)
73
    {
74
    x = ox;
75
    }
76
  
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
/**
79
 * Return the value of the float contained.
80
 * 
81
 * @return The single float.
82
 */
83
  public float getX()
84
    {
85
    return x;  
86
    }
87
  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// end of class   
90
  }
src/main/java/org/distorted/library/Float2D.java
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;
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * A 2-dimensional data structure containing two floats. The floats have no particular meaning; 
25
 * when this data structure is used in Interpolators, we can think of it as a 2-dimensional Point 
26
 * a few of which the Interpolator interpolates between.
27
 */
28

  
29
public class Float2D extends Float1D
30
  {
31
  float y;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Constructor that initialises the value of the two floats to (ox,oy).   
36
 *   
37
 * @param ox value of the first float.
38
 * @param oy value of the second float.
39
 */  
40
  public Float2D(int ox, int oy)
41
    {
42
    super(ox);
43
    y = oy;
44
    }
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
/**
48
 * Constructor that initialises the value of the two floats to (ox,oy).   
49
 *   
50
 * @param ox value of the first float.
51
 * @param oy value of the second float.
52
 */    
53
  public Float2D(float ox, float oy)
54
    {
55
    super(ox);
56
    y = oy;
57
    }
58
  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
/**
61
 * Reset the value of the floats to (ox,oy).
62
 * 
63
 * @param ox new value of the first float
64
 * @param oy new value of the second float
65
 */
66
  public void set(int ox, int oy)
67
    {
68
    x = ox;
69
    y = oy;
70
    }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
/**
74
 * Reset the value of the floats to (ox,oy).
75
 * 
76
 * @param ox new value of the first float
77
 * @param oy new value of the seond float
78
 */
79
  public void set(float ox, float oy)
80
    {
81
    x = ox;
82
    y = oy;
83
    }
84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
/**
87
 * Return the value of the second float contained.
88
 * 
89
 * @return The second float.
90
 */
91
  public float getY()
92
    {
93
    return y;  
94
    }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// end of class   
98
  }
src/main/java/org/distorted/library/Float3D.java
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;
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * A 3-dimensional data structure containing three floats. The floats have no particular meaning; 
25
 * when this data structure is used in Interpolators, we can think of it as a 3-dimensional Point 
26
 * a few of which the Interpolator interpolates between.
27
 */
28

  
29
public class Float3D extends Float2D 
30
  {
31
  float z;
32
  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Constructor that initialises the value of the three floats to (vx,vy,vz).   
36
 *   
37
 * @param vx value of the first float.
38
 * @param vy value of the second float.
39
 * @param vz value of the third float.
40
 */ 
41
  public Float3D(int vx, int vy, int vz)
42
    {
43
    super(vx,vy);
44
    z = vz;
45
    }
46

  
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
/**
49
 * Constructor that initialises the value of the three floats to (vx,vy,vz).   
50
 *   
51
 * @param vx value of the first float.
52
 * @param vy value of the second float.
53
 * @param vz value of the third float.
54
 */ 
55
  public Float3D(float vx, float vy, float vz)
56
    {
57
    super(vx,vy);
58
    z = vz;
59
    }
60

  
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
/**
63
 * Reset the value of the floats to (vx,vy,vz).
64
 * 
65
 * @param vx new value of the first float
66
 * @param vy new value of the second float
67
 * @param vz new value of the third float
68
 */
69
  public void set(int vx, int vy, int vz)
70
    {
71
    x = vx;
72
    y = vy;
73
    z = vz;
74
    }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
/**
78
 * Reset the value of the floats to (vx,vy,vz).
79
 * 
80
 * @param vx new value of the first float
81
 * @param vy new value of the second float
82
 * @param vz new value of the third float
83
 */
84
  public void set(float vx, float vy, float vz)
85
    {
86
    x = vx;
87
    y = vy;
88
    z = vz;
89
    }
90
  
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
/**
93
 * Return the value of the third float contained.
94
 * 
95
 * @return The third float.
96
 */
97
  public float getZ()
98
    {
99
    return z;  
100
    }
101
  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// end of class   
104
  }
src/main/java/org/distorted/library/Float4D.java
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;
21

  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * A 4-dimensional data structure containing four floats. The floats have no particular meaning; 
25
 * when this data structure is used in Interpolators, we can think of it as a 4-dimensional Point 
26
 * a few of which the Interpolator interpolates between.
27
 */
28

  
29
public class Float4D extends Float3D 
30
  {
31
  float w;
32
  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Constructor that initialises the value of the four floats to (vx,vy,vz,vw).   
36
 *   
37
 * @param vx value of the first float.
38
 * @param vy value of the second float.
39
 * @param vz value of the third float.
40
 * @param vw value of the fourth float.
41
 */ 
42
  public Float4D(int vx, int vy, int vz, int vw)
43
    {
44
    super(vx,vy,vz);
45
    w = vw;
46
    }
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
/**
50
 * Constructor that initialises the value of the four floats to (vx,vy,vz,vw).   
51
 *   
52
 * @param vx value of the first float.
53
 * @param vy value of the second float.
54
 * @param vz value of the third float.
55
 * @param vw value of the fourth float.
56
 */ 
57
  public Float4D(float vx, float vy, float vz, float vw)
58
    {
59
    super(vx,vy,vz);
60
    w = vw;
61
    }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
/**
65
 * Reset the value of the floats to (vx,vy,vz,vw).
66
 * 
67
 * @param vx new value of the first float
68
 * @param vy new value of the second float
69
 * @param vz new value of the third float
70
 * @param vw new value of the fourth float
71
 */
72
  public void set(int vx, int vy, int vz, int vw)
73
    {
74
    x = vx;
75
    y = vy;
76
    z = vz;
77
    w = vw;
78
    }
79

  
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
/**
82
 * Reset the value of the floats to (vx,vy,vz,vw).
83
 * 
84
 * @param vx new value of the first float
85
 * @param vy new value of the second float
86
 * @param vz new value of the third float
87
 * @param vw new value of the fourth float
88
 */
89
  public void set(float vx, float vy, float vz, float vw)
90
    {
91
    x = vx;
92
    y = vy;
93
    z = vz;
94
    w = vw;
95
    }
96

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
/**
99
 * Return the value of the fourth float contained.
100
 * 
101
 * @return The fourth float.
102
 */
103
  public float getW()
104
    {
105
    return w;  
106
    }
107
  
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
// end of class   
110
  }
src/main/java/org/distorted/library/Interpolator.java
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;
21

  
22
import java.util.Random;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/** A class to interpolate between a List of Float{1,2,3,4}Ds.
26
* <p><ul>
27
* <li>if there is only one Point, just jump to it.
28
* <li>if there are two Points, linearly bounce between them
29
* <li>if there are more, interpolate a loop (or a path!) between them.
30
* </ul>
31
*/
32

  
33
// The way Interpolation between more than 2 Points is done:
34
// 
35
// Def: let w[i] = (w[i](x), w[i](y), w[i](z)) be the direction and speed we have to be flying at Point P[i]
36
//
37
// time it takes to fly though one segment v[i] --> v[i+1] : 0.0 --> 1.0
38
// w[i] should be parallel to v[i+1] - v[i-1]   (cyclic notation)
39
// |w[i]| proportional to | P[i]-P[i+1] |
40
//
41
// Given that the flight route (X(t), Y(t), Z(t)) from P(i) to P(i+1)  (0<=t<=1) has to satisfy
42
// X(0) = P[i  ](x), Y(0)=P[i  ](y), Z(0)=P[i  ](z), X'(0) = w[i  ](x), Y'(0) = w[i  ](y), Z'(0) = w[i  ](z)
43
// X(1) = P[i+1](x), Y(1)=P[i+1](y), Z(1)=P[i+1](z), X'(1) = w[i+1](x), Y'(1) = w[i+1](y), Z'(1) = w[i+1](z)
44
//
45
// we have the solution:  X(t) = at^3 + bt^2 + ct + d where
46
// a =  2*P[i](x) +   w[i](x) - 2*P[i+1](x) + w[i+1](x)
47
// b = -3*P[i](x) - 2*w[i](x) + 3*P[i+1](x) - w[i+1](x)
48
// c = w[i](x)<br>
49
// d = P[i](x)
50
//
51
// and similarly Y(t) and Z(t).
52

  
53
public abstract class Interpolator 
54
  {
55
  /**
56
   * One revolution takes us from the first vector to the last and back to first through the shortest path. 
57
   */
58
  public static final int MODE_LOOP = 0; 
59
  /**
60
   * We come back from the last to the first vector through the same way we got there.
61
   */
62
  public static final int MODE_PATH = 1; 
63
  /**
64
   * We just jump back from the last point to the first.
65
   */
66
  public static final int MODE_JUMP = 2; 
67
 
68
  protected static Random mRnd = new Random();
69
  
70
  protected static final int NUM_NOISE = 5; // used iff mNoise>0.0. Number of intermediary points between each pair of adjacent vectors
71
                                            // where we randomize noise factors to make the way between the two vectors not so smooth.
72
  protected int numPoints;
73
  protected int mVecCurr;    
74
  protected boolean cacheDirty; // VectorCache not up to date
75
  protected int mMode;          // LOOP, PATH or JUMP
76
  protected long mDuration;     // number of miliseconds it takes to do a full loop/path from first vector to the last and back to the first 
77
  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
  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
  
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// hide this from Javadoc
82
  
83
  Interpolator()
84
    {
85
    }
86
  
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
  
89
  public void interpolateMain(float[] buffer, int offset, long currentDuration)
90
    {
91
    if( mDuration<=0.0f ) 
92
      {
93
      interpolate(buffer,offset,mCount-(int)mCount);  
94
      }
95
    else
96
      {
97
      float x = (float)currentDuration/mDuration;
98
           
99
      if( x<=mCount || mCount<=0.0f )
100
        {
101
        interpolate(buffer,offset,x-(int)x);
102
        }
103
      }
104
    }
105
  
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

  
108
  public boolean interpolateMain(float[] buffer, int offset, long currentDuration, long step)
109
    {
110
    if( mDuration<=0.0f ) 
111
      {
112
      interpolate(buffer,offset,mCount-(int)mCount);
113
      return false;
114
      }
115
     
116
    float x = (float)currentDuration/mDuration;
117
           
118
    if( x<=mCount || mCount<=0.0f )
119
      {
120
      interpolate(buffer,offset,x-(int)x);
121
        
122
      if( currentDuration+step > mDuration*mCount && mCount>0.0f )
123
        {
124
        interpolate(buffer,offset,mCount-(int)mCount);
125
        return true;
126
        }
127
      }
128
    
129
    return false;
130
    }
131
 
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// internal debugging only!
134
  
135
  String print()
136
    {
137
    return "duration="+mDuration+" count="+mCount+" Noise="+mNoise+" numVectors="+numPoints+" mMode="+mMode;
138
    }
139
  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
  
142
  abstract void interpolate(float[] buffer, int offset, float time);
143
  abstract void createNoise();
144

  
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// PUBLIC API
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
/**
149
 * Sets the mode of the interpolation to Loop, Path or Jump.
150
 * <ul>
151
 * <li>Loop is when we go from the first point all the way to the last, and the back to the first through 
152
 * the shortest way.
153
 * <li>Path is when we come back from the last point back to the first the same way we got there.
154
 * <li>Jump is when we go from first to last and then jump back to the first.
155
 * </ul>
156
 * 
157
 * @param mode {@link Interpolator#MODE_LOOP}, {@link Interpolator#MODE_PATH} or {@link Interpolator#MODE_JUMP}.
158
 */
159

  
160
  public void setMode(int mode)
161
    {
162
    mMode = mode;  
163
    }
164

  
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
/**
167
 * Returns the number of Float{1,2,3,4}Ds this Interpolator has been fed with.
168
 *   
169
 * @return the number of Float{1,2,3,4}Ds we are currently interpolating through.
170
 */
171
  public synchronized int getNumPoints()
172
    {
173
    return numPoints;  
174
    }
175

  
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
/**
178
 * Controls how many times we want to interpolate.
179
 * <p>
180
 * Count equal to 1 means 'go from the first Float{1,2,3,4}D to the last and back'. Does not have to be an
181
 * integer - i.e. count=1.5 would mean 'start at the first Point, go to the last, come back to the first, 
182
 * go to the last again and stop'.
183
 * Count<=0 means 'go on interpolating indefinitely'.
184
 * 
185
 * @param count the number of times we want to interpolate between our collection of Float{1,2,3,4}Ds.
186
 */
187
  public void setCount(float count)
188
    {
189
    mCount = count;  
190
    }
191

  
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
/**
194
 * Sets the time it takes to do one full interpolation.
195
 * 
196
 * @param duration Time, in milliseconds, it takes to do one full interpolation, i.e. go from the first 
197
 *                 Point to the last and back. 
198
 */
199
  
200
  public void setDuration(long duration)
201
    {
202
    mDuration = duration;
203
    }
204

  
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
/**
207
 * Sets the 'smoothness' of interpolation. 
208
 * <p>
209
 * When Noise=0 (the default), we interpolate between our Points through the most smooth path possible. 
210
 * Increasing noise makes the Interpolator increasingly deviate from this path, pseudo-randomly speeding 
211
 * up and slowing down, etc.
212
 * 
213
 * @param noise The noise level. Permitted range: 0 <= noise <= 1.
214
 */
215
  
216
  public void setNoise(float noise)
217
    {
218
    if( mNoise==0.0f && noise != 0.0f )  
219
      createNoise();
220
   
221
    if( mNoise<0.0f ) mNoise = 0.0f;
222
    if( mNoise>1.0f ) mNoise = 1.0f;
223
   
224
    mNoise = noise;
225
    }
226

  
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
// end of DistortedInterpolator
229
  }
src/main/java/org/distorted/library/Interpolator1D.java
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;
21

  
22
import java.util.Vector;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/** 
26
* A 1-dimensional implementation of the Interpolator class to interpolate between a list 
27
* of Float1Ds.
28
*/
29

  
30
public class Interpolator1D extends Interpolator 
31
  {
32
  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// the coefficients of the X(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx  etc.
35
// (x) is the vector tangent to the path.
36
// (vx) 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
   
43
    float x;
44
    float vx;
45
    }
46
  
47
  private class VectorNoise
48
    {
49
    float[] nx;
50
   
51
    public VectorNoise()
52
      {
53
      nx = new float[NUM_NOISE]; 
54
      nx[0] = mRnd.nextFloat();
55
      for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1]+mRnd.nextFloat();
56
      float sum = nx[NUM_NOISE-1] + mRnd.nextFloat();
57
      for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum;
58
      }
59
    }
60
  
61
  private Vector<VectorCache> vc;
62
  private VectorCache tmp1, tmp2;
63
 
64
  private Vector<Float1D> vv;
65
  private Float1D prev, curr, next;
66
 
67
  private Vector<VectorNoise> vn;
68
  private VectorNoise tmpN;
69
  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

  
72
  synchronized void createNoise()
73
    {
74
    if( vn==null )
75
      {
76
      vn = new Vector<VectorNoise>();
77
      for(int i=0; i<numPoints; i++) vn.add(new VectorNoise());
78
      }
79
    }
80
  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
// no array bounds checking!
83
  
84
  private void vec(int c)
85
    {
86
    int p = c>0 ? c-1: numPoints-1;
87
    int n = c<numPoints-1 ? c+1: 0;
88
    
89
    prev = vv.elementAt(p);
90
    curr = vv.elementAt(c);
91
    next = vv.elementAt(n);
92

  
93
    tmp1 = vc.elementAt(c);
94
    
95
    float px = curr.x - prev.x;
96
    float nx = next.x - curr.x;
97
     
98
    float d = nx*nx;
99
    
100
    if( d>0 )
101
      {
102
      float q = (float)Math.sqrt((px*px)/d);
103
      
104
      if( q>1 )
105
        {
106
        tmp1.x = nx+px/q;
107
        }
108
      else
109
        {
110
        tmp1.x = px+nx*q;
111
        }
112
      }
113
    else
114
      {
115
      tmp1.x = 0.0f;
116
      }
117
    }
118
      
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
  
121
  private void recomputeCache()
122
    {  
123
    if( numPoints==1 )
124
      {
125
      tmp1= vc.elementAt(0);
126
      curr= vv.elementAt(0);
127
        
128
      tmp1.ax = 0.0f;
129
      tmp1.bx = 0.0f;
130
      tmp1.cx = curr.x;
131
      tmp1.dx = 0.0f;
132
      }
133
    else if( numPoints==2 )
134
      {
135
      tmp1= vc.elementAt(0);
136
      tmp2= vc.elementAt(1);
137
      curr= vv.elementAt(0);
138
      next= vv.elementAt(1);
139
          
140
      tmp1.ax = 0.0f;
141
      tmp1.bx = 0.0f;
142
      tmp1.cx = next.x - curr.x;
143
      tmp1.dx = curr.x;
144
      
145
      tmp2.ax = 0.0f;
146
      tmp2.bx = 0.0f;
147
      tmp2.cx = curr.x - next.x;
148
      tmp2.dx = next.x;
149
      }
150
    else
151
      {
152
      int i, n;  
153
         
154
      for(i=0; i<numPoints; i++) vec(i);
155
   
156
      for(i=0; i<numPoints; i++)
157
        {
158
        n = i<numPoints-1 ? i+1:0;  
159
      
160
        tmp1= vc.elementAt(i);
161
        tmp2= vc.elementAt(n);
162
        curr= vv.elementAt(i);
163
        next= vv.elementAt(n);
164
    
165
        tmp1.vx = curr.x;
166
        
167
        tmp1.ax =  2*curr.x +   tmp1.x - 2*next.x + tmp2.x;
168
        tmp1.bx = -3*curr.x - 2*tmp1.x + 3*next.x - tmp2.x;
169
        tmp1.cx = tmp1.x;
170
        tmp1.dx = curr.x;
171
        }
172
      }
173
   
174
    cacheDirty = false;
175
    }
176
  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

  
179
  private float noise(float time,int vecNum)
180
    {
181
    float lower, upper, len;  
182
    float d = time*(NUM_NOISE+1);
183
    int index = (int)d;
184
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
185
    tmpN = vn.elementAt(vecNum);
186
   
187
    if( index==0 )
188
      {
189
      len = 1.0f/(NUM_NOISE+1);  
190
      return (len + mNoise*(tmpN.nx[0]-len))*d;
191
      }
192
    if( index==NUM_NOISE )
193
      {
194
      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
195
      lower = len + mNoise*(tmpN.nx[NUM_NOISE-1]-len);   
196
      return (1.0f-lower)*(d-NUM_NOISE) + lower;   
197
      }
198
   
199
    len = ((float)index)/(NUM_NOISE+1);
200
    lower = len + mNoise*(tmpN.nx[index-1]-len);   
201
    len = ((float)index+1)/(NUM_NOISE+1); 
202
    upper = len + mNoise*(tmpN.nx[index  ]-len);
203
            
204
    return (upper-lower)*(d-index) + lower; 
205
    }
206
   
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// PUBLIC API
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
/**
211
 * Default constructor.
212
 */
213
  public Interpolator1D()
214
    {
215
    vv = new Vector<Float1D>();
216
    vc = new Vector<VectorCache>();
217
    vn = null;
218
    numPoints = 0;
219
    cacheDirty = false;
220
    mMode = MODE_LOOP;
221
    mDuration = 0;
222
    mCount = 0.5f;
223
    }
224
  
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
/**
227
 * Returns the location'th Float1D. 
228
 *   
229
 * @param location the index of the Point we are interested in.
230
 * @return The Float1D, if 0<=location&lt;getNumPoints(), or null otherwise. 
231
 */
232
  public synchronized Float1D getPoint(int location)
233
    {
234
    return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;  
235
    }
236
  
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
/**
239
 * Resets the location'th Point.
240
 * 
241
 * @param location the index of the Point we are setting.
242
 * @param x New value of its first float.
243
 */
244
  public synchronized void setPoint(int location, float x)
245
    {
246
    if( location>=0 && location<numPoints )
247
      {
248
      curr = vv.elementAt(location);
249
   
250
      if( curr!=null )
251
        {
252
        curr.set(x);
253
        cacheDirty=true;
254
        }
255
      }
256
    }
257
 
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
/**
260
 * Adds a new Float1D to the end of our list of Points to interpolate through.
261
 * <p>   
262
 * Only a reference to the Point gets added to the List; this means that one can add a Point 
263
 * here, and later on {@link Float1D#set(float)} it to some new value and the change will
264
 * be seamlessly reflected in the interpolated path.  
265
 * <p>
266
 * A Point can be added multiple times.
267
 *   
268
 * @param v The Point to add.
269
 */
270
  public synchronized void add(Float1D v)
271
    {
272
    if( v!=null )
273
      {
274
      vv.add(v);
275
     
276
      if( vn!=null ) vn.add(new VectorNoise());
277
       
278
      switch(numPoints)
279
        {
280
        case 0: 
281
        case 1: break;
282
        case 2: vc.add(new VectorCache());
283
                vc.add(new VectorCache());
284
                vc.add(new VectorCache());
285
                cacheDirty = true;
286
                break;
287
        default:vc.add(new VectorCache());
288
                cacheDirty = true;
289
        }
290
     
291
      numPoints++;
292
      }
293
    }
294

  
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296
/**
297
 * Adds a new Float1D to the location'th place in our List of Points to interpolate through.  
298
 *   
299
 * @param location Index in our List to add the new Point at.
300
 * @param v The Point to add.
301
 */
302
  public synchronized void add(int location, Float1D v)
303
    {
304
    if( v!=null )
305
      {
306
      vv.add(location, v);
307
      
308
      if( vn!=null ) vn.add(new VectorNoise());
309
             
310
      switch(numPoints)
311
        {
312
        case 0:
313
        case 1: break;
314
        case 2: vc.add(new VectorCache());
315
                vc.add(new VectorCache());
316
                vc.add(new VectorCache());
317
                cacheDirty = true;
318
                break;
319
        default:vc.add(location,new VectorCache());
320
                cacheDirty = true;
321
        }
322
      
323
      numPoints++;
324
      }
325
    }
326
  
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
/**
329
 * Removes all occurrences of Point v from the List of Points to interpolate through.  
330
 * 
331
 * @param v The Point to remove.
332
 * @return <code>true</code> if we have removed at least one Point.
333
 */
334
  public synchronized boolean remove(Float1D v)
335
    {
336
    int n = vv.indexOf(v);
337
    boolean found = false;
338
   
339
    while( n>=0 ) 
340
      {
341
      vv.remove(n);
342
     
343
      if( vn!=null ) vn.remove(0);
344
     
345
      switch(numPoints)
346
        {
347
        case 0:
348
        case 1:
349
        case 2: break;
350
        case 3: vc.removeAllElements();
351
                break;
352
        default:vc.remove(n);
353
                cacheDirty=true;
354
        }
355

  
356
      numPoints--;
357
      found = true;
358
      n = vv.indexOf(v);
359
      }
360
   
361
    return found;
362
    }
363

  
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365
/**
366
 * Removes a location'th Point from the List of Points we interpolate through.
367
 * 
368
 * @param location index of the Point we want to remove. 
369
 * @return <code>true</code> if location is valid, i.e. if 0<=location&lt;getNumPoints().
370
 */
371
  public synchronized boolean remove(int location)
372
    {
373
    if( location>=0 && location<numPoints ) 
374
      {
375
      vv.removeElementAt(location);
376
      
377
      if( vn!=null ) vn.remove(0);
378
     
379
      switch(numPoints)
380
        {
381
        case 0:
382
        case 1: 
383
        case 2: break;
384
        case 3: vc.removeAllElements();
385
                break;
386
        default:vc.removeElementAt(location);
387
        }
388

  
389
      numPoints--;
390
      cacheDirty = true; 
391
      return true;
392
      }
393

  
394
   return false;
395
   }
396
  
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
/**
399
 * Removes all Points.
400
 */
401
  public synchronized void removeAll()
402
    {
403
    numPoints = 0;
404
    vv.removeAllElements();
405
    vc.removeAllElements();
406
    cacheDirty = false;
407
   
408
    if( vn!=null ) vn.removeAllElements();
409
    }
410
 
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
/**
413
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
414
 * <p>
415
 * Since this is a 1-dimensional Interpolator, the resulting interpolated Float1D gets written
416
 * to a single location in the buffer: buffer[offset]. 
417
 * 
418
 * @param buffer Float buffer we will write the resulting Float1D to.
419
 * @param offset Offset in the buffer where to write the result.
420
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
421
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
422
 */
423
  public synchronized void interpolate(float[] buffer, int offset, float time)
424
    {
425
    switch(numPoints)
426
      {
427
      case 0: buffer[offset] = 0.0f;
428
              break;
429
      case 1: curr = vv.elementAt(0);
430
              buffer[offset] = curr.x;
431
              break;
432
      case 2: curr = vv.elementAt(0);
433
              next = vv.elementAt(1);
434
             
435
              if( mMode==MODE_LOOP || mMode==MODE_PATH ) time = (time>0.5f ? 2-2*time : 2*time);
436
             
437
              if( vn!=null )
438
                {
439
                time = noise(time,0);
440
                }
441
             
442
              buffer[offset] = (next.x-curr.x)*time + curr.x;
443
              break;
444
      default:float t = time;
445
            
446
              switch(mMode)
447
                {
448
                case MODE_LOOP: time = time*numPoints;
449
                                break;
450
                case MODE_PATH: time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
451
                                break;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff