Blender V2.61 - r43446

MT_Transform.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 /*
00034 
00035   MoTo - 3D Motion Toolkit 
00036   Copyright (C) 2000  Gino van den Bergen <gino@acm.org>
00037 
00038   This library is free software; you can redistribute it and/or
00039   modify it under the terms of the GNU Library General Public
00040   License as published by the Free Software Foundation; either
00041   version 2 of the License, or (at your option) any later version.
00042 
00043   This library is distributed in the hope that it will be useful,
00044   but WITHOUT ANY WARRANTY; without even the implied warranty of
00045   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00046   Library General Public License for more details.
00047 
00048   You should have received a copy of the GNU Library General Public
00049   License along with this library; if not, write to the Free
00050   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00051 
00052 */
00053 
00054 #ifndef MT_TRANSFORM_H
00055 #define MT_TRANSFORM_H
00056 
00057 #include "MT_Point3.h"
00058 #include "MT_Matrix3x3.h"
00059 
00060 class MT_Transform {
00061 public:
00062     MT_Transform() {}
00063     MT_Transform(const float *m) { setValue(m); }
00064     MT_Transform(const double *m) { setValue(m); }
00065     MT_Transform(const MT_Point3& p, const MT_Quaternion& q)
00066         : m_type(IDENTITY)
00067     { 
00068         setOrigin(p);
00069         setRotation(q);
00070     }
00071 
00072     MT_Transform(const MT_Point3& p, const MT_Matrix3x3& m) 
00073         : m_type(IDENTITY)
00074     { 
00075         setOrigin(p);
00076         setBasis(m);
00077     }
00078 
00079     static MT_Transform Identity()
00080     {
00081         MT_Transform t;
00082         t.setIdentity();
00083         return t;
00084     }
00085 
00086 
00087     MT_Point3 operator()(const MT_Point3& p) const {
00088         return MT_Point3(MT_dot(m_basis[0], p) + m_origin[0], 
00089                          MT_dot(m_basis[1], p) + m_origin[1], 
00090                          MT_dot(m_basis[2], p) + m_origin[2]);
00091     }
00092 
00093     MT_Vector3 operator()(const MT_Vector3& p) const {
00094         return MT_Vector3(MT_dot(m_basis[0], p) + m_origin[0], 
00095                          MT_dot(m_basis[1], p) + m_origin[1], 
00096                          MT_dot(m_basis[2], p) + m_origin[2]);
00097     }
00098     
00099     MT_Point3 operator*(const MT_Point3& p) const {
00100         return (*this)(p);
00101     }
00102  
00103     MT_Vector3 operator*(const MT_Vector3& p) const {
00104         return (*this)(p);
00105     }
00106 
00107 
00108     MT_Matrix3x3&         getBasis()          { return m_basis; }
00109     const MT_Matrix3x3&   getBasis()    const { return m_basis; }
00110     MT_Point3&            getOrigin()         { return m_origin; }
00111     const MT_Point3&      getOrigin()   const { return m_origin; }
00112     MT_Quaternion         getRotation() const { return m_basis.getRotation(); }
00113     
00114     void setValue(const float *m);
00115     void setValue(const double *m);
00116 
00117     void setOrigin(const MT_Point3& origin) { 
00118         m_origin = origin;
00119         m_type |= TRANSLATION;
00120     }
00121 
00122     void setBasis(const MT_Matrix3x3& basis) { 
00123         m_basis = basis;
00124         m_type |= LINEAR;
00125     }
00126 
00127     void setRotation(const MT_Quaternion& q) {
00128         m_basis.setRotation(q);
00129         m_type &= ~SCALING;
00130         m_type |= ROTATION;
00131     }
00132     
00133     void getValue(float *m) const;
00134     void getValue(double *m) const;
00135 
00136     void setIdentity();
00137     
00138     MT_Transform& operator*=(const MT_Transform& t);
00139 
00145     void translate(const MT_Vector3& v);
00146     void rotate(const MT_Quaternion& q);
00147     void scale(MT_Scalar x, MT_Scalar y, MT_Scalar z);
00148     
00149     void invert(const MT_Transform& t);
00150     void mult(const MT_Transform& t1, const MT_Transform& t2);
00151     void multInverseLeft(const MT_Transform& t1, const MT_Transform& t2); 
00152     
00153 private:
00154     enum { 
00155         IDENTITY    = 0x00, 
00156         TRANSLATION = 0x01,
00157         ROTATION    = 0x02,
00158         RIGID       = TRANSLATION | ROTATION,  
00159         SCALING     = 0x04,
00160         LINEAR      = ROTATION | SCALING,
00161         AFFINE      = TRANSLATION | LINEAR
00162     };
00163     
00164     MT_Transform(const MT_Matrix3x3& basis, const MT_Point3& origin,
00165                  unsigned int type) {
00166         setValue(basis, origin, type);
00167     }
00168     
00169     void setValue(const MT_Matrix3x3& basis, const MT_Point3& origin,
00170                   unsigned int type) {
00171         m_basis  = basis;
00172         m_origin = origin;
00173         m_type   = type;
00174     }
00175     
00176     friend MT_Transform operator*(const MT_Transform& t1, const MT_Transform& t2);
00177 
00178     MT_Matrix3x3 m_basis;
00179     MT_Point3    m_origin;
00180     unsigned int m_type;
00181 };
00182 
00183 inline MT_Transform operator*(const MT_Transform& t1, const MT_Transform& t2) {
00184     return MT_Transform(t1.m_basis * t2.m_basis, 
00185                         t1(t2.m_origin), 
00186                         t1.m_type | t2.m_type);
00187 }
00188 
00189 #endif
00190