RavEngine
Loading...
Searching...
No Matches
PxQuat.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_QUAT_H
30#define PX_QUAT_H
31
36#include "foundation/PxVec3.h"
37#if !PX_DOXYGEN
38namespace physx
39{
40#endif
41
47template<class Type>
49{
50 public:
51
55 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT()
56 {
57 }
58
60 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(PxIDENTITY) : x(Type(0.0)), y(Type(0.0)), z(Type(0.0)), w(Type(1.0))
61 {
62 }
63
67 explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(Type r) : x(Type(0.0)), y(Type(0.0)), z(Type(0.0)), w(r)
68 {
69 }
70
74 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(Type nx, Type ny, Type nz, Type nw) : x(nx), y(ny), z(nz), w(nw)
75 {
76 }
77
87 PX_CUDA_CALLABLE PX_INLINE PxQuatT(Type angleRadians, const PxVec3T<Type>& unitAxis)
88 {
89 PX_ASSERT(PxAbs(Type(1.0) - unitAxis.magnitude()) < Type(1e-3));
90 const Type a = angleRadians * Type(0.5);
91
92 Type s;
93 PxSinCos(a, s, w);
94 x = unitAxis.x * s;
95 y = unitAxis.y * s;
96 z = unitAxis.z * s;
97 }
98
102 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(const PxQuatT& v) : x(v.x), y(v.y), z(v.z), w(v.w)
103 {
104 }
105
111 PX_CUDA_CALLABLE PX_INLINE explicit PxQuatT(const PxMat33T<Type>& m); /* defined in PxMat33.h */
112
116 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isIdentity() const
117 {
118 return x==Type(0.0) && y==Type(0.0) && z==Type(0.0) && w==Type(1.0);
119 }
120
124 PX_CUDA_CALLABLE bool isFinite() const
125 {
126 return PxIsFinite(x) && PxIsFinite(y) && PxIsFinite(z) && PxIsFinite(w);
127 }
128
132 PX_CUDA_CALLABLE bool isUnit() const
133 {
134 const Type unitTolerance = Type(1e-3);
135 return isFinite() && PxAbs(magnitude() - Type(1.0)) < unitTolerance;
136 }
137
142 PX_CUDA_CALLABLE bool isSane() const
143 {
144 const Type unitTolerance = Type(1e-2);
145 return isFinite() && PxAbs(magnitude() - Type(1.0)) < unitTolerance;
146 }
147
151 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxQuatT& q) const
152 {
153 return x == q.x && y == q.y && z == q.z && w == q.w;
154 }
155
159 PX_CUDA_CALLABLE PX_INLINE void toRadiansAndUnitAxis(Type& angle, PxVec3T<Type>& axis) const
160 {
161 const Type quatEpsilon = Type(1.0e-8);
162 const Type s2 = x * x + y * y + z * z;
163 if(s2 < quatEpsilon * quatEpsilon) // can't extract a sensible axis
164 {
165 angle = Type(0.0);
166 axis = PxVec3T<Type>(Type(1.0), Type(0.0), Type(0.0));
167 }
168 else
169 {
170 const Type s = PxRecipSqrt(s2);
171 axis = PxVec3T<Type>(x, y, z) * s;
172 angle = PxAbs(w) < quatEpsilon ? Type(PxPi) : PxAtan2(s2 * s, w) * Type(2.0);
173 }
174 }
175
181 PX_CUDA_CALLABLE PX_FORCE_INLINE Type getAngle() const
182 {
183 return PxAcos(w) * Type(2.0);
184 }
185
191 PX_CUDA_CALLABLE PX_FORCE_INLINE Type getAngle(const PxQuatT& q) const
192 {
193 return PxAcos(dot(q)) * Type(2.0);
194 }
195
199 PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitudeSquared() const
200 {
201 return x * x + y * y + z * z + w * w;
202 }
203
207 PX_CUDA_CALLABLE PX_FORCE_INLINE Type dot(const PxQuatT& v) const
208 {
209 return x * v.x + y * v.y + z * v.z + w * v.w;
210 }
211
212 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT getNormalized() const
213 {
214 const Type s = Type(1.0) / magnitude();
215 return PxQuatT(x * s, y * s, z * s, w * s);
216 }
217
218 PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitude() const
219 {
220 return PxSqrt(magnitudeSquared());
221 }
222
223 // modifiers:
227 PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalize() // convert this PxQuatT to a unit quaternion
228 {
229 const Type mag = magnitude();
230 if(mag != Type(0.0))
231 {
232 const Type imag = Type(1.0) / mag;
233
234 x *= imag;
235 y *= imag;
236 z *= imag;
237 w *= imag;
238 }
239 return mag;
240 }
241
242 /*
243 \brief returns the conjugate.
244
245 \note for unit quaternions, this is the inverse.
246 */
247 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT getConjugate() const
248 {
249 return PxQuatT(-x, -y, -z, w);
250 }
251
252 /*
253 \brief returns imaginary part.
254 */
255 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T<Type> getImaginaryPart() const
256 {
257 return PxVec3T<Type>(x, y, z);
258 }
259
262 {
263 const Type x2 = x * Type(2.0);
264 const Type w2 = w * Type(2.0);
265 return PxVec3T<Type>((w * w2) - Type(1.0) + x * x2, (z * w2) + y * x2, (-y * w2) + z * x2);
266 }
267
270 {
271 const Type y2 = y * Type(2.0);
272 const Type w2 = w * Type(2.0);
273 return PxVec3T<Type>((-z * w2) + x * y2, (w * w2) - Type(1.0) + y * y2, (x * w2) + z * y2);
274 }
275
278 {
279 const Type z2 = z * Type(2.0);
280 const Type w2 = w * Type(2.0);
281 return PxVec3T<Type>((y * w2) + x * z2, (-x * w2) + y * z2, (w * w2) - Type(1.0) + z * z2);
282 }
283
287 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3T<Type> rotate(const PxVec3T<Type>& v) const
288 {
289 const Type vx = Type(2.0) * v.x;
290 const Type vy = Type(2.0) * v.y;
291 const Type vz = Type(2.0) * v.z;
292 const Type w2 = w * w - 0.5f;
293 const Type dot2 = (x * vx + y * vy + z * vz);
294 return PxVec3T<Type>((vx * w2 + (y * vz - z * vy) * w + x * dot2), (vy * w2 + (z * vx - x * vz) * w + y * dot2),
295 (vz * w2 + (x * vy - y * vx) * w + z * dot2));
296 }
297
301 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3T<Type> rotateInv(const PxVec3T<Type>& v) const
302 {
303 const Type vx = Type(2.0) * v.x;
304 const Type vy = Type(2.0) * v.y;
305 const Type vz = Type(2.0) * v.z;
306 const Type w2 = w * w - 0.5f;
307 const Type dot2 = (x * vx + y * vy + z * vz);
308 return PxVec3T<Type>((vx * w2 - (y * vz - z * vy) * w + x * dot2), (vy * w2 - (z * vx - x * vz) * w + y * dot2),
309 (vz * w2 - (x * vy - y * vx) * w + z * dot2));
310 }
311
315 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT& operator=(const PxQuatT& p)
316 {
317 x = p.x;
318 y = p.y;
319 z = p.z;
320 w = p.w;
321 return *this;
322 }
323
324 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT& operator*=(const PxQuatT& q)
325 {
326 const Type tx = w * q.x + q.w * x + y * q.z - q.y * z;
327 const Type ty = w * q.y + q.w * y + z * q.x - q.z * x;
328 const Type tz = w * q.z + q.w * z + x * q.y - q.x * y;
329
330 w = w * q.w - q.x * x - y * q.y - q.z * z;
331 x = tx;
332 y = ty;
333 z = tz;
334 return *this;
335 }
336
337 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT& operator+=(const PxQuatT& q)
338 {
339 x += q.x;
340 y += q.y;
341 z += q.z;
342 w += q.w;
343 return *this;
344 }
345
346 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT& operator-=(const PxQuatT& q)
347 {
348 x -= q.x;
349 y -= q.y;
350 z -= q.z;
351 w -= q.w;
352 return *this;
353 }
354
355 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT& operator*=(const Type s)
356 {
357 x *= s;
358 y *= s;
359 z *= s;
360 w *= s;
361 return *this;
362 }
363
365 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator*(const PxQuatT& q) const
366 {
367 return PxQuatT(w * q.x + q.w * x + y * q.z - q.y * z, w * q.y + q.w * y + z * q.x - q.z * x,
368 w * q.z + q.w * z + x * q.y - q.x * y, w * q.w - x * q.x - y * q.y - z * q.z);
369 }
370
372 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator+(const PxQuatT& q) const
373 {
374 return PxQuatT(x + q.x, y + q.y, z + q.z, w + q.w);
375 }
376
378 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator-() const
379 {
380 return PxQuatT(-x, -y, -z, -w);
381 }
382
383 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator-(const PxQuatT& q) const
384 {
385 return PxQuatT(x - q.x, y - q.y, z - q.z, w - q.w);
386 }
387
388 PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator*(Type r) const
389 {
390 return PxQuatT(x * r, y * r, z * r, w * r);
391 }
392
394 Type x, y, z, w;
395};
396
397typedef PxQuatT<float> PxQuat;
399
400#if !PX_DOXYGEN
401} // namespace physx
402#endif
403
405#endif
406
3x3 matrix class
Definition PxMat33.h:91
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:49
PX_CUDA_CALLABLE PX_FORCE_INLINE Type dot(const PxQuatT &v) const
returns the scalar product of this and other.
Definition PxQuat.h:207
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT()
Default constructor, does not do any initialization.
Definition PxQuat.h:55
PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalize()
maps to the closest unit quaternion.
Definition PxQuat.h:227
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxQuatT &q) const
returns true if the two quaternions are exactly equal
Definition PxQuat.h:151
PX_CUDA_CALLABLE PX_INLINE void toRadiansAndUnitAxis(Type &angle, PxVec3T< Type > &axis) const
converts this quaternion to angle-axis representation
Definition PxQuat.h:159
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(Type r)
Constructor from a scalar: sets the real part w to the scalar value, and the imaginary parts (x,...
Definition PxQuat.h:67
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T< Type > getBasisVector0() const
Definition PxQuat.h:261
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3T< Type > rotateInv(const PxVec3T< Type > &v) const
Definition PxQuat.h:301
PX_CUDA_CALLABLE bool isSane() const
returns true if finite and magnitude is reasonably close to unit to allow for some accumulation of er...
Definition PxQuat.h:142
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator-() const
Definition PxQuat.h:378
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T< Type > getBasisVector2() const
Definition PxQuat.h:277
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator+(const PxQuatT &q) const
Definition PxQuat.h:372
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(const PxQuatT &v)
Copy ctor.
Definition PxQuat.h:102
PX_CUDA_CALLABLE bool isUnit() const
returns true if finite and magnitude is close to unit
Definition PxQuat.h:132
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(Type nx, Type ny, Type nz, Type nw)
Constructor. Take note of the order of the elements!
Definition PxQuat.h:74
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3T< Type > rotate(const PxVec3T< Type > &v) const
Definition PxQuat.h:287
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isIdentity() const
returns true if quat is identity
Definition PxQuat.h:116
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT operator*(const PxQuatT &q) const
Definition PxQuat.h:365
PX_CUDA_CALLABLE PX_FORCE_INLINE Type getAngle() const
Gets the angle between this quat and the identity quaternion.
Definition PxQuat.h:181
PX_CUDA_CALLABLE bool isFinite() const
returns true if all elements are finite (not NAN or INF, etc.)
Definition PxQuat.h:124
PX_CUDA_CALLABLE PX_FORCE_INLINE Type getAngle(const PxQuatT &q) const
Gets the angle between this quat and the argument.
Definition PxQuat.h:191
Type x
Definition PxQuat.h:394
PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitudeSquared() const
This is the squared 4D vector length, should be 1 for unit quaternions.
Definition PxQuat.h:199
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT(PxIDENTITY)
identity constructor
Definition PxQuat.h:60
PX_CUDA_CALLABLE PX_FORCE_INLINE PxQuatT & operator=(const PxQuatT &p)
Assignment operator.
Definition PxQuat.h:315
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T< Type > getBasisVector1() const
Definition PxQuat.h:269
PX_CUDA_CALLABLE PX_INLINE PxQuatT(Type angleRadians, const PxVec3T< Type > &unitAxis)
Creates from angle-axis representation.
Definition PxQuat.h:87
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:50
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitude() const
returns the magnitude
Definition PxVec3.h:181
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAtan2(float x, float y)
Arctangent of (x/y) with correct sign. Returns angle between -PI and PI in radians Unit: Radians.
Definition PxMath.h:302
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PxIDENTITY
Definition Px.h:99
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition PxMath.h:158
PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(float f)
returns true if the passed number is a finite floating point number as opposed to INF,...
Definition PxMath.h:326
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition PxMath.h:146
PX_CUDA_CALLABLE PX_FORCE_INLINE void PxSinCos(const PxF32 a, PxF32 &sin, PxF32 &cos)
compute sine and cosine at the same time
Definition PxMath.h:202
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAcos(float f)
Arccosine. Returns angle between 0 and PI in radians Unit: Radians.
Definition PxMath.h:262