1/*
2 * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 */
10// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12
13
14#ifndef PX_FOUNDATION_PX_MAT44_H
15#define PX_FOUNDATION_PX_MAT44_H
16/** \addtogroup foundation
17@{
18*/
19
20#include "foundation/PxQuat.h"
21#include "foundation/PxVec4.h"
22#include "foundation/PxMat33.h"
23#include "foundation/PxTransform.h"
24
25#ifndef PX_DOXYGEN
26namespace physx
27{
28#endif
29
30/*!
31\brief 4x4 matrix class
32
33This class is layout-compatible with D3D and OpenGL matrices. More notes on layout are given in the PxMat33
34
35@see PxMat33 PxTransform
36*/
37
38class PxMat44
39{
40public:
41 //! Default constructor
42 PX_CUDA_CALLABLE PX_INLINE PxMat44()
43 {}
44
45 //! identity constructor
46 PX_CUDA_CALLABLE PX_INLINE PxMat44(PxIDENTITY r)
47 : column0(1.0f,0.0f,0.0f,0.0f), column1(0.0f,1.0f,0.0f,0.0f), column2(0.0f,0.0f,1.0f,0.0f), column3(0.0f,0.0f,0.0f,1.0f)
48 {
49 PX_UNUSED(r);
50 }
51
52 //! zero constructor
53 PX_CUDA_CALLABLE PX_INLINE PxMat44(PxZERO r)
54 : column0(PxZero), column1(PxZero), column2(PxZero), column3(PxZero)
55 {
56 PX_UNUSED(r);
57 }
58
59 //! Construct from four 4-vectors
60 PX_CUDA_CALLABLE PxMat44(const PxVec4& col0, const PxVec4& col1, const PxVec4& col2, const PxVec4 &col3)
61 : column0(col0), column1(col1), column2(col2), column3(col3)
62 {}
63
64 //! constructor that generates a multiple of the identity matrix
65 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44(PxReal r)
66 : column0(r,0.0f,0.0f,0.0f), column1(0.0f,r,0.0f,0.0f), column2(0.0f,0.0f,r,0.0f), column3(0.0f,0.0f,0.0f,r)
67 {}
68
69
70 //! Construct from three base vectors and a translation
71 PX_CUDA_CALLABLE PxMat44(const PxVec3& col0, const PxVec3& col1, const PxVec3& col2, const PxVec3& col3)
72 : column0(col0,0), column1(col1,0), column2(col2,0), column3(col3,1.0f)
73 {}
74
75 //! Construct from float[16]
76 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44(PxReal values[]):
77 column0(values[0],values[1],values[2], values[3]),
78 column1(values[4],values[5],values[6], values[7]),
79 column2(values[8],values[9],values[10], values[11]),
80 column3(values[12], values[13], values[14], values[15])
81 {
82 }
83
84 //! Construct from a quaternion
85 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44(const PxQuat& q)
86 {
87 const PxReal x = q.x;
88 const PxReal y = q.y;
89 const PxReal z = q.z;
90 const PxReal w = q.w;
91
92 const PxReal x2 = x + x;
93 const PxReal y2 = y + y;
94 const PxReal z2 = z + z;
95
96 const PxReal xx = x2*x;
97 const PxReal yy = y2*y;
98 const PxReal zz = z2*z;
99
100 const PxReal xy = x2*y;
101 const PxReal xz = x2*z;
102 const PxReal xw = x2*w;
103
104 const PxReal yz = y2*z;
105 const PxReal yw = y2*w;
106 const PxReal zw = z2*w;
107
108 column0 = PxVec4(1.0f - yy - zz, xy + zw, xz - yw, 0.0f);
109 column1 = PxVec4(xy - zw, 1.0f - xx - zz, yz + xw, 0.0f);
110 column2 = PxVec4(xz + yw, yz - xw, 1.0f - xx - yy, 0.0f);
111 column3 = PxVec4(0.0f, 0.0f, 0.0f, 1.0f);
112 }
113
114 //! Construct from a diagonal vector
115 explicit PX_CUDA_CALLABLE PX_INLINE PxMat44(const PxVec4& diagonal):
116 column0(diagonal.x,0.0f,0.0f,0.0f),
117 column1(0.0f,diagonal.y,0.0f,0.0f),
118 column2(0.0f,0.0f,diagonal.z,0.0f),
119 column3(0.0f,0.0f,0.0f,diagonal.w)
120 {
121 }
122
123 PX_CUDA_CALLABLE PxMat44(const PxMat33& orientation, const PxVec3& position):
124 column0(orientation.column0,0.0f),
125 column1(orientation.column1,0.0f),
126 column2(orientation.column2,0.0f),
127 column3(position,1.0f)
128 {
129 }
130
131 PX_CUDA_CALLABLE PxMat44(const PxTransform& t)
132 {
133 *this = PxMat44(PxMat33(t.q), t.p);
134 }
135
136
137 /**
138 \brief returns true if the two matrices are exactly equal
139 */
140 PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxMat44& m) const { return column0 == m.column0 && column1 == m.column1 && column2 == m.column2 && column3 == m.column3; }
141
142 //! Copy constructor
143 PX_CUDA_CALLABLE PX_INLINE PxMat44(const PxMat44& other)
144 : column0(other.column0), column1(other.column1), column2(other.column2), column3(other.column3)
145 {}
146
147 //! Assignment operator
148 PX_CUDA_CALLABLE PX_INLINE const PxMat44& operator=(const PxMat44& other)
149 {
150 column0 = other.column0;
151 column1 = other.column1;
152 column2 = other.column2;
153 column3 = other.column3;
154 return *this;
155 }
156
157 //! \deprecated Set to identity matrix. Deprecated. use PxMat44(PxIdentity).
158 PX_DEPRECATED PX_CUDA_CALLABLE PX_INLINE static PxMat44 createIdentity()
159 {
160 return PxMat44(PxIdentity);
161 }
162
163
164 //! \deprecated Set to zero matrix. Deprecated. use PxMat44(PxZero).
165 PX_DEPRECATED PX_CUDA_CALLABLE PX_INLINE static PxMat44 createZero()
166 {
167 return PxMat44(PxZero);
168 }
169
170 //! Get transposed matrix
171 PX_CUDA_CALLABLE PX_INLINE PxMat44 getTranspose() const
172 {
173 return PxMat44(PxVec4(column0.x, column1.x, column2.x, column3.x),
174 PxVec4(column0.y, column1.y, column2.y, column3.y),
175 PxVec4(column0.z, column1.z, column2.z, column3.z),
176 PxVec4(column0.w, column1.w, column2.w, column3.w));
177 }
178
179 //! Unary minus
180 PX_CUDA_CALLABLE PX_INLINE PxMat44 operator-() const
181 {
182 return PxMat44(-column0, -column1, -column2, -column3);
183 }
184
185 //! Add
186 PX_CUDA_CALLABLE PX_INLINE PxMat44 operator+(const PxMat44& other) const
187 {
188 return PxMat44( column0+other.column0,
189 column1+other.column1,
190 column2+other.column2,
191 column3+other.column3);
192 }
193
194 //! Subtract
195 PX_CUDA_CALLABLE PX_INLINE PxMat44 operator-(const PxMat44& other) const
196 {
197 return PxMat44( column0-other.column0,
198 column1-other.column1,
199 column2-other.column2,
200 column3-other.column3);
201 }
202
203 //! Scalar multiplication
204 PX_CUDA_CALLABLE PX_INLINE PxMat44 operator*(PxReal scalar) const
205 {
206 return PxMat44(column0*scalar, column1*scalar, column2*scalar, column3*scalar);
207 }
208
209 friend PxMat44 operator*(PxReal, const PxMat44&);
210
211 //! Matrix multiplication
212 PX_CUDA_CALLABLE PX_INLINE PxMat44 operator*(const PxMat44& other) const
213 {
214 //Rows from this <dot> columns from other
215 //column0 = transform(other.column0) etc
216 return PxMat44(transform(other.column0), transform(other.column1), transform(other.column2), transform(other.column3));
217 }
218
219 // a <op>= b operators
220
221 //! Equals-add
222 PX_CUDA_CALLABLE PX_INLINE PxMat44& operator+=(const PxMat44& other)
223 {
224 column0 += other.column0;
225 column1 += other.column1;
226 column2 += other.column2;
227 column3 += other.column3;
228 return *this;
229 }
230
231 //! Equals-sub
232 PX_CUDA_CALLABLE PX_INLINE PxMat44& operator-=(const PxMat44& other)
233 {
234 column0 -= other.column0;
235 column1 -= other.column1;
236 column2 -= other.column2;
237 column3 -= other.column3;
238 return *this;
239 }
240
241 //! Equals scalar multiplication
242 PX_CUDA_CALLABLE PX_INLINE PxMat44& operator*=(PxReal scalar)
243 {
244 column0 *= scalar;
245 column1 *= scalar;
246 column2 *= scalar;
247 column3 *= scalar;
248 return *this;
249 }
250
251 //! Equals matrix multiplication
252 PX_CUDA_CALLABLE PX_INLINE PxMat44& operator*=(const PxMat44 &other)
253 {
254 *this = *this * other;
255 return *this;
256 }
257
258
259 //! Element access, mathematical way!
260 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal operator()(unsigned int row, unsigned int col) const
261 {
262 return (*this)[col][row];
263 }
264
265 //! Element access, mathematical way!
266 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal& operator()(unsigned int row, unsigned int col)
267 {
268 return (*this)[col][row];
269 }
270
271 //! Transform vector by matrix, equal to v' = M*v
272 PX_CUDA_CALLABLE PX_INLINE PxVec4 transform(const PxVec4& other) const
273 {
274 return column0*other.x + column1*other.y + column2*other.z + column3*other.w;
275 }
276
277 //! Transform vector by matrix, equal to v' = M*v
278 PX_CUDA_CALLABLE PX_INLINE PxVec3 transform(const PxVec3& other) const
279 {
280 return transform(PxVec4(other,1.0f)).getXYZ();
281 }
282
283 //! Rotate vector by matrix, equal to v' = M*v
284 PX_CUDA_CALLABLE PX_INLINE const PxVec4 rotate(const PxVec4& other) const
285 {
286 return column0*other.x + column1*other.y + column2*other.z;// + column3*0;
287 }
288
289 //! Rotate vector by matrix, equal to v' = M*v
290 PX_CUDA_CALLABLE PX_INLINE const PxVec3 rotate(const PxVec3& other) const
291 {
292 return rotate(PxVec4(other,1.0f)).getXYZ();
293 }
294
295
296 PX_CUDA_CALLABLE PX_INLINE PxVec3 getBasis(int num) const
297 {
298 PX_ASSERT(num>=0 && num<3);
299 return (&column0)[num].getXYZ();
300 }
301
302 PX_CUDA_CALLABLE PX_INLINE PxVec3 getPosition() const
303 {
304 return column3.getXYZ();
305 }
306
307 PX_CUDA_CALLABLE PX_INLINE void setPosition(const PxVec3& position)
308 {
309 column3.x = position.x;
310 column3.y = position.y;
311 column3.z = position.z;
312 }
313
314 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxReal* front() const
315 {
316 return &column0.x;
317 }
318
319 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec4& operator[](unsigned int num) {return (&column0)[num];}
320 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec4& operator[](unsigned int num) const {return (&column0)[num];}
321
322 PX_DEPRECATED PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec4& operator[](int num) {return (&column0)[num];}
323 PX_DEPRECATED PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec4& operator[](int num) const {return (&column0)[num];}
324
325 PX_CUDA_CALLABLE PX_INLINE void scale(const PxVec4& p)
326 {
327 column0 *= p.x;
328 column1 *= p.y;
329 column2 *= p.z;
330 column3 *= p.w;
331 }
332
333 PX_CUDA_CALLABLE PX_INLINE PxMat44 inverseRT(void) const
334 {
335 PxVec3 r0(column0.x, column1.x, column2.x),
336 r1(column0.y, column1.y, column2.y),
337 r2(column0.z, column1.z, column2.z);
338
339 return PxMat44(r0, r1, r2, -(r0 * column3.x + r1 * column3.y + r2 * column3.z));
340 }
341
342 PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
343 {
344 return column0.isFinite() && column1.isFinite() && column2.isFinite() && column3.isFinite();
345 }
346
347
348 //Data, see above for format!
349
350 PxVec4 column0, column1, column2, column3; //the four base vectors
351};
352
353// implementation from PxTransform.h
354PX_CUDA_CALLABLE PX_FORCE_INLINE PxTransform::PxTransform(const PxMat44& m)
355{
356 PxVec3 column0 = PxVec3(m.column0.x, m.column0.y, m.column0.z);
357 PxVec3 column1 = PxVec3(m.column1.x, m.column1.y, m.column1.z);
358 PxVec3 column2 = PxVec3(m.column2.x, m.column2.y, m.column2.z);
359
360 q = PxQuat(PxMat33(column0, column1, column2));
361 p = PxVec3(m.column3.x, m.column3.y, m.column3.z);
362}
363
364#ifndef PX_DOXYGEN
365} // namespace physx
366#endif
367
368/** @} */
369#endif // PX_FOUNDATION_PX_MAT44_H
370