b3Transform.h 8.4 KB

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