PxMat33.h
Go to the documentation of this file.
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_MAT33_H
15 #define PX_FOUNDATION_PX_MAT33_H
16 
20 #include "foundation/PxVec3.h"
21 #include "foundation/PxQuat.h"
22 
23 #ifndef PX_DOXYGEN
24 namespace physx
25 {
26 #endif
27 
74 class PxMat33
75 {
76 public:
79  {}
80 
83  : column0(1.0f,0.0f,0.0f), column1(0.0f,1.0f,0.0f), column2(0.0f,0.0f,1.0f)
84  {
85  PX_UNUSED(r);
86  }
87 
90  : column0(0.0f), column1(0.0f), column2(0.0f)
91  {
92  PX_UNUSED(r);
93  }
94 
95 
97  PX_CUDA_CALLABLE PxMat33(const PxVec3& col0, const PxVec3& col1, const PxVec3& col2)
98  : column0(col0), column1(col1), column2(col2)
99  {}
100 
101 
104  : column0(r,0.0f,0.0f), column1(0.0f,r,0.0f), column2(0.0f,0.0f,r)
105  {}
106 
107 
110  column0(values[0],values[1],values[2]),
111  column1(values[3],values[4],values[5]),
112  column2(values[6],values[7],values[8])
113  {
114  }
115 
118  {
119  const PxReal x = q.x;
120  const PxReal y = q.y;
121  const PxReal z = q.z;
122  const PxReal w = q.w;
123 
124  const PxReal x2 = x + x;
125  const PxReal y2 = y + y;
126  const PxReal z2 = z + z;
127 
128  const PxReal xx = x2*x;
129  const PxReal yy = y2*y;
130  const PxReal zz = z2*z;
131 
132  const PxReal xy = x2*y;
133  const PxReal xz = x2*z;
134  const PxReal xw = x2*w;
135 
136  const PxReal yz = y2*z;
137  const PxReal yw = y2*w;
138  const PxReal zw = z2*w;
139 
140  column0 = PxVec3(1.0f - yy - zz, xy + zw, xz - yw);
141  column1 = PxVec3(xy - zw, 1.0f - xx - zz, yz + xw);
142  column2 = PxVec3(xz + yw, yz - xw, 1.0f - xx - yy);
143  }
144 
147  : column0(other.column0), column1(other.column1), column2(other.column2)
148  {}
149 
152  {
153  column0 = other.column0;
154  column1 = other.column1;
155  column2 = other.column2;
156  return *this;
157  }
158 
161  {
162  return PxMat33(PxIdentity);
163  }
164 
167  {
168  return PxMat33(PxZero); // PxMat33(0) is ambiguous, it can either be the array constructor or the scalar constructor
169  }
170 
172  PX_CUDA_CALLABLE PX_INLINE static PxMat33 createDiagonal(const PxVec3& d)
173  {
174  return PxMat33(PxVec3(d.x,0.0f,0.0f), PxVec3(0.0f,d.y,0.0f), PxVec3(0.0f,0.0f,d.z));
175  }
176 
180  PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxMat33& m) const { return column0 == m.column0 && column1 == m.column1 && column2 == m.column2; }
181 
182 
183 
186  {
187  const PxVec3 v0(column0.x, column1.x, column2.x);
188  const PxVec3 v1(column0.y, column1.y, column2.y);
189  const PxVec3 v2(column0.z, column1.z, column2.z);
190 
191  return PxMat33(v0,v1,v2);
192  }
193 
195  PX_CUDA_CALLABLE PX_INLINE PxMat33 getInverse() const
196  {
197  const PxReal det = getDeterminant();
198  PxMat33 inverse;
199 
200  if(det != 0)
201  {
202  const PxReal invDet = 1.0f/det;
203 
204  inverse.column0[0] = invDet * (column1[1]*column2[2] - column2[1]*column1[2]);
205  inverse.column0[1] = invDet *-(column0[1]*column2[2] - column2[1]*column0[2]);
206  inverse.column0[2] = invDet * (column0[1]*column1[2] - column0[2]*column1[1]);
207 
208  inverse.column1[0] = invDet *-(column1[0]*column2[2] - column1[2]*column2[0]);
209  inverse.column1[1] = invDet * (column0[0]*column2[2] - column0[2]*column2[0]);
210  inverse.column1[2] = invDet *-(column0[0]*column1[2] - column0[2]*column1[0]);
211 
212  inverse.column2[0] = invDet * (column1[0]*column2[1] - column1[1]*column2[0]);
213  inverse.column2[1] = invDet *-(column0[0]*column2[1] - column0[1]*column2[0]);
214  inverse.column2[2] = invDet * (column0[0]*column1[1] - column1[0]*column0[1]);
215 
216  return inverse;
217  }
218  else
219  {
220  return createIdentity();
221  }
222  }
223 
225  PX_CUDA_CALLABLE PX_INLINE PxReal getDeterminant() const
226  {
227  return column0.dot(column1.cross(column2));
228  }
229 
232  {
233  return PxMat33(-column0, -column1, -column2);
234  }
235 
238  {
239  return PxMat33( column0+other.column0,
240  column1+other.column1,
241  column2+other.column2);
242  }
243 
245  PX_CUDA_CALLABLE PX_INLINE PxMat33 operator-(const PxMat33& other) const
246  {
247  return PxMat33( column0-other.column0,
248  column1-other.column1,
249  column2-other.column2);
250  }
251 
254  {
255  return PxMat33(column0*scalar, column1*scalar, column2*scalar);
256  }
257 
258  friend PxMat33 operator*(PxReal, const PxMat33&);
259 
262  {
263  return transform(vec);
264  }
265 
266 
267  // a <op>= b operators
268 
271  {
272  //Rows from this <dot> columns from other
273  //column0 = transform(other.column0) etc
274  return PxMat33(transform(other.column0), transform(other.column1), transform(other.column2));
275  }
276 
278  PX_CUDA_CALLABLE PX_INLINE PxMat33& operator+=(const PxMat33& other)
279  {
280  column0 += other.column0;
281  column1 += other.column1;
282  column2 += other.column2;
283  return *this;
284  }
285 
287  PX_CUDA_CALLABLE PX_INLINE PxMat33& operator-=(const PxMat33& other)
288  {
289  column0 -= other.column0;
290  column1 -= other.column1;
291  column2 -= other.column2;
292  return *this;
293  }
294 
297  {
298  column0 *= scalar;
299  column1 *= scalar;
300  column2 *= scalar;
301  return *this;
302  }
303 
305  PX_CUDA_CALLABLE PX_INLINE PxMat33& operator*=(const PxMat33 &other)
306  {
307  *this = *this * other;
308  return *this;
309  }
310 
311 
313  PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal operator()(unsigned int row, unsigned int col) const
314  {
315  return (*this)[col][row];
316  }
317 
319  PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal& operator()(unsigned int row, unsigned int col)
320  {
321  return (*this)[col][row];
322  }
323 
324  // Transform etc
325 
327  PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 transform(const PxVec3& other) const
328  {
329  return column0*other.x + column1*other.y + column2*other.z;
330  }
331 
333  PX_CUDA_CALLABLE PX_INLINE PxVec3 transformTranspose(const PxVec3& other) const
334  {
335  return PxVec3( column0.dot(other),
336  column1.dot(other),
337  column2.dot(other));
338  }
339 
341  {
342  return &column0.x;
343  }
344 
345  PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator[](unsigned int num) {return (&column0)[num];}
346  PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3& operator[](unsigned int num) const {return (&column0)[num];}
347 
348  PX_DEPRECATED PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator[](int num) {return (&column0)[num];}
349  PX_DEPRECATED PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3& operator[](int num) const {return (&column0)[num];}
350 
351  //Data, see above for format!
352 
353  PxVec3 column0, column1, column2; //the three base vectors
354 };
355 
356 // implementation from PxQuat.h
358 {
359  PxReal tr = m(0,0) + m(1,1) + m(2,2), h;
360  if(tr >= 0)
361  {
362  h = PxSqrt(tr +1);
363  w = 0.5f * h;
364  h = 0.5f / h;
365 
366  x = (m(2,1) - m(1,2)) * h;
367  y = (m(0,2) - m(2,0)) * h;
368  z = (m(1,0) - m(0,1)) * h;
369  }
370  else
371  {
372  unsigned int i = 0;
373  if (m(1,1) > m(0,0))
374  i = 1;
375  if (m(2,2) > m(i,i))
376  i = 2;
377  switch (i)
378  {
379  case 0:
380  h = PxSqrt((m(0,0) - (m(1,1) + m(2,2))) + 1);
381  x = 0.5f * h;
382  h = 0.5f / h;
383 
384  y = (m(0,1) + m(1,0)) * h;
385  z = (m(2,0) + m(0,2)) * h;
386  w = (m(2,1) - m(1,2)) * h;
387  break;
388  case 1:
389  h = PxSqrt((m(1,1) - (m(2,2) + m(0,0))) + 1);
390  y = 0.5f * h;
391  h = 0.5f / h;
392 
393  z = (m(1,2) + m(2,1)) * h;
394  x = (m(0,1) + m(1,0)) * h;
395  w = (m(0,2) - m(2,0)) * h;
396  break;
397  case 2:
398  h = PxSqrt((m(2,2) - (m(0,0) + m(1,1))) + 1);
399  z = 0.5f * h;
400  h = 0.5f / h;
401 
402  x = (m(2,0) + m(0,2)) * h;
403  y = (m(1,2) + m(2,1)) * h;
404  w = (m(1,0) - m(0,1)) * h;
405  break;
406  default: // Make compiler happy
407  x = y = z = w = 0;
408  break;
409  }
410  }
411 }
412 
413 #ifndef PX_DOXYGEN
414 } // namespace physx
415 #endif
416 
418 #endif // PX_FOUNDATION_PX_MAT33_H


Copyright © 2008-2015 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com