[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3Common / b3Transform.h
1 /*
2 Copyright (c) 2003-2013 Gino van den Bergen / Erwin Coumans  http://bulletphysics.org
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 #ifndef B3_TRANSFORM_H
16 #define B3_TRANSFORM_H
17
18 #include "b3Matrix3x3.h"
19
20 #ifdef B3_USE_DOUBLE_PRECISION
21 #define b3TransformData b3TransformDoubleData
22 #else
23 #define b3TransformData b3TransformFloatData
24 #endif
25
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)
29 b3Transform
30 {
31         ///Storage for the rotation
32         b3Matrix3x3 m_basis;
33         ///Storage for the translation
34         b3Vector3 m_origin;
35
36 public:
37         /**@brief No initialization constructor */
38         b3Transform() {}
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)))
44                 : m_basis(q),
45                   m_origin(c)
46         {
47         }
48
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)))
54                 : m_basis(b),
55                   m_origin(c)
56         {
57         }
58         /**@brief Copy constructor */
59         B3_FORCE_INLINE b3Transform(const b3Transform& other)
60                 : m_basis(other.m_basis),
61                   m_origin(other.m_origin)
62         {
63         }
64         /**@brief Assignment Operator */
65         B3_FORCE_INLINE b3Transform& operator=(const b3Transform& other)
66         {
67                 m_basis = other.m_basis;
68                 m_origin = other.m_origin;
69                 return *this;
70         }
71
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)
77         {
78                 m_basis = t1.m_basis * t2.m_basis;
79                 m_origin = t1(t2.m_origin);
80         }
81
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;
86                 }
87                 */
88
89         /**@brief Return the transform of the vector */
90         B3_FORCE_INLINE b3Vector3 operator()(const b3Vector3& x) const
91         {
92                 return x.dot3(m_basis[0], m_basis[1], m_basis[2]) + m_origin;
93         }
94
95         /**@brief Return the transform of the vector */
96         B3_FORCE_INLINE b3Vector3 operator*(const b3Vector3& x) const
97         {
98                 return (*this)(x);
99         }
100
101         /**@brief Return the transform of the b3Quaternion */
102         B3_FORCE_INLINE b3Quaternion operator*(const b3Quaternion& q) const
103         {
104                 return getRotation() * q;
105         }
106
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; }
111
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; }
116
117         /**@brief Return a quaternion representing the rotation */
118         b3Quaternion getRotation() const
119         {
120                 b3Quaternion q;
121                 m_basis.getRotation(q);
122                 return q;
123         }
124
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)
128         {
129                 m_basis.setFromOpenGLSubMatrix(m);
130                 m_origin.setValue(m[12], m[13], m[14]);
131         }
132
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
136         {
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);
142         }
143
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)
147         {
148                 m_origin = origin;
149         }
150
151         B3_FORCE_INLINE b3Vector3 invXform(const b3Vector3& inVec) const;
152
153         /**@brief Set the rotational element by b3Matrix3x3 */
154         B3_FORCE_INLINE void setBasis(const b3Matrix3x3& basis)
155         {
156                 m_basis = basis;
157         }
158
159         /**@brief Set the rotational element by b3Quaternion */
160         B3_FORCE_INLINE void setRotation(const b3Quaternion& q)
161         {
162                 m_basis.setRotation(q);
163         }
164
165         /**@brief Set this transformation to the identity */
166         void setIdentity()
167         {
168                 m_basis.setIdentity();
169                 m_origin.setValue(b3Scalar(0.0), b3Scalar(0.0), b3Scalar(0.0));
170         }
171
172         /**@brief Multiply this Transform by another(this = this * another) 
173    * @param t The other transform */
174         b3Transform& operator*=(const b3Transform& t)
175         {
176                 m_origin += m_basis * t.m_origin;
177                 m_basis *= t.m_basis;
178                 return *this;
179         }
180
181         /**@brief Return the inverse of this transform */
182         b3Transform inverse() const
183         {
184                 b3Matrix3x3 inv = m_basis.transpose();
185                 return b3Transform(inv, inv * -m_origin);
186         }
187
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;
192
193         /**@brief Return the product of this transform and the other */
194         b3Transform operator*(const b3Transform& t) const;
195
196         /**@brief Return an identity transform */
197         static const b3Transform& getIdentity()
198         {
199                 static const b3Transform identityTransform(b3Matrix3x3::getIdentity());
200                 return identityTransform;
201         }
202
203         void serialize(struct b3TransformData & dataOut) const;
204
205         void serializeFloat(struct b3TransformFloatData & dataOut) const;
206
207         void deSerialize(const struct b3TransformData& dataIn);
208
209         void deSerializeDouble(const struct b3TransformDoubleData& dataIn);
210
211         void deSerializeFloat(const struct b3TransformFloatData& dataIn);
212 };
213
214 B3_FORCE_INLINE b3Vector3
215 b3Transform::invXform(const b3Vector3& inVec) const
216 {
217         b3Vector3 v = inVec - m_origin;
218         return (m_basis.transpose() * v);
219 }
220
221 B3_FORCE_INLINE b3Transform
222 b3Transform::inverseTimes(const b3Transform& t) const
223 {
224         b3Vector3 v = t.getOrigin() - m_origin;
225         return b3Transform(m_basis.transposeTimes(t.m_basis),
226                                            v * m_basis);
227 }
228
229 B3_FORCE_INLINE b3Transform
230         b3Transform::operator*(const b3Transform& t) const
231 {
232         return b3Transform(m_basis * t.m_basis,
233                                            (*this)(t.m_origin));
234 }
235
236 /**@brief Test if two transforms have all elements equal */
237 B3_FORCE_INLINE bool operator==(const b3Transform& t1, const b3Transform& t2)
238 {
239         return (t1.getBasis() == t2.getBasis() &&
240                         t1.getOrigin() == t2.getOrigin());
241 }
242
243 ///for serialization
244 struct b3TransformFloatData
245 {
246         b3Matrix3x3FloatData m_basis;
247         b3Vector3FloatData m_origin;
248 };
249
250 struct b3TransformDoubleData
251 {
252         b3Matrix3x3DoubleData m_basis;
253         b3Vector3DoubleData m_origin;
254 };
255
256 B3_FORCE_INLINE void b3Transform::serialize(b3TransformData& dataOut) const
257 {
258         m_basis.serialize(dataOut.m_basis);
259         m_origin.serialize(dataOut.m_origin);
260 }
261
262 B3_FORCE_INLINE void b3Transform::serializeFloat(b3TransformFloatData& dataOut) const
263 {
264         m_basis.serializeFloat(dataOut.m_basis);
265         m_origin.serializeFloat(dataOut.m_origin);
266 }
267
268 B3_FORCE_INLINE void b3Transform::deSerialize(const b3TransformData& dataIn)
269 {
270         m_basis.deSerialize(dataIn.m_basis);
271         m_origin.deSerialize(dataIn.m_origin);
272 }
273
274 B3_FORCE_INLINE void b3Transform::deSerializeFloat(const b3TransformFloatData& dataIn)
275 {
276         m_basis.deSerializeFloat(dataIn.m_basis);
277         m_origin.deSerializeFloat(dataIn.m_origin);
278 }
279
280 B3_FORCE_INLINE void b3Transform::deSerializeDouble(const b3TransformDoubleData& dataIn)
281 {
282         m_basis.deSerializeDouble(dataIn.m_basis);
283         m_origin.deSerializeDouble(dataIn.m_origin);
284 }
285
286 #endif  //B3_TRANSFORM_H