Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/Transform

Go to the documentation of this file.
00001 /* **-*-c++-*-**************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (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. (http://www.gnu.org)
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
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Transform 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.3 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #ifndef _BASE_TRANSFORM_
00026 #define _BASE_TRANSFORM_
00027 
00028 #include <base/base>
00029 #include <base/Matrix4>
00030 #include <base/Orient>
00031 #include <base/Quat4>
00032 #include <base/Point4>
00033 
00034 
00035 namespace base {
00036 
00037 //
00038 // A 3D geometric transformation 
00039 //
00040 
00041 class Transform
00042 {
00043 
00044 public:
00045   Transform() : isIdentity(true) {}
00046   explicit Transform(const Vector3& translation);
00047   Transform(const Orient& rotation);
00048   Transform(const Quat4& rotation);
00049   Transform(const Matrix3& rotation);
00050   Transform(const Vector3& translation, const Orient& rotation);
00051   Transform(const Vector3& translation, const Quat4& rotation);
00052   Transform(const Vector3& translation, const Matrix3& rotation);
00053   Transform(const Matrix4& t);
00054   Transform(const Transform& tr);
00055   ~Transform() {}
00056 
00057   Transform& operator=(const Transform& t);
00058   
00059   bool operator==(const Transform& rhs) const;
00060   bool equals(const Transform& rhs, Real epsilon = consts::epsilon) const throw();
00061  
00062 
00063   Matrix4 getTransform() const;
00064 
00065   void setIdentity() { isIdentity=true; }
00066   void setToRotation(const Vector3& axis, Real angle);
00067   void setToRotation(const Quat4& orient);
00068   void setRotationComponent(const Vector3& axis, Real angle);
00069   void setRotationComponent(const Quat4& orient);
00070   void setRotationComponent(const Orient& orient);
00071   void setRotationComponent(const Matrix3& rotation);
00072   void setToTranslation(const Vector3& trans);
00073   void setTranslationComponent(const Vector3& trans);
00074   void setTransform(const Matrix4& m);
00075 
00076   const Vector3& getTranslation() const;
00077   const Orient& getRotation() const;
00078 
00079   bool identity() const;
00080   bool isTransRotationOnly() const;
00081   bool containsRotation() const;
00082   bool containsTranslation() const;
00083 
00084   Transform& invert(); ///< inverse transformation
00085 
00086   void transformPoint(Point3& p) const;
00087   void transformPoint(Point4& p) const;
00088   Point3 transform(const Point3& p) const;
00089   Point4 transform(const Point4& p) const;
00090 
00091   Point3 rotate(const Point3& p) const;
00092   Point3 translate(const Point3& p) const;
00093 
00094   Point3 operator()(const Point3& p) const 
00095     { Point3 ret(p); transform(ret); return ret; }
00096 
00097   Transform& operator*=(const Transform& rhs);
00098 
00099 protected:
00100 
00101   bool isIdentity;                 ///< identity transform
00102   bool isPureTranslationRotation;  ///< is not an arbitrary transform in t
00103   bool hasTranslation;             ///< transform includes translation in trans (if not arbitrary)
00104   bool hasRotation;                ///< transform includes rotation in axis & angle (if not arbitrary)
00105 
00106   mutable Orient orient;
00107   mutable Vector3 trans;
00108   Matrix4 t;
00109 };
00110 
00111 
00112 // implementation
00113 
00114 
00115 inline const Vector3& Transform::getTranslation() const
00116 {
00117   if (isIdentity || (isPureTranslationRotation && !hasTranslation)) 
00118     trans.setZero();
00119 
00120   if (!isPureTranslationRotation)
00121     trans = Vector3(t.e(1,4),t.e(2,4),t.e(3,4));
00122 
00123   return trans;
00124 }
00125 
00126 inline const Orient& Transform::getRotation() const
00127 {
00128   if (isIdentity || (isPureTranslationRotation && !hasRotation)) 
00129     orient.setIdentity();
00130 
00131   if (!isPureTranslationRotation)
00132     orient.setFromRotationComponent(t);
00133 
00134   return orient;
00135 }
00136 
00137 inline bool Transform::identity() const 
00138 {
00139   return isIdentity;
00140 }
00141 
00142 inline bool Transform::isTransRotationOnly() const
00143 {
00144   return isIdentity || isPureTranslationRotation;
00145 }
00146 
00147 inline bool Transform::containsRotation() const
00148 {
00149   return !isIdentity && ( (isPureTranslationRotation && hasRotation) || !isPureTranslationRotation);
00150 }
00151 
00152 inline bool Transform::containsTranslation() const
00153 {
00154   return !isIdentity && ( (isPureTranslationRotation && hasTranslation) || !isPureTranslationRotation);
00155 }
00156 
00157 
00158 
00159 inline Point3 Transform::transform(const Point3& p) const
00160 {
00161   Point3 r(p); transformPoint(r); return r;
00162 }
00163 
00164 inline Point4 Transform::transform(const Point4& p) const
00165 {
00166   Point4 r(p); transformPoint(r); return r;
00167 }
00168 
00169 
00170 
00171 /*
00172 inline void Transform::transform(Triangle3& t) const
00173 {
00174   if (isIdentity) return;
00175   if (isPureTranslationRotation) {
00176     if (hasRotation) {
00177       t.setp1(orient.rotate(t.p1()));
00178       t.setp2(orient.rotate(t.p2()));
00179       t.setp3(orient.rotate(t.p3()));
00180     }
00181     if (hasTranslation) {
00182       t.setp1(t.p1()+trans);
00183       t.setp2(t.p2()+trans);
00184       t.setp3(t.p3()+trans);
00185     }
00186   }
00187   else {
00188     t.setp1(this->t*t.p1());
00189     t.setp2(this->t*t.p2());
00190     t.setp3(this->t*t.p3());
00191   }
00192   
00193 }
00194 */
00195 
00196 
00197 inline Transform operator*(const Transform& t1, const Transform& t2)
00198 {
00199   Transform r(t1); r*=t2; return r;
00200 }
00201 
00202 
00203 inline Point3 operator*(const Transform& t, const Point3& p)
00204 {
00205   return t.transform(p);
00206 }
00207 
00208 inline Point4 operator*(const Transform& t, const Point4& p)
00209 {
00210   return t.transform(p);
00211 }
00212 
00213 
00214 inline Transform inverse(const Transform& t)
00215 {
00216   Transform r(t); r.invert(); return r;
00217 }
00218 
00219 
00220 std::ostream& operator<<(std::ostream& out, const Transform& t); // Output
00221 
00222 
00223 } // base
00224 
00225 #endif
00226 

Generated on Thu Jul 29 15:56:16 2004 for OpenSim by doxygen 1.3.6