• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

rotmatrix.h

00001 // rotmatrix.h (RotMatrix<> class definition)
00002 //
00003 //  The WorldForge Project
00004 //  Copyright (C) 2001  The WorldForge Project
00005 //
00006 //  This program is free software; you can redistribute it and/or modify
00007 //  it under the terms of the GNU General Public License as published by
00008 //  the Free Software Foundation; either version 2 of the License, or
00009 //  (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 //
00020 //  For information about WorldForge and its authors, please contact
00021 //  the Worldforge Web Site at http://www.worldforge.org.
00022 
00023 // Author: Ron Steinke
00024 // Created: 2001-12-7
00025 
00026 #ifndef WFMATH_ROTMATRIX_H
00027 #define WFMATH_ROTMATRIX_H
00028 
00029 #include <wfmath/const.h>
00030 
00031 #include <iosfwd>
00032 
00033 namespace WFMath {
00034 
00035 template<const int dim> class Vector;
00036 class Quaternion;
00037 
00038 template<const int dim> class RotMatrix;
00039 
00041 template<const int dim> // m1 * m2
00042 RotMatrix<dim> Prod(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00044 template<const int dim> // m1 * m2^-1
00045 RotMatrix<dim> ProdInv(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00047 template<const int dim> // m1^-1 * m2
00048 RotMatrix<dim> InvProd(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00050 template<const int dim> // m1^-1 * m2^-1
00051 RotMatrix<dim> InvProdInv(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00052 
00053 template<const int dim> // m * v
00054 Vector<dim> Prod(const RotMatrix<dim>& m, const Vector<dim>& v);
00055 template<const int dim> // m^-1 * v
00056 Vector<dim> InvProd(const RotMatrix<dim>& m, const Vector<dim>& v);
00057 template<const int dim> // v * m
00058 Vector<dim> Prod(const Vector<dim>& v, const RotMatrix<dim>& m);
00059 template<const int dim> // v * m^-1
00060 Vector<dim> ProdInv(const Vector<dim>& v, const RotMatrix<dim>& m);
00061 
00063 template<const int dim>
00064 RotMatrix<dim> operator*(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00065 template<const int dim>
00066 Vector<dim> operator*(const RotMatrix<dim>& m, const Vector<dim>& v);
00067 template<const int dim>
00068 Vector<dim> operator*(const Vector<dim>& v, const RotMatrix<dim>& m);
00069 
00070 template<const int dim>
00071 std::ostream& operator<<(std::ostream& os, const RotMatrix<dim>& m);
00072 template<const int dim>
00073 std::istream& operator>>(std::istream& is, RotMatrix<dim>& m);
00074 
00076 
00091 template<const int dim>
00092 class RotMatrix {
00093  public:
00095   RotMatrix() : m_valid(false) {}
00097   RotMatrix(const RotMatrix& m);
00098 
00099   friend std::ostream& operator<< <dim>(std::ostream& os, const RotMatrix& m);
00100   friend std::istream& operator>> <dim>(std::istream& is, RotMatrix& m);
00101 
00102   RotMatrix& operator=(const RotMatrix& m);
00103   // No operator=(CoordType d[dim][dim]), since it can fail.
00104   // Use setVals() instead.
00105 
00106   bool isEqualTo(const RotMatrix& m, double epsilon = WFMATH_EPSILON) const;
00107 
00108   bool operator==(const RotMatrix& m) const {return isEqualTo(m);}
00109   bool operator!=(const RotMatrix& m) const {return !isEqualTo(m);}
00110 
00111   bool isValid() const {return m_valid;}
00112 
00114   RotMatrix& identity();
00115 
00117   CoordType elem(const int i, const int j) const {return m_elem[i][j];}
00118 
00120 
00127   bool setVals(const CoordType vals[dim][dim], double precision = WFMATH_EPSILON);
00129 
00136   bool setVals(const CoordType vals[dim*dim], double precision = WFMATH_EPSILON);
00137 
00139   Vector<dim> row(const int i) const;
00141   Vector<dim> column(const int i) const;
00142 
00144   CoordType trace() const;
00146 
00149   CoordType determinant() const {return (CoordType) (m_flip ? -1 : 1);}
00151 
00154   RotMatrix inverse() const;
00156 
00159   bool parity() const {return m_flip;}
00160 
00161   // documented outside the class
00162 
00163   friend RotMatrix Prod<dim>       (const RotMatrix& m1, const RotMatrix& m2);
00164   friend RotMatrix ProdInv<dim>    (const RotMatrix& m1, const RotMatrix& m2);
00165   friend RotMatrix InvProd<dim>    (const RotMatrix& m1, const RotMatrix& m2);
00166   friend RotMatrix InvProdInv<dim> (const RotMatrix& m1, const RotMatrix& m2);
00167   friend Vector<dim> Prod<dim>     (const RotMatrix& m, const Vector<dim>& v);
00168   friend Vector<dim> InvProd<dim>  (const RotMatrix& m, const Vector<dim>& v);
00169 
00170   // Set the value to a given rotation
00171 
00173   RotMatrix& rotation   (const int i, const int j, CoordType theta);
00175 
00178   RotMatrix& rotation   (const Vector<dim>& v1, const Vector<dim>& v2,
00179                          CoordType theta);
00181 
00186   RotMatrix& rotation   (const Vector<dim>& from, const Vector<dim>& to);
00187 
00188   // Set the value to mirror image about a certain axis
00189 
00191   RotMatrix& mirror(const int i);
00193   RotMatrix& mirror(const Vector<dim>& v);
00195 
00198   RotMatrix& mirror();
00199 
00201   RotMatrix& rotate(const RotMatrix& m) {return *this = Prod(*this, m);}
00202 
00204   void normalize();
00206   unsigned age() const {return m_age;}
00207 
00208   // 2D/3D stuff
00209 
00211 
00217   RotMatrix(const Quaternion& q, const bool not_flip = true)
00218         {fromQuaternion(q, not_flip);}
00219 
00221   RotMatrix<2>& rotation(CoordType theta)
00222         {return rotation(0, 1, theta);}
00223 
00225   RotMatrix<3>& rotationX(CoordType theta) {return rotation(1, 2, theta);}
00227   RotMatrix<3>& rotationY(CoordType theta) {return rotation(2, 0, theta);}
00229   RotMatrix<3>& rotationZ(CoordType theta) {return rotation(0, 1, theta);}
00231   RotMatrix<3>& rotation(const Vector<3>& axis, CoordType theta);
00233 
00236   RotMatrix<3>& rotation(const Vector<3>& axis); // angle taken from magnitude of axis
00237 
00239 
00245   RotMatrix<3>& fromQuaternion(const Quaternion& q, const bool not_flip = true);
00246 
00248   RotMatrix<3>& rotate(const Quaternion&);
00249 
00251   RotMatrix& mirrorX()  {return mirror(0);}
00253   RotMatrix& mirrorY()  {return mirror(1);}
00255   RotMatrix& mirrorZ()  {return mirror(2);}
00256 
00257  private:
00258   CoordType m_elem[dim][dim];
00259   bool m_flip; // True if the matrix is parity odd
00260   bool m_valid;
00261   unsigned m_age;
00262 
00263   // Backend to setVals() above, also used in fromStream()
00264   bool _setVals(CoordType *vals, double precision = WFMATH_EPSILON);
00265   void checkNormalization() {if(m_age >= WFMATH_MAX_NORM_AGE && m_valid) normalize();}
00266 };
00267 
00268 } // namespace WFMath
00269 
00270 #include <wfmath/rotmatrix_funcs.h>
00271 
00272 #endif // WFMATH_ROTMATRIX_H

Generated for WFMath by  doxygen 1.7.2