VR-Vantage
Loading...
Searching...
No Matches
DtQuat.inl
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies, Inc.
3** All rights reserved.
4******************************************************************************/
5
9
10#pragma once
11#ifndef DtQuat_INL_
12#error This file may only be included by DtQuat.h
13#endif
14
15#include <cmath>
16#include "DtQuat.h"
17
18namespace makVrv
19{
21 : xyz()
22 , w(1.0)
23 {
24 }
25
26 DtQuat::DtQuat(double x, double y, double z, double _w)
27 : xyz(x,y,z)
28 , w(_w)
29 {
30 }
31
32 DtQuat::DtQuat(const DtVector& i,double real)
33 : xyz(i)
34 ,w(real)
35 {
36 }
37
38 DtQuat::DtQuat(double theta_z, double theta_y, double theta_x)
39 {
40 double cos_z_2 = cosf(0.5*theta_z);
41 double cos_y_2 = cosf(0.5*theta_y);
42 double cos_x_2 = cosf(0.5*theta_x);
43
44 double sin_z_2 = sinf(0.5*theta_z);
45 double sin_y_2 = sinf(0.5*theta_y);
46 double sin_x_2 = sinf(0.5*theta_x);
47
48 // and now compute quaternion
49 w = cos_z_2*cos_y_2*cos_x_2 + sin_z_2*sin_y_2*sin_x_2;
50 xyz[0] = cos_z_2*cos_y_2*sin_x_2 - sin_z_2*sin_y_2*cos_x_2;
51 xyz[1] = cos_z_2*sin_y_2*cos_x_2 + sin_z_2*cos_y_2*sin_x_2;
52 xyz[2] = sin_z_2*cos_y_2*cos_x_2 - cos_z_2*sin_y_2*sin_x_2;
53 }
54
55 DtQuat::DtQuat(const DtVector& angles)
56 {
57 double cos_z_2 = cosf(0.5*angles[2]);
58 double cos_y_2 = cosf(0.5*angles[1]);
59 double cos_x_2 = cosf(0.5*angles[0]);
60
61 double sin_z_2 = sinf(0.5*angles[2]);
62 double sin_y_2 = sinf(0.5*angles[1]);
63 double sin_x_2 = sinf(0.5*angles[0]);
64
65 // and now compute quaternion
66 w = cos_z_2*cos_y_2*cos_x_2 + sin_z_2*sin_y_2*sin_x_2;
67 xyz[0] = cos_z_2*cos_y_2*sin_x_2 - sin_z_2*sin_y_2*cos_x_2;
68 xyz[1] = cos_z_2*sin_y_2*cos_x_2 + sin_z_2*cos_y_2*sin_x_2;
69 xyz[2] = sin_z_2*cos_y_2*cos_x_2 - cos_z_2*sin_y_2*sin_x_2;
70 }
71
72 void DtQuat::set( double x,double y,double z,double _w )
73 {
74 xyz[0] = x;
75 xyz[1] = y;
76 xyz[2] = z;
77 w = _w;
78 }
79
80
82 {
83 w = q.w;
84 xyz = q.xyz;
85 return *this;
86 }
87
89 {
90 return DtQuat(xyz+q.xyz,w+q.w);
91 }
92
94 {
95 return DtQuat(xyz-q.xyz,w-q.w);
96 }
97
99 {
100 double scalar= (xyz[0] * q.xyz[0]) + (xyz[1] * q.xyz[1]) + (xyz[2] * q.xyz[2]);
101
102 return DtQuat(
103 xyz[1]*q.xyz[2] - xyz[2]*q.xyz[1] + w*q.xyz[0] + xyz[0]*q.w,
104 xyz[2]*q.xyz[0] - xyz[0]*q.xyz[2] + w*q.xyz[1] + xyz[1]*q.w,
105 xyz[0]*q.xyz[1] - xyz[1]*q.xyz[0] + w*q.xyz[2] + xyz[2]*q.w,
106 w*q.w - scalar);
107 }
108
110 {
111 DtQuat p(q);
112 p.invert();
113 return *this * p;
114 }
115
116 DtQuat DtQuat::operator*(double scale) const
117 {
118 return DtQuat(xyz[0] * scale,xyz[1] * scale,xyz[2] *scale, w * scale);
119 }
120
121 DtQuat DtQuat::operator/(double scale) const
122 {
123 return DtQuat(xyz[0] * scale, xyz[1] / scale, xyz[2] / scale, w / scale);
124 }
125
127 {
128 return DtQuat( -xyz[0], -xyz[1],-xyz[2], -w );
129 }
130
132 {
133 xyz += q.xyz;
134 w += q.w;
135 return *this;
136 }
137
139 {
140 xyz-=q.xyz;
141 w-=q.w;
142 return *this;
143 }
144
146 {
147 double scalar= (xyz[0] * q.xyz[0]) + (xyz[1] * q.xyz[1]) + (xyz[2] * q.xyz[2]);
148 w= w * q.w - scalar;
149
150 double x = xyz[0], y= xyz[1], z= xyz[2];
151 xyz[0]= y*q.xyz[2] - z*q.xyz[1] + w*q.xyz[0] + x*q.w;
152 xyz[1]= z*q.xyz[0] - x*q.xyz[2] + w*q.xyz[1] + y*q.w;
153 xyz[2]= x*q.xyz[1] - y*q.xyz[0] + w*q.xyz[2] + z*q.w;
154 return *this;
155 }
156
157 const DtQuat& DtQuat::operator*=(double scale)
158 {
159 DtVecScale(xyz, scale, xyz);
160 w *= scale;
161 return *this;
162 }
163
164 const DtQuat& DtQuat::operator/=(double scale)
165 {
166 DtVecScale(xyz, scale, xyz);
167 w /= scale;
168 return *this;
169 }
170
172 double DtQuat::length() const
173 {
174 return sqrt((xyz[0] * xyz[0]) + (xyz[1] * xyz[1]) + (xyz[2] * xyz[2]) + (w*w));
175 }
176
179 {
180 return ((xyz[0] * xyz[0]) + (xyz[1] * xyz[1]) + (xyz[2] * xyz[2]) + (w*w));
181 }
182
184 {
185 *this /= length();
186 }
187
189 {
190 return *this / length();
191 }
192
194 {
195 DtVecNeg(xyz,xyz);
196 }
197
199 {
200 conjugate();
201 *this /= length_squared();
202 }
203
205 {
206 double a = 0.0;
207 double sina = sin(a);
208 if (sina <= 0)
209 {
210 //Cause Quat to be 0,0,0,0
211 sina = 1.0;
212 }
213 else
214 {
215 a = acos(w) / sina;
216 }
217 return DtQuat( a * xyz[0], a * xyz[1], a * xyz[2], 0);
218 }
219
221 {
222 double a = sqrt(xyz.magnitudeSquared());
223 double sina = 0;
224 double cosa = cos(a);
225 if (a > 0)
226 {
227 sina = sin(a) / a;
228 }
229 return DtQuat( sina * xyz[0], sina * xyz[1], sina * xyz[2], cosa);
230 }
231
232 void DtQuat::getMatrix4(double matrix[16]) const
233 {
234 matrix[0] = w;
235 matrix[1] = -xyz[0];
236 matrix[2] = -xyz[1];
237 matrix[3] = -xyz[2];
238 matrix[4] = xyz[0];
239 matrix[5] = w;
240 matrix[6] = -xyz[2];
241 matrix[7] = xyz[1];
242 matrix[8] = xyz[1];
243 matrix[9] = xyz[2];
244 matrix[10] = w;
245 matrix[11] = -xyz[0];
246 matrix[12] = xyz[2];
247 matrix[13] = -xyz[1];
248 matrix[14] = xyz[0];
249 matrix[15] = w;
250 }
251
252 void DtQuat::getMatrix3(double matrix[9]) const
253 {
254 matrix[0] = 1-2*(xyz[1]*xyz[1]+xyz[2]*xyz[2]);
255 matrix[1] = 2*(xyz[0]*xyz[1]-w*xyz[2]);
256 matrix[2] = 2*(xyz[0]*xyz[2]+w*xyz[1]);
257 matrix[3] = 2*(xyz[0]*xyz[1]+w*xyz[2]);
258 matrix[4] = 1-2*(xyz[0]*xyz[0]+xyz[2]*xyz[2]);
259 matrix[5] = 2*(xyz[1]*xyz[2]-w*xyz[0]);
260 matrix[6] = 2*(xyz[0]*xyz[2]-w*xyz[1]);
261 matrix[7] = 2*(xyz[1]*xyz[2]+w*xyz[0]);
262 matrix[8] = 1-2*(xyz[0]*xyz[0]+xyz[1]*xyz[1]);
263 }
264
265 double DtQuat::dot(const DtQuat& q1, const DtQuat& q2)
266 {
267 return (q1.xyz[0] * q2.xyz[0]) + (q1.xyz[1] * q2.xyz[1]) + (q1.xyz[2] * q2.xyz[2]) + (q1.w * q2.w);
268 }
269
270 DtQuat DtQuat::lerp(const DtQuat& q1, const DtQuat& q2, double t)
271 {
272 if(t<=0.0)
273 return q1;
274
275 if(t>=1.0)
276 return q2;
277
278 return (q1*(1-t) + q2*t).normalized();
279 }
280
281 DtQuat DtQuat::slerp(const DtQuat& q1, const DtQuat& q2, double t)
282 {
283 if(t<=0.0)
284 return q1;
285
286 if(t>=1.0)
287 return q2;
288
289 DtQuat q3;
290 double dot = DtQuat::dot(q1, q2);
291
292 /* dot = cos(theta)
293 if (dot < 0), q1 and q2 are more than 90 degrees apart,
294 so we can invert one to reduce spinning */
295 if (dot < 0)
296 {
297 dot = -dot;
298 q3 = -q2;
299 }
300 else
301 {
302 q3 = q2;
303 }
304
305 if (dot > 0.95f)
306 {
307 // if the angle is small, use linear interpolation
308 return lerp(q1,q3,t);
309 }
310 else
311 {
312 double halfTheta=acos(dot);
313 double sinHalfTheta=sin(halfTheta);
314 double ratioA=sin((1-t) * halfTheta) / sinHalfTheta;
315 double ratioB=sin(t*halfTheta) / sinHalfTheta;
316
317 q3.xyz[0]=((q1.xyz[0] * ratioA) + (q3.xyz[0] * ratioB));
318 q3.xyz[1]=((q1.xyz[1] * ratioA) + (q3.xyz[1] * ratioB));
319 q3.xyz[2]=((q1.xyz[2] * ratioA) + (q3.xyz[2] * ratioB));
320 q3.w=((q1.w * ratioA) + (q3.w * ratioB));
321 }
322
323 return q3.normalized();
324 }
325
326 DtQuat DtQuat::slerpNoInvert(const DtQuat& q1, const DtQuat& q2, double t)
327 {
328 double dot = DtQuat::dot(q1, q2);
329 if (dot > -0.95f && dot < 0.95f)
330 {
331 double angle = acosf(dot);
332 return (q1*sinf(angle*(1-t)) + q2*sinf(angle*t))/sinf(angle);
333 }
334 else
335 {
336 // if the angle is small, use linear interpolation
337 return lerp(q1,q2,t);
338 }
339 }
340
341 DtQuat DtQuat::squad(const DtQuat& q1,const DtQuat& q2,const DtQuat& a,const DtQuat& b,double t)
342 {
343 DtQuat c = slerpNoInvert(q1,q2,t);
344 DtQuat d = slerpNoInvert(a,b,t);
345 return slerpNoInvert(c,d,2*t*(1-t));
346 }
347
348 DtQuat DtQuat::bezier(const DtQuat& q1,const DtQuat& q2,const DtQuat& a,const DtQuat& b,double t)
349 {
350 // level 1
351 DtQuat q11= slerpNoInvert(q1,a,t), q12= slerpNoInvert(a,b,t), q13= slerpNoInvert(b,q2,t);
352 // level 2 and 3
353 return slerpNoInvert(slerpNoInvert(q11,q12,t), slerpNoInvert(q12,q13,t), t);
354 }
355
356 DtQuat DtQuat::spline(const DtQuat& qnm1,const DtQuat& qn,const DtQuat& qnp1)
357 {
358 DtQuat qni( -qn.xyz[0],-qn.xyz[1],-qn.xyz[2],qn.w);
359 return qn * (( (qni * qnm1).log() + (qni*qnp1).log() ) / -4 ).exp();
360 }
361
363 DtQuat DtQuat::from_axis_angle(const DtVector& axis, double angle)
364 {
365 const double sina = sinf(angle/2);
366 return DtQuat(axis[0] * sina, axis[1] * sina, axis[2] * sina,cosf(angle/2));
367 }
368
369 void DtQuat::to_axis_angle(DtVector& axis, double& angle) const
370 {
371 angle = acosf(w);
372
373 // pre-compute to save time
374 double sinf_theta_inv = 1.0/sinf(angle);
375
376 // now the vector
377 axis[0] = xyz[0]*sinf_theta_inv;
378 axis[1] = xyz[1]*sinf_theta_inv;
379 axis[2] = xyz[2]*sinf_theta_inv;
380
381 // multiply by 2
382 angle *= 2;
383 }
384
385 DtVector DtQuat::rotate(const DtVector& v) const
386 {
387 DtQuat V(v,0);
388 DtQuat conjugate(*this);
389 conjugate.conjugate();
390 return (*this * V * conjugate).xyz;
391 }
392
393
394 DtVector DtQuat::euler_angles() const
395 {
396 //double sqw = w*w;
397 double sqx = xyz[0]*xyz[0];
398 double sqy = xyz[1]*xyz[1];
399 double sqz = xyz[2]*xyz[2];
400
401 // euler.x = atan2f(2.f * (xyz[0]*xyz[1] + xyz[2]*w), sqx - sqy - sqz + sqw);
402 // euler[1] = asinf(-2.f * (xyz[0]*xyz[2] - xyz[1]*w));
403 // euler[2] = atan2f(2.f * (xyz[1]*xyz[2] + xyz[0]*w), -sqx - sqy + sqz + sqw);
404 return DtVector(
405 atan2f(2.f * (xyz[2] * xyz[1] + xyz[0] * w), 1 - 2 * (sqx + sqy)),
406 asinf(-2.f * (xyz[0] * xyz[2] - xyz[1] * w)),
407 atan2f(2.f * (xyz[0] * xyz[1] + xyz[2] * w), 1 - 2 * (sqy + sqz))
408 );
409 }
410
411 inline std::ostream& operator<<( std::ostream& os, const DtQuat& q )
412 {
413 os << "X: " << q.xyz[0] << " Y: " << q.xyz[1] << " Z: " << q.xyz[2] << " W: " << q.w;
414 return os;
415 }
416}
Contains makVrv::DtQuat class.
const DtQuat & operator*=(const DtQuat &q)
Definition DtQuat.inl:145
double length() const
gets the length of this quaternion
Definition DtQuat.inl:172
void getMatrix3(double matrix[9]) const
Definition DtQuat.inl:252
static DtQuat slerp(const DtQuat &q1, const DtQuat &q2, double t)
spherical linear interpolation
Definition DtQuat.inl:281
DtQuat operator+(const DtQuat &q) const
Definition DtQuat.inl:88
void conjugate()
computes the conjugate of this quaternion
Definition DtQuat.inl:193
DtQuat normalized() const
returns the normalized version of this quaternion
Definition DtQuat.inl:188
void set(double x, double y, double z, double w)
Set the quat.
Definition DtQuat.inl:72
double length_squared() const
gets the squared length of this quaternion
Definition DtQuat.inl:178
DtQuat log() const
returns the logarithm of a quaternion = v*a where q = [cos(a),v*sin(a)]
Definition DtQuat.inl:204
static double dot(const DtQuat &q1, const DtQuat &q2)
computes the dot product of 2 quaternions
Definition DtQuat.inl:265
DtQuat & operator=(const DtQuat &q)
basic operations
Definition DtQuat.inl:81
DtVector euler_angles() const
returns the euler angles from a rotation quaternion
Definition DtQuat.inl:394
static DtQuat slerpNoInvert(const DtQuat &q1, const DtQuat &q2, double t)
This version of slerp, used by squad, does not check for theta > 90.
Definition DtQuat.inl:326
const DtQuat & operator/=(double scale)
Definition DtQuat.inl:164
double w
the real component
Definition DtQuat.h:23
const DtQuat & operator-=(const DtQuat &q)
Definition DtQuat.inl:138
DtQuat operator-() const
Definition DtQuat.inl:126
static DtQuat bezier(const DtQuat &q1, const DtQuat &q2, const DtQuat &a, const DtQuat &b, double t)
Shoemake-Bezier interpolation using De Castlejau algorithm.
Definition DtQuat.inl:348
void getMatrix4(double matrix[16]) const
casting to a 4x4 isomorphic matrix for right multiplication with vector
Definition DtQuat.inl:232
const DtQuat & operator+=(const DtQuat &q)
Definition DtQuat.inl:131
void invert()
inverts this quaternion
Definition DtQuat.inl:198
DtVector xyz
the imaginary components
Definition DtQuat.h:22
void normalize()
normalizes this quaternion
Definition DtQuat.inl:183
DtQuat operator/(const DtQuat &q) const
Definition DtQuat.inl:109
void to_axis_angle(DtVector &axis, double &angle) const
returns the axis and angle of this unit quaternion
Definition DtQuat.inl:369
static DtQuat squad(const DtQuat &q1, const DtQuat &q2, const DtQuat &a, const DtQuat &b, double t)
spherical cubic interpolation
Definition DtQuat.inl:341
DtVector rotate(const DtVector &v) const
rotates v by this quaternion (quaternion must be unit)
Definition DtQuat.inl:385
DtQuat()
constructors
Definition DtQuat.inl:20
static DtQuat spline(const DtQuat &qnm1, const DtQuat &qn, const DtQuat &qnp1)
Given 3 quaternions, qn-1,qn and qn+1, calculate a control point to be used in spline interpolation.
Definition DtQuat.inl:356
static DtQuat from_axis_angle(const DtVector &axis, double angle)
converts from a normalized axis - angle pair rotation to a quaternion
Definition DtQuat.inl:363
DtQuat operator*(const DtQuat &q) const
Definition DtQuat.inl:98
DtQuat exp() const
returns e^quaternion = exp(v*a) = [cos(a),vsin(a)]
Definition DtQuat.inl:220
static DtQuat lerp(const DtQuat &q1, const DtQuat &q2, double t)
linear quaternion interpolation
Definition DtQuat.inl:270
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
DT_DLL_VRVAUDIO DtMessage & operator<<(DtMessage &msg, const makAudio::DtAbstractAudio::Priority &priority)
Encode a makAudio::DtAbstractAudio::Priority value onto a DtMessage.