17 |
17 |
// License along with this library; if not, write to the Free Software //
|
18 |
18 |
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19 |
19 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
20 |
package org.distorted.library.helpers
|
20 |
21 |
|
21 |
|
package org.distorted.library.helpers;
|
|
22 |
import org.distorted.library.type.Static4D
|
|
23 |
import kotlin.math.*
|
22 |
24 |
|
23 |
|
import org.distorted.library.type.Static4D;
|
24 |
|
|
25 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26 |
|
|
27 |
|
public class QuatHelper
|
28 |
|
{
|
29 |
25 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30 |
|
// return quat1*quat2
|
31 |
26 |
|
32 |
|
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
|
27 |
object QuatHelper
|
|
28 |
{
|
|
29 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
30 |
// return quat1*quat2
|
|
31 |
@kotlin.jvm.JvmStatic
|
|
32 |
fun quatMultiply(quat1: Static4D, quat2: Static4D): Static4D
|
33 |
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);
|
|
34 |
val qx = quat1.get0()
|
|
35 |
val qy = quat1.get1()
|
|
36 |
val qz = quat1.get2()
|
|
37 |
val qw = quat1.get3()
|
|
38 |
|
|
39 |
val rx = quat2.get0()
|
|
40 |
val ry = quat2.get1()
|
|
41 |
val rz = quat2.get2()
|
|
42 |
val rw = quat2.get3()
|
|
43 |
|
|
44 |
val tx = rw*qx - rz*qy + ry*qz + rx*qw
|
|
45 |
val ty = rw*qy + rz*qx + ry*qw - rx*qz
|
|
46 |
val tz = rw*qz + rz*qw - ry*qx + rx*qy
|
|
47 |
val tw = rw*qw - rz*qz - ry*qy - rx*qx
|
|
48 |
|
|
49 |
return Static4D(tx,ty,tz,tw)
|
50 |
50 |
}
|
51 |
51 |
|
52 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
// return quat1*quat2
|
54 |
|
|
55 |
|
public static void quatMultiply( float[] ret, Static4D quat1, Static4D quat2 )
|
|
52 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
53 |
// return quat1*quat2
|
|
54 |
@kotlin.jvm.JvmStatic
|
|
55 |
fun quatMultiply(ret: FloatArray, quat1: Static4D, quat2: Static4D)
|
56 |
56 |
{
|
57 |
|
float qx = quat1.get0();
|
58 |
|
float qy = quat1.get1();
|
59 |
|
float qz = quat1.get2();
|
60 |
|
float qw = quat1.get3();
|
61 |
|
|
62 |
|
float rx = quat2.get0();
|
63 |
|
float ry = quat2.get1();
|
64 |
|
float rz = quat2.get2();
|
65 |
|
float rw = quat2.get3();
|
66 |
|
|
67 |
|
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw;
|
68 |
|
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz;
|
69 |
|
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy;
|
70 |
|
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx;
|
|
57 |
val qx = quat1.get0()
|
|
58 |
val qy = quat1.get1()
|
|
59 |
val qz = quat1.get2()
|
|
60 |
val qw = quat1.get3()
|
|
61 |
|
|
62 |
val rx = quat2.get0()
|
|
63 |
val ry = quat2.get1()
|
|
64 |
val rz = quat2.get2()
|
|
65 |
val rw = quat2.get3()
|
|
66 |
|
|
67 |
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw
|
|
68 |
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz
|
|
69 |
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy
|
|
70 |
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx
|
71 |
71 |
}
|
72 |
72 |
|
73 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74 |
|
// return quat1*quat(rx,ry,rz,rw)
|
75 |
|
|
76 |
|
public static void quatMultiply( float[] ret, Static4D quat1, float rx, float ry, float rz, float rw )
|
77 |
|
{
|
78 |
|
float qx = quat1.get0();
|
79 |
|
float qy = quat1.get1();
|
80 |
|
float qz = quat1.get2();
|
81 |
|
float qw = quat1.get3();
|
82 |
|
|
83 |
|
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw;
|
84 |
|
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz;
|
85 |
|
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy;
|
86 |
|
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx;
|
87 |
|
}
|
88 |
|
|
89 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
90 |
|
// return quat1*quat2
|
91 |
|
|
92 |
|
public static float[] quatMultiply( float[] quat1, float[] quat2 )
|
|
73 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
74 |
// return quat1*quat(rx,ry,rz,rw)
|
|
75 |
@kotlin.jvm.JvmStatic
|
|
76 |
fun quatMultiply(ret: FloatArray, quat1: Static4D, rx: Float, ry: Float, rz: Float, rw: Float)
|
93 |
77 |
{
|
94 |
|
float qx = quat1[0];
|
95 |
|
float qy = quat1[1];
|
96 |
|
float qz = quat1[2];
|
97 |
|
float qw = quat1[3];
|
98 |
|
|
99 |
|
float rx = quat2[0];
|
100 |
|
float ry = quat2[1];
|
101 |
|
float rz = quat2[2];
|
102 |
|
float rw = quat2[3];
|
103 |
|
|
104 |
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
105 |
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
106 |
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
107 |
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
108 |
|
|
109 |
|
return new float[] {tx,ty,tz,tw};
|
|
78 |
val qx = quat1.get0()
|
|
79 |
val qy = quat1.get1()
|
|
80 |
val qz = quat1.get2()
|
|
81 |
val qw = quat1.get3()
|
|
82 |
|
|
83 |
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw
|
|
84 |
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz
|
|
85 |
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy
|
|
86 |
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx
|
110 |
87 |
}
|
111 |
88 |
|
112 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113 |
|
// return quat1*(rx,ry,rz,rw)
|
|
89 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
90 |
// return quat1*quat2
|
|
91 |
@kotlin.jvm.JvmStatic
|
|
92 |
fun quatMultiply(quat1: FloatArray, quat2: FloatArray): FloatArray
|
|
93 |
{
|
|
94 |
val qx = quat1[0]
|
|
95 |
val qy = quat1[1]
|
|
96 |
val qz = quat1[2]
|
|
97 |
val qw = quat1[3]
|
|
98 |
|
|
99 |
val rx = quat2[0]
|
|
100 |
val ry = quat2[1]
|
|
101 |
val rz = quat2[2]
|
|
102 |
val rw = quat2[3]
|
|
103 |
|
|
104 |
val tx = rw*qx - rz*qy + ry*qz + rx*qw
|
|
105 |
val ty = rw*qy + rz*qx + ry*qw - rx*qz
|
|
106 |
val tz = rw*qz + rz*qw - ry*qx + rx*qy
|
|
107 |
val tw = rw*qw - rz*qz - ry*qy - rx*qx
|
|
108 |
|
|
109 |
return floatArrayOf(tx,ty,tz,tw)
|
|
110 |
}
|
114 |
111 |
|
115 |
|
public static Static4D quatMultiply( Static4D quat, float rx, float ry, float rz, float rw )
|
|
112 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
113 |
// return quat1*(rx,ry,rz,rw)
|
|
114 |
fun quatMultiply(quat: Static4D, rx: Float, ry: Float, rz: Float, rw: Float): Static4D
|
116 |
115 |
{
|
117 |
|
float qx = quat.get0();
|
118 |
|
float qy = quat.get1();
|
119 |
|
float qz = quat.get2();
|
120 |
|
float qw = quat.get3();
|
|
116 |
val qx = quat.get0()
|
|
117 |
val qy = quat.get1()
|
|
118 |
val qz = quat.get2()
|
|
119 |
val qw = quat.get3()
|
121 |
120 |
|
122 |
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
123 |
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
124 |
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
125 |
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
|
121 |
val tx = rw*qx - rz*qy + ry*qz + rx*qw
|
|
122 |
val ty = rw*qy + rz*qx + ry*qw - rx*qz
|
|
123 |
val tz = rw*qz + rz*qw - ry*qx + rx*qy
|
|
124 |
val tw = rw*qw - rz*qz - ry*qy - rx*qx
|
126 |
125 |
|
127 |
|
return new Static4D(tx,ty,tz,tw);
|
|
126 |
return Static4D(tx,ty,tz,tw)
|
128 |
127 |
}
|
129 |
128 |
|
130 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131 |
|
// return (qx,qy,qz,qw)*quat
|
132 |
|
|
133 |
|
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, Static4D quat )
|
|
129 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
130 |
// return (qx,qy,qz,qw)*quat
|
|
131 |
fun quatMultiply(qx: Float, qy: Float, qz: Float, qw: Float, quat: Static4D): Static4D
|
134 |
132 |
{
|
135 |
|
float rx = quat.get0();
|
136 |
|
float ry = quat.get1();
|
137 |
|
float rz = quat.get2();
|
138 |
|
float rw = quat.get3();
|
|
133 |
val rx = quat.get0()
|
|
134 |
val ry = quat.get1()
|
|
135 |
val rz = quat.get2()
|
|
136 |
val rw = quat.get3()
|
139 |
137 |
|
140 |
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
141 |
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
142 |
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
143 |
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
|
138 |
val tx = rw*qx - rz*qy + ry*qz + rx*qw
|
|
139 |
val ty = rw*qy + rz*qx + ry*qw - rx*qz
|
|
140 |
val tz = rw*qz + rz*qw - ry*qx + rx*qy
|
|
141 |
val tw = rw*qw - rz*qz - ry*qy - rx*qx
|
144 |
142 |
|
145 |
|
return new Static4D(tx,ty,tz,tw);
|
|
143 |
return Static4D(tx,ty,tz,tw)
|
146 |
144 |
}
|
147 |
145 |
|
148 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149 |
|
// return (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
150 |
|
|
151 |
|
public static Static4D quatMultiply( float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
|
|
146 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
147 |
// return (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
|
148 |
fun quatMultiply( qx: Float, qy: Float, qz: Float, qw: Float, rx: Float, ry: Float, rz: Float, rw: Float): Static4D
|
152 |
149 |
{
|
153 |
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
154 |
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
155 |
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
156 |
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
|
150 |
val tx = rw*qx - rz*qy + ry*qz + rx*qw
|
|
151 |
val ty = rw*qy + rz*qx + ry*qw - rx*qz
|
|
152 |
val tz = rw*qz + rz*qw - ry*qx + rx*qy
|
|
153 |
val tw = rw*qw - rz*qz - ry*qy - rx*qx
|
157 |
154 |
|
158 |
|
return new Static4D(tx,ty,tz,tw);
|
|
155 |
return Static4D(tx,ty,tz,tw)
|
159 |
156 |
}
|
160 |
157 |
|
161 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162 |
|
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
163 |
|
|
164 |
|
public static void quatMultiply( float[] ret, float[] q, float[] r )
|
|
158 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
159 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
|
160 |
@kotlin.jvm.JvmStatic
|
|
161 |
fun quatMultiply(ret: FloatArray, q: FloatArray, r: FloatArray)
|
165 |
162 |
{
|
166 |
|
ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3];
|
167 |
|
ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2];
|
168 |
|
ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1];
|
169 |
|
ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0];
|
|
163 |
ret[0] = r[3]*q[0] - r[2]*q[1] + r[1]*q[2] + r[0]*q[3]
|
|
164 |
ret[1] = r[3]*q[1] + r[2]*q[0] + r[1]*q[3] - r[0]*q[2]
|
|
165 |
ret[2] = r[3]*q[2] + r[2]*q[3] - r[1]*q[0] + r[0]*q[1]
|
|
166 |
ret[3] = r[3]*q[3] - r[2]*q[2] - r[1]*q[1] - r[0]*q[0]
|
170 |
167 |
}
|
171 |
168 |
|
172 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173 |
|
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
174 |
|
|
175 |
|
public static void quatMultiply( float[] ret, Static4D q, float[] r )
|
|
169 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
170 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
|
171 |
fun quatMultiply(ret: FloatArray, q: Static4D, r: FloatArray)
|
176 |
172 |
{
|
177 |
|
float q0 = q.get0();
|
178 |
|
float q1 = q.get1();
|
179 |
|
float q2 = q.get2();
|
180 |
|
float q3 = q.get3();
|
181 |
|
|
182 |
|
ret[0] = r[3]*q0 - r[2]*q1 + r[1]*q2 + r[0]*q3;
|
183 |
|
ret[1] = r[3]*q1 + r[2]*q0 + r[1]*q3 - r[0]*q2;
|
184 |
|
ret[2] = r[3]*q2 + r[2]*q3 - r[1]*q0 + r[0]*q1;
|
185 |
|
ret[3] = r[3]*q3 - r[2]*q2 - r[1]*q1 - r[0]*q0;
|
|
173 |
val q0 = q.get0()
|
|
174 |
val q1 = q.get1()
|
|
175 |
val q2 = q.get2()
|
|
176 |
val q3 = q.get3()
|
|
177 |
|
|
178 |
ret[0] = r[3]*q0 - r[2]*q1 + r[1]*q2 + r[0]*q3
|
|
179 |
ret[1] = r[3]*q1 + r[2]*q0 + r[1]*q3 - r[0]*q2
|
|
180 |
ret[2] = r[3]*q2 + r[2]*q3 - r[1]*q0 + r[0]*q1
|
|
181 |
ret[3] = r[3]*q3 - r[2]*q2 - r[1]*q1 - r[0]*q0
|
186 |
182 |
}
|
187 |
183 |
|
188 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189 |
|
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
190 |
|
|
191 |
|
public static void quatMultiply( float[] ret, float[] q, Static4D r )
|
|
184 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
185 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
|
186 |
fun quatMultiply(ret: FloatArray, q: FloatArray, r: Static4D)
|
192 |
187 |
{
|
193 |
|
float r0 = r.get0();
|
194 |
|
float r1 = r.get1();
|
195 |
|
float r2 = r.get2();
|
196 |
|
float r3 = r.get3();
|
197 |
|
|
198 |
|
ret[0] = r3*q[0] - r2*q[1] + r1*q[2] + r0*q[3];
|
199 |
|
ret[1] = r3*q[1] + r2*q[0] + r1*q[3] - r0*q[2];
|
200 |
|
ret[2] = r3*q[2] + r2*q[3] - r1*q[0] + r0*q[1];
|
201 |
|
ret[3] = r3*q[3] - r2*q[2] - r1*q[1] - r0*q[0];
|
|
188 |
val r0 = r.get0()
|
|
189 |
val r1 = r.get1()
|
|
190 |
val r2 = r.get2()
|
|
191 |
val r3 = r.get3()
|
|
192 |
|
|
193 |
ret[0] = r3*q[0] - r2*q[1] + r1*q[2] + r0*q[3]
|
|
194 |
ret[1] = r3*q[1] + r2*q[0] + r1*q[3] - r0*q[2]
|
|
195 |
ret[2] = r3*q[2] + r2*q[3] - r1*q[0] + r0*q[1]
|
|
196 |
ret[3] = r3*q[3] - r2*q[2] - r1*q[1] - r0*q[0]
|
202 |
197 |
}
|
203 |
198 |
|
204 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205 |
|
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
206 |
|
|
207 |
|
public static void quatMultiply( float[] ret, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
|
|
199 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
200 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
|
201 |
fun quatMultiply(ret: FloatArray, qx: Float, qy: Float, qz: Float, qw: Float, rx: Float, ry: Float, rz: Float, rw: Float)
|
208 |
202 |
{
|
209 |
|
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw;
|
210 |
|
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz;
|
211 |
|
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy;
|
212 |
|
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx;
|
|
203 |
ret[0] = rw*qx - rz*qy + ry*qz + rx*qw
|
|
204 |
ret[1] = rw*qy + rz*qx + ry*qw - rx*qz
|
|
205 |
ret[2] = rw*qz + rz*qw - ry*qx + rx*qy
|
|
206 |
ret[3] = rw*qw - rz*qz - ry*qy - rx*qx
|
213 |
207 |
}
|
214 |
208 |
|
215 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216 |
|
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
217 |
|
|
218 |
|
private static void quatMultiply( float[] ret, int index, float qx, float qy, float qz, float qw, float rx, float ry, float rz, float rw )
|
|
209 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
210 |
// ret = (qx,qy,qz,qw)*(rx,ry,rz,rw)
|
|
211 |
private fun quatMultiply(ret: FloatArray, index: Int, qx: Float, qy: Float, qz: Float, qw: Float, rx: Float, ry: Float, rz: Float, rw: Float)
|
219 |
212 |
{
|
220 |
|
ret[index ] = rw*qx - rz*qy + ry*qz + rx*qw;
|
221 |
|
ret[index+1] = rw*qy + rz*qx + ry*qw - rx*qz;
|
222 |
|
ret[index+2] = rw*qz + rz*qw - ry*qx + rx*qy;
|
223 |
|
ret[index+3] = rw*qw - rz*qz - ry*qy - rx*qx;
|
|
213 |
ret[index ] = rw*qx - rz*qy + ry*qz + rx*qw
|
|
214 |
ret[index+1] = rw*qy + rz*qx + ry*qw - rx*qz
|
|
215 |
ret[index+2] = rw*qz + rz*qw - ry*qx + rx*qy
|
|
216 |
ret[index+3] = rw*qw - rz*qz - ry*qy - rx*qx
|
224 |
217 |
}
|
225 |
218 |
|
226 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227 |
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
228 |
|
|
229 |
|
public static Static4D rotateVectorByQuat(float x, float y, float z, float w, Static4D quat)
|
|
219 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
220 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
|
221 |
@kotlin.jvm.JvmStatic
|
|
222 |
fun rotateVectorByQuat(x: Float, y: Float, z: Float, w: Float, quat: Static4D): Static4D
|
230 |
223 |
{
|
231 |
|
float qx = quat.get0();
|
232 |
|
float qy = quat.get1();
|
233 |
|
float qz = quat.get2();
|
234 |
|
float qw = quat.get3();
|
|
224 |
val qx = quat.get0()
|
|
225 |
val qy = quat.get1()
|
|
226 |
val qz = quat.get2()
|
|
227 |
val qw = quat.get3()
|
235 |
228 |
|
236 |
|
Static4D tmp = quatMultiply(qx,qy,qz,qw,x,y,z,w);
|
|
229 |
val tmp = quatMultiply(qx,qy,qz,qw,x,y,z,w)
|
237 |
230 |
|
238 |
|
return quatMultiply(tmp,-qx,-qy,-qz,qw);
|
|
231 |
return quatMultiply(tmp,-qx,-qy,-qz,qw)
|
239 |
232 |
}
|
240 |
233 |
|
241 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242 |
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
243 |
|
|
244 |
|
public static void rotateVectorByQuat(float[] output, float x, float y, float z, float w, Static4D quat)
|
|
234 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
235 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
|
236 |
@kotlin.jvm.JvmStatic
|
|
237 |
fun rotateVectorByQuat(output: FloatArray, x: Float, y: Float, z: Float, w: Float, quat: Static4D)
|
245 |
238 |
{
|
246 |
|
float[] tmp = new float[4];
|
|
239 |
val tmp = FloatArray(4)
|
247 |
240 |
|
248 |
|
float qx = quat.get0();
|
249 |
|
float qy = quat.get1();
|
250 |
|
float qz = quat.get2();
|
251 |
|
float qw = quat.get3();
|
|
241 |
val qx = quat.get0()
|
|
242 |
val qy = quat.get1()
|
|
243 |
val qz = quat.get2()
|
|
244 |
val qw = quat.get3()
|
252 |
245 |
|
253 |
|
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w);
|
254 |
|
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw);
|
|
246 |
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w)
|
|
247 |
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw)
|
255 |
248 |
}
|
256 |
249 |
|
257 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258 |
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
259 |
|
|
260 |
|
public static void rotateVectorByQuat(float[] output, int index, float x, float y, float z, float w, Static4D quat)
|
|
250 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
251 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
|
252 |
@kotlin.jvm.JvmStatic
|
|
253 |
fun rotateVectorByQuat(output: FloatArray, index: Int, x: Float, y: Float, z: Float, w: Float, quat: Static4D)
|
261 |
254 |
{
|
262 |
|
float[] tmp = new float[4];
|
|
255 |
val tmp = FloatArray(4)
|
263 |
256 |
|
264 |
|
float qx = quat.get0();
|
265 |
|
float qy = quat.get1();
|
266 |
|
float qz = quat.get2();
|
267 |
|
float qw = quat.get3();
|
|
257 |
val qx = quat.get0()
|
|
258 |
val qy = quat.get1()
|
|
259 |
val qz = quat.get2()
|
|
260 |
val qw = quat.get3()
|
268 |
261 |
|
269 |
|
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w);
|
270 |
|
quatMultiply(output,index,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw);
|
|
262 |
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w)
|
|
263 |
quatMultiply(output,index,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw)
|
271 |
264 |
}
|
272 |
265 |
|
273 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274 |
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
275 |
|
|
276 |
|
public static void rotateVectorByQuat(float[] output, float x, float y, float z, float w, float[] quat)
|
|
266 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
267 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
|
268 |
@kotlin.jvm.JvmStatic
|
|
269 |
fun rotateVectorByQuat(output: FloatArray, x: Float, y: Float, z: Float, w: Float, quat: FloatArray)
|
277 |
270 |
{
|
278 |
|
float[] tmp = new float[4];
|
|
271 |
val tmp = FloatArray(4)
|
279 |
272 |
|
280 |
|
float qx = quat[0];
|
281 |
|
float qy = quat[1];
|
282 |
|
float qz = quat[2];
|
283 |
|
float qw = quat[3];
|
|
273 |
val qx = quat[0]
|
|
274 |
val qy = quat[1]
|
|
275 |
val qz = quat[2]
|
|
276 |
val qw = quat[3]
|
284 |
277 |
|
285 |
|
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w);
|
286 |
|
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw);
|
|
278 |
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w)
|
|
279 |
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw)
|
287 |
280 |
}
|
288 |
281 |
|
289 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290 |
|
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
291 |
|
|
292 |
|
public static void rotateVectorByQuat(float[] output, int index, float x, float y, float z, float w, float[] quat)
|
|
282 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
283 |
// rotate (x1,x2,x3,x4) by quat ( i.e. return quat*vector*(quat^-1) )
|
|
284 |
fun rotateVectorByQuat(output: FloatArray, index: Int, x: Float, y: Float, z: Float, w: Float, quat: FloatArray)
|
293 |
285 |
{
|
294 |
|
float[] tmp = new float[4];
|
|
286 |
val tmp = FloatArray(4)
|
295 |
287 |
|
296 |
|
float qx = quat[0];
|
297 |
|
float qy = quat[1];
|
298 |
|
float qz = quat[2];
|
299 |
|
float qw = quat[3];
|
|
288 |
val qx = quat[0]
|
|
289 |
val qy = quat[1]
|
|
290 |
val qz = quat[2]
|
|
291 |
val qw = quat[3]
|
300 |
292 |
|
301 |
|
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w);
|
302 |
|
quatMultiply(output,index,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw);
|
|
293 |
quatMultiply(tmp,qx,qy,qz,qw,x,y,z,w)
|
|
294 |
quatMultiply(output,index,tmp[0],tmp[1],tmp[2],tmp[3],-qx,-qy,-qz,qw)
|
303 |
295 |
}
|
304 |
296 |
|
305 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306 |
|
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
|
307 |
|
|
308 |
|
public static void rotateVectorByQuat(float[] output, float[] vec, float[] quat)
|
|
297 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
298 |
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
|
|
299 |
@kotlin.jvm.JvmStatic
|
|
300 |
fun rotateVectorByQuat(output: FloatArray, vec: FloatArray, quat: FloatArray)
|
309 |
301 |
{
|
310 |
|
float[] tmp = new float[4];
|
|
302 |
val tmp = FloatArray(4)
|
311 |
303 |
|
312 |
|
quatMultiply(tmp,quat,vec);
|
|
304 |
quatMultiply(tmp, quat, vec)
|
313 |
305 |
|
314 |
|
quat[0] = -quat[0];
|
315 |
|
quat[1] = -quat[1];
|
316 |
|
quat[2] = -quat[2];
|
|
306 |
quat[0] = -quat[0]
|
|
307 |
quat[1] = -quat[1]
|
|
308 |
quat[2] = -quat[2]
|
317 |
309 |
|
318 |
|
quatMultiply(output,tmp,quat);
|
|
310 |
quatMultiply(output, tmp, quat)
|
319 |
311 |
|
320 |
|
quat[0] = -quat[0];
|
321 |
|
quat[1] = -quat[1];
|
322 |
|
quat[2] = -quat[2];
|
|
312 |
quat[0] = -quat[0]
|
|
313 |
quat[1] = -quat[1]
|
|
314 |
quat[2] = -quat[2]
|
323 |
315 |
}
|
324 |
316 |
|
325 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
326 |
|
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
|
327 |
|
|
328 |
|
public static void rotateVectorByQuat(float[] output, float[] vec, Static4D quat)
|
|
317 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
318 |
// rotate vec by quat ( i.e. return quat*vector*(quat^-1) )
|
|
319 |
fun rotateVectorByQuat(output: FloatArray, vec: FloatArray, quat: Static4D)
|
329 |
320 |
{
|
330 |
|
float[] tmp = new float[4];
|
|
321 |
val tmp = FloatArray(4)
|
331 |
322 |
|
332 |
|
quatMultiply(tmp,quat,vec);
|
|
323 |
quatMultiply(tmp, quat, vec)
|
333 |
324 |
|
334 |
|
float x1 = quat.get0();
|
335 |
|
float y1 = quat.get1();
|
336 |
|
float z1 = quat.get2();
|
337 |
|
float w1 = quat.get3();
|
338 |
|
quat.set(-x1,-y1,-z1,w1);
|
|
325 |
val x1 = quat.get0()
|
|
326 |
val y1 = quat.get1()
|
|
327 |
val z1 = quat.get2()
|
|
328 |
val w1 = quat.get3()
|
|
329 |
quat.set(-x1,-y1,-z1,w1)
|
339 |
330 |
|
340 |
|
quatMultiply(output,tmp,quat);
|
|
331 |
quatMultiply(output, tmp, quat)
|
341 |
332 |
|
342 |
|
float x2 = quat.get0();
|
343 |
|
float y2 = quat.get1();
|
344 |
|
float z2 = quat.get2();
|
345 |
|
float w2 = quat.get3();
|
346 |
|
quat.set(-x2,-y2,-z2,w2);
|
|
333 |
val x2 = quat.get0()
|
|
334 |
val y2 = quat.get1()
|
|
335 |
val z2 = quat.get2()
|
|
336 |
val w2 = quat.get3()
|
|
337 |
quat.set(-x2,-y2,-z2,w2)
|
347 |
338 |
}
|
348 |
339 |
|
349 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
350 |
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
351 |
|
|
352 |
|
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
|
340 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
341 |
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
|
342 |
@kotlin.jvm.JvmStatic
|
|
343 |
fun rotateVectorByQuat(vector: Static4D, quat: Static4D): Static4D
|
353 |
344 |
{
|
354 |
|
float qx = quat.get0();
|
355 |
|
float qy = quat.get1();
|
356 |
|
float qz = quat.get2();
|
357 |
|
float qw = quat.get3();
|
|
345 |
val qx = quat.get0()
|
|
346 |
val qy = quat.get1()
|
|
347 |
val qz = quat.get2()
|
|
348 |
val qw = quat.get3()
|
358 |
349 |
|
359 |
|
Static4D tmp = quatMultiply(qx,qy,qz,qw,vector);
|
|
350 |
val tmp = quatMultiply(qx,qy,qz,qw,vector)
|
360 |
351 |
|
361 |
|
return quatMultiply(tmp,-qx,-qy,-qz,qw);
|
|
352 |
return quatMultiply(tmp,-qx,-qy,-qz,qw)
|
362 |
353 |
}
|
363 |
354 |
|
364 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
365 |
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
366 |
|
|
367 |
|
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
|
355 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
356 |
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
|
357 |
@kotlin.jvm.JvmStatic
|
|
358 |
fun rotateVectorByInvertedQuat(vector: Static4D, quat: Static4D): Static4D
|
368 |
359 |
{
|
369 |
|
float qx = quat.get0();
|
370 |
|
float qy = quat.get1();
|
371 |
|
float qz = quat.get2();
|
372 |
|
float qw = quat.get3();
|
|
360 |
val qx = quat.get0()
|
|
361 |
val qy = quat.get1()
|
|
362 |
val qz = quat.get2()
|
|
363 |
val qw = quat.get3()
|
373 |
364 |
|
374 |
|
Static4D tmp = quatMultiply(-qx,-qy,-qz,qw,vector);
|
|
365 |
val tmp = quatMultiply(-qx,-qy,-qz,qw,vector)
|
375 |
366 |
|
376 |
|
return quatMultiply(tmp,qx,qy,qz,qw);
|
|
367 |
return quatMultiply(tmp,qx,qy,qz,qw)
|
377 |
368 |
}
|
378 |
369 |
|
379 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380 |
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
381 |
|
|
382 |
|
public static void rotateVectorByInvertedQuat(float[] output, float x, float y, float z, float w, float[] quat)
|
383 |
|
{
|
384 |
|
float[] tmp = new float[4];
|
385 |
|
|
386 |
|
float qx = quat[0];
|
387 |
|
float qy = quat[1];
|
388 |
|
float qz = quat[2];
|
389 |
|
float qw = quat[3];
|
|
370 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
371 |
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
|
372 |
@kotlin.jvm.JvmStatic
|
|
373 |
fun rotateVectorByInvertedQuat(output: FloatArray, x: Float, y: Float, z: Float, w: Float, quat: FloatArray)
|
|
374 |
{
|
|
375 |
val tmp = FloatArray(4)
|
390 |
376 |
|
391 |
|
quatMultiply(tmp,-qx,-qy,-qz,qw,x,y,z,w);
|
392 |
|
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],qx,qy,qz,qw);
|
393 |
|
}
|
|
377 |
val qx = quat[0]
|
|
378 |
val qy = quat[1]
|
|
379 |
val qz = quat[2]
|
|
380 |
val qw = quat[3]
|
394 |
381 |
|
395 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
396 |
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
|
382 |
quatMultiply(tmp,-qx,-qy,-qz,qw,x,y,z,w)
|
|
383 |
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],qx,qy,qz,qw)
|
|
384 |
}
|
397 |
385 |
|
398 |
|
public static void rotateVectorByInvertedQuat(float[] output, float x, float y, float z, float w, Static4D quat)
|
|
386 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
387 |
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
|
388 |
@kotlin.jvm.JvmStatic
|
|
389 |
fun rotateVectorByInvertedQuat(output: FloatArray, x: Float, y: Float, z: Float, w: Float, quat: Static4D)
|
399 |
390 |
{
|
400 |
|
float[] tmp = new float[4];
|
|
391 |
val tmp = FloatArray(4)
|
401 |
392 |
|
402 |
|
float qx = quat.get0();
|
403 |
|
float qy = quat.get1();
|
404 |
|
float qz = quat.get2();
|
405 |
|
float qw = quat.get3();
|
|
393 |
val qx = quat.get0()
|
|
394 |
val qy = quat.get1()
|
|
395 |
val qz = quat.get2()
|
|
396 |
val qw = quat.get3()
|
406 |
397 |
|
407 |
|
quatMultiply(tmp,-qx,-qy,-qz,qw,x,y,z,w);
|
408 |
|
quatMultiply(output,tmp[0],tmp[1],tmp[2],tmp[3],qx,qy,qz,qw);
|
|
398 |
quatMultiply(tmp, -qx, -qy, -qz, qw, x, y, z, w)
|
|
399 |
quatMultiply(output, tmp[0], tmp[1], tmp[2], tmp[3], qx, qy, qz, qw)
|
409 |
400 |
}
|
410 |
401 |
|
411 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
412 |
|
|
413 |
|
public static Static4D quatFromDrag(float dragX, float dragY)
|
|
402 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
403 |
@kotlin.jvm.JvmStatic
|
|
404 |
fun quatFromDrag(dragX: Float, dragY: Float): Static4D
|
414 |
405 |
{
|
415 |
|
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
|
416 |
|
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted!
|
417 |
|
float axisZ = 0;
|
418 |
|
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
|
|
406 |
var axisX = dragY // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
|
|
407 |
var axisY = dragX // Why not (-dragY, dragX) ? because Y axis is also inverted!
|
|
408 |
var axisZ = 0f
|
|
409 |
val axisL = sqrt(axisX * axisX + axisY * axisY + axisZ * axisZ)
|
419 |
410 |
|
420 |
|
if( axisL>0 )
|
421 |
|
{
|
422 |
|
axisX /= axisL;
|
423 |
|
axisY /= axisL;
|
424 |
|
axisZ /= axisL;
|
|
411 |
if (axisL > 0)
|
|
412 |
{
|
|
413 |
axisX /= axisL
|
|
414 |
axisY /= axisL
|
|
415 |
axisZ /= axisL
|
425 |
416 |
|
426 |
|
float ratio = axisL;
|
427 |
|
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi)
|
|
417 |
var ratio = axisL
|
|
418 |
ratio -= ratio.toInt() // the cos() is only valid in (0,Pi)
|
428 |
419 |
|
429 |
|
float cosA = (float)Math.cos(Math.PI*ratio);
|
430 |
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
|
420 |
val cosA = cos(Math.PI * ratio).toFloat()
|
|
421 |
val sinA = sqrt(1 - cosA * cosA)
|
431 |
422 |
|
432 |
|
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
433 |
|
}
|
|
423 |
return Static4D(axisX * sinA, axisY * sinA, axisZ * sinA, cosA)
|
|
424 |
}
|
434 |
425 |
|
435 |
|
return new Static4D(0f, 0f, 0f, 1f);
|
|
426 |
return Static4D(0f, 0f, 0f, 1f)
|
436 |
427 |
}
|
437 |
428 |
|
438 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439 |
|
|
440 |
|
public static double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2)
|
|
429 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
430 |
@kotlin.jvm.JvmStatic
|
|
431 |
fun computeCos(oldX: Double, oldY: Double, newX: Double, newY: Double, len1: Double, len2: Double): Double
|
441 |
432 |
{
|
442 |
|
double ret= (oldX*newX+oldY*newY) / (len1*len2);
|
443 |
|
if( ret<-1.0 ) return -1.0;
|
444 |
|
if( ret> 1.0 ) return 1.0;
|
|
433 |
val ret = (oldX*newX + oldY*newY) / (len1*len2)
|
|
434 |
if (ret < -1.0) return -1.0
|
|
435 |
if (ret > 1.0) return 1.0
|
445 |
436 |
|
446 |
|
return ret;
|
|
437 |
return ret
|
447 |
438 |
}
|
448 |
439 |
|
449 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
450 |
|
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise!
|
451 |
|
|
452 |
|
public static double computeSin(double oldX, double oldY, double newX, double newY, double len1, double len2)
|
|
440 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
441 |
// sin of (signed!) angle between vectors 'old' and 'new', counterclockwise!
|
|
442 |
@kotlin.jvm.JvmStatic
|
|
443 |
fun computeSin(oldX: Double, oldY: Double, newX: Double, newY: Double, len1: Double, len2: Double): Double
|
453 |
444 |
{
|
454 |
|
double ret= (newX*oldY-oldX*newY) / (len1*len2);
|
455 |
|
if( ret<-1.0 ) return -1.0;
|
456 |
|
if( ret> 1.0 ) return 1.0;
|
|
445 |
val ret = (newX*oldY - oldX*newY) / (len1*len2)
|
|
446 |
if (ret < -1.0) return -1.0
|
|
447 |
if (ret > 1.0) return 1.0
|
457 |
448 |
|
458 |
|
return ret;
|
|
449 |
return ret
|
459 |
450 |
}
|
460 |
451 |
|
461 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
462 |
|
// return quat Q that turns 3D vector A=(ax,ay,az) to another 3D vector B=(bx,by,bz)
|
463 |
|
// take care of double-cover by ensuring that always Q.get3() >=0
|
464 |
|
|
465 |
|
public static Static4D retRotationQuat(float ax, float ay, float az, float bx, float by, float bz)
|
|
452 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
453 |
// return quat Q that turns 3D vector A=(ax,ay,az) to another 3D vector B=(bx,by,bz)
|
|
454 |
// take care of double-cover by ensuring that always Q.get3() >=0
|
|
455 |
fun retRotationQuat(ax: Float, ay: Float, az: Float, bx: Float, by: Float, bz: Float): Static4D
|
466 |
456 |
{
|
467 |
|
float nx = ay*bz - az*by;
|
468 |
|
float ny = az*bx - ax*bz;
|
469 |
|
float nz = ax*by - ay*bx;
|
470 |
|
|
471 |
|
float sin = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
|
472 |
|
float cos = ax*bx + ay*by + az*bz;
|
473 |
|
|
474 |
|
if( sin!=0 )
|
475 |
|
{
|
476 |
|
nx /= sin;
|
477 |
|
ny /= sin;
|
478 |
|
nz /= sin;
|
479 |
|
}
|
480 |
|
|
481 |
|
// Why sin<=0 and cos>=0 ?
|
482 |
|
// 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive.
|
483 |
|
// But1: quats work counterclockwise -> negate cos.
|
484 |
|
// But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1))
|
485 |
|
// so negate again both cos and sin.
|
486 |
|
float sinHalf =-(float)Math.sqrt((1-cos)/2);
|
487 |
|
float cosHalf = (float)Math.sqrt((1+cos)/2);
|
488 |
|
|
489 |
|
return new Static4D(nx*sinHalf,ny*sinHalf,nz*sinHalf,cosHalf);
|
|
457 |
var nx = ay*bz - az*by
|
|
458 |
var ny = az*bx - ax*bz
|
|
459 |
var nz = ax*by - ay*bx
|
|
460 |
|
|
461 |
val sin = sqrt(nx*nx + ny*ny + nz*nz)
|
|
462 |
val cos = ax*bx + ay*by + az*bz
|
|
463 |
|
|
464 |
if (sin != 0f)
|
|
465 |
{
|
|
466 |
nx /= sin
|
|
467 |
ny /= sin
|
|
468 |
nz /= sin
|
|
469 |
}
|
|
470 |
|
|
471 |
// Why sin<=0 and cos>=0 ?
|
|
472 |
// 0<angle<180 -> 0<halfAngle<90 -> both sin and cos are positive.
|
|
473 |
// But1: quats work counterclockwise -> negate cos.
|
|
474 |
// But2: double-cover, we prefer to have the cos positive (so that unit=(0,0,0,1))
|
|
475 |
// so negate again both cos and sin.
|
|
476 |
val sinHalf = -sqrt((1-cos) / 2)
|
|
477 |
val cosHalf = sqrt((1+cos) / 2)
|
|
478 |
|
|
479 |
return Static4D(nx*sinHalf, ny*sinHalf, nz*sinHalf, cosHalf)
|
490 |
480 |
}
|
491 |
|
}
|
|
481 |
}
|
first two files converted to Kotlin