btTransform.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans https://bulletphysics.org
  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)
  23. btTransform
  24. {
  25. ///Storage for the rotation
  26. btMatrix3x3 m_basis;
  27. ///Storage for the translation
  28. btVector3 m_origin;
  29. public:
  30. BT_DECLARE_ALIGNED_ALLOCATOR();
  31. /**@brief No initialization constructor */
  32. btTransform() {}
  33. /**@brief Constructor from btQuaternion (optional btVector3 )
  34. * @param q Rotation from quaternion
  35. * @param c Translation from Vector (default 0,0,0) */
  36. explicit SIMD_FORCE_INLINE btTransform(const btQuaternion& q,
  37. const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0)))
  38. : m_basis(q),
  39. m_origin(c)
  40. {
  41. }
  42. /**@brief Constructor from btMatrix3x3 (optional btVector3)
  43. * @param b Rotation from Matrix
  44. * @param c Translation from Vector default (0,0,0)*/
  45. explicit SIMD_FORCE_INLINE btTransform(const btMatrix3x3& b,
  46. const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0)))
  47. : m_basis(b),
  48. m_origin(c)
  49. {
  50. }
  51. /**@brief Copy constructor */
  52. SIMD_FORCE_INLINE btTransform(const btTransform& other)
  53. : m_basis(other.m_basis),
  54. m_origin(other.m_origin)
  55. {
  56. }
  57. /**@brief Assignment Operator */
  58. SIMD_FORCE_INLINE btTransform& operator=(const btTransform& other)
  59. {
  60. m_basis = other.m_basis;
  61. m_origin = other.m_origin;
  62. return *this;
  63. }
  64. /**@brief Set the current transform as the value of the product of two transforms
  65. * @param t1 Transform 1
  66. * @param t2 Transform 2
  67. * This = Transform1 * Transform2 */
  68. SIMD_FORCE_INLINE void mult(const btTransform& t1, const btTransform& t2)
  69. {
  70. m_basis = t1.m_basis * t2.m_basis;
  71. m_origin = t1(t2.m_origin);
  72. }
  73. /* void multInverseLeft(const btTransform& t1, const btTransform& t2) {
  74. btVector3 v = t2.m_origin - t1.m_origin;
  75. m_basis = btMultTransposeLeft(t1.m_basis, t2.m_basis);
  76. m_origin = v * t1.m_basis;
  77. }
  78. */
  79. /**@brief Return the transform of the vector */
  80. SIMD_FORCE_INLINE btVector3 operator()(const btVector3& x) const
  81. {
  82. return x.dot3(m_basis[0], m_basis[1], m_basis[2]) + m_origin;
  83. }
  84. /**@brief Return the transform of the vector */
  85. SIMD_FORCE_INLINE btVector3 operator*(const btVector3& x) const
  86. {
  87. return (*this)(x);
  88. }
  89. /**@brief Return the transform of the btQuaternion */
  90. SIMD_FORCE_INLINE btQuaternion operator*(const btQuaternion& q) const
  91. {
  92. return getRotation() * q;
  93. }
  94. /**@brief Return the basis matrix for the rotation */
  95. SIMD_FORCE_INLINE btMatrix3x3& getBasis() { return m_basis; }
  96. /**@brief Return the basis matrix for the rotation */
  97. SIMD_FORCE_INLINE const btMatrix3x3& getBasis() const { return m_basis; }
  98. /**@brief Return the origin vector translation */
  99. SIMD_FORCE_INLINE btVector3& getOrigin() { return m_origin; }
  100. /**@brief Return the origin vector translation */
  101. SIMD_FORCE_INLINE const btVector3& getOrigin() const { return m_origin; }
  102. /**@brief Return a quaternion representing the rotation */
  103. btQuaternion getRotation() const
  104. {
  105. btQuaternion q;
  106. m_basis.getRotation(q);
  107. return q;
  108. }
  109. /**@brief Set from an array
  110. * @param m A pointer to a 16 element array (12 rotation(row major padded on the right by 1), and 3 translation */
  111. void setFromOpenGLMatrix(const btScalar* m)
  112. {
  113. m_basis.setFromOpenGLSubMatrix(m);
  114. m_origin.setValue(m[12], m[13], m[14]);
  115. }
  116. /**@brief Fill an array representation
  117. * @param m A pointer to a 16 element array (12 rotation(row major padded on the right by 1), and 3 translation */
  118. void getOpenGLMatrix(btScalar * m) const
  119. {
  120. m_basis.getOpenGLSubMatrix(m);
  121. m[12] = m_origin.x();
  122. m[13] = m_origin.y();
  123. m[14] = m_origin.z();
  124. m[15] = btScalar(1.0);
  125. }
  126. /**@brief Set the translational element
  127. * @param origin The vector to set the translation to */
  128. SIMD_FORCE_INLINE void setOrigin(const btVector3& origin)
  129. {
  130. m_origin = origin;
  131. }
  132. SIMD_FORCE_INLINE btVector3 invXform(const btVector3& inVec) const;
  133. /**@brief Set the rotational element by btMatrix3x3 */
  134. SIMD_FORCE_INLINE void setBasis(const btMatrix3x3& basis)
  135. {
  136. m_basis = basis;
  137. }
  138. /**@brief Set the rotational element by btQuaternion */
  139. SIMD_FORCE_INLINE void setRotation(const btQuaternion& q)
  140. {
  141. m_basis.setRotation(q);
  142. }
  143. /**@brief Set this transformation to the identity */
  144. void setIdentity()
  145. {
  146. m_basis.setIdentity();
  147. m_origin.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
  148. }
  149. /**@brief Multiply this Transform by another(this = this * another)
  150. * @param t The other transform */
  151. btTransform& operator*=(const btTransform& t)
  152. {
  153. m_origin += m_basis * t.m_origin;
  154. m_basis *= t.m_basis;
  155. return *this;
  156. }
  157. /**@brief Return the inverse of this transform */
  158. btTransform inverse() const
  159. {
  160. btMatrix3x3 inv = m_basis.transpose();
  161. return btTransform(inv, inv * -m_origin);
  162. }
  163. /**@brief Return the inverse of this transform times the other transform
  164. * @param t The other transform
  165. * return this.inverse() * the other */
  166. btTransform inverseTimes(const btTransform& t) const;
  167. /**@brief Return the product of this transform and the other */
  168. btTransform operator*(const btTransform& t) const;
  169. /**@brief Return an identity transform */
  170. static const btTransform& getIdentity()
  171. {
  172. static const btTransform identityTransform(btMatrix3x3::getIdentity());
  173. return identityTransform;
  174. }
  175. void serialize(struct btTransformData & dataOut) const;
  176. void serializeFloat(struct btTransformFloatData & dataOut) const;
  177. void deSerialize(const struct btTransformData& dataIn);
  178. void deSerializeDouble(const struct btTransformDoubleData& dataIn);
  179. void deSerializeFloat(const struct btTransformFloatData& dataIn);
  180. };
  181. SIMD_FORCE_INLINE btVector3
  182. btTransform::invXform(const btVector3& inVec) const
  183. {
  184. btVector3 v = inVec - m_origin;
  185. return (m_basis.transpose() * v);
  186. }
  187. SIMD_FORCE_INLINE btTransform
  188. btTransform::inverseTimes(const btTransform& t) const
  189. {
  190. btVector3 v = t.getOrigin() - m_origin;
  191. return btTransform(m_basis.transposeTimes(t.m_basis),
  192. v * m_basis);
  193. }
  194. SIMD_FORCE_INLINE btTransform
  195. btTransform::operator*(const btTransform& t) const
  196. {
  197. return btTransform(m_basis * t.m_basis,
  198. (*this)(t.m_origin));
  199. }
  200. /**@brief Test if two transforms have all elements equal */
  201. SIMD_FORCE_INLINE bool operator==(const btTransform& t1, const btTransform& t2)
  202. {
  203. return (t1.getBasis() == t2.getBasis() &&
  204. t1.getOrigin() == t2.getOrigin());
  205. }
  206. ///for serialization
  207. struct btTransformFloatData
  208. {
  209. btMatrix3x3FloatData m_basis;
  210. btVector3FloatData m_origin;
  211. };
  212. struct btTransformDoubleData
  213. {
  214. btMatrix3x3DoubleData m_basis;
  215. btVector3DoubleData m_origin;
  216. };
  217. SIMD_FORCE_INLINE void btTransform::serialize(btTransformData& dataOut) const
  218. {
  219. m_basis.serialize(dataOut.m_basis);
  220. m_origin.serialize(dataOut.m_origin);
  221. }
  222. SIMD_FORCE_INLINE void btTransform::serializeFloat(btTransformFloatData& dataOut) const
  223. {
  224. m_basis.serializeFloat(dataOut.m_basis);
  225. m_origin.serializeFloat(dataOut.m_origin);
  226. }
  227. SIMD_FORCE_INLINE void btTransform::deSerialize(const btTransformData& dataIn)
  228. {
  229. m_basis.deSerialize(dataIn.m_basis);
  230. m_origin.deSerialize(dataIn.m_origin);
  231. }
  232. SIMD_FORCE_INLINE void btTransform::deSerializeFloat(const btTransformFloatData& dataIn)
  233. {
  234. m_basis.deSerializeFloat(dataIn.m_basis);
  235. m_origin.deSerializeFloat(dataIn.m_origin);
  236. }
  237. SIMD_FORCE_INLINE void btTransform::deSerializeDouble(const btTransformDoubleData& dataIn)
  238. {
  239. m_basis.deSerializeDouble(dataIn.m_basis);
  240. m_origin.deSerializeDouble(dataIn.m_origin);
  241. }
  242. #endif //BT_TRANSFORM_H