btTransform.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #ifndef BT_TRANSFORM_H
  13. #define BT_TRANSFORM_H
  14. #include "btMatrix3x3.h"
  15. #ifdef BT_USE_DOUBLE_PRECISION
  16. #define btTransformData btTransformDoubleData
  17. #else
  18. #define btTransformData btTransformFloatData
  19. #endif
  20. /**@brief The btTransform class supports rigid transforms with only translation and rotation and no scaling/shear.
  21. *It can be used in combination with btVector3, btQuaternion and btMatrix3x3 linear algebra classes. */
  22. ATTRIBUTE_ALIGNED16(class) btTransform {
  23. ///Storage for the rotation
  24. btMatrix3x3 m_basis;
  25. ///Storage for the translation
  26. btVector3 m_origin;
  27. public:
  28. /**@brief No initialization constructor */
  29. btTransform() {}
  30. /**@brief Constructor from btQuaternion (optional btVector3 )
  31. * @param q Rotation from quaternion
  32. * @param c Translation from Vector (default 0,0,0) */
  33. explicit SIMD_FORCE_INLINE btTransform(const btQuaternion& q,
  34. const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0)))
  35. : m_basis(q),
  36. m_origin(c)
  37. {}
  38. /**@brief Constructor from btMatrix3x3 (optional btVector3)
  39. * @param b Rotation from Matrix
  40. * @param c Translation from Vector default (0,0,0)*/
  41. explicit SIMD_FORCE_INLINE btTransform(const btMatrix3x3& b,
  42. const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0)))
  43. : m_basis(b),
  44. m_origin(c)
  45. {}
  46. /**@brief Copy constructor */
  47. SIMD_FORCE_INLINE btTransform (const btTransform& other)
  48. : m_basis(other.m_basis),
  49. m_origin(other.m_origin)
  50. {
  51. }
  52. /**@brief Assignment Operator */
  53. SIMD_FORCE_INLINE btTransform& operator=(const btTransform& other)
  54. {
  55. m_basis = other.m_basis;
  56. m_origin = other.m_origin;
  57. return *this;
  58. }
  59. /**@brief Set the current transform as the value of the product of two transforms
  60. * @param t1 Transform 1
  61. * @param t2 Transform 2
  62. * This = Transform1 * Transform2 */
  63. SIMD_FORCE_INLINE void mult(const btTransform& t1, const btTransform& t2) {
  64. m_basis = t1.m_basis * t2.m_basis;
  65. m_origin = t1(t2.m_origin);
  66. }
  67. /* void multInverseLeft(const btTransform& t1, const btTransform& t2) {
  68. btVector3 v = t2.m_origin - t1.m_origin;
  69. m_basis = btMultTransposeLeft(t1.m_basis, t2.m_basis);
  70. m_origin = v * t1.m_basis;
  71. }
  72. */
  73. /**@brief Return the transform of the vector */
  74. SIMD_FORCE_INLINE btVector3 operator()(const btVector3& x) const
  75. {
  76. return x.dot3(m_basis[0], m_basis[1], m_basis[2]) + m_origin;
  77. }
  78. /**@brief Return the transform of the vector */
  79. SIMD_FORCE_INLINE btVector3 operator*(const btVector3& x) const
  80. {
  81. return (*this)(x);
  82. }
  83. /**@brief Return the transform of the btQuaternion */
  84. SIMD_FORCE_INLINE btQuaternion operator*(const btQuaternion& q) const
  85. {
  86. return getRotation() * q;
  87. }
  88. /**@brief Return the basis matrix for the rotation */
  89. SIMD_FORCE_INLINE btMatrix3x3& getBasis() { return m_basis; }
  90. /**@brief Return the basis matrix for the rotation */
  91. SIMD_FORCE_INLINE const btMatrix3x3& getBasis() const { return m_basis; }
  92. /**@brief Return the origin vector translation */
  93. SIMD_FORCE_INLINE btVector3& getOrigin() { return m_origin; }
  94. /**@brief Return the origin vector translation */
  95. SIMD_FORCE_INLINE const btVector3& getOrigin() const { return m_origin; }
  96. /**@brief Return a quaternion representing the rotation */
  97. btQuaternion getRotation() const {
  98. btQuaternion q;
  99. m_basis.getRotation(q);
  100. return q;
  101. }
  102. /**@brief Set from an array
  103. * @param m A pointer to a 15 element array (12 rotation(row major padded on the right by 1), and 3 translation */
  104. void setFromOpenGLMatrix(const btScalar *m)
  105. {
  106. m_basis.setFromOpenGLSubMatrix(m);
  107. m_origin.setValue(m[12],m[13],m[14]);
  108. }
  109. /**@brief Fill an array representation
  110. * @param m A pointer to a 15 element array (12 rotation(row major padded on the right by 1), and 3 translation */
  111. void getOpenGLMatrix(btScalar *m) const
  112. {
  113. m_basis.getOpenGLSubMatrix(m);
  114. m[12] = m_origin.x();
  115. m[13] = m_origin.y();
  116. m[14] = m_origin.z();
  117. m[15] = btScalar(1.0);
  118. }
  119. /**@brief Set the translational element
  120. * @param origin The vector to set the translation to */
  121. SIMD_FORCE_INLINE void setOrigin(const btVector3& origin)
  122. {
  123. m_origin = origin;
  124. }
  125. SIMD_FORCE_INLINE btVector3 invXform(const btVector3& inVec) const;
  126. /**@brief Set the rotational element by btMatrix3x3 */
  127. SIMD_FORCE_INLINE void setBasis(const btMatrix3x3& basis)
  128. {
  129. m_basis = basis;
  130. }
  131. /**@brief Set the rotational element by btQuaternion */
  132. SIMD_FORCE_INLINE void setRotation(const btQuaternion& q)
  133. {
  134. m_basis.setRotation(q);
  135. }
  136. /**@brief Set this transformation to the identity */
  137. void setIdentity()
  138. {
  139. m_basis.setIdentity();
  140. m_origin.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
  141. }
  142. /**@brief Multiply this Transform by another(this = this * another)
  143. * @param t The other transform */
  144. btTransform& operator*=(const btTransform& t)
  145. {
  146. m_origin += m_basis * t.m_origin;
  147. m_basis *= t.m_basis;
  148. return *this;
  149. }
  150. /**@brief Return the inverse of this transform */
  151. btTransform inverse() const
  152. {
  153. btMatrix3x3 inv = m_basis.transpose();
  154. return btTransform(inv, inv * -m_origin);
  155. }
  156. /**@brief Return the inverse of this transform times the other transform
  157. * @param t The other transform
  158. * return this.inverse() * the other */
  159. btTransform inverseTimes(const btTransform& t) const;
  160. /**@brief Return the product of this transform and the other */
  161. btTransform operator*(const btTransform& t) const;
  162. /**@brief Return an identity transform */
  163. static const btTransform& getIdentity()
  164. {
  165. static const btTransform identityTransform(btMatrix3x3::getIdentity());
  166. return identityTransform;
  167. }
  168. void serialize(struct btTransformData& dataOut) const;
  169. void serializeFloat(struct btTransformFloatData& dataOut) const;
  170. void deSerialize(const struct btTransformData& dataIn);
  171. void deSerializeDouble(const struct btTransformDoubleData& dataIn);
  172. void deSerializeFloat(const struct btTransformFloatData& dataIn);
  173. };
  174. SIMD_FORCE_INLINE btVector3
  175. btTransform::invXform(const btVector3& inVec) const
  176. {
  177. btVector3 v = inVec - m_origin;
  178. return (m_basis.transpose() * v);
  179. }
  180. SIMD_FORCE_INLINE btTransform
  181. btTransform::inverseTimes(const btTransform& t) const
  182. {
  183. btVector3 v = t.getOrigin() - m_origin;
  184. return btTransform(m_basis.transposeTimes(t.m_basis),
  185. v * m_basis);
  186. }
  187. SIMD_FORCE_INLINE btTransform
  188. btTransform::operator*(const btTransform& t) const
  189. {
  190. return btTransform(m_basis * t.m_basis,
  191. (*this)(t.m_origin));
  192. }
  193. /**@brief Test if two transforms have all elements equal */
  194. SIMD_FORCE_INLINE bool operator==(const btTransform& t1, const btTransform& t2)
  195. {
  196. return ( t1.getBasis() == t2.getBasis() &&
  197. t1.getOrigin() == t2.getOrigin() );
  198. }
  199. ///for serialization
  200. struct btTransformFloatData
  201. {
  202. btMatrix3x3FloatData m_basis;
  203. btVector3FloatData m_origin;
  204. };
  205. struct btTransformDoubleData
  206. {
  207. btMatrix3x3DoubleData m_basis;
  208. btVector3DoubleData m_origin;
  209. };
  210. SIMD_FORCE_INLINE void btTransform::serialize(btTransformData& dataOut) const
  211. {
  212. m_basis.serialize(dataOut.m_basis);
  213. m_origin.serialize(dataOut.m_origin);
  214. }
  215. SIMD_FORCE_INLINE void btTransform::serializeFloat(btTransformFloatData& dataOut) const
  216. {
  217. m_basis.serializeFloat(dataOut.m_basis);
  218. m_origin.serializeFloat(dataOut.m_origin);
  219. }
  220. SIMD_FORCE_INLINE void btTransform::deSerialize(const btTransformData& dataIn)
  221. {
  222. m_basis.deSerialize(dataIn.m_basis);
  223. m_origin.deSerialize(dataIn.m_origin);
  224. }
  225. SIMD_FORCE_INLINE void btTransform::deSerializeFloat(const btTransformFloatData& dataIn)
  226. {
  227. m_basis.deSerializeFloat(dataIn.m_basis);
  228. m_origin.deSerializeFloat(dataIn.m_origin);
  229. }
  230. SIMD_FORCE_INLINE void btTransform::deSerializeDouble(const btTransformDoubleData& dataIn)
  231. {
  232. m_basis.deSerializeDouble(dataIn.m_basis);
  233. m_origin.deSerializeDouble(dataIn.m_origin);
  234. }
  235. #endif //BT_TRANSFORM_H