1 |
d333eb6b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
a4835695
|
Leszek Koltunski
|
package org.distorted.library.type;
|
21 |
6a06a912
|
Leszek Koltunski
|
|
22 |
|
|
import java.util.Vector;
|
23 |
|
|
|
24 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25 |
|
|
/**
|
26 |
568b29d8
|
Leszek Koltunski
|
* A 4-dimensional implementation of the Dynamic class to interpolate between a list
|
27 |
|
|
* of Static4Ds.
|
28 |
6a06a912
|
Leszek Koltunski
|
* 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 |
568b29d8
|
Leszek Koltunski
|
public class DynamicQuat extends Dynamic implements Data4D
|
33 |
6a06a912
|
Leszek Koltunski
|
{
|
34 |
|
|
|
35 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36 |
649544b8
|
Leszek Koltunski
|
// Here we implement our own Cache as we need something slightly different.
|
37 |
6a06a912
|
Leszek Koltunski
|
// 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 |
649544b8
|
Leszek Koltunski
|
private class VectorCacheQuat
|
43 |
6a06a912
|
Leszek Koltunski
|
{
|
44 |
|
|
float omega, sinOmega,cosOmega;
|
45 |
|
|
float vx,vy,vz,vw;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
649544b8
|
Leszek Koltunski
|
private Vector<VectorCacheQuat> vc;
|
49 |
|
|
private VectorCacheQuat tmp1, tmp2;
|
50 |
6a06a912
|
Leszek Koltunski
|
|
51 |
568b29d8
|
Leszek Koltunski
|
private Vector<Static4D> vv;
|
52 |
|
|
private Static4D curr, next;
|
53 |
6a06a912
|
Leszek Koltunski
|
|
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 |
568b29d8
|
Leszek Koltunski
|
public DynamicQuat()
|
113 |
6a06a912
|
Leszek Koltunski
|
{
|
114 |
3002bef3
|
Leszek Koltunski
|
this(0,0.5f);
|
115 |
6a06a912
|
Leszek Koltunski
|
}
|
116 |
8c893ffc
|
Leszek Koltunski
|
|
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 |
20dc919b
|
Leszek Koltunski
|
vv = new Vector<>();
|
130 |
|
|
vc = new Vector<>();
|
131 |
|
|
numPoints = 0;
|
132 |
8c893ffc
|
Leszek Koltunski
|
cacheDirty = false;
|
133 |
20dc919b
|
Leszek Koltunski
|
mMode = MODE_LOOP;
|
134 |
|
|
mDuration = duration;
|
135 |
|
|
mCount = count;
|
136 |
bdb341bc
|
Leszek Koltunski
|
mLastPos = -1;
|
137 |
|
|
mAccessMode= ACCESS_RANDOM;
|
138 |
3002bef3
|
Leszek Koltunski
|
mDimension = 4;
|
139 |
8c893ffc
|
Leszek Koltunski
|
}
|
140 |
|
|
|
141 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142 |
|
|
/**
|
143 |
568b29d8
|
Leszek Koltunski
|
* Returns the location'th Static4D.
|
144 |
6a06a912
|
Leszek Koltunski
|
*
|
145 |
|
|
* @param location the index of the Point we are interested in.
|
146 |
568b29d8
|
Leszek Koltunski
|
* @return The Static4D, if 0<=location<getNumPoints(), or null otherwise.
|
147 |
6a06a912
|
Leszek Koltunski
|
*/
|
148 |
568b29d8
|
Leszek Koltunski
|
public synchronized Static4D getPoint(int location)
|
149 |
6a06a912
|
Leszek Koltunski
|
{
|
150 |
|
|
return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154 |
|
|
/**
|
155 |
|
|
* Resets the location'th Point.
|
156 |
|
|
*
|
157 |
|
|
* @param location the index of the Point we are setting.
|
158 |
|
|
* @param x New value of its first float.
|
159 |
|
|
*/
|
160 |
|
|
public synchronized void setPoint(int location, float x, float y, float z, float w)
|
161 |
|
|
{
|
162 |
|
|
if( location>=0 && location<numPoints )
|
163 |
|
|
{
|
164 |
|
|
curr = vv.elementAt(location);
|
165 |
|
|
|
166 |
|
|
if( curr!=null )
|
167 |
|
|
{
|
168 |
|
|
curr.set(x,y,z,w);
|
169 |
|
|
cacheDirty=true;
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175 |
|
|
/**
|
176 |
568b29d8
|
Leszek Koltunski
|
* Adds a new Static4D to the end of our list of Points to interpolate through.
|
177 |
6a06a912
|
Leszek Koltunski
|
* <p>
|
178 |
|
|
* Only a reference to the Point gets added to the List; this means that one can add a Point
|
179 |
568b29d8
|
Leszek Koltunski
|
* here, and later on {@link Static4D#set(float,float,float,float)} it to some new value and
|
180 |
6a06a912
|
Leszek Koltunski
|
* the change will be seamlessly reflected in the interpolated path.
|
181 |
|
|
* <p>
|
182 |
|
|
* A Point can be added multiple times.
|
183 |
|
|
*
|
184 |
|
|
* @param v The Point to add.
|
185 |
|
|
*/
|
186 |
568b29d8
|
Leszek Koltunski
|
public synchronized void add(Static4D v)
|
187 |
6a06a912
|
Leszek Koltunski
|
{
|
188 |
|
|
if( v!=null )
|
189 |
|
|
{
|
190 |
|
|
vv.add(v);
|
191 |
|
|
|
192 |
|
|
switch(numPoints)
|
193 |
|
|
{
|
194 |
|
|
case 0:
|
195 |
649544b8
|
Leszek Koltunski
|
case 1: vc.add(new VectorCacheQuat());
|
196 |
|
|
vc.add(new VectorCacheQuat());
|
197 |
6a06a912
|
Leszek Koltunski
|
break;
|
198 |
649544b8
|
Leszek Koltunski
|
default:vc.add(new VectorCacheQuat());
|
199 |
6a06a912
|
Leszek Koltunski
|
}
|
200 |
|
|
|
201 |
|
|
numPoints++;
|
202 |
|
|
cacheDirty = true;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207 |
|
|
/**
|
208 |
568b29d8
|
Leszek Koltunski
|
* Adds a new Static4D to the location'th place in our List of Points to interpolate through.
|
209 |
6a06a912
|
Leszek Koltunski
|
*
|
210 |
|
|
* @param location Index in our List to add the new Point at.
|
211 |
568b29d8
|
Leszek Koltunski
|
* @param v The Static4D to add.
|
212 |
6a06a912
|
Leszek Koltunski
|
*/
|
213 |
568b29d8
|
Leszek Koltunski
|
public synchronized void add(int location, Static4D v)
|
214 |
6a06a912
|
Leszek Koltunski
|
{
|
215 |
|
|
if( v!=null )
|
216 |
|
|
{
|
217 |
|
|
vv.add(location, v);
|
218 |
|
|
|
219 |
|
|
switch(numPoints)
|
220 |
|
|
{
|
221 |
|
|
case 0:
|
222 |
649544b8
|
Leszek Koltunski
|
case 1: vc.add(new VectorCacheQuat());
|
223 |
|
|
vc.add(new VectorCacheQuat());
|
224 |
6a06a912
|
Leszek Koltunski
|
break;
|
225 |
649544b8
|
Leszek Koltunski
|
default:vc.add(location,new VectorCacheQuat());
|
226 |
6a06a912
|
Leszek Koltunski
|
}
|
227 |
|
|
|
228 |
|
|
numPoints++;
|
229 |
|
|
cacheDirty = true;
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
234 |
|
|
/**
|
235 |
|
|
* Removes all occurrences of Point v from the List of Points to interpolate through.
|
236 |
|
|
*
|
237 |
|
|
* @param v The Point to remove.
|
238 |
|
|
* @return <code>true</code> if we have removed at least one Point.
|
239 |
|
|
*/
|
240 |
568b29d8
|
Leszek Koltunski
|
public synchronized boolean remove(Static4D v)
|
241 |
6a06a912
|
Leszek Koltunski
|
{
|
242 |
|
|
int n = vv.indexOf(v);
|
243 |
|
|
boolean found = false;
|
244 |
|
|
|
245 |
|
|
while( n>=0 )
|
246 |
|
|
{
|
247 |
|
|
vv.remove(n);
|
248 |
|
|
|
249 |
|
|
switch(numPoints)
|
250 |
|
|
{
|
251 |
|
|
case 0:
|
252 |
|
|
case 1: break;
|
253 |
|
|
case 2: vc.removeAllElements();
|
254 |
|
|
break;
|
255 |
|
|
default:vc.remove(n);
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
numPoints--;
|
259 |
|
|
found = true;
|
260 |
|
|
n = vv.indexOf(v);
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
if( found )
|
264 |
|
|
{
|
265 |
|
|
cacheDirty=true;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
return found;
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272 |
|
|
/**
|
273 |
|
|
* Removes a location'th Point from the List of Points we interpolate through.
|
274 |
|
|
*
|
275 |
|
|
* @param location index of the Point we want to remove.
|
276 |
|
|
* @return <code>true</code> if location is valid, i.e. if 0<=location<getNumPoints().
|
277 |
|
|
*/
|
278 |
|
|
public synchronized boolean remove(int location)
|
279 |
|
|
{
|
280 |
|
|
if( location>=0 && location<numPoints )
|
281 |
|
|
{
|
282 |
|
|
vv.removeElementAt(location);
|
283 |
|
|
|
284 |
|
|
switch(numPoints)
|
285 |
|
|
{
|
286 |
|
|
case 0:
|
287 |
|
|
case 1: break;
|
288 |
|
|
case 2: vc.removeAllElements();
|
289 |
|
|
break;
|
290 |
|
|
default:vc.removeElementAt(location);
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
numPoints--;
|
294 |
|
|
cacheDirty = true;
|
295 |
|
|
return true;
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
return false;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302 |
|
|
/**
|
303 |
|
|
* Removes all Points.
|
304 |
|
|
*/
|
305 |
|
|
public synchronized void removeAll()
|
306 |
|
|
{
|
307 |
|
|
numPoints = 0;
|
308 |
|
|
vv.removeAllElements();
|
309 |
|
|
vc.removeAllElements();
|
310 |
|
|
cacheDirty = false;
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
314 |
|
|
/**
|
315 |
|
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
316 |
|
|
* Interpolation is done using the spherical linear algorithm, aka SLERP.
|
317 |
|
|
* <p>
|
318 |
568b29d8
|
Leszek Koltunski
|
* Since this is a 4-dimensional Dynamic, the resulting interpolated Static4D gets written
|
319 |
6a06a912
|
Leszek Koltunski
|
* to four locations in the buffer: buffer[offset], buffer[offset+1], buffer[offset+2] and buffer[offset+3].
|
320 |
|
|
*
|
321 |
568b29d8
|
Leszek Koltunski
|
* @param buffer Float buffer we will write the resulting Static4D to.
|
322 |
6a06a912
|
Leszek Koltunski
|
* @param offset Offset in the buffer where to write the result.
|
323 |
|
|
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
|
324 |
|
|
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
|
325 |
|
|
*/
|
326 |
bdb341bc
|
Leszek Koltunski
|
synchronized void interpolate(float[] buffer, int offset, float time)
|
327 |
6a06a912
|
Leszek Koltunski
|
{
|
328 |
|
|
switch(numPoints)
|
329 |
|
|
{
|
330 |
|
|
case 0: buffer[offset ] = 0.0f;
|
331 |
|
|
buffer[offset+1] = 0.0f;
|
332 |
|
|
buffer[offset+2] = 0.0f;
|
333 |
|
|
buffer[offset+3] = 0.0f;
|
334 |
|
|
break;
|
335 |
|
|
case 1: curr = vv.elementAt(0);
|
336 |
|
|
buffer[offset ] = curr.x;
|
337 |
|
|
buffer[offset+1] = curr.y;
|
338 |
|
|
buffer[offset+2] = curr.z;
|
339 |
|
|
buffer[offset+3] = curr.w;
|
340 |
|
|
break;
|
341 |
|
|
default:float t = time;
|
342 |
|
|
float scale0, scale1;
|
343 |
|
|
|
344 |
|
|
if( mMode==MODE_JUMP ) time = time*(numPoints-1);
|
345 |
|
|
else if( mMode==MODE_PATH || numPoints==2 ) time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
|
346 |
|
|
else time = time*numPoints;
|
347 |
|
|
|
348 |
|
|
int vecNext, vecCurr = (int)time;
|
349 |
|
|
time = time-vecCurr;
|
350 |
|
|
|
351 |
|
|
if( vecCurr>=0 && vecCurr<numPoints )
|
352 |
|
|
{
|
353 |
|
|
if( cacheDirty ) recomputeCache(); // recompute cache if we have added or remove vectors since last computation
|
354 |
|
|
|
355 |
|
|
switch(mMode)
|
356 |
|
|
{
|
357 |
|
|
case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1;
|
358 |
|
|
break;
|
359 |
|
|
case MODE_PATH: if( t<0.5f ) vecNext = vecCurr+1;
|
360 |
|
|
else vecNext = vecCurr==0 ? 1 : vecCurr-1;
|
361 |
|
|
break;
|
362 |
|
|
case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
|
363 |
|
|
break;
|
364 |
|
|
default : vecNext = 0;
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
curr = vv.elementAt(vecCurr);
|
368 |
|
|
next = vv.elementAt(vecNext);
|
369 |
|
|
tmp1 = vc.elementAt(vecCurr);
|
370 |
|
|
tmp2 = vc.elementAt(vecNext);
|
371 |
|
|
|
372 |
|
|
if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z || tmp2.vw!=next.w ) recomputeCache();
|
373 |
|
|
|
374 |
|
|
if( tmp1.sinOmega==0 )
|
375 |
|
|
{
|
376 |
|
|
scale0 = 0f;
|
377 |
|
|
scale1 = 1f;
|
378 |
|
|
}
|
379 |
|
|
else if( tmp1.cosOmega < 0.99 )
|
380 |
|
|
{
|
381 |
|
|
scale0 = (float)Math.sin( (1f-time)*tmp1.omega ) / tmp1.sinOmega;
|
382 |
|
|
scale1 = (float)Math.sin( time *tmp1.omega ) / tmp1.sinOmega;
|
383 |
|
|
}
|
384 |
|
|
else
|
385 |
|
|
{
|
386 |
|
|
scale0 = 1f-time;
|
387 |
|
|
scale1 = time;
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
buffer[offset ] = scale0*curr.x + scale1*next.x;
|
391 |
|
|
buffer[offset+1] = scale0*curr.y + scale1*next.y;
|
392 |
|
|
buffer[offset+2] = scale0*curr.z + scale1*next.z;
|
393 |
|
|
buffer[offset+3] = scale0*curr.w + scale1*next.w;
|
394 |
|
|
|
395 |
|
|
break;
|
396 |
|
|
}
|
397 |
|
|
}
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
}
|
401 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
402 |
|
|
//
|