RavEngine
Loading...
Searching...
No Matches
PxMat44.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_MAT44_H
30#define PX_MAT44_H
35#include "foundation/PxQuat.h"
36#include "foundation/PxVec4.h"
37#include "foundation/PxMat33.h"
38#include "foundation/PxTransform.h"
39
40#if !PX_DOXYGEN
41namespace physx
42{
43#endif
44
53template<class Type>
55{
56 public:
58 PX_CUDA_CALLABLE PX_INLINE PxMat44T()
59 {
60 }
61
63 PX_CUDA_CALLABLE PX_INLINE PxMat44T(PxIDENTITY) :
64 column0(Type(1.0), Type(0.0), Type(0.0), Type(0.0)),
65 column1(Type(0.0), Type(1.0), Type(0.0), Type(0.0)),
66 column2(Type(0.0), Type(0.0), Type(1.0), Type(0.0)),
67 column3(Type(0.0), Type(0.0), Type(0.0), Type(1.0))
68 {
69 }
70
72 PX_CUDA_CALLABLE PX_INLINE PxMat44T(PxZERO) : column0(PxZero), column1(PxZero), column2(PxZero), column3(PxZero)
73 {
74 }
75
77 PX_CUDA_CALLABLE PxMat44T(const PxVec4T<Type>& col0, const PxVec4T<Type>& col1, const PxVec4T<Type>& col2, const PxVec4T<Type>& col3) :
78 column0(col0),
79 column1(col1),
80 column2(col2),
81 column3(col3)
82 {
83 }
84
86 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44T(Type r) :
87 column0(r, Type(0.0), Type(0.0), Type(0.0)),
88 column1(Type(0.0), r, Type(0.0), Type(0.0)),
89 column2(Type(0.0), Type(0.0), r, Type(0.0)),
90 column3(Type(0.0), Type(0.0), Type(0.0), r)
91 {
92 }
93
95 PX_CUDA_CALLABLE PxMat44T(const PxVec3T<Type>& col0, const PxVec3T<Type>& col1, const PxVec3T<Type>& col2, const PxVec3T<Type>& col3) :
96 column0(col0, Type(0.0)),
97 column1(col1, Type(0.0)),
98 column2(col2, Type(0.0)),
99 column3(col3, Type(1.0))
100 {
101 }
102
104 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44T(Type values[]) :
105 column0(values[0], values[1], values[2], values[3]),
106 column1(values[4], values[5], values[6], values[7]),
107 column2(values[8], values[9], values[10], values[11]),
108 column3(values[12], values[13], values[14], values[15])
109 {
110 }
111
113 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44T(const PxQuatT<Type>& q)
114 {
115 // PT: TODO: PX-566
116 const Type x = q.x;
117 const Type y = q.y;
118 const Type z = q.z;
119 const Type w = q.w;
120
121 const Type x2 = x + x;
122 const Type y2 = y + y;
123 const Type z2 = z + z;
124
125 const Type xx = x2 * x;
126 const Type yy = y2 * y;
127 const Type zz = z2 * z;
128
129 const Type xy = x2 * y;
130 const Type xz = x2 * z;
131 const Type xw = x2 * w;
132
133 const Type yz = y2 * z;
134 const Type yw = y2 * w;
135 const Type zw = z2 * w;
136
137 column0 = PxVec4T<Type>(Type(1.0) - yy - zz, xy + zw, xz - yw, Type(0.0));
138 column1 = PxVec4T<Type>(xy - zw, Type(1.0) - xx - zz, yz + xw, Type(0.0));
139 column2 = PxVec4T<Type>(xz + yw, yz - xw, Type(1.0) - xx - yy, Type(0.0));
140 column3 = PxVec4T<Type>(Type(0.0), Type(0.0), Type(0.0), Type(1.0));
141 }
142
144 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44T(const PxVec4T<Type>& diagonal) :
145 column0(diagonal.x, Type(0.0), Type(0.0), Type(0.0)),
146 column1(Type(0.0), diagonal.y, Type(0.0), Type(0.0)),
147 column2(Type(0.0), Type(0.0), diagonal.z, Type(0.0)),
148 column3(Type(0.0), Type(0.0), Type(0.0), diagonal.w)
149 {
150 }
151
153 PX_CUDA_CALLABLE PxMat44T(const PxMat33T<Type>& axes, const PxVec3T<Type>& position) :
154 column0(axes.column0, Type(0.0)),
155 column1(axes.column1, Type(0.0)),
156 column2(axes.column2, Type(0.0)),
157 column3(position, Type(1.0))
158 {
159 }
160
161 PX_CUDA_CALLABLE PxMat44T(const PxTransform& t)
162 {
163 *this = PxMat44T(PxMat33T<Type>(t.q), t.p);
164 }
165
169 PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxMat44T& m) const
170 {
171 return column0 == m.column0 && column1 == m.column1 && column2 == m.column2 && column3 == m.column3;
172 }
173
175 PX_CUDA_CALLABLE PX_INLINE PxMat44T(const PxMat44T& other) :
176 column0(other.column0),
177 column1(other.column1),
178 column2(other.column2),
179 column3(other.column3)
180 {
181 }
182
184 PX_CUDA_CALLABLE PX_INLINE PxMat44T& operator=(const PxMat44T& other)
185 {
186 column0 = other.column0;
187 column1 = other.column1;
188 column2 = other.column2;
189 column3 = other.column3;
190 return *this;
191 }
192
194 PX_CUDA_CALLABLE PX_INLINE const PxMat44T getTranspose() const
195 {
196 return PxMat44T(
197 PxVec4T<Type>(column0.x, column1.x, column2.x, column3.x), PxVec4T<Type>(column0.y, column1.y, column2.y, column3.y),
198 PxVec4T<Type>(column0.z, column1.z, column2.z, column3.z), PxVec4T<Type>(column0.w, column1.w, column2.w, column3.w));
199 }
200
202 PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator-() const
203 {
204 return PxMat44T(-column0, -column1, -column2, -column3);
205 }
206
208 PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator+(const PxMat44T& other) const
209 {
210 return PxMat44T(column0 + other.column0, column1 + other.column1, column2 + other.column2, column3 + other.column3);
211 }
212
214 PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator-(const PxMat44T& other) const
215 {
216 return PxMat44T(column0 - other.column0, column1 - other.column1, column2 - other.column2, column3 - other.column3);
217 }
218
220 PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator*(Type scalar) const
221 {
222 return PxMat44T(column0 * scalar, column1 * scalar, column2 * scalar, column3 * scalar);
223 }
224
225 template<class Type2>
226 friend PxMat44T<Type2> operator*(Type2, const PxMat44T<Type2>&);
227
229 PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator*(const PxMat44T& other) const
230 {
231 // Rows from this <dot> columns from other
232 // column0 = transform(other.column0) etc
233 return PxMat44T(transform(other.column0), transform(other.column1), transform(other.column2), transform(other.column3));
234 }
235
236 // a <op>= b operators
237
239 PX_CUDA_CALLABLE PX_INLINE PxMat44T& operator+=(const PxMat44T& other)
240 {
241 column0 += other.column0;
242 column1 += other.column1;
243 column2 += other.column2;
244 column3 += other.column3;
245 return *this;
246 }
247
249 PX_CUDA_CALLABLE PX_INLINE PxMat44T& operator-=(const PxMat44T& other)
250 {
251 column0 -= other.column0;
252 column1 -= other.column1;
253 column2 -= other.column2;
254 column3 -= other.column3;
255 return *this;
256 }
257
259 PX_CUDA_CALLABLE PX_INLINE PxMat44T& operator*=(Type scalar)
260 {
261 column0 *= scalar;
262 column1 *= scalar;
263 column2 *= scalar;
264 column3 *= scalar;
265 return *this;
266 }
267
269 PX_CUDA_CALLABLE PX_INLINE PxMat44T& operator*=(const PxMat44T& other)
270 {
271 *this = *this * other;
272 return *this;
273 }
274
276 PX_CUDA_CALLABLE PX_FORCE_INLINE Type operator()(PxU32 row, PxU32 col) const
277 {
278 return (*this)[col][row];
279 }
280
282 PX_CUDA_CALLABLE PX_FORCE_INLINE Type& operator()(PxU32 row, PxU32 col)
283 {
284 return (*this)[col][row];
285 }
286
288 PX_CUDA_CALLABLE PX_INLINE const PxVec4T<Type> transform(const PxVec4T<Type>& other) const
289 {
290 return column0 * other.x + column1 * other.y + column2 * other.z + column3 * other.w;
291 }
292
294 PX_CUDA_CALLABLE PX_INLINE const PxVec3T<Type> transform(const PxVec3T<Type>& other) const
295 {
296 return transform(PxVec4T<Type>(other, Type(1.0))).getXYZ();
297 }
298
300 PX_CUDA_CALLABLE PX_INLINE const PxVec4T<Type> rotate(const PxVec4T<Type>& other) const
301 {
302 return column0 * other.x + column1 * other.y + column2 * other.z; // + column3*0;
303 }
304
306 PX_CUDA_CALLABLE PX_INLINE const PxVec3T<Type> rotate(const PxVec3T<Type>& other) const
307 {
308 return rotate(PxVec4T<Type>(other, Type(1.0))).getXYZ();
309 }
310
311 PX_CUDA_CALLABLE PX_INLINE const PxVec3T<Type> getBasis(PxU32 num) const
312 {
313 PX_ASSERT(num < 3);
314 return (&column0)[num].getXYZ();
315 }
316
317 PX_CUDA_CALLABLE PX_INLINE const PxVec3T<Type> getPosition() const
318 {
319 return column3.getXYZ();
320 }
321
322 PX_CUDA_CALLABLE PX_INLINE void setPosition(const PxVec3T<Type>& position)
323 {
324 column3.x = position.x;
325 column3.y = position.y;
326 column3.z = position.z;
327 }
328
329 PX_CUDA_CALLABLE PX_FORCE_INLINE const Type* front() const
330 {
331 return &column0.x;
332 }
333
334 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec4T<Type>& operator[](PxU32 num)
335 {
336 return (&column0)[num];
337 }
338 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec4T<Type>& operator[](PxU32 num) const
339 {
340 return (&column0)[num];
341 }
342
343 PX_CUDA_CALLABLE PX_INLINE void scale(const PxVec4T<Type>& p)
344 {
345 column0 *= p.x;
346 column1 *= p.y;
347 column2 *= p.z;
348 column3 *= p.w;
349 }
350
351 PX_CUDA_CALLABLE PX_INLINE const PxMat44T inverseRT(void) const
352 {
353 const PxVec3T<Type> r0(column0.x, column1.x, column2.x);
354 const PxVec3T<Type> r1(column0.y, column1.y, column2.y);
355 const PxVec3T<Type> r2(column0.z, column1.z, column2.z);
356
357 return PxMat44T(r0, r1, r2, -(r0 * column3.x + r1 * column3.y + r2 * column3.z));
358 }
359
360 PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
361 {
362 return column0.isFinite() && column1.isFinite() && column2.isFinite() && column3.isFinite();
363 }
364
365 // Data, see above for format!
366
367 PxVec4T<Type> column0, column1, column2, column3; // the four base vectors
368};
369
370// implementation from PxTransform.h
371template<class Type>
372PX_CUDA_CALLABLE PX_FORCE_INLINE PxTransformT<Type>::PxTransformT(const PxMat44T<Type>& m)
373{
374 const PxVec3T<Type> column0(m.column0.x, m.column0.y, m.column0.z);
375 const PxVec3T<Type> column1(m.column1.x, m.column1.y, m.column1.z);
376 const PxVec3T<Type> column2(m.column2.x, m.column2.y, m.column2.z);
377
378 q = PxQuatT<Type>(PxMat33T<Type>(column0, column1, column2));
379 p = PxVec3T<Type>(m.column3.x, m.column3.y, m.column3.z);
380}
381
382typedef PxMat44T<float> PxMat44;
383typedef PxMat44T<double> PxMat44d;
384
385#if !PX_DOXYGEN
386} // namespace physx
387#endif
388
390#endif
391
3x3 matrix class
Definition PxMat33.h:91
4x4 matrix class
Definition PxMat44.h:55
PX_CUDA_CALLABLE PX_INLINE const PxVec3T< Type > transform(const PxVec3T< Type > &other) const
Transform vector by matrix, equal to v' = M*v.
Definition PxMat44.h:294
PX_CUDA_CALLABLE PX_INLINE PxMat44T(const PxQuatT< Type > &q)
Construct from a quaternion.
Definition PxMat44.h:113
PX_CUDA_CALLABLE PX_INLINE PxMat44T(const PxVec4T< Type > &diagonal)
Construct from a diagonal vector.
Definition PxMat44.h:144
PX_CUDA_CALLABLE PX_INLINE PxMat44T(Type r)
constructor that generates a multiple of the identity matrix
Definition PxMat44.h:86
PX_CUDA_CALLABLE PxMat44T(const PxVec3T< Type > &col0, const PxVec3T< Type > &col1, const PxVec3T< Type > &col2, const PxVec3T< Type > &col3)
Construct from three base vectors and a translation.
Definition PxMat44.h:95
PX_CUDA_CALLABLE PX_INLINE PxMat44T(const PxMat44T &other)
Copy constructor.
Definition PxMat44.h:175
PX_CUDA_CALLABLE PX_INLINE const PxVec4T< Type > rotate(const PxVec4T< Type > &other) const
Rotate vector by matrix, equal to v' = M*v.
Definition PxMat44.h:300
PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxMat44T &m) const
returns true if the two matrices are exactly equal
Definition PxMat44.h:169
PX_CUDA_CALLABLE PX_INLINE const PxMat44T getTranspose() const
Get transposed matrix.
Definition PxMat44.h:194
PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator*(Type scalar) const
Scalar multiplication.
Definition PxMat44.h:220
PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator+(const PxMat44T &other) const
Add.
Definition PxMat44.h:208
PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator-(const PxMat44T &other) const
Subtract.
Definition PxMat44.h:214
PX_CUDA_CALLABLE PX_FORCE_INLINE Type & operator()(PxU32 row, PxU32 col)
Element access, mathematical way!
Definition PxMat44.h:282
PX_CUDA_CALLABLE PX_INLINE PxMat44T & operator-=(const PxMat44T &other)
Equals-sub.
Definition PxMat44.h:249
PX_CUDA_CALLABLE PX_INLINE PxMat44T & operator+=(const PxMat44T &other)
Equals-add.
Definition PxMat44.h:239
PX_CUDA_CALLABLE PxMat44T(const PxMat33T< Type > &axes, const PxVec3T< Type > &position)
Construct from Mat33 and a translation.
Definition PxMat44.h:153
PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator-() const
Unary minus.
Definition PxMat44.h:202
PX_CUDA_CALLABLE PX_INLINE PxMat44T(Type values[])
Construct from Type[16].
Definition PxMat44.h:104
PX_CUDA_CALLABLE PX_FORCE_INLINE Type operator()(PxU32 row, PxU32 col) const
Element access, mathematical way!
Definition PxMat44.h:276
PX_CUDA_CALLABLE PX_INLINE const PxMat44T operator*(const PxMat44T &other) const
Matrix multiplication.
Definition PxMat44.h:229
PX_CUDA_CALLABLE PX_INLINE PxMat44T & operator=(const PxMat44T &other)
Assignment operator.
Definition PxMat44.h:184
PX_CUDA_CALLABLE PX_INLINE PxMat44T & operator*=(Type scalar)
Equals scalar multiplication.
Definition PxMat44.h:259
PX_CUDA_CALLABLE PX_INLINE PxMat44T(PxZERO)
zero constructor
Definition PxMat44.h:72
PX_CUDA_CALLABLE PX_INLINE const PxVec3T< Type > rotate(const PxVec3T< Type > &other) const
Rotate vector by matrix, equal to v' = M*v.
Definition PxMat44.h:306
PX_CUDA_CALLABLE PX_INLINE const PxVec4T< Type > transform(const PxVec4T< Type > &other) const
Transform vector by matrix, equal to v' = M*v.
Definition PxMat44.h:288
PX_CUDA_CALLABLE PX_INLINE PxMat44T(PxIDENTITY)
identity constructor
Definition PxMat44.h:63
PX_CUDA_CALLABLE PxMat44T(const PxVec4T< Type > &col0, const PxVec4T< Type > &col1, const PxVec4T< Type > &col2, const PxVec4T< Type > &col3)
Construct from four 4-vectors.
Definition PxMat44.h:77
PX_CUDA_CALLABLE PX_INLINE PxMat44T & operator*=(const PxMat44T &other)
Equals matrix multiplication.
Definition PxMat44.h:269
PX_CUDA_CALLABLE PX_INLINE PxMat44T()
Default constructor.
Definition PxMat44.h:58
This is a quaternion class. For more information on quaternion mathematics consult a mathematics sour...
Definition PxQuat.h:49
Type x
Definition PxQuat.h:394
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
3 Element vector class.
Definition PxVec3.h:50
Definition PxVec4.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
PxZERO
Definition Px.h:93