btTransform.h

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
00003 
00004 This software is provided 'as-is', without any express or implied warranty.
00005 In no event will the authors be held liable for any damages arising from the use of this software.
00006 Permission is granted to anyone to use this software for any purpose, 
00007 including commercial applications, and to alter it and redistribute it freely, 
00008 subject to the following restrictions:
00009 
00010 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.
00011 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00012 3. This notice may not be removed or altered from any source distribution.
00013 */
00014 
00015 
00016 
00017 #ifndef BT_TRANSFORM_H
00018 #define BT_TRANSFORM_H
00019 
00020 
00021 #include "btMatrix3x3.h"
00022 
00023 #ifdef BT_USE_DOUBLE_PRECISION
00024 #define btTransformData btTransformDoubleData
00025 #else
00026 #define btTransformData btTransformFloatData
00027 #endif
00028 
00029 
00030 
00031 
00034 ATTRIBUTE_ALIGNED16(class) btTransform {
00035         
00037         btMatrix3x3 m_basis;
00039         btVector3   m_origin;
00040 
00041 public:
00042         
00044         btTransform() {}
00048         explicit SIMD_FORCE_INLINE btTransform(const btQuaternion& q, 
00049                 const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0))) 
00050                 : m_basis(q),
00051                 m_origin(c)
00052         {}
00053 
00057         explicit SIMD_FORCE_INLINE btTransform(const btMatrix3x3& b, 
00058                 const btVector3& c = btVector3(btScalar(0), btScalar(0), btScalar(0)))
00059                 : m_basis(b),
00060                 m_origin(c)
00061         {}
00063         SIMD_FORCE_INLINE btTransform (const btTransform& other)
00064                 : m_basis(other.m_basis),
00065                 m_origin(other.m_origin)
00066         {
00067         }
00069         SIMD_FORCE_INLINE btTransform& operator=(const btTransform& other)
00070         {
00071                 m_basis = other.m_basis;
00072                 m_origin = other.m_origin;
00073                 return *this;
00074         }
00075 
00076 
00081                 SIMD_FORCE_INLINE void mult(const btTransform& t1, const btTransform& t2) {
00082                         m_basis = t1.m_basis * t2.m_basis;
00083                         m_origin = t1(t2.m_origin);
00084                 }
00085 
00086 /*              void multInverseLeft(const btTransform& t1, const btTransform& t2) {
00087                         btVector3 v = t2.m_origin - t1.m_origin;
00088                         m_basis = btMultTransposeLeft(t1.m_basis, t2.m_basis);
00089                         m_origin = v * t1.m_basis;
00090                 }
00091                 */
00092 
00094         SIMD_FORCE_INLINE btVector3 operator()(const btVector3& x) const
00095         {
00096         return x.dot3(m_basis[0], m_basis[1], m_basis[2]) + m_origin;
00097         }
00098 
00100         SIMD_FORCE_INLINE btVector3 operator*(const btVector3& x) const
00101         {
00102                 return (*this)(x);
00103         }
00104 
00106         SIMD_FORCE_INLINE btQuaternion operator*(const btQuaternion& q) const
00107         {
00108                 return getRotation() * q;
00109         }
00110 
00112         SIMD_FORCE_INLINE btMatrix3x3&       getBasis()          { return m_basis; }
00114         SIMD_FORCE_INLINE const btMatrix3x3& getBasis()    const { return m_basis; }
00115 
00117         SIMD_FORCE_INLINE btVector3&         getOrigin()         { return m_origin; }
00119         SIMD_FORCE_INLINE const btVector3&   getOrigin()   const { return m_origin; }
00120 
00122         btQuaternion getRotation() const { 
00123                 btQuaternion q;
00124                 m_basis.getRotation(q);
00125                 return q;
00126         }
00127         
00128         
00131         void setFromOpenGLMatrix(const btScalar *m)
00132         {
00133                 m_basis.setFromOpenGLSubMatrix(m);
00134                 m_origin.setValue(m[12],m[13],m[14]);
00135         }
00136 
00139         void getOpenGLMatrix(btScalar *m) const 
00140         {
00141                 m_basis.getOpenGLSubMatrix(m);
00142                 m[12] = m_origin.x();
00143                 m[13] = m_origin.y();
00144                 m[14] = m_origin.z();
00145                 m[15] = btScalar(1.0);
00146         }
00147 
00150         SIMD_FORCE_INLINE void setOrigin(const btVector3& origin) 
00151         { 
00152                 m_origin = origin;
00153         }
00154 
00155         SIMD_FORCE_INLINE btVector3 invXform(const btVector3& inVec) const;
00156 
00157 
00159         SIMD_FORCE_INLINE void setBasis(const btMatrix3x3& basis)
00160         { 
00161                 m_basis = basis;
00162         }
00163 
00165         SIMD_FORCE_INLINE void setRotation(const btQuaternion& q)
00166         {
00167                 m_basis.setRotation(q);
00168         }
00169 
00170 
00172         void setIdentity()
00173         {
00174                 m_basis.setIdentity();
00175                 m_origin.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
00176         }
00177 
00180         btTransform& operator*=(const btTransform& t) 
00181         {
00182                 m_origin += m_basis * t.m_origin;
00183                 m_basis *= t.m_basis;
00184                 return *this;
00185         }
00186 
00188         btTransform inverse() const
00189         { 
00190                 btMatrix3x3 inv = m_basis.transpose();
00191                 return btTransform(inv, inv * -m_origin);
00192         }
00193 
00197         btTransform inverseTimes(const btTransform& t) const;  
00198 
00200         btTransform operator*(const btTransform& t) const;
00201 
00203         static const btTransform&       getIdentity()
00204         {
00205                 static const btTransform identityTransform(btMatrix3x3::getIdentity());
00206                 return identityTransform;
00207         }
00208 
00209         void    serialize(struct        btTransformData& dataOut) const;
00210 
00211         void    serializeFloat(struct   btTransformFloatData& dataOut) const;
00212 
00213         void    deSerialize(const struct        btTransformData& dataIn);
00214 
00215         void    deSerializeDouble(const struct  btTransformDoubleData& dataIn);
00216 
00217         void    deSerializeFloat(const struct   btTransformFloatData& dataIn);
00218 
00219 };
00220 
00221 
00222 SIMD_FORCE_INLINE btVector3
00223 btTransform::invXform(const btVector3& inVec) const
00224 {
00225         btVector3 v = inVec - m_origin;
00226         return (m_basis.transpose() * v);
00227 }
00228 
00229 SIMD_FORCE_INLINE btTransform 
00230 btTransform::inverseTimes(const btTransform& t) const  
00231 {
00232         btVector3 v = t.getOrigin() - m_origin;
00233                 return btTransform(m_basis.transposeTimes(t.m_basis),
00234                         v * m_basis);
00235 }
00236 
00237 SIMD_FORCE_INLINE btTransform 
00238 btTransform::operator*(const btTransform& t) const
00239 {
00240         return btTransform(m_basis * t.m_basis, 
00241                 (*this)(t.m_origin));
00242 }
00243 
00245 SIMD_FORCE_INLINE bool operator==(const btTransform& t1, const btTransform& t2)
00246 {
00247    return ( t1.getBasis()  == t2.getBasis() &&
00248             t1.getOrigin() == t2.getOrigin() );
00249 }
00250 
00251 
00253 struct  btTransformFloatData
00254 {
00255         btMatrix3x3FloatData    m_basis;
00256         btVector3FloatData      m_origin;
00257 };
00258 
00259 struct  btTransformDoubleData
00260 {
00261         btMatrix3x3DoubleData   m_basis;
00262         btVector3DoubleData     m_origin;
00263 };
00264 
00265 
00266 
00267 SIMD_FORCE_INLINE       void    btTransform::serialize(btTransformData& dataOut) const
00268 {
00269         m_basis.serialize(dataOut.m_basis);
00270         m_origin.serialize(dataOut.m_origin);
00271 }
00272 
00273 SIMD_FORCE_INLINE       void    btTransform::serializeFloat(btTransformFloatData& dataOut) const
00274 {
00275         m_basis.serializeFloat(dataOut.m_basis);
00276         m_origin.serializeFloat(dataOut.m_origin);
00277 }
00278 
00279 
00280 SIMD_FORCE_INLINE       void    btTransform::deSerialize(const btTransformData& dataIn)
00281 {
00282         m_basis.deSerialize(dataIn.m_basis);
00283         m_origin.deSerialize(dataIn.m_origin);
00284 }
00285 
00286 SIMD_FORCE_INLINE       void    btTransform::deSerializeFloat(const btTransformFloatData& dataIn)
00287 {
00288         m_basis.deSerializeFloat(dataIn.m_basis);
00289         m_origin.deSerializeFloat(dataIn.m_origin);
00290 }
00291 
00292 SIMD_FORCE_INLINE       void    btTransform::deSerializeDouble(const btTransformDoubleData& dataIn)
00293 {
00294         m_basis.deSerializeDouble(dataIn.m_basis);
00295         m_origin.deSerializeDouble(dataIn.m_origin);
00296 }
00297 
00298 
00299 #endif //BT_TRANSFORM_H
00300 
00301 
00302 
00303 
00304 
00305