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