2 Copyright (c) 2003-2013 Gino van den Bergen / Erwin Coumans http://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 B3_TRANSFORM_H
16 #define B3_TRANSFORM_H
18 #include "b3Matrix3x3.h"
20 #ifdef B3_USE_DOUBLE_PRECISION
21 #define b3TransformData b3TransformDoubleData
23 #define b3TransformData b3TransformFloatData
26 /**@brief The b3Transform class supports rigid transforms with only translation and rotation and no scaling/shear.
27 *It can be used in combination with b3Vector3, b3Quaternion and b3Matrix3x3 linear algebra classes. */
28 B3_ATTRIBUTE_ALIGNED16(class)
31 ///Storage for the rotation
33 ///Storage for the translation
37 /**@brief No initialization constructor */
39 /**@brief Constructor from b3Quaternion (optional b3Vector3 )
40 * @param q Rotation from quaternion
41 * @param c Translation from Vector (default 0,0,0) */
42 explicit B3_FORCE_INLINE b3Transform(const b3Quaternion& q,
43 const b3Vector3& c = b3MakeVector3(b3Scalar(0), b3Scalar(0), b3Scalar(0)))
49 /**@brief Constructor from b3Matrix3x3 (optional b3Vector3)
50 * @param b Rotation from Matrix
51 * @param c Translation from Vector default (0,0,0)*/
52 explicit B3_FORCE_INLINE b3Transform(const b3Matrix3x3& b,
53 const b3Vector3& c = b3MakeVector3(b3Scalar(0), b3Scalar(0), b3Scalar(0)))
58 /**@brief Copy constructor */
59 B3_FORCE_INLINE b3Transform(const b3Transform& other)
60 : m_basis(other.m_basis),
61 m_origin(other.m_origin)
64 /**@brief Assignment Operator */
65 B3_FORCE_INLINE b3Transform& operator=(const b3Transform& other)
67 m_basis = other.m_basis;
68 m_origin = other.m_origin;
72 /**@brief Set the current transform as the value of the product of two transforms
73 * @param t1 Transform 1
74 * @param t2 Transform 2
75 * This = Transform1 * Transform2 */
76 B3_FORCE_INLINE void mult(const b3Transform& t1, const b3Transform& t2)
78 m_basis = t1.m_basis * t2.m_basis;
79 m_origin = t1(t2.m_origin);
82 /* void multInverseLeft(const b3Transform& t1, const b3Transform& t2) {
83 b3Vector3 v = t2.m_origin - t1.m_origin;
84 m_basis = b3MultTransposeLeft(t1.m_basis, t2.m_basis);
85 m_origin = v * t1.m_basis;
89 /**@brief Return the transform of the vector */
90 B3_FORCE_INLINE b3Vector3 operator()(const b3Vector3& x) const
92 return x.dot3(m_basis[0], m_basis[1], m_basis[2]) + m_origin;
95 /**@brief Return the transform of the vector */
96 B3_FORCE_INLINE b3Vector3 operator*(const b3Vector3& x) const
101 /**@brief Return the transform of the b3Quaternion */
102 B3_FORCE_INLINE b3Quaternion operator*(const b3Quaternion& q) const
104 return getRotation() * q;
107 /**@brief Return the basis matrix for the rotation */
108 B3_FORCE_INLINE b3Matrix3x3& getBasis() { return m_basis; }
109 /**@brief Return the basis matrix for the rotation */
110 B3_FORCE_INLINE const b3Matrix3x3& getBasis() const { return m_basis; }
112 /**@brief Return the origin vector translation */
113 B3_FORCE_INLINE b3Vector3& getOrigin() { return m_origin; }
114 /**@brief Return the origin vector translation */
115 B3_FORCE_INLINE const b3Vector3& getOrigin() const { return m_origin; }
117 /**@brief Return a quaternion representing the rotation */
118 b3Quaternion getRotation() const
121 m_basis.getRotation(q);
125 /**@brief Set from an array
126 * @param m A pointer to a 15 element array (12 rotation(row major padded on the right by 1), and 3 translation */
127 void setFromOpenGLMatrix(const b3Scalar* m)
129 m_basis.setFromOpenGLSubMatrix(m);
130 m_origin.setValue(m[12], m[13], m[14]);
133 /**@brief Fill an array representation
134 * @param m A pointer to a 15 element array (12 rotation(row major padded on the right by 1), and 3 translation */
135 void getOpenGLMatrix(b3Scalar * m) const
137 m_basis.getOpenGLSubMatrix(m);
138 m[12] = m_origin.getX();
139 m[13] = m_origin.getY();
140 m[14] = m_origin.getZ();
141 m[15] = b3Scalar(1.0);
144 /**@brief Set the translational element
145 * @param origin The vector to set the translation to */
146 B3_FORCE_INLINE void setOrigin(const b3Vector3& origin)
151 B3_FORCE_INLINE b3Vector3 invXform(const b3Vector3& inVec) const;
153 /**@brief Set the rotational element by b3Matrix3x3 */
154 B3_FORCE_INLINE void setBasis(const b3Matrix3x3& basis)
159 /**@brief Set the rotational element by b3Quaternion */
160 B3_FORCE_INLINE void setRotation(const b3Quaternion& q)
162 m_basis.setRotation(q);
165 /**@brief Set this transformation to the identity */
168 m_basis.setIdentity();
169 m_origin.setValue(b3Scalar(0.0), b3Scalar(0.0), b3Scalar(0.0));
172 /**@brief Multiply this Transform by another(this = this * another)
173 * @param t The other transform */
174 b3Transform& operator*=(const b3Transform& t)
176 m_origin += m_basis * t.m_origin;
177 m_basis *= t.m_basis;
181 /**@brief Return the inverse of this transform */
182 b3Transform inverse() const
184 b3Matrix3x3 inv = m_basis.transpose();
185 return b3Transform(inv, inv * -m_origin);
188 /**@brief Return the inverse of this transform times the other transform
189 * @param t The other transform
190 * return this.inverse() * the other */
191 b3Transform inverseTimes(const b3Transform& t) const;
193 /**@brief Return the product of this transform and the other */
194 b3Transform operator*(const b3Transform& t) const;
196 /**@brief Return an identity transform */
197 static const b3Transform& getIdentity()
199 static const b3Transform identityTransform(b3Matrix3x3::getIdentity());
200 return identityTransform;
203 void serialize(struct b3TransformData & dataOut) const;
205 void serializeFloat(struct b3TransformFloatData & dataOut) const;
207 void deSerialize(const struct b3TransformData& dataIn);
209 void deSerializeDouble(const struct b3TransformDoubleData& dataIn);
211 void deSerializeFloat(const struct b3TransformFloatData& dataIn);
214 B3_FORCE_INLINE b3Vector3
215 b3Transform::invXform(const b3Vector3& inVec) const
217 b3Vector3 v = inVec - m_origin;
218 return (m_basis.transpose() * v);
221 B3_FORCE_INLINE b3Transform
222 b3Transform::inverseTimes(const b3Transform& t) const
224 b3Vector3 v = t.getOrigin() - m_origin;
225 return b3Transform(m_basis.transposeTimes(t.m_basis),
229 B3_FORCE_INLINE b3Transform
230 b3Transform::operator*(const b3Transform& t) const
232 return b3Transform(m_basis * t.m_basis,
233 (*this)(t.m_origin));
236 /**@brief Test if two transforms have all elements equal */
237 B3_FORCE_INLINE bool operator==(const b3Transform& t1, const b3Transform& t2)
239 return (t1.getBasis() == t2.getBasis() &&
240 t1.getOrigin() == t2.getOrigin());
244 struct b3TransformFloatData
246 b3Matrix3x3FloatData m_basis;
247 b3Vector3FloatData m_origin;
250 struct b3TransformDoubleData
252 b3Matrix3x3DoubleData m_basis;
253 b3Vector3DoubleData m_origin;
256 B3_FORCE_INLINE void b3Transform::serialize(b3TransformData& dataOut) const
258 m_basis.serialize(dataOut.m_basis);
259 m_origin.serialize(dataOut.m_origin);
262 B3_FORCE_INLINE void b3Transform::serializeFloat(b3TransformFloatData& dataOut) const
264 m_basis.serializeFloat(dataOut.m_basis);
265 m_origin.serializeFloat(dataOut.m_origin);
268 B3_FORCE_INLINE void b3Transform::deSerialize(const b3TransformData& dataIn)
270 m_basis.deSerialize(dataIn.m_basis);
271 m_origin.deSerialize(dataIn.m_origin);
274 B3_FORCE_INLINE void b3Transform::deSerializeFloat(const b3TransformFloatData& dataIn)
276 m_basis.deSerializeFloat(dataIn.m_basis);
277 m_origin.deSerializeFloat(dataIn.m_origin);
280 B3_FORCE_INLINE void b3Transform::deSerializeDouble(const b3TransformDoubleData& dataIn)
282 m_basis.deSerializeDouble(dataIn.m_basis);
283 m_origin.deSerializeDouble(dataIn.m_origin);
286 #endif //B3_TRANSFORM_H