Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

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 namespace WFMath {
00032 
00033 template<const int dim> class Vector;
00034 class Quaternion;
00035 
00036 template<const int dim> class RotMatrix;
00037 
00039 template<const int dim> // m1 * m2
00040 RotMatrix<dim> Prod(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00042 template<const int dim> // m1 * m2^-1
00043 RotMatrix<dim> ProdInv(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00045 template<const int dim> // m1^-1 * m2
00046 RotMatrix<dim> InvProd(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00048 template<const int dim> // m1^-1 * m2^-1
00049 RotMatrix<dim> InvProdInv(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00050 
00051 template<const int dim> // m * v
00052 Vector<dim> Prod(const RotMatrix<dim>& m, const Vector<dim>& v);
00053 template<const int dim> // m^-1 * v
00054 Vector<dim> InvProd(const RotMatrix<dim>& m, const Vector<dim>& v);
00055 template<const int dim> // v * m
00056 Vector<dim> Prod(const Vector<dim>& v, const RotMatrix<dim>& m);
00057 template<const int dim> // v * m^-1
00058 Vector<dim> ProdInv(const Vector<dim>& v, const RotMatrix<dim>& m);
00059 
00061 template<const int dim>
00062 RotMatrix<dim> operator*(const RotMatrix<dim>& m1, const RotMatrix<dim>& m2);
00063 template<const int dim>
00064 Vector<dim> operator*(const RotMatrix<dim>& m, const Vector<dim>& v);
00065 template<const int dim>
00066 Vector<dim> operator*(const Vector<dim>& v, const RotMatrix<dim>& m);
00067 
00068 template<const int dim>
00069 std::ostream& operator<<(std::ostream& os, const RotMatrix<dim>& m);
00070 template<const int dim>
00071 std::istream& operator>>(std::istream& is, RotMatrix<dim>& m);
00072 
00074 
00089 template<const int dim>
00090 class RotMatrix {
00091  public:
00093   RotMatrix() : m_valid(false) {}
00095   RotMatrix(const RotMatrix& m);
00096 
00097   friend std::ostream& operator<< <dim>(std::ostream& os, const RotMatrix& m);
00098   friend std::istream& operator>> <dim>(std::istream& is, RotMatrix& m);
00099 
00100   RotMatrix& operator=(const RotMatrix& m);
00101   // No operator=(CoordType d[dim][dim]), since it can fail.
00102   // Use setVals() instead.
00103 
00104   bool isEqualTo(const RotMatrix& m, double epsilon = WFMATH_EPSILON) const;
00105 
00106   bool operator==(const RotMatrix& m) const {return isEqualTo(m);}
00107   bool operator!=(const RotMatrix& m) const {return !isEqualTo(m);}
00108 
00109   bool isValid() const {return m_valid;}
00110 
00112   RotMatrix& identity();
00113 
00114   bool operator< (const RotMatrix& m) const;
00115 
00117   CoordType elem(const int i, const int j) const
00118         {assert(i >= 0 && j >= 0 && i < dim && j < dim); return m_elem[i][j];}
00119 
00121 
00128   bool setVals(const CoordType vals[dim][dim], double precision = WFMATH_EPSILON);
00130 
00137   bool setVals(const CoordType vals[dim*dim], double precision = WFMATH_EPSILON);
00138 
00140   Vector<dim> row(const int i) const;
00142   Vector<dim> column(const int i) const;
00143 
00145   CoordType trace() const;
00147 
00150   CoordType determinant() const {return (CoordType) (m_flip ? -1 : 1);}
00152 
00155   RotMatrix inverse() const;
00157 
00160   bool parity() const {return m_flip;}
00161 
00162   // documented outside the class
00163 
00164   friend RotMatrix Prod<dim>       (const RotMatrix& m1, const RotMatrix& m2);
00165   friend RotMatrix ProdInv<dim>    (const RotMatrix& m1, const RotMatrix& m2);
00166   friend RotMatrix InvProd<dim>    (const RotMatrix& m1, const RotMatrix& m2);
00167   friend RotMatrix InvProdInv<dim> (const RotMatrix& m1, const RotMatrix& m2);
00168   friend Vector<dim> Prod<dim>     (const RotMatrix& m, const Vector<dim>& v);
00169   friend Vector<dim> InvProd<dim>  (const RotMatrix& m, const Vector<dim>& v);
00170 
00171   // Set the value to a given rotation
00172 
00174   RotMatrix& rotation   (const int i, const int j, CoordType theta);
00176 
00179   RotMatrix& rotation   (const Vector<dim>& v1, const Vector<dim>& v2,
00180                          CoordType theta);
00182 
00187   RotMatrix& rotation   (const Vector<dim>& from, const Vector<dim>& to);
00188 
00189   // Set the value to mirror image about a certain axis
00190 
00192   RotMatrix& mirror(const int i);
00194   RotMatrix& mirror(const Vector<dim>& v);
00196 
00199   RotMatrix& mirror();
00200 
00201   // 2D/3D stuff
00202 
00204 
00210   RotMatrix(const Quaternion& q, const bool not_flip = true)
00211         {fromQuaternion(q, not_flip);}
00212 
00214   RotMatrix<2>& rotation(CoordType theta)
00215         {return rotation(0, 1, theta);}
00216 
00218   RotMatrix<3>& rotationX(CoordType theta) {return rotation(1, 2, theta);}
00220   RotMatrix<3>& rotationY(CoordType theta) {return rotation(2, 0, theta);}
00222   RotMatrix<3>& rotationZ(CoordType theta) {return rotation(0, 1, theta);}
00224   RotMatrix<3>& rotation(const Vector<3>& axis, CoordType theta);
00226 
00229   RotMatrix<3>& rotation(const Vector<3>& axis); // angle taken from magnitude of axis
00230 
00232 
00238   RotMatrix<3>& fromQuaternion(const Quaternion& q, const bool not_flip = true);
00239 
00241   RotMatrix& mirrorX()  {return mirror(0);}
00243   RotMatrix& mirrorY()  {return mirror(1);}
00245   RotMatrix& mirrorZ()  {return mirror(2);}
00246 
00247  private:
00248   CoordType m_elem[dim][dim];
00249   bool m_flip; // True if the matrix is parity odd
00250   bool m_valid;
00251 
00252   // Backend to setVals() above, also used in fromStream()
00253   bool _setVals(CoordType *vals, double precision = WFMATH_EPSILON);
00254 };
00255 
00256 } // namespace WFMath
00257 
00258 #include <wfmath/rotmatrix_funcs.h>
00259 
00260 #endif // WFMATH_ROTMATRIX_H

Generated on Wed May 28 09:20:32 2003 for WFMath by doxygen1.3-rc3