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

vector.h

00001 // vector.h (Vector<> 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 // Extensive amounts of this material come from the Vector2D
00027 // and Vector3D classes from stage/math, written by Bryce W.
00028 // Harrington, Kosh, and Jari Sundell (Rakshasa).
00029 
00030 #ifndef WFMATH_VECTOR_H
00031 #define WFMATH_VECTOR_H
00032 
00033 #include <wfmath/const.h>
00034 #include <wfmath/zero.h>
00035 
00036 #include <iosfwd>
00037 
00038 namespace WFMath {
00039 
00040 template<const int dim>
00041 Vector<dim>& operator+=(Vector<dim>& v1, const Vector<dim>& v2);
00042 template<const int dim>
00043 Vector<dim>& operator-=(Vector<dim>& v1, const Vector<dim>& v2);
00044 template<const int dim>
00045 Vector<dim>& operator*=(Vector<dim>& v, CoordType d);
00046 template<const int dim>
00047 Vector<dim>& operator/=(Vector<dim>& v, CoordType d);
00048 
00049 template<const int dim>
00050 Vector<dim> operator+(const Vector<dim>& v1, const Vector<dim>& v2);
00051 template<const int dim>
00052 Vector<dim> operator-(const Vector<dim>& v1, const Vector<dim>& v2);
00053 template<const int dim>
00054 Vector<dim> operator-(const Vector<dim>& v); // Unary minus
00055 template<const int dim>
00056 Vector<dim> operator*(CoordType d, const Vector<dim>& v);
00057 template<const int dim>
00058 Vector<dim> operator*(const Vector<dim>& v, CoordType d);
00059 template<const int dim>
00060 Vector<dim> operator/(const Vector<dim>& v, CoordType d);
00061 
00062 template<const int dim>
00063 CoordType Dot(const Vector<dim>& v1, const Vector<dim>& v2);
00064 
00065 template<const int dim>
00066 CoordType Angle(const Vector<dim>& v, const Vector<dim>& u);
00067 
00068 // The following are defined in rotmatrix_funcs.h
00070 template<const int dim> // m * v
00071 Vector<dim> Prod(const RotMatrix<dim>& m, const Vector<dim>& v);
00073 template<const int dim> // m^-1 * v
00074 Vector<dim> InvProd(const RotMatrix<dim>& m, const Vector<dim>& v);
00076 
00079 template<const int dim> // v * m
00080 Vector<dim> Prod(const Vector<dim>& v, const RotMatrix<dim>& m);
00082 template<const int dim> // v * m^-1
00083 Vector<dim> ProdInv(const Vector<dim>& v, const RotMatrix<dim>& m);
00084 
00086 template<const int dim>
00087 Vector<dim> operator*(const RotMatrix<dim>& m, const Vector<dim>& v);
00089 template<const int dim>
00090 Vector<dim> operator*(const Vector<dim>& v, const RotMatrix<dim>& m);
00091 
00092 template<const int dim>
00093 Vector<dim> operator-(const Point<dim>& c1, const Point<dim>& c2);
00094 template<const int dim>
00095 Point<dim> operator+(const Point<dim>& c, const Vector<dim>& v);
00096 template<const int dim>
00097 Point<dim> operator-(const Point<dim>& c, const Vector<dim>& v);
00098 template<const int dim>
00099 Point<dim> operator+(const Vector<dim>& v, const Point<dim>& c);
00100 
00101 template<const int dim>
00102 Point<dim>& operator+=(Point<dim>& p, const Vector<dim>& v);
00103 template<const int dim>
00104 Point<dim>& operator-=(Point<dim>& p, const Vector<dim>& v);
00105 
00106 template<const int dim>
00107 std::ostream& operator<<(std::ostream& os, const Vector<dim>& v);
00108 template<const int dim>
00109 std::istream& operator>>(std::istream& is, Vector<dim>& v);
00110 
00112 
00116 template<const int dim>
00117 class Vector {
00118  friend class ZeroPrimitive<Vector<dim> >;
00119  public:
00121   Vector() : m_valid(false) {}
00123   Vector(const Vector& v);
00125   explicit Vector(const AtlasInType& a);
00127   explicit Vector(const Point<dim> point) {
00128     for (int i = 0; i < dim; ++i) {
00129       m_elem[i] = point.elements()[i];
00130     }
00131     m_valid = true;
00132   }
00133 
00137   static const Vector<dim>& ZERO()
00138   {
00139     static ZeroPrimitive<Vector<dim> > zeroVector(dim);
00140     return zeroVector.getShape();
00141   }
00142   
00143   friend std::ostream& operator<< <dim>(std::ostream& os, const Vector& v);
00144   friend std::istream& operator>> <dim>(std::istream& is, Vector& v);
00145 
00147   AtlasOutType toAtlas() const;
00149   void fromAtlas(const AtlasInType& a);
00150 
00151   Vector& operator=(const Vector& v);
00152 
00153   bool isEqualTo(const Vector& v, double epsilon = WFMATH_EPSILON) const;
00154   bool operator==(const Vector& v) const {return isEqualTo(v);}
00155   bool operator!=(const Vector& v) const {return !isEqualTo(v);}
00156 
00157   bool isValid() const {return m_valid;}
00159   void setValid(bool valid = true) {m_valid = valid;}
00160 
00162   Vector& zero();
00163 
00164   // Math operators
00165 
00167   friend Vector& operator+=<dim>(Vector& v1, const Vector& v2);
00169   friend Vector& operator-=<dim>(Vector& v1, const Vector& v2);
00171   friend Vector& operator*=<dim>(Vector& v, CoordType d);
00173   friend Vector& operator/=<dim>(Vector& v, CoordType d);
00174 
00176   friend Vector operator+<dim>(const Vector& v1, const Vector& v2);
00178   friend Vector operator-<dim>(const Vector& v1, const Vector& v2);
00180   friend Vector operator-<dim>(const Vector& v); // Unary minus
00182   friend Vector operator*<dim>(CoordType d, const Vector& v);
00184   friend Vector operator*<dim>(const Vector& v, CoordType d);
00186   friend Vector operator/<dim>(const Vector& v, CoordType d);
00187 
00188   // documented outside the class definition
00189   friend Vector Prod<dim>(const RotMatrix<dim>& m, const Vector& v);
00190   friend Vector InvProd<dim>(const RotMatrix<dim>& m, const Vector& v);
00191 
00193   CoordType operator[](const int i) const {return m_elem[i];}
00195   CoordType& operator[](const int i)      {return m_elem[i];}
00196 
00198   friend Vector operator-<dim>(const Point<dim>& c1, const Point<dim>& c2);
00200   friend Point<dim> operator+<dim>(const Point<dim>& c, const Vector& v);
00202   friend Point<dim> operator-<dim>(const Point<dim>& c, const Vector& v);
00204   friend Point<dim> operator+<dim>(const Vector& v, const Point<dim>& c);
00205 
00207   friend Point<dim>& operator+=<dim>(Point<dim>& p, const Vector& rhs);
00209   friend Point<dim>& operator-=<dim>(Point<dim>& p, const Vector& rhs);
00210 
00212   friend CoordType Dot<dim>(const Vector& v1, const Vector& v2);
00214   friend CoordType Angle<dim>(const Vector& v, const Vector& u);
00215 
00217   CoordType sqrMag() const;
00219   CoordType mag() const         {return (CoordType) sqrt(sqrMag());}
00221   Vector& normalize(CoordType norm = 1.0)
00222   {CoordType themag = mag(); return (*this *= norm / themag);}
00223 
00225 
00236   CoordType sloppyMag() const;
00238 
00243   Vector& sloppyNorm(CoordType norm = 1.0);
00244 
00245   // Can't seem to implement these as constants, implementing
00246   // inline lookup functions instead.
00248   static const CoordType sloppyMagMax();
00250 
00256   static const CoordType sloppyMagMaxSqrt();
00257 
00259   Vector& rotate(int axis1, int axis2, CoordType theta);
00260 
00262 
00265   Vector& rotate(const Vector& v1, const Vector& v2, CoordType theta);
00266 
00268   Vector& rotate(const RotMatrix<dim>&);
00269 
00270   // mirror image functions
00271 
00273   Vector& mirror(const int i) { m_elem[i] *= -1; return *this;}
00275   Vector& mirror(const Vector& v)
00276   {return operator-=(*this, 2 * v * Dot(v, *this) / v.sqrMag());}
00278 
00281   Vector& mirror()              {return operator*=(*this, -1);}
00282 
00283   // Specialized 2D/3D stuff starts here
00284 
00285   // The following functions are defined only for
00286   // two dimensional (rotate(CoordType), Vector<>(CoordType, CoordType))
00287   // and three dimensional (the rest of them) vectors.
00288   // Attempting to call these on any other vector will
00289   // result in a linker error.
00290 
00292   Vector(CoordType x, CoordType y);
00294   Vector(CoordType x, CoordType y, CoordType z);
00295 
00297   Vector<2>& rotate(CoordType theta);
00298 
00300   Vector<3>& rotateX(CoordType theta);
00302   Vector<3>& rotateY(CoordType theta);
00304   Vector<3>& rotateZ(CoordType theta);
00305 
00307   Vector<3>& rotate(const Vector<3>& axis, CoordType theta);
00309   Vector<3>& rotate(const Quaternion& q);
00310 
00311   // Label the first three components of the vector as (x,y,z) for
00312   // 2D/3D convienience
00313 
00315   CoordType x() const   {return m_elem[0];}
00317   CoordType& x()        {return m_elem[0];}
00319   CoordType y() const   {return m_elem[1];}
00321   CoordType& y()        {return m_elem[1];}
00323   CoordType z() const   {return m_elem[2];}
00325   CoordType& z()        {return m_elem[2];}
00326 
00328   Vector& mirrorX()     {return mirror(0);}
00330   Vector& mirrorY()     {return mirror(1);}
00332   Vector& mirrorZ()     {return mirror(2);}
00333 
00335   Vector<2>& polar(CoordType r, CoordType theta);
00337   void asPolar(CoordType& r, CoordType& theta) const;
00338 
00340   Vector<3>& polar(CoordType r, CoordType theta, CoordType z);
00342   void asPolar(CoordType& r, CoordType& theta, CoordType& z) const;
00344   Vector<3>& spherical(CoordType r, CoordType theta, CoordType phi);
00346   void asSpherical(CoordType& r, CoordType& theta, CoordType& phi) const;
00347 
00348   // FIXME make Cross() a friend function, and make this private
00349   double _scaleEpsilon(const Vector& v, double epsilon = WFMATH_EPSILON) const
00350   {return _ScaleEpsilon(m_elem, v.m_elem, dim, epsilon);}
00351 
00352   const CoordType* elements() const {return m_elem;}
00353 
00354  private:
00355   CoordType m_elem[dim];
00356   bool m_valid;
00357 };
00358 
00360 CoordType Cross(const Vector<2>& v1, const Vector<2>& v2);
00362 Vector<3> Cross(const Vector<3>& v1, const Vector<3>& v2);
00363 
00365 
00370 template<const int dim>
00371 bool Parallel(const Vector<dim>& v1, const Vector<dim>& v2, bool& same_dir);
00372 
00374 
00377 template<const int dim>
00378 bool Parallel(const Vector<dim>& v1, const Vector<dim>& v2);
00379 
00381 template<const int dim>
00382 bool Perpendicular(const Vector<dim>& v1, const Vector<dim>& v2);
00383 
00384 } // namespace WFMath
00385 
00386 #endif // WFMATH_VECTOR_H

Generated for WFMath by  doxygen 1.7.1