RavEngine
Loading...
Searching...
No Matches
PxMat33.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_MAT33_H
30#define PX_MAT33_H
35#include "foundation/PxVec3.h"
36#include "foundation/PxQuat.h"
37
38#if !PX_DOXYGEN
39namespace physx
40{
41#endif
89template<class Type>
91{
92 public:
94 PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33T()
95 {
96 }
97
99 PX_CUDA_CALLABLE PX_INLINE PxMat33T(PxIDENTITY) :
100 column0(Type(1.0), Type(0.0), Type(0.0)),
101 column1(Type(0.0), Type(1.0), Type(0.0)),
102 column2(Type(0.0), Type(0.0), Type(1.0))
103 {
104 }
105
107 PX_CUDA_CALLABLE PX_INLINE PxMat33T(PxZERO) :
108 column0(Type(0.0)),
109 column1(Type(0.0)),
110 column2(Type(0.0))
112 }
113
115 PX_CUDA_CALLABLE PxMat33T(const PxVec3T<Type>& col0, const PxVec3T<Type>& col1, const PxVec3T<Type>& col2) :
116 column0(col0),
117 column1(col1),
118 column2(col2)
119 {
120 }
121
123 explicit PX_CUDA_CALLABLE PX_INLINE PxMat33T(Type r) :
124 column0(r, Type(0.0), Type(0.0)),
125 column1(Type(0.0), r, Type(0.0)),
126 column2(Type(0.0), Type(0.0), r)
127 {
128 }
129
131 explicit PX_CUDA_CALLABLE PX_INLINE PxMat33T(Type values[]) :
132 column0(values[0], values[1], values[2]),
133 column1(values[3], values[4], values[5]),
134 column2(values[6], values[7], values[8])
135 {
136 }
137
139 explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33T(const PxQuatT<Type>& q)
140 {
141 // PT: TODO: PX-566
142 const Type x = q.x;
143 const Type y = q.y;
144 const Type z = q.z;
145 const Type w = q.w;
146
147 const Type x2 = x + x;
148 const Type y2 = y + y;
149 const Type z2 = z + z;
150
151 const Type xx = x2 * x;
152 const Type yy = y2 * y;
153 const Type zz = z2 * z;
154
155 const Type xy = x2 * y;
156 const Type xz = x2 * z;
157 const Type xw = x2 * w;
158
159 const Type yz = y2 * z;
160 const Type yw = y2 * w;
161 const Type zw = z2 * w;
162
163 column0 = PxVec3T<Type>(Type(1.0) - yy - zz, xy + zw, xz - yw);
164 column1 = PxVec3T<Type>(xy - zw, Type(1.0) - xx - zz, yz + xw);
165 column2 = PxVec3T<Type>(xz + yw, yz - xw, Type(1.0) - xx - yy);
166 }
167
169 PX_CUDA_CALLABLE PX_INLINE PxMat33T(const PxMat33T& other) :
170 column0(other.column0),
171 column1(other.column1),
172 column2(other.column2)
173 {
174 }
175
177 PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33T& operator=(const PxMat33T& other)
178 {
179 column0 = other.column0;
180 column1 = other.column1;
181 column2 = other.column2;
182 return *this;
183 }
184
186 PX_CUDA_CALLABLE PX_INLINE static const PxMat33T createDiagonal(const PxVec3T<Type>& d)
187 {
188 return PxMat33T(PxVec3T<Type>(d.x, Type(0.0), Type(0.0)),
189 PxVec3T<Type>(Type(0.0), d.y, Type(0.0)),
190 PxVec3T<Type>(Type(0.0), Type(0.0), d.z));
191 }
192
194 PX_CUDA_CALLABLE PX_INLINE static const PxMat33T outer(const PxVec3T<Type>& a, const PxVec3T<Type>& b)
195 {
196 return PxMat33T(a * b.x, a * b.y, a * b.z);
197 }
198
202 PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxMat33T& m) const
203 {
204 return column0 == m.column0 && column1 == m.column1 && column2 == m.column2;
205 }
206
208 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxMat33T getTranspose() const
209 {
210 const PxVec3T<Type> v0(column0.x, column1.x, column2.x);
211 const PxVec3T<Type> v1(column0.y, column1.y, column2.y);
212 const PxVec3T<Type> v2(column0.z, column1.z, column2.z);
213
214 return PxMat33T(v0, v1, v2);
215 }
216
218 PX_CUDA_CALLABLE PX_INLINE const PxMat33T getInverse() const
219 {
220 const Type det = getDeterminant();
221 PxMat33T inverse;
222
223 if(det != Type(0.0))
224 {
225 const Type invDet = Type(1.0) / det;
226
227 inverse.column0.x = invDet * (column1.y * column2.z - column2.y * column1.z);
228 inverse.column0.y = invDet * -(column0.y * column2.z - column2.y * column0.z);
229 inverse.column0.z = invDet * (column0.y * column1.z - column0.z * column1.y);
230
231 inverse.column1.x = invDet * -(column1.x * column2.z - column1.z * column2.x);
232 inverse.column1.y = invDet * (column0.x * column2.z - column0.z * column2.x);
233 inverse.column1.z = invDet * -(column0.x * column1.z - column0.z * column1.x);
234
235 inverse.column2.x = invDet * (column1.x * column2.y - column1.y * column2.x);
236 inverse.column2.y = invDet * -(column0.x * column2.y - column0.y * column2.x);
237 inverse.column2.z = invDet * (column0.x * column1.y - column1.x * column0.y);
238
239 return inverse;
240 }
241 else
242 {
243 return PxMat33T(PxIdentity);
244 }
245 }
246
248 PX_CUDA_CALLABLE PX_INLINE Type getDeterminant() const
249 {
250 return column0.dot(column1.cross(column2));
251 }
252
254 PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator-() const
255 {
256 return PxMat33T(-column0, -column1, -column2);
257 }
258
260 PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator+(const PxMat33T& other) const
261 {
262 return PxMat33T(column0 + other.column0, column1 + other.column1, column2 + other.column2);
263 }
264
266 PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator-(const PxMat33T& other) const
267 {
268 return PxMat33T(column0 - other.column0, column1 - other.column1, column2 - other.column2);
269 }
270
272 PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator*(Type scalar) const
273 {
274 return PxMat33T(column0 * scalar, column1 * scalar, column2 * scalar);
275 }
276
277 template<class Type2>
278 PX_CUDA_CALLABLE PX_INLINE friend PxMat33T<Type2> operator*(Type2, const PxMat33T<Type2>&);
279
281 PX_CUDA_CALLABLE PX_INLINE const PxVec3T<Type> operator*(const PxVec3T<Type>& vec) const
282 {
283 return transform(vec);
284 }
285
286 // a <op>= b operators
287
289 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxMat33T operator*(const PxMat33T& other) const
290 {
291 // Rows from this <dot> columns from other
292 // column0 = transform(other.column0) etc
293 return PxMat33T(transform(other.column0),
294 transform(other.column1),
295 transform(other.column2));
296 }
297
299 PX_CUDA_CALLABLE PX_INLINE PxMat33T& operator+=(const PxMat33T& other)
300 {
301 column0 += other.column0;
302 column1 += other.column1;
303 column2 += other.column2;
304 return *this;
305 }
306
308 PX_CUDA_CALLABLE PX_INLINE PxMat33T& operator-=(const PxMat33T& other)
309 {
310 column0 -= other.column0;
311 column1 -= other.column1;
312 column2 -= other.column2;
313 return *this;
314 }
315
317 PX_CUDA_CALLABLE PX_INLINE PxMat33T& operator*=(Type scalar)
318 {
319 column0 *= scalar;
320 column1 *= scalar;
321 column2 *= scalar;
322 return *this;
323 }
324
326 PX_CUDA_CALLABLE PX_INLINE PxMat33T& operator*=(const PxMat33T& other)
327 {
328 *this = *this * other;
329 return *this;
330 }
331
333 PX_CUDA_CALLABLE PX_FORCE_INLINE Type operator()(PxU32 row, PxU32 col) const
334 {
335 return (*this)[col][row];
336 }
337
339 PX_CUDA_CALLABLE PX_FORCE_INLINE Type& operator()(PxU32 row, PxU32 col)
340 {
341 return (*this)[col][row];
342 }
343
344 // Transform etc
345
347 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3T<Type> transform(const PxVec3T<Type>& other) const
348 {
349 return column0 * other.x + column1 * other.y + column2 * other.z;
350 }
351
353 PX_CUDA_CALLABLE PX_INLINE const PxVec3T<Type> transformTranspose(const PxVec3T<Type>& other) const
354 {
355 return PxVec3T<Type>(column0.dot(other), column1.dot(other), column2.dot(other));
356 }
357
358 PX_CUDA_CALLABLE PX_FORCE_INLINE const Type* front() const
359 {
360 return &column0.x;
361 }
362
363 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T<Type>& operator[](PxU32 num)
364 {
365 return (&column0)[num];
366 }
367
368 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3T<Type>& operator[](PxU32 num) const
369 {
370 return (&column0)[num];
371 }
372
373 // Data, see above for format!
374
375 PxVec3T<Type> column0, column1, column2; // the three base vectors
376};
377
378template<class Type>
379PX_CUDA_CALLABLE PX_INLINE PxMat33T<Type> operator*(Type scalar, const PxMat33T<Type>& m)
380{
381 return PxMat33T<Type>(scalar * m.column0, scalar * m.column1, scalar * m.column2);
382}
383
384// implementation from PxQuat.h
385template<class Type>
387{
388 if(m.column2.z < Type(0))
389 {
390 if(m.column0.x > m.column1.y)
391 {
392 const Type t = Type(1.0) + m.column0.x - m.column1.y - m.column2.z;
393 *this = PxQuatT<Type>(t, m.column0.y + m.column1.x, m.column2.x + m.column0.z, m.column1.z - m.column2.y) * (Type(0.5) / PxSqrt(t));
394 }
395 else
396 {
397 const Type t = Type(1.0) - m.column0.x + m.column1.y - m.column2.z;
398 *this = PxQuatT<Type>(m.column0.y + m.column1.x, t, m.column1.z + m.column2.y, m.column2.x - m.column0.z) * (Type(0.5) / PxSqrt(t));
399 }
400 }
401 else
402 {
403 if(m.column0.x < -m.column1.y)
404 {
405 const Type t = Type(1.0) - m.column0.x - m.column1.y + m.column2.z;
406 *this = PxQuatT<Type>(m.column2.x + m.column0.z, m.column1.z + m.column2.y, t, m.column0.y - m.column1.x) * (Type(0.5) / PxSqrt(t));
407 }
408 else
409 {
410 const Type t = Type(1.0) + m.column0.x + m.column1.y + m.column2.z;
411 *this = PxQuatT<Type>(m.column1.z - m.column2.y, m.column2.x - m.column0.z, m.column0.y - m.column1.x, t) * (Type(0.5) / PxSqrt(t));
412 }
413 }
414}
415
418
419#if !PX_DOXYGEN
420} // namespace physx
421#endif
422
424#endif
425
3x3 matrix class
Definition PxMat33.h:91
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3T< Type > transform(const PxVec3T< Type > &other) const
Transform vector by matrix, equal to v' = M*v.
Definition PxMat33.h:347
PX_CUDA_CALLABLE PX_INLINE const PxVec3T< Type > transformTranspose(const PxVec3T< Type > &other) const
Transform vector by matrix transpose, v' = M^t*v.
Definition PxMat33.h:353
PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33T & operator=(const PxMat33T &other)
Assignment operator.
Definition PxMat33.h:177
PX_CUDA_CALLABLE PX_INLINE PxMat33T(Type values[])
Construct from Type[9].
Definition PxMat33.h:131
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxMat33T getTranspose() const
Get transposed matrix.
Definition PxMat33.h:208
PX_CUDA_CALLABLE PX_INLINE const PxMat33T getInverse() const
Get the real inverse.
Definition PxMat33.h:218
PX_CUDA_CALLABLE PX_INLINE PxMat33T(Type r)
constructor from a scalar, which generates a multiple of the identity matrix
Definition PxMat33.h:123
PX_CUDA_CALLABLE static PX_INLINE const PxMat33T outer(const PxVec3T< Type > &a, const PxVec3T< Type > &b)
Computes the outer product of two vectors.
Definition PxMat33.h:194
PX_CUDA_CALLABLE PX_INLINE PxMat33T & operator*=(const PxMat33T &other)
Equals matrix multiplication.
Definition PxMat33.h:326
PX_CUDA_CALLABLE PxMat33T(const PxVec3T< Type > &col0, const PxVec3T< Type > &col1, const PxVec3T< Type > &col2)
Construct from three base vectors.
Definition PxMat33.h:115
PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33T()
Default constructor.
Definition PxMat33.h:94
PX_CUDA_CALLABLE PX_INLINE PxMat33T(const PxMat33T &other)
Copy constructor.
Definition PxMat33.h:169
PX_CUDA_CALLABLE PX_INLINE PxMat33T & operator+=(const PxMat33T &other)
Equals-add.
Definition PxMat33.h:299
PX_CUDA_CALLABLE PX_INLINE const PxVec3T< Type > operator*(const PxVec3T< Type > &vec) const
Matrix vector multiplication (returns 'this->transform(vec)')
Definition PxMat33.h:281
PX_CUDA_CALLABLE PX_INLINE PxMat33T & operator*=(Type scalar)
Equals scalar multiplication.
Definition PxMat33.h:317
PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxMat33T &m) const
returns true if the two matrices are exactly equal
Definition PxMat33.h:202
PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator-() const
Unary minus.
Definition PxMat33.h:254
PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator+(const PxMat33T &other) const
Add.
Definition PxMat33.h:260
PX_CUDA_CALLABLE PX_FORCE_INLINE const PxMat33T operator*(const PxMat33T &other) const
Matrix multiplication.
Definition PxMat33.h:289
PX_CUDA_CALLABLE static PX_INLINE const PxMat33T createDiagonal(const PxVec3T< Type > &d)
Construct from diagonal, off-diagonals are zero.
Definition PxMat33.h:186
PX_CUDA_CALLABLE PX_INLINE Type getDeterminant() const
Get determinant.
Definition PxMat33.h:248
PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator-(const PxMat33T &other) const
Subtract.
Definition PxMat33.h:266
PX_CUDA_CALLABLE PX_INLINE const PxMat33T operator*(Type scalar) const
Scalar multiplication.
Definition PxMat33.h:272
PX_CUDA_CALLABLE PX_FORCE_INLINE Type operator()(PxU32 row, PxU32 col) const
Element access, mathematical way!
Definition PxMat33.h:333
PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33T(const PxQuatT< Type > &q)
Construct from a quaternion.
Definition PxMat33.h:139
PX_CUDA_CALLABLE PX_INLINE PxMat33T & operator-=(const PxMat33T &other)
Equals-sub.
Definition PxMat33.h:308
PX_CUDA_CALLABLE PX_INLINE PxMat33T(PxZERO)
zero constructor
Definition PxMat33.h:107
PX_CUDA_CALLABLE PX_INLINE PxMat33T(PxIDENTITY)
identity constructor
Definition PxMat33.h:99
PX_CUDA_CALLABLE PX_FORCE_INLINE Type & operator()(PxU32 row, PxU32 col)
Element access, mathematical way!
Definition PxMat33.h:339
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 PxQuatT()
Default constructor, does not do any initialization.
Definition PxQuat.h:55
Type x
Definition PxQuat.h:394
3 Element vector class.
Definition PxVec3.h:50
#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
PxIDENTITY
Definition Px.h:99
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition PxMath.h:146
PxZERO
Definition Px.h:93