1 |
8fc3b80a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
2e569ff6
|
Leszek Koltunski
|
// Copyright 2021 Leszek Koltunski leszek@koltunski.pl //
|
3 |
8fc3b80a
|
Leszek Koltunski
|
// //
|
4 |
2e569ff6
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
8fc3b80a
|
Leszek Koltunski
|
// //
|
6 |
2e569ff6
|
Leszek Koltunski
|
// This library is free software; you can redistribute it and/or //
|
7 |
|
|
// modify it under the terms of the GNU Lesser General Public //
|
8 |
|
|
// License as published by the Free Software Foundation; either //
|
9 |
|
|
// version 2.1 of the License, or (at your option) any later version. //
|
10 |
8fc3b80a
|
Leszek Koltunski
|
// //
|
11 |
2e569ff6
|
Leszek Koltunski
|
// This library is distributed in the hope that it will be useful, //
|
12 |
8fc3b80a
|
Leszek Koltunski
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
2e569ff6
|
Leszek Koltunski
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
8fc3b80a
|
Leszek Koltunski
|
// //
|
16 |
2e569ff6
|
Leszek Koltunski
|
// You should have received a copy of the GNU Lesser General Public //
|
17 |
|
|
// License along with this library; if not, write to the Free Software //
|
18 |
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19 |
8fc3b80a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
1dec66e0
|
Leszek Koltunski
|
package org.distorted.library.helpers;
|
22 |
8fc3b80a
|
Leszek Koltunski
|
|
23 |
|
|
import org.distorted.library.type.Static4D;
|
24 |
|
|
|
25 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26 |
|
|
|
27 |
|
|
public class QuatHelper
|
28 |
|
|
{
|
29 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30 |
|
|
// return quat1*quat2
|
31 |
|
|
|
32 |
|
|
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
33 |
|
|
{
|
34 |
|
|
float qx = quat1.get0();
|
35 |
|
|
float qy = quat1.get1();
|
36 |
|
|
float qz = quat1.get2();
|
37 |
|
|
float qw = quat1.get3();
|
38 |
|
|
|
39 |
|
|
float rx = quat2.get0();
|
40 |
|
|
float ry = quat2.get1();
|
41 |
|
|
float rz = quat2.get2();
|
42 |
|
|
float rw = quat2.get3();
|
43 |
|
|
|
44 |
|
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
45 |
|
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
46 |
|
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
47 |
|
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
48 |
|
|
|
49 |
|
|
return new Static4D(tx,ty,tz,tw);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
a4999c9d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
// return quat1*quat2
|
54 |
|
|
|
55 |
|
|
public static float[] quatMultiply( float[] quat1, float[] quat2 )
|
56 |
|
|
{
|
57 |
|
|
float qx = quat1[0];
|
58 |
|
|
float qy = quat1[1];
|
59 |
|
|
float qz = quat1[2];
|
60 |
|
|
float qw = quat1[3];
|
61 |
|
|
|
62 |
|
|
float rx = quat2[0];
|
63 |
|
|
float ry = quat2[1];
|
64 |
|
|
float rz = quat2[2];
|
65 |
|
|
float rw = quat2[3];
|
66 |
|
|
|
67 |
|
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
68 |
|
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
69 |
|
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
70 |
|
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
71 |
|
|
|
72 |
|
|
return new float[] {tx,ty,tz,tw};
|
73 |
|
|
}
|
74 |
|
|
|
75 |
4c568a67
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
// return quat1*(rx,ry,rz,rw)
|
77 |
|
|
|
78 |
|
|
public static Static4D quatMultiply( Static4D quat1, float rx, float ry, float rz, float rw )
|
79 |
|
|
{
|
80 |
|
|
float qx = quat1.get0();
|
81 |
|
|
float qy = quat1.get1();
|
82 |
|
|
float qz = quat1.get2();
|
83 |
|
|
float qw = quat1.get3();
|
84 |
|
|
|
85 |
|
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
86 |
|
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
87 |
|
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
88 |
|
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
89 |
|
|
|
90 |
|
|
return new Static4D(tx,ty,tz,tw);
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94 |
|
|
// return (qx,qy,qz,qw)*quat2
|
95 |
|
|
|
96 |
|
|
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, Static4D quat2 )
|
97 |
|
|
{
|
98 |
|
|
float rx = quat2.get0();
|
99 |
|
|
float ry = quat2.get1();
|
100 |
|
|
float rz = quat2.get2();
|
101 |
|
|
float rw = quat2.get3();
|
102 |
|
|
|
103 |
|
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
104 |
|
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
105 |
|
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
106 |
|
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
107 |
|
|
|
108 |
|
|
return new Static4D(tx,ty,tz,tw);
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112 |
|
|
// return (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
113 |
|
|
|
114 |
|
|
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
|
115 |
|
|
{
|
116 |
|
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
117 |
|
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
118 |
|
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
119 |
|
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
120 |
|
|
|
121 |
|
|
return new Static4D(tx,ty,tz,tw);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125 |
|
|
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
126 |
|
|
|
127 |
|
|
public static void quatMultiply( float[] ret, float[] q, float[] r )
|
128 |
|
|
{
|
129 |
|
|
ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3];
|
130 |
|
|
ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2];
|
131 |
|
|
ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1];
|
132 |
|
|
ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0];
|
133 |
|
|
}
|
134 |
|
|
|
135 |
0e1d3d2e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136 |
|
|
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
137 |
|
|
|
138 |
|
|
public static void quatMultiply( float[] ret, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
|
139 |
|
|
{
|
140 |
|
|
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw;
|
141 |
|
|
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz;
|
142 |
|
|
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy;
|
143 |
|
|
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
8fc3b80a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147 |
|
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
148 |
|
|
|
149 |
|
|
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
150 |
|
|
{
|
151 |
|
|
float qx = quat.get0();
|
152 |
|
|
float qy = quat.get1();
|
153 |
|
|
float qz = quat.get2();
|
154 |
|
|
float qw = quat.get3();
|
155 |
|
|
|
156 |
4c568a67
|
Leszek Koltunski
|
Static4D tmp = quatMultiply(qx,qy,qz,qw,vector);
|
157 |
|
|
|
158 |
|
|
return quatMultiply(tmp,-qx,-qy,-qz,qw);
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162 |
|
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
163 |
|
|
|
164 |
|
|
public static Static4D rotateVectorByQuat(float x, float y, float z, float w, Static4D quat)
|
165 |
|
|
{
|
166 |
|
|
float qx = quat.get0();
|
167 |
|
|
float qy = quat.get1();
|
168 |
|
|
float qz = quat.get2();
|
169 |
|
|
float qw = quat.get3();
|
170 |
|
|
|
171 |
|
|
Static4D tmp = quatMultiply(qx,qy,qz,qw,x,y,z,w);
|
172 |
|
|
|
173 |
|
|
return quatMultiply(tmp,-qx,-qy,-qz,qw);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
0e1d3d2e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
177 |
|
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
178 |
|
|
|
179 |
|
|
public static void rotateVectorByQuat(float[] output, float x, float y, float z, float w, Static4D quat)
|
180 |
|
|
{
|
181 |
|
|
float[] tmp = new float[4];
|
182 |
|
|
|
183 |
|
|
float qx = quat.get0();
|
184 |
|
|
float qy = quat.get1();
|
185 |
|
|
float qz = quat.get2();
|
186 |
|
|
float qw = quat.get3();
|
187 |
|
|
|
188 |
|
|
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w);
|
189 |
|
|
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw);
|
190 |
|
|
}
|
191 |
|
|
|
192 |
a4999c9d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193 |
|
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
194 |
|
|
|
195 |
|
|
public static void rotateVectorByQuat(float[] output, float x, float y, float z, float w, float[] quat)
|
196 |
|
|
{
|
197 |
|
|
float[] tmp = new float[4];
|
198 |
|
|
|
199 |
|
|
float qx = quat[0];
|
200 |
|
|
float qy = quat[1];
|
201 |
|
|
float qz = quat[2];
|
202 |
|
|
float qw = quat[3];
|
203 |
|
|
|
204 |
|
|
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w);
|
205 |
|
|
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw);
|
206 |
|
|
}
|
207 |
|
|
|
208 |
4c568a67
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209 |
|
|
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
|
210 |
|
|
|
211 |
|
|
public static void rotateVectorByQuat(float[] output, float[] vec, float[] quat)
|
212 |
|
|
{
|
213 |
|
|
float[] tmp = new float[4];
|
214 |
|
|
|
215 |
|
|
quatMultiply(tmp,quat,vec);
|
216 |
|
|
|
217 |
|
|
quat[0] = -quat[0];
|
218 |
|
|
quat[1] = -quat[1];
|
219 |
|
|
quat[2] = -quat[2];
|
220 |
|
|
|
221 |
|
|
quatMultiply(output,tmp,quat);
|
222 |
8fc3b80a
|
Leszek Koltunski
|
|
223 |
4c568a67
|
Leszek Koltunski
|
quat[0] = -quat[0];
|
224 |
|
|
quat[1] = -quat[1];
|
225 |
|
|
quat[2] = -quat[2];
|
226 |
8fc3b80a
|
Leszek Koltunski
|
}
|
227 |
|
|
|
228 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
229 |
|
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
230 |
|
|
|
231 |
|
|
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
232 |
|
|
{
|
233 |
|
|
float qx = quat.get0();
|
234 |
|
|
float qy = quat.get1();
|
235 |
|
|
float qz = quat.get2();
|
236 |
|
|
float qw = quat.get3();
|
237 |
|
|
|
238 |
4c568a67
|
Leszek Koltunski
|
Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector);
|
239 |
8fc3b80a
|
Leszek Koltunski
|
|
240 |
|
|
return quatMultiply(tmp,quat);
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
244 |
|
|
|
245 |
|
|
public static Static4D quatFromDrag(float dragX, float dragY)
|
246 |
|
|
{
|
247 |
|
|
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
|
248 |
|
|
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted!
|
249 |
|
|
float axisZ = 0;
|
250 |
|
|
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
|
251 |
|
|
|
252 |
|
|
if( axisL>0 )
|
253 |
|
|
{
|
254 |
|
|
axisX /= axisL;
|
255 |
|
|
axisY /= axisL;
|
256 |
|
|
axisZ /= axisL;
|
257 |
|
|
|
258 |
|
|
float ratio = axisL;
|
259 |
|
|
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi)
|
260 |
|
|
|
261 |
|
|
float cosA = (float)Math.cos(Math.PI*ratio);
|
262 |
|
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
263 |
|
|
|
264 |
|
|
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
return new Static4D(0f, 0f, 0f, 1f);
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271 |
|
|
|
272 |
|
|
public static double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2)
|
273 |
|
|
{
|
274 |
|
|
double ret= (oldX*newX+oldY*newY) / (len1*len2);
|
275 |
|
|
if( ret<-1.0 ) return -1.0;
|
276 |
|
|
if( ret> 1.0 ) return 1.0;
|
277 |
|
|
|
278 |
|
|
return ret;
|
279 |
|
|
}
|
280 |
|
|
|
281 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
282 |
|
|
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise!
|
283 |
|
|
|
284 |
|
|
public static double computeSin(double oldX, double oldY, double newX, double newY, double len1, double len2)
|
285 |
|
|
{
|
286 |
|
|
double ret= (newX*oldY-oldX*newY) / (len1*len2);
|
287 |
|
|
if( ret<-1.0 ) return -1.0;
|
288 |
|
|
if( ret> 1.0 ) return 1.0;
|
289 |
|
|
|
290 |
|
|
return ret;
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
294 |
|
|
// return quat Q that turns 3D vector A=(ax,ay,az) to another 3D vector B=(bx,by,bz)
|
295 |
|
|
// take care of double-cover by ensuring that always Q.get3() >=0
|
296 |
|
|
|
297 |
|
|
public static Static4D retRotationQuat(float ax, float ay, float az, float bx, float by, float bz)
|
298 |
|
|
{
|
299 |
|
|
float nx = ay*bz - az*by;
|
300 |
|
|
float ny = az*bx - ax*bz;
|
301 |
|
|
float nz = ax*by - ay*bx;
|
302 |
|
|
|
303 |
|
|
float sin = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
|
304 |
|
|
float cos = ax*bx + ay*by + az*bz;
|
305 |
|
|
|
306 |
|
|
if( sin!=0 )
|
307 |
|
|
{
|
308 |
|
|
nx /= sin;
|
309 |
|
|
ny /= sin;
|
310 |
|
|
nz /= sin;
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
// Why sin<=0 and cos>=0 ?
|
314 |
|
|
// 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive.
|
315 |
|
|
// But1: quats work counterclockwise -> negate cos.
|
316 |
|
|
// But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1))
|
317 |
|
|
// so negate again both cos and sin.
|
318 |
|
|
float sinHalf =-(float)Math.sqrt((1-cos)/2);
|
319 |
|
|
float cosHalf = (float)Math.sqrt((1+cos)/2);
|
320 |
|
|
|
321 |
|
|
return new Static4D(nx*sinHalf,ny*sinHalf,nz*sinHalf,cosHalf);
|
322 |
|
|
}
|
323 |
|
|
}
|