Project

General

Profile

Download (13.5 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / type / DynamicQuat.java @ 649544b8

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.type;
21

    
22
import java.util.Vector;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/** 
26
* A 4-dimensional implementation of the Dynamic class to interpolate between a list
27
* of Static4Ds.
28
* Here, the Points are assumed to be Quaternions - thus we do the Spherical Linear Interpolation, aka
29
* SLERP. Noise not supported (yet?).
30
*/
31

    
32
public class DynamicQuat extends Dynamic implements Data4D
33
  {
34
 
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
// Here we implement our own Cache as we need something slightly different.
37
// omega, sinOmega, cosOmega - angle between pair of quaternions, its sinus and cosinus.
38
//  
39
// (vx,vy,vz,vw) is the original vector from vv (copied here so when interpolating we can see if it is 
40
// still valid and if not - rebuild the Cache
41
  
42
  private class VectorCacheQuat
43
    {
44
    float omega, sinOmega,cosOmega;
45
    float vx,vy,vz,vw;
46
    }
47
  
48
  private Vector<VectorCacheQuat> vc;
49
  private VectorCacheQuat tmp1, tmp2;
50

    
51
  private Vector<Static4D> vv;
52
  private Static4D curr, next;
53
 
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
//Abramowitz / Stegun
56

    
57
  private static float arcCos(float x)
58
    {
59
    if( x<0 )
60
      return 3.14159265358979f - (float)Math.sqrt(1+x)*(1.5707288f + 0.2121144f*x + 0.074261f*x*x + 0.0187293f*x*x*x);
61
     
62
    return (float)Math.sqrt(1-x)*(1.5707288f - 0.2121144f*x + 0.074261f*x*x - 0.0187293f*x*x*x);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
  
67
  private void recomputeCache()
68
    {  
69
    if( numPoints>=2 )
70
      {
71
      int i, n;  
72
     
73
      for(i=0; i<numPoints; i++)
74
        {
75
        n = i<numPoints-1 ? i+1:0;  
76
      
77
        tmp1= vc.elementAt(i);
78
        tmp2= vc.elementAt(n);
79
        curr= vv.elementAt(i);
80
        next= vv.elementAt(n);
81
      
82
        tmp1.vx = curr.x;
83
        tmp1.vy = curr.y;
84
        tmp1.vz = curr.z;
85
        tmp1.vw = curr.w;
86
    	
87
        tmp1.cosOmega = curr.x*next.x + curr.y*next.y + curr.z*next.z + curr.w*next.w;
88
      	
89
        if( tmp1.cosOmega<0 && n!=0 )  // do not invert the last quaternion even if we'd have to go the long way around!
90
          {
91
          tmp1.cosOmega = -tmp1.cosOmega;
92
          next.x = -next.x;
93
          next.y = -next.y;
94
          next.z = -next.z;
95
          next.w = -next.w;
96
          }
97
      	
98
        tmp1.sinOmega = (float)Math.sqrt(1-tmp1.cosOmega*tmp1.cosOmega);
99
        tmp1.omega = arcCos(tmp1.cosOmega);
100
        }
101
      }
102
   
103
    cacheDirty = false;
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
// PUBLIC API
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
/**
110
 * Default constructor.
111
 */
112
  public DynamicQuat()
113
    {
114
    this(0,0.5f);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
/**
120
 * Default constructor.
121
 *
122
 * @param duration number of milliseconds it takes to do a full loop/path from first vector to the
123
 *                 last and back to the first
124
 * @param count    number of loops/paths we will do; mCount = 1.5 means we go from the first vector
125
 *                 to the last, back to first, and to the last again.
126
 */
127
  public DynamicQuat(int duration, float count)
128
    {
129
    vv = new Vector<>();
130
    vc = new Vector<>();
131
    numPoints = 0;
132
    cacheDirty = false;
133
    mMode = MODE_LOOP;
134
    mDuration = duration;
135
    mCount = count;
136
    mNoise = 0.0f;
137
    mDimension = 4;
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
/**
142
 * Returns the location'th Static4D.
143
 *   
144
 * @param location the index of the Point we are interested in.
145
 * @return The Static4D, if 0<=location&lt;getNumPoints(), or null otherwise.
146
 */  
147
  public synchronized Static4D getPoint(int location)
148
    {
149
    return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;  
150
    }
151
  
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
/**
154
 * Resets the location'th Point.
155
 * 
156
 * @param location the index of the Point we are setting.
157
 * @param x New value of its first float.
158
 */
159
  public synchronized void setPoint(int location, float x, float y, float z, float w)
160
    {
161
    if( location>=0 && location<numPoints )
162
      {
163
      curr = vv.elementAt(location);
164
   
165
      if( curr!=null )
166
        {
167
        curr.set(x,y,z,w);
168
        cacheDirty=true;
169
        }
170
      }
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
/**
175
 * Adds a new Static4D to the end of our list of Points to interpolate through.
176
 * <p>   
177
 * Only a reference to the Point gets added to the List; this means that one can add a Point 
178
 * here, and later on {@link Static4D#set(float,float,float,float)} it to some new value and
179
 * the change will be seamlessly reflected in the interpolated path.  
180
 * <p>
181
 * A Point can be added multiple times.
182
 *   
183
 * @param v The Point to add.
184
 */    
185
  public synchronized void add(Static4D v)
186
    {
187
    if( v!=null )
188
      {
189
      vv.add(v);
190
      
191
      switch(numPoints)
192
         {
193
         case 0: 
194
         case 1: vc.add(new VectorCacheQuat());
195
                 vc.add(new VectorCacheQuat());
196
        	     break;
197
         default:vc.add(new VectorCacheQuat());
198
         }
199

    
200
       numPoints++;
201
       cacheDirty = true;
202
       }
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
/**
207
 * Adds a new Static4D to the location'th place in our List of Points to interpolate through.
208
 *   
209
 * @param location Index in our List to add the new Point at.
210
 * @param v The Static4D to add.
211
 */  
212
  public synchronized void add(int location, Static4D v)
213
    {
214
    if( v!=null )
215
      {
216
      vv.add(location, v);
217
      
218
      switch(numPoints)
219
        {
220
        case 0: 
221
        case 1: vc.add(new VectorCacheQuat());
222
                vc.add(new VectorCacheQuat());
223
                break;
224
        default:vc.add(location,new VectorCacheQuat());
225
        }
226

    
227
      numPoints++;
228
      cacheDirty = true;
229
      }
230
    }
231
  
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
/**
234
 * Removes all occurrences of Point v from the List of Points to interpolate through.  
235
 * 
236
 * @param v The Point to remove.
237
 * @return <code>true</code> if we have removed at least one Point.
238
 */
239
  public synchronized boolean remove(Static4D v)
240
    {
241
    int n = vv.indexOf(v);
242
    boolean found = false;
243
   
244
    while( n>=0 ) 
245
      {
246
      vv.remove(n);
247
     
248
      switch(numPoints)
249
        {
250
        case 0:
251
        case 1: break;
252
        case 2: vc.removeAllElements();
253
                break;
254
        default:vc.remove(n);
255
        }
256

    
257
      numPoints--;
258
      found = true;
259
      n = vv.indexOf(v);
260
      }
261
   
262
    if( found ) 
263
      {
264
      cacheDirty=true;
265
      }
266
   
267
    return found;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
/**
272
 * Removes a location'th Point from the List of Points we interpolate through.
273
 * 
274
 * @param location index of the Point we want to remove. 
275
 * @return <code>true</code> if location is valid, i.e. if 0<=location&lt;getNumPoints().
276
 */
277
  public synchronized boolean remove(int location)
278
    {
279
    if( location>=0 && location<numPoints ) 
280
      {
281
      vv.removeElementAt(location);
282
      
283
      switch(numPoints)
284
        {
285
        case 0: 
286
        case 1: break;
287
        case 2: vc.removeAllElements();
288
                break;
289
        default:vc.removeElementAt(location);
290
        }
291

    
292
      numPoints--;
293
      cacheDirty = true; 
294
      return true;
295
      }
296

    
297
    return false;
298
    }
299
  
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301
/**
302
 * Removes all Points.
303
 */
304
  public synchronized void removeAll()
305
    {
306
    numPoints = 0;
307
    vv.removeAllElements();
308
    vc.removeAllElements();
309
    cacheDirty = false;
310
    }
311
  
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
/**
314
 * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
315
 * Interpolation is done using the spherical linear algorithm, aka SLERP.
316
 * <p>
317
 * Since this is a 4-dimensional Dynamic, the resulting interpolated Static4D gets written
318
 * to four locations in the buffer: buffer[offset], buffer[offset+1], buffer[offset+2] and buffer[offset+3]. 
319
 * 
320
 * @param buffer Float buffer we will write the resulting Static4D to.
321
 * @param offset Offset in the buffer where to write the result.
322
 * @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
323
 *             time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
324
 */    
325
  public synchronized void interpolate(float[] buffer, int offset, float time)
326
    {  
327
    switch(numPoints)
328
      {
329
      case 0: buffer[offset  ] = 0.0f;
330
              buffer[offset+1] = 0.0f;
331
              buffer[offset+2] = 0.0f;
332
              buffer[offset+3] = 0.0f;
333
              break;
334
      case 1: curr = vv.elementAt(0);
335
              buffer[offset  ] = curr.x; 
336
              buffer[offset+1] = curr.y;
337
              buffer[offset+2] = curr.z;
338
              buffer[offset+3] = curr.w;
339
              break;
340
      default:float t = time;
341
              float scale0, scale1;
342
  
343
              if( mMode==MODE_JUMP ) time = time*(numPoints-1);
344
              else if( mMode==MODE_PATH || numPoints==2 ) time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
345
              else time = time*numPoints;
346
              
347
              int vecNext, vecCurr = (int)time;
348
              time = time-vecCurr;
349
      
350
              if( vecCurr>=0 && vecCurr<numPoints )
351
                {
352
                if( cacheDirty ) recomputeCache();    // recompute cache if we have added or remove vectors since last computation
353
                   
354
                switch(mMode)
355
                  {
356
                  case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1; 
357
                                  break;
358
                  case MODE_PATH: if( t<0.5f ) vecNext = vecCurr+1;  
359
                                  else         vecNext = vecCurr==0 ? 1 : vecCurr-1;  
360
                                  break;
361
                  case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
362
                                  break;
363
                  default       : vecNext = 0;                
364
                  }
365
     
366
                curr = vv.elementAt(vecCurr);
367
                next = vv.elementAt(vecNext);
368
                tmp1 = vc.elementAt(vecCurr);
369
                tmp2 = vc.elementAt(vecNext);
370
              
371
                if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z || tmp2.vw!=next.w ) recomputeCache();
372
               
373
                if( tmp1.sinOmega==0 )
374
                  {
375
                  scale0 = 0f;
376
                  scale1 = 1f;
377
                  }
378
                else if( tmp1.cosOmega < 0.99 ) 
379
                  {
380
                  scale0 = (float)Math.sin( (1f-time)*tmp1.omega ) / tmp1.sinOmega;
381
                  scale1 = (float)Math.sin(     time *tmp1.omega ) / tmp1.sinOmega;
382
                  }
383
                else 
384
                  {
385
                  scale0 = 1f-time;
386
                  scale1 = time;
387
                  }
388

    
389
                buffer[offset  ] = scale0*curr.x + scale1*next.x;
390
                buffer[offset+1] = scale0*curr.y + scale1*next.y; 
391
                buffer[offset+2] = scale0*curr.z + scale1*next.z; 
392
                buffer[offset+3] = scale0*curr.w + scale1*next.w; 
393
                
394
                break;
395
                }
396
      }
397
    }  
398

    
399
  }
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
//
(12-12/17)