px3Casts.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PX3CASTS_H_
  23. #define _PX3CASTS_H_
  24. #ifndef _MPOINT3_H_
  25. #include "math/mPoint3.h"
  26. #endif
  27. #ifndef _MMATRIX_H_
  28. #include "math/mMatrix.h"
  29. #endif
  30. #ifndef _MBOX_H_
  31. #include "math/mBox.h"
  32. #endif
  33. #ifndef _MQUAT_H_
  34. #include "math/mQuat.h"
  35. #endif
  36. #ifndef _MTRANSFORM_H_
  37. #include "math/mTransform.h"
  38. #endif
  39. template <class T, class F> inline T px3Cast( const F &from );
  40. //-------------------------------------------------------------------------
  41. template<>
  42. inline Point3F px3Cast( const physx::PxVec3 &vec )
  43. {
  44. return Point3F( vec.x, vec.y, vec.z );
  45. }
  46. template<>
  47. inline physx::PxVec3 px3Cast( const Point3F &point )
  48. {
  49. return physx::PxVec3( point.x, point.y, point.z );
  50. }
  51. //-------------------------------------------------------------------------
  52. template<>
  53. inline QuatF px3Cast( const physx::PxQuat &quat )
  54. {
  55. /// The Torque quat has the opposite winding order.
  56. return QuatF( -quat.x, -quat.y, -quat.z, quat.w );
  57. }
  58. template<>
  59. inline physx::PxQuat px3Cast( const QuatF &quat )
  60. {
  61. /// The Torque quat has the opposite winding order.
  62. physx::PxQuat result( -quat.x, -quat.y, -quat.z, quat.w );
  63. return result;
  64. }
  65. //-------------------------------------------------------------------------
  66. template<>
  67. inline physx::PxExtendedVec3 px3Cast( const Point3F &point )
  68. {
  69. return physx::PxExtendedVec3( point.x, point.y, point.z );
  70. }
  71. template<>
  72. inline Point3F px3Cast( const physx::PxExtendedVec3 &xvec )
  73. {
  74. return Point3F( xvec.x, xvec.y, xvec.z );
  75. }
  76. //-------------------------------------------------------------------------
  77. template<>
  78. inline physx::PxBounds3 px3Cast( const Box3F &box )
  79. {
  80. physx::PxBounds3 bounds(px3Cast<physx::PxVec3>(box.minExtents),
  81. px3Cast<physx::PxVec3>(box.maxExtents));
  82. return bounds;
  83. }
  84. template<>
  85. inline Box3F px3Cast( const physx::PxBounds3 &bounds )
  86. {
  87. return Box3F( bounds.minimum.x,
  88. bounds.minimum.y,
  89. bounds.minimum.z,
  90. bounds.maximum.x,
  91. bounds.maximum.y,
  92. bounds.maximum.z );
  93. }
  94. //-------------------------------------------------------------------------
  95. template<>
  96. inline physx::PxTransform px3Cast( const MatrixF &xfm )
  97. {
  98. physx::PxTransform out;
  99. QuatF q;
  100. q.set(xfm);
  101. out.q = px3Cast<physx::PxQuat>(q);
  102. out.p = px3Cast<physx::PxVec3>(xfm.getPosition());
  103. return out;
  104. }
  105. template<>
  106. inline TransformF px3Cast(const physx::PxTransform &xfm)
  107. {
  108. TransformF out(px3Cast<Point3F>(xfm.p),AngAxisF(px3Cast<QuatF>(xfm.q)));
  109. return out;
  110. }
  111. template<>
  112. inline MatrixF px3Cast( const physx::PxTransform &xfm )
  113. {
  114. MatrixF out;
  115. TransformF t = px3Cast<TransformF>(xfm);
  116. out = t.getMatrix();
  117. return out;
  118. }
  119. #endif //_PX3CASTS_H_