PxMat33.h
Go to the documentation of this file.00001 // This code contains NVIDIA Confidential Information and is disclosed to you 00002 // under a form of NVIDIA software license agreement provided separately to you. 00003 // 00004 // Notice 00005 // NVIDIA Corporation and its licensors retain all intellectual property and 00006 // proprietary rights in and to this software and related documentation and 00007 // any modifications thereto. Any use, reproduction, disclosure, or 00008 // distribution of this software and related documentation without an express 00009 // license agreement from NVIDIA Corporation is strictly prohibited. 00010 // 00011 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES 00012 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO 00013 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT, 00014 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. 00015 // 00016 // Information and code furnished is believed to be accurate and reliable. 00017 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such 00018 // information or for any infringement of patents or other rights of third parties that may 00019 // result from its use. No license is granted by implication or otherwise under any patent 00020 // or patent rights of NVIDIA Corporation. Details are subject to change without notice. 00021 // This code supersedes and replaces all information previously supplied. 00022 // NVIDIA Corporation products are not authorized for use as critical 00023 // components in life support devices or systems without express written approval of 00024 // NVIDIA Corporation. 00025 // 00026 // Copyright (c) 2008-2012 NVIDIA Corporation. All rights reserved. 00027 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. 00028 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. 00029 00030 00031 #ifndef PX_FOUNDATION_PX_MAT33_H 00032 #define PX_FOUNDATION_PX_MAT33_H 00033 00037 #include "foundation/PxVec3.h" 00038 #include "foundation/PxQuat.h" 00039 00040 #ifndef PX_DOXYGEN 00041 namespace physx 00042 { 00043 #endif 00044 00091 class PxMat33 00092 { 00093 public: 00095 PX_CUDA_CALLABLE PX_INLINE PxMat33() 00096 {} 00097 00099 PX_CUDA_CALLABLE PxMat33(const PxVec3& col0, const PxVec3& col1, const PxVec3& col2) 00100 : column0(col0), column1(col1), column2(col2) 00101 {} 00102 00104 PX_CUDA_CALLABLE explicit PX_INLINE PxMat33(PxReal values[]): 00105 column0(values[0],values[1],values[2]), 00106 column1(values[3],values[4],values[5]), 00107 column2(values[6],values[7],values[8]) 00108 { 00109 } 00110 00112 PX_CUDA_CALLABLE explicit PX_FORCE_INLINE PxMat33(const PxQuat& q) 00113 { 00114 const PxReal x = q.x; 00115 const PxReal y = q.y; 00116 const PxReal z = q.z; 00117 const PxReal w = q.w; 00118 00119 const PxReal x2 = x + x; 00120 const PxReal y2 = y + y; 00121 const PxReal z2 = z + z; 00122 00123 const PxReal xx = x2*x; 00124 const PxReal yy = y2*y; 00125 const PxReal zz = z2*z; 00126 00127 const PxReal xy = x2*y; 00128 const PxReal xz = x2*z; 00129 const PxReal xw = x2*w; 00130 00131 const PxReal yz = y2*z; 00132 const PxReal yw = y2*w; 00133 const PxReal zw = z2*w; 00134 00135 column0 = PxVec3(1.0f - yy - zz, xy + zw, xz - yw); 00136 column1 = PxVec3(xy - zw, 1.0f - xx - zz, yz + xw); 00137 column2 = PxVec3(xz + yw, yz - xw, 1.0f - xx - yy); 00138 } 00139 00141 PX_CUDA_CALLABLE PX_INLINE PxMat33(const PxMat33& other) 00142 : column0(other.column0), column1(other.column1), column2(other.column2) 00143 {} 00144 00146 PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33& operator=(const PxMat33& other) 00147 { 00148 column0 = other.column0; 00149 column1 = other.column1; 00150 column2 = other.column2; 00151 return *this; 00152 } 00153 00155 PX_CUDA_CALLABLE PX_INLINE static PxMat33 createIdentity() 00156 { 00157 return PxMat33(PxVec3(1,0,0), PxVec3(0,1,0), PxVec3(0,0,1)); 00158 } 00159 00161 PX_CUDA_CALLABLE PX_INLINE static PxMat33 createZero() 00162 { 00163 return PxMat33(PxVec3(0.0f), PxVec3(0.0f), PxVec3(0.0f)); 00164 } 00165 00167 PX_CUDA_CALLABLE PX_INLINE static PxMat33 createDiagonal(const PxVec3& d) 00168 { 00169 return PxMat33(PxVec3(d.x,0.0f,0.0f), PxVec3(0.0f,d.y,0.0f), PxVec3(0.0f,0.0f,d.z)); 00170 } 00171 00172 00174 PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33 getTranspose() const 00175 { 00176 const PxVec3 v0(column0.x, column1.x, column2.x); 00177 const PxVec3 v1(column0.y, column1.y, column2.y); 00178 const PxVec3 v2(column0.z, column1.z, column2.z); 00179 00180 return PxMat33(v0,v1,v2); 00181 } 00182 00184 PX_CUDA_CALLABLE PX_INLINE PxMat33 getInverse() const 00185 { 00186 const PxReal det = getDeterminant(); 00187 PxMat33 inverse; 00188 00189 if(det != 0) 00190 { 00191 const PxReal invDet = 1.0f/det; 00192 00193 inverse.column0[0] = invDet * (column1[1]*column2[2] - column2[1]*column1[2]); 00194 inverse.column0[1] = invDet *-(column0[1]*column2[2] - column2[1]*column0[2]); 00195 inverse.column0[2] = invDet * (column0[1]*column1[2] - column0[2]*column1[1]); 00196 00197 inverse.column1[0] = invDet *-(column1[0]*column2[2] - column1[2]*column2[0]); 00198 inverse.column1[1] = invDet * (column0[0]*column2[2] - column0[2]*column2[0]); 00199 inverse.column1[2] = invDet *-(column0[0]*column1[2] - column0[2]*column1[0]); 00200 00201 inverse.column2[0] = invDet * (column1[0]*column2[1] - column1[1]*column2[0]); 00202 inverse.column2[1] = invDet *-(column0[0]*column2[1] - column0[1]*column2[0]); 00203 inverse.column2[2] = invDet * (column0[0]*column1[1] - column1[0]*column0[1]); 00204 00205 return inverse; 00206 } 00207 else 00208 { 00209 return createIdentity(); 00210 } 00211 } 00212 00214 PX_CUDA_CALLABLE PX_INLINE PxReal getDeterminant() const 00215 { 00216 return column0.dot(column1.cross(column2)); 00217 } 00218 00220 PX_CUDA_CALLABLE PX_INLINE PxMat33 operator-() const 00221 { 00222 return PxMat33(-column0, -column1, -column2); 00223 } 00224 00226 PX_CUDA_CALLABLE PX_INLINE PxMat33 operator+(const PxMat33& other) const 00227 { 00228 return PxMat33( column0+other.column0, 00229 column1+other.column1, 00230 column2+other.column2); 00231 } 00232 00234 PX_CUDA_CALLABLE PX_INLINE PxMat33 operator-(const PxMat33& other) const 00235 { 00236 return PxMat33( column0-other.column0, 00237 column1-other.column1, 00238 column2-other.column2); 00239 } 00240 00242 PX_CUDA_CALLABLE PX_INLINE PxMat33 operator*(PxReal scalar) const 00243 { 00244 return PxMat33(column0*scalar, column1*scalar, column2*scalar); 00245 } 00246 00247 friend PxMat33 operator*(PxReal, const PxMat33&); 00248 00250 PX_CUDA_CALLABLE PX_INLINE PxVec3 operator*(const PxVec3& vec) const 00251 { 00252 return transform(vec); 00253 } 00254 00256 PX_CUDA_CALLABLE PX_FORCE_INLINE PxMat33 operator*(const PxMat33& other) const 00257 { 00258 //Rows from this <dot> columns from other 00259 //column0 = transform(other.column0) etc 00260 return PxMat33(transform(other.column0), transform(other.column1), transform(other.column2)); 00261 } 00262 00263 // a <op>= b operators 00264 00266 PX_CUDA_CALLABLE PX_INLINE PxMat33& operator+=(const PxMat33& other) 00267 { 00268 column0 += other.column0; 00269 column1 += other.column1; 00270 column2 += other.column2; 00271 return *this; 00272 } 00273 00275 PX_CUDA_CALLABLE PX_INLINE PxMat33& operator-=(const PxMat33& other) 00276 { 00277 column0 -= other.column0; 00278 column1 -= other.column1; 00279 column2 -= other.column2; 00280 return *this; 00281 } 00282 00284 PX_CUDA_CALLABLE PX_INLINE PxMat33& operator*=(PxReal scalar) 00285 { 00286 column0 *= scalar; 00287 column1 *= scalar; 00288 column2 *= scalar; 00289 return *this; 00290 } 00291 00293 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal operator()(unsigned int row, unsigned int col) const 00294 { 00295 return (*this)[col][row]; 00296 } 00297 00299 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal& operator()(unsigned int row, unsigned int col) 00300 { 00301 return (*this)[col][row]; 00302 } 00303 00304 // Transform etc 00305 00307 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 transform(const PxVec3& other) const 00308 { 00309 return column0*other.x + column1*other.y + column2*other.z; 00310 } 00311 00313 PX_CUDA_CALLABLE PX_INLINE PxVec3 transformTranspose(const PxVec3& other) const 00314 { 00315 return PxVec3( column0.dot(other), 00316 column1.dot(other), 00317 column2.dot(other)); 00318 } 00319 00320 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxReal* front() const 00321 { 00322 return &column0.x; 00323 } 00324 00325 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator[](int num) {return (&column0)[num];} 00326 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxVec3& operator[](int num) const {return (&column0)[num];} 00327 00328 //Data, see above for format! 00329 00330 PxVec3 column0, column1, column2; //the three base vectors 00331 }; 00332 00333 // implementation from PxQuat.h 00334 PX_CUDA_CALLABLE PX_INLINE PxQuat::PxQuat(const PxMat33& m) 00335 { 00336 PxReal tr = m(0,0) + m(1,1) + m(2,2), h; 00337 if(tr >= 0) 00338 { 00339 h = PxSqrt(tr +1); 00340 w = PxReal(0.5) * h; 00341 h = PxReal(0.5) / h; 00342 00343 x = (m(2,1) - m(1,2)) * h; 00344 y = (m(0,2) - m(2,0)) * h; 00345 z = (m(1,0) - m(0,1)) * h; 00346 } 00347 else 00348 { 00349 int i = 0; 00350 if (m(1,1) > m(0,0)) 00351 i = 1; 00352 if (m(2,2) > m(i,i)) 00353 i = 2; 00354 switch (i) 00355 { 00356 case 0: 00357 h = PxSqrt((m(0,0) - (m(1,1) + m(2,2))) + 1); 00358 x = PxReal(0.5) * h; 00359 h = PxReal(0.5) / h; 00360 00361 y = (m(0,1) + m(1,0)) * h; 00362 z = (m(2,0) + m(0,2)) * h; 00363 w = (m(2,1) - m(1,2)) * h; 00364 break; 00365 case 1: 00366 h = PxSqrt((m(1,1) - (m(2,2) + m(0,0))) + 1); 00367 y = PxReal(0.5) * h; 00368 h = PxReal(0.5) / h; 00369 00370 z = (m(1,2) + m(2,1)) * h; 00371 x = (m(0,1) + m(1,0)) * h; 00372 w = (m(0,2) - m(2,0)) * h; 00373 break; 00374 case 2: 00375 h = PxSqrt((m(2,2) - (m(0,0) + m(1,1))) + 1); 00376 z = PxReal(0.5) * h; 00377 h = PxReal(0.5) / h; 00378 00379 x = (m(2,0) + m(0,2)) * h; 00380 y = (m(1,2) + m(2,1)) * h; 00381 w = (m(1,0) - m(0,1)) * h; 00382 break; 00383 default: // Make compiler happy 00384 x = y = z = w = 0; 00385 break; 00386 } 00387 } 00388 } 00389 00390 #ifndef PX_DOXYGEN 00391 } // namespace physx 00392 #endif 00393 00395 #endif // PX_FOUNDATION_PX_MAT33_H
Copyright © 2008-2012 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com