pxCasts.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 _PHYSX_CASTS_H_
  23. #define _PHYSX_CASTS_H_
  24. #ifndef _MPOINT3_H_
  25. #include "math/mPoint3.h"
  26. #endif
  27. #ifndef _MBOX_H_
  28. #include "math/mBox.h"
  29. #endif
  30. #ifndef _MQUAT_H_
  31. #include "math/mQuat.h"
  32. #endif
  33. template <class T, class F> inline T pxCast( const F &from );
  34. //-------------------------------------------------------------------------
  35. template<>
  36. inline Point3F pxCast( const NxVec3 &vec )
  37. {
  38. return Point3F( vec.x, vec.y, vec.z );
  39. }
  40. template<>
  41. inline NxVec3 pxCast( const Point3F &point )
  42. {
  43. return NxVec3( point.x, point.y, point.z );
  44. }
  45. //-------------------------------------------------------------------------
  46. template<>
  47. inline QuatF pxCast( const NxQuat &quat )
  48. {
  49. /// The Torque quat has the opposite winding order.
  50. return QuatF( -quat.x, -quat.y, -quat.z, quat.w );
  51. }
  52. template<>
  53. inline NxQuat pxCast( const QuatF &quat )
  54. {
  55. /// The Torque quat has the opposite winding order.
  56. NxQuat result;
  57. result.setWXYZ( quat.w, -quat.x, -quat.y, -quat.z );
  58. return result;
  59. }
  60. //-------------------------------------------------------------------------
  61. template<>
  62. inline NxBounds3 pxCast( const Box3F &box )
  63. {
  64. NxBounds3 bounds;
  65. bounds.set( box.minExtents.x,
  66. box.minExtents.y,
  67. box.minExtents.z,
  68. box.maxExtents.x,
  69. box.maxExtents.y,
  70. box.maxExtents.z );
  71. return bounds;
  72. }
  73. template<>
  74. inline Box3F pxCast( const NxBounds3 &bounds )
  75. {
  76. return Box3F( bounds.min.x,
  77. bounds.min.y,
  78. bounds.min.z,
  79. bounds.max.x,
  80. bounds.max.y,
  81. bounds.max.z );
  82. }
  83. //-------------------------------------------------------------------------
  84. template<>
  85. inline NxVec3 pxCast( const NxExtendedVec3 &xvec )
  86. {
  87. return NxVec3( xvec.x, xvec.y, xvec.z );
  88. }
  89. template<>
  90. inline NxExtendedVec3 pxCast( const NxVec3 &vec )
  91. {
  92. return NxExtendedVec3( vec.x, vec.y, vec.z );
  93. }
  94. //-------------------------------------------------------------------------
  95. template<>
  96. inline NxExtendedVec3 pxCast( const Point3F &point )
  97. {
  98. return NxExtendedVec3( point.x, point.y, point.z );
  99. }
  100. template<>
  101. inline Point3F pxCast( const NxExtendedVec3 &xvec )
  102. {
  103. return Point3F( xvec.x, xvec.y, xvec.z );
  104. }
  105. //-------------------------------------------------------------------------
  106. template<>
  107. inline NxBox pxCast( const NxExtendedBounds3 &exBounds )
  108. {
  109. NxExtendedVec3 center;
  110. exBounds.getCenter( center );
  111. NxVec3 extents;
  112. exBounds.getExtents( extents );
  113. NxBox box;
  114. box.center.set( center.x, center.y, center.z );
  115. box.extents = extents;
  116. box.rot.id();
  117. return box;
  118. }
  119. template<>
  120. inline NxExtendedBounds3 pxCast( const NxBox &box )
  121. {
  122. AssertFatal( false, "Casting a NxBox to NxExtendedBounds3 is impossible without losing rotation data!" );
  123. return NxExtendedBounds3();
  124. }
  125. #endif // _PHYSX_CASTS_H_