2 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans https://bulletphysics.org
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
15 #ifndef BT_TRANSFORM_H
16 #define BT_TRANSFORM_H
18 #include "btMatrix3x3.h"
20 #ifdef BT_USE_DOUBLE_PRECISION
21 #define btTransformData btTransformDoubleData
23 #define btTransformData btTransformFloatData
26 /**@brief The btTransform class supports rigid transforms with only translation and rotation and no scaling/shear.
27 *It can be used in combination with btVector3, btQuaternion and btMatrix3x3 linear algebra classes. */
28 ATTRIBUTE_ALIGNED16(class)
31 ///Storage for the rotation
33 ///Storage for the translation
37 BT_DECLARE_ALIGNED_ALLOCATOR();
38 /**@brief No initialization constructor */
40 /**@brief Constructor from btQuaternion (optional btVector3 )
41 * @param q Rotation from quaternion
42 * @param c Translation from Vector (default 0,0,0) */
43 explicit SIMD_FORCE_INLINE btTransform(const btQuaternion& q,
44 const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0)))
50 /**@brief Constructor from btMatrix3x3 (optional btVector3)
51 * @param b Rotation from Matrix
52 * @param c Translation from Vector default (0,0,0)*/
53 explicit SIMD_FORCE_INLINE btTransform(const btMatrix3x3& b,
54 const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0)))
59 /**@brief Copy constructor */
60 SIMD_FORCE_INLINE btTransform(const btTransform& other)
61 : m_basis(other.m_basis),
62 m_origin(other.m_origin)
65 /**@brief Assignment Operator */
66 SIMD_FORCE_INLINE btTransform& operator=(const btTransform& other)
68 m_basis = other.m_basis;
69 m_origin = other.m_origin;
73 /**@brief Set the current transform as the value of the product of two transforms
74 * @param t1 Transform 1
75 * @param t2 Transform 2
76 * This = Transform1 * Transform2 */
77 SIMD_FORCE_INLINE void mult(const btTransform& t1, const btTransform& t2)
79 m_basis = t1.m_basis * t2.m_basis;
80 m_origin = t1(t2.m_origin);
83 /* void multInverseLeft(const btTransform& t1, const btTransform& t2) {
84 btVector3 v = t2.m_origin - t1.m_origin;
85 m_basis = btMultTransposeLeft(t1.m_basis, t2.m_basis);
86 m_origin = v * t1.m_basis;
90 /**@brief Return the transform of the vector */
91 SIMD_FORCE_INLINE btVector3 operator()(const btVector3& x) const
93 return x.dot3(m_basis[0], m_basis[1], m_basis[2]) + m_origin;
96 /**@brief Return the transform of the vector */
97 SIMD_FORCE_INLINE btVector3 operator*(const btVector3& x) const
102 /**@brief Return the transform of the btQuaternion */
103 SIMD_FORCE_INLINE btQuaternion operator*(const btQuaternion& q) const
105 return getRotation() * q;
108 /**@brief Return the basis matrix for the rotation */
109 SIMD_FORCE_INLINE btMatrix3x3& getBasis() { return m_basis; }
110 /**@brief Return the basis matrix for the rotation */
111 SIMD_FORCE_INLINE const btMatrix3x3& getBasis() const { return m_basis; }
113 /**@brief Return the origin vector translation */
114 SIMD_FORCE_INLINE btVector3& getOrigin() { return m_origin; }
115 /**@brief Return the origin vector translation */
116 SIMD_FORCE_INLINE const btVector3& getOrigin() const { return m_origin; }
118 /**@brief Return a quaternion representing the rotation */
119 btQuaternion getRotation() const
122 m_basis.getRotation(q);
126 /**@brief Set from an array
127 * @param m A pointer to a 16 element array (12 rotation(row major padded on the right by 1), and 3 translation */
128 void setFromOpenGLMatrix(const btScalar* m)
130 m_basis.setFromOpenGLSubMatrix(m);
131 m_origin.setValue(m[12], m[13], m[14]);
134 /**@brief Fill an array representation
135 * @param m A pointer to a 16 element array (12 rotation(row major padded on the right by 1), and 3 translation */
136 void getOpenGLMatrix(btScalar * m) const
138 m_basis.getOpenGLSubMatrix(m);
139 m[12] = m_origin.x();
140 m[13] = m_origin.y();
141 m[14] = m_origin.z();
142 m[15] = btScalar(1.0);
145 /**@brief Set the translational element
146 * @param origin The vector to set the translation to */
147 SIMD_FORCE_INLINE void setOrigin(const btVector3& origin)
152 SIMD_FORCE_INLINE btVector3 invXform(const btVector3& inVec) const;
154 /**@brief Set the rotational element by btMatrix3x3 */
155 SIMD_FORCE_INLINE void setBasis(const btMatrix3x3& basis)
160 /**@brief Set the rotational element by btQuaternion */
161 SIMD_FORCE_INLINE void setRotation(const btQuaternion& q)
163 m_basis.setRotation(q);
166 /**@brief Set this transformation to the identity */
169 m_basis.setIdentity();
170 m_origin.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
173 /**@brief Multiply this Transform by another(this = this * another)
174 * @param t The other transform */
175 btTransform& operator*=(const btTransform& t)
177 m_origin += m_basis * t.m_origin;
178 m_basis *= t.m_basis;
182 /**@brief Return the inverse of this transform */
183 btTransform inverse() const
185 btMatrix3x3 inv = m_basis.transpose();
186 return btTransform(inv, inv * -m_origin);
189 /**@brief Return the inverse of this transform times the other transform
190 * @param t The other transform
191 * return this.inverse() * the other */
192 btTransform inverseTimes(const btTransform& t) const;
194 /**@brief Return the product of this transform and the other */
195 btTransform operator*(const btTransform& t) const;
197 /**@brief Return an identity transform */
198 static const btTransform& getIdentity()
200 static const btTransform identityTransform(btMatrix3x3::getIdentity());
201 return identityTransform;
204 void serialize(struct btTransformData & dataOut) const;
206 void serializeFloat(struct btTransformFloatData & dataOut) const;
208 void deSerialize(const struct btTransformData& dataIn);
210 void deSerializeDouble(const struct btTransformDoubleData& dataIn);
212 void deSerializeFloat(const struct btTransformFloatData& dataIn);
215 SIMD_FORCE_INLINE btVector3
216 btTransform::invXform(const btVector3& inVec) const
218 btVector3 v = inVec - m_origin;
219 return (m_basis.transpose() * v);
222 SIMD_FORCE_INLINE btTransform
223 btTransform::inverseTimes(const btTransform& t) const
225 btVector3 v = t.getOrigin() - m_origin;
226 return btTransform(m_basis.transposeTimes(t.m_basis),
230 SIMD_FORCE_INLINE btTransform
231 btTransform::operator*(const btTransform& t) const
233 return btTransform(m_basis * t.m_basis,
234 (*this)(t.m_origin));
237 /**@brief Test if two transforms have all elements equal */
238 SIMD_FORCE_INLINE bool operator==(const btTransform& t1, const btTransform& t2)
240 return (t1.getBasis() == t2.getBasis() &&
241 t1.getOrigin() == t2.getOrigin());
245 struct btTransformFloatData
247 btMatrix3x3FloatData m_basis;
248 btVector3FloatData m_origin;
251 struct btTransformDoubleData
253 btMatrix3x3DoubleData m_basis;
254 btVector3DoubleData m_origin;
257 SIMD_FORCE_INLINE void btTransform::serialize(btTransformData& dataOut) const
259 m_basis.serialize(dataOut.m_basis);
260 m_origin.serialize(dataOut.m_origin);
263 SIMD_FORCE_INLINE void btTransform::serializeFloat(btTransformFloatData& dataOut) const
265 m_basis.serializeFloat(dataOut.m_basis);
266 m_origin.serializeFloat(dataOut.m_origin);
269 SIMD_FORCE_INLINE void btTransform::deSerialize(const btTransformData& dataIn)
271 m_basis.deSerialize(dataIn.m_basis);
272 m_origin.deSerialize(dataIn.m_origin);
275 SIMD_FORCE_INLINE void btTransform::deSerializeFloat(const btTransformFloatData& dataIn)
277 m_basis.deSerializeFloat(dataIn.m_basis);
278 m_origin.deSerializeFloat(dataIn.m_origin);
281 SIMD_FORCE_INLINE void btTransform::deSerializeDouble(const btTransformDoubleData& dataIn)
283 m_basis.deSerializeDouble(dataIn.m_basis);
284 m_origin.deSerializeDouble(dataIn.m_origin);
287 #endif //BT_TRANSFORM_H