1 | /* |
---|
2 | Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
3 | |
---|
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: |
---|
9 | |
---|
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. |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | #ifndef BT_TRANSFORM_H |
---|
18 | #define BT_TRANSFORM_H |
---|
19 | |
---|
20 | |
---|
21 | #include "btMatrix3x3.h" |
---|
22 | |
---|
23 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
24 | #define btTransformData btTransformDoubleData |
---|
25 | #else |
---|
26 | #define btTransformData btTransformFloatData |
---|
27 | #endif |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | /**@brief The btTransform class supports rigid transforms with only translation and rotation and no scaling/shear. |
---|
33 | *It can be used in combination with btVector3, btQuaternion and btMatrix3x3 linear algebra classes. */ |
---|
34 | class btTransform { |
---|
35 | |
---|
36 | ///Storage for the rotation |
---|
37 | btMatrix3x3 m_basis; |
---|
38 | ///Storage for the translation |
---|
39 | btVector3 m_origin; |
---|
40 | |
---|
41 | public: |
---|
42 | |
---|
43 | /**@brief No initialization constructor */ |
---|
44 | btTransform() {} |
---|
45 | /**@brief Constructor from btQuaternion (optional btVector3 ) |
---|
46 | * @param q Rotation from quaternion |
---|
47 | * @param c Translation from Vector (default 0,0,0) */ |
---|
48 | explicit SIMD_FORCE_INLINE btTransform(const btQuaternion& q, |
---|
49 | const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0))) |
---|
50 | : m_basis(q), |
---|
51 | m_origin(c) |
---|
52 | {} |
---|
53 | |
---|
54 | /**@brief Constructor from btMatrix3x3 (optional btVector3) |
---|
55 | * @param b Rotation from Matrix |
---|
56 | * @param c Translation from Vector default (0,0,0)*/ |
---|
57 | explicit SIMD_FORCE_INLINE btTransform(const btMatrix3x3& b, |
---|
58 | const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0))) |
---|
59 | : m_basis(b), |
---|
60 | m_origin(c) |
---|
61 | {} |
---|
62 | /**@brief Copy constructor */ |
---|
63 | SIMD_FORCE_INLINE btTransform (const btTransform& other) |
---|
64 | : m_basis(other.m_basis), |
---|
65 | m_origin(other.m_origin) |
---|
66 | { |
---|
67 | } |
---|
68 | /**@brief Assignment Operator */ |
---|
69 | SIMD_FORCE_INLINE btTransform& operator=(const btTransform& other) |
---|
70 | { |
---|
71 | m_basis = other.m_basis; |
---|
72 | m_origin = other.m_origin; |
---|
73 | return *this; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /**@brief Set the current transform as the value of the product of two transforms |
---|
78 | * @param t1 Transform 1 |
---|
79 | * @param t2 Transform 2 |
---|
80 | * This = Transform1 * Transform2 */ |
---|
81 | SIMD_FORCE_INLINE void mult(const btTransform& t1, const btTransform& t2) { |
---|
82 | m_basis = t1.m_basis * t2.m_basis; |
---|
83 | m_origin = t1(t2.m_origin); |
---|
84 | } |
---|
85 | |
---|
86 | /* void multInverseLeft(const btTransform& t1, const btTransform& t2) { |
---|
87 | btVector3 v = t2.m_origin - t1.m_origin; |
---|
88 | m_basis = btMultTransposeLeft(t1.m_basis, t2.m_basis); |
---|
89 | m_origin = v * t1.m_basis; |
---|
90 | } |
---|
91 | */ |
---|
92 | |
---|
93 | /**@brief Return the transform of the vector */ |
---|
94 | SIMD_FORCE_INLINE btVector3 operator()(const btVector3& x) const |
---|
95 | { |
---|
96 | return btVector3(m_basis[0].dot(x) + m_origin.x(), |
---|
97 | m_basis[1].dot(x) + m_origin.y(), |
---|
98 | m_basis[2].dot(x) + m_origin.z()); |
---|
99 | } |
---|
100 | |
---|
101 | /**@brief Return the transform of the vector */ |
---|
102 | SIMD_FORCE_INLINE btVector3 operator*(const btVector3& x) const |
---|
103 | { |
---|
104 | return (*this)(x); |
---|
105 | } |
---|
106 | |
---|
107 | /**@brief Return the transform of the btQuaternion */ |
---|
108 | SIMD_FORCE_INLINE btQuaternion operator*(const btQuaternion& q) const |
---|
109 | { |
---|
110 | return getRotation() * q; |
---|
111 | } |
---|
112 | |
---|
113 | /**@brief Return the basis matrix for the rotation */ |
---|
114 | SIMD_FORCE_INLINE btMatrix3x3& getBasis() { return m_basis; } |
---|
115 | /**@brief Return the basis matrix for the rotation */ |
---|
116 | SIMD_FORCE_INLINE const btMatrix3x3& getBasis() const { return m_basis; } |
---|
117 | |
---|
118 | /**@brief Return the origin vector translation */ |
---|
119 | SIMD_FORCE_INLINE btVector3& getOrigin() { return m_origin; } |
---|
120 | /**@brief Return the origin vector translation */ |
---|
121 | SIMD_FORCE_INLINE const btVector3& getOrigin() const { return m_origin; } |
---|
122 | |
---|
123 | /**@brief Return a quaternion representing the rotation */ |
---|
124 | btQuaternion getRotation() const { |
---|
125 | btQuaternion q; |
---|
126 | m_basis.getRotation(q); |
---|
127 | return q; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | /**@brief Set from an array |
---|
132 | * @param m A pointer to a 15 element array (12 rotation(row major padded on the right by 1), and 3 translation */ |
---|
133 | void setFromOpenGLMatrix(const btScalar *m) |
---|
134 | { |
---|
135 | m_basis.setFromOpenGLSubMatrix(m); |
---|
136 | m_origin.setValue(m[12],m[13],m[14]); |
---|
137 | } |
---|
138 | |
---|
139 | /**@brief Fill an array representation |
---|
140 | * @param m A pointer to a 15 element array (12 rotation(row major padded on the right by 1), and 3 translation */ |
---|
141 | void getOpenGLMatrix(btScalar *m) const |
---|
142 | { |
---|
143 | m_basis.getOpenGLSubMatrix(m); |
---|
144 | m[12] = m_origin.x(); |
---|
145 | m[13] = m_origin.y(); |
---|
146 | m[14] = m_origin.z(); |
---|
147 | m[15] = btScalar(1.0); |
---|
148 | } |
---|
149 | |
---|
150 | /**@brief Set the translational element |
---|
151 | * @param origin The vector to set the translation to */ |
---|
152 | SIMD_FORCE_INLINE void setOrigin(const btVector3& origin) |
---|
153 | { |
---|
154 | m_origin = origin; |
---|
155 | } |
---|
156 | |
---|
157 | SIMD_FORCE_INLINE btVector3 invXform(const btVector3& inVec) const; |
---|
158 | |
---|
159 | |
---|
160 | /**@brief Set the rotational element by btMatrix3x3 */ |
---|
161 | SIMD_FORCE_INLINE void setBasis(const btMatrix3x3& basis) |
---|
162 | { |
---|
163 | m_basis = basis; |
---|
164 | } |
---|
165 | |
---|
166 | /**@brief Set the rotational element by btQuaternion */ |
---|
167 | SIMD_FORCE_INLINE void setRotation(const btQuaternion& q) |
---|
168 | { |
---|
169 | m_basis.setRotation(q); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | /**@brief Set this transformation to the identity */ |
---|
174 | void setIdentity() |
---|
175 | { |
---|
176 | m_basis.setIdentity(); |
---|
177 | m_origin.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0)); |
---|
178 | } |
---|
179 | |
---|
180 | /**@brief Multiply this Transform by another(this = this * another) |
---|
181 | * @param t The other transform */ |
---|
182 | btTransform& operator*=(const btTransform& t) |
---|
183 | { |
---|
184 | m_origin += m_basis * t.m_origin; |
---|
185 | m_basis *= t.m_basis; |
---|
186 | return *this; |
---|
187 | } |
---|
188 | |
---|
189 | /**@brief Return the inverse of this transform */ |
---|
190 | btTransform inverse() const |
---|
191 | { |
---|
192 | btMatrix3x3 inv = m_basis.transpose(); |
---|
193 | return btTransform(inv, inv * -m_origin); |
---|
194 | } |
---|
195 | |
---|
196 | /**@brief Return the inverse of this transform times the other transform |
---|
197 | * @param t The other transform |
---|
198 | * return this.inverse() * the other */ |
---|
199 | btTransform inverseTimes(const btTransform& t) const; |
---|
200 | |
---|
201 | /**@brief Return the product of this transform and the other */ |
---|
202 | btTransform operator*(const btTransform& t) const; |
---|
203 | |
---|
204 | /**@brief Return an identity transform */ |
---|
205 | static const btTransform& getIdentity() |
---|
206 | { |
---|
207 | static const btTransform identityTransform(btMatrix3x3::getIdentity()); |
---|
208 | return identityTransform; |
---|
209 | } |
---|
210 | |
---|
211 | void serialize(struct btTransformData& dataOut) const; |
---|
212 | |
---|
213 | void serializeFloat(struct btTransformFloatData& dataOut) const; |
---|
214 | |
---|
215 | void deSerialize(const struct btTransformData& dataIn); |
---|
216 | |
---|
217 | void deSerializeDouble(const struct btTransformDoubleData& dataIn); |
---|
218 | |
---|
219 | void deSerializeFloat(const struct btTransformFloatData& dataIn); |
---|
220 | |
---|
221 | }; |
---|
222 | |
---|
223 | |
---|
224 | SIMD_FORCE_INLINE btVector3 |
---|
225 | btTransform::invXform(const btVector3& inVec) const |
---|
226 | { |
---|
227 | btVector3 v = inVec - m_origin; |
---|
228 | return (m_basis.transpose() * v); |
---|
229 | } |
---|
230 | |
---|
231 | SIMD_FORCE_INLINE btTransform |
---|
232 | btTransform::inverseTimes(const btTransform& t) const |
---|
233 | { |
---|
234 | btVector3 v = t.getOrigin() - m_origin; |
---|
235 | return btTransform(m_basis.transposeTimes(t.m_basis), |
---|
236 | v * m_basis); |
---|
237 | } |
---|
238 | |
---|
239 | SIMD_FORCE_INLINE btTransform |
---|
240 | btTransform::operator*(const btTransform& t) const |
---|
241 | { |
---|
242 | return btTransform(m_basis * t.m_basis, |
---|
243 | (*this)(t.m_origin)); |
---|
244 | } |
---|
245 | |
---|
246 | /**@brief Test if two transforms have all elements equal */ |
---|
247 | SIMD_FORCE_INLINE bool operator==(const btTransform& t1, const btTransform& t2) |
---|
248 | { |
---|
249 | return ( t1.getBasis() == t2.getBasis() && |
---|
250 | t1.getOrigin() == t2.getOrigin() ); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | ///for serialization |
---|
255 | struct btTransformFloatData |
---|
256 | { |
---|
257 | btMatrix3x3FloatData m_basis; |
---|
258 | btVector3FloatData m_origin; |
---|
259 | }; |
---|
260 | |
---|
261 | struct btTransformDoubleData |
---|
262 | { |
---|
263 | btMatrix3x3DoubleData m_basis; |
---|
264 | btVector3DoubleData m_origin; |
---|
265 | }; |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | SIMD_FORCE_INLINE void btTransform::serialize(btTransformData& dataOut) const |
---|
270 | { |
---|
271 | m_basis.serialize(dataOut.m_basis); |
---|
272 | m_origin.serialize(dataOut.m_origin); |
---|
273 | } |
---|
274 | |
---|
275 | SIMD_FORCE_INLINE void btTransform::serializeFloat(btTransformFloatData& dataOut) const |
---|
276 | { |
---|
277 | m_basis.serializeFloat(dataOut.m_basis); |
---|
278 | m_origin.serializeFloat(dataOut.m_origin); |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | SIMD_FORCE_INLINE void btTransform::deSerialize(const btTransformData& dataIn) |
---|
283 | { |
---|
284 | m_basis.deSerialize(dataIn.m_basis); |
---|
285 | m_origin.deSerialize(dataIn.m_origin); |
---|
286 | } |
---|
287 | |
---|
288 | SIMD_FORCE_INLINE void btTransform::deSerializeFloat(const btTransformFloatData& dataIn) |
---|
289 | { |
---|
290 | m_basis.deSerializeFloat(dataIn.m_basis); |
---|
291 | m_origin.deSerializeFloat(dataIn.m_origin); |
---|
292 | } |
---|
293 | |
---|
294 | SIMD_FORCE_INLINE void btTransform::deSerializeDouble(const btTransformDoubleData& dataIn) |
---|
295 | { |
---|
296 | m_basis.deSerializeDouble(dataIn.m_basis); |
---|
297 | m_origin.deSerializeDouble(dataIn.m_origin); |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | #endif //BT_TRANSFORM_H |
---|
302 | |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | |
---|
307 | |
---|