Project

General

Profile

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

library / src / main / java / org / distorted / library / type / DynamicQuat.java @ 20dc919b

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
    mDimension = 4;
137
    }
138

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

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

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

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

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

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

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

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

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

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

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