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

point.h

00001 // point.h (point class copied from libCoal, subsequently modified)
00002 //
00003 //  The WorldForge Project
00004 //  Copyright (C) 2000, 2001, 2002  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 
00024 // Author: Ron Steinke
00025 
00026 #ifndef WFMATH_POINT_H
00027 #define WFMATH_POINT_H
00028 
00029 #include <wfmath/const.h>
00030 #include <wfmath/vector.h>
00031 #include <wfmath/rotmatrix.h>
00032 
00033 namespace WFMath {
00034 
00035 template<const int dim> class Point;
00036 template<const int dim> class AxisBox;
00037 template<const int dim> class Ball;
00038 
00039 template<const int dim>
00040 Vector<dim> operator-(const Point<dim>& c1, const Point<dim>& c2);
00041 template<const int dim>
00042 Point<dim> operator+(const Point<dim>& c, const Vector<dim>& v);
00043 template<const int dim>
00044 Point<dim> operator-(const Point<dim>& c, const Vector<dim>& v);
00045 template<const int dim>
00046 Point<dim> operator+(const Vector<dim>& v, const Point<dim>& c);
00047 
00048 template<const int dim>
00049 Point<dim>& operator+=(Point<dim>& p, const Vector<dim>& v);
00050 template<const int dim>
00051 Point<dim>& operator-=(Point<dim>& p, const Vector<dim>& v);
00052 
00053 template<const int dim>
00054 CoordType SquaredDistance(const Point<dim>& p1, const Point<dim>& p2);
00055 template<const int dim>
00056 CoordType Distance(const Point<dim>& p1, const Point<dim>& p2)
00057         {return sqrt(SquaredDistance(p1, p2));}
00058 template<const int dim>
00059 CoordType SloppyDistance(const Point<dim>& p1, const Point<dim>& p2)
00060         {return (p1 - p2).sloppyMag();}
00061 
00062 #ifndef WFMATH_NO_TEMPLATES_AS_TEMPLATE_PARAMETERS
00063 
00064 template<const int dim, template<class> class container>
00065 Point<dim> Barycenter(const container<Point<dim> >& c);
00067 
00073 template<const int dim, template<class> class container,
00074                         template<class> class container2>
00075 Point<dim> Barycenter(const container<Point<dim> >& c,
00076                       const container2<CoordType>& weights);
00077 #endif
00078 
00079 // This is used a couple of places in the library
00080 template<const int dim>
00081 Point<dim> Midpoint(const Point<dim>& p1, const Point<dim>& p2,
00082                     CoordType dist = 0.5);
00083 
00084 template<const int dim>
00085 std::ostream& operator<<(std::ostream& os, const Point<dim>& m);
00086 template<const int dim>
00087 std::istream& operator>>(std::istream& is, Point<dim>& m);
00088 
00090 
00094 template<const int dim>
00095 class Point
00096 {
00097  public:
00099   Point () : m_valid(false) {}
00101   Point (const Point& p);
00103   explicit Point (const Atlas::Message::Object& a) {fromAtlas(a);}
00104 
00105   friend std::ostream& operator<< <dim>(std::ostream& os, const Point& p);
00106   friend std::istream& operator>> <dim>(std::istream& is, Point& p);
00107 
00109   Atlas::Message::Object toAtlas() const;
00111   void fromAtlas(const Atlas::Message::Object& a);
00112 
00113   Point& operator= (const Point& rhs);
00114 
00115   bool isEqualTo(const Point &p, double epsilon = WFMATH_EPSILON) const;
00116   bool operator== (const Point& rhs) const      {return isEqualTo(rhs);}
00117   bool operator!= (const Point& rhs) const      {return !isEqualTo(rhs);}
00118 
00119   bool isValid() const {return m_valid;}
00121   void setValid(bool valid = true) {m_valid = valid;}
00122 
00124   Point& setToOrigin();
00125 
00126   bool operator< (const Point& rhs) const;
00127 
00128   // Operators
00129 
00130   // Documented in vector.h
00131   friend Vector<dim> operator-<dim>(const Point& c1, const Point& c2);
00132   friend Point operator+<dim>(const Point& c, const Vector<dim>& v);
00133   friend Point operator-<dim>(const Point& c, const Vector<dim>& v);
00134   friend Point operator+<dim>(const Vector<dim>& v, const Point& c);
00135 
00136   friend Point& operator+=<dim>(Point& p, const Vector<dim>& rhs);
00137   friend Point& operator-=<dim>(Point& p, const Vector<dim>& rhs);
00138 
00140   Point& rotate(const RotMatrix<dim>& m, const Point& p)
00141         {return (*this = p + Prod(*this - p, m));}
00142 
00143   // Functions so that Point<> has the generic shape interface
00144 
00145   int numCorners() const {return 1;}
00146   Point<dim> getCorner(int i) const {assert(i == 0); return *this;}
00147   Point<dim> getCenter() const {return *this;}
00148 
00149   Point shift(const Vector<dim>& v) {return *this += v;}
00150   Point moveCornerTo(const Point& p, int corner)
00151         {assert(corner == 0); return operator=(p);}
00152   Point moveCenterTo(const Point& p) {return operator=(p);}
00153 
00154   Point rotateCorner(const RotMatrix<dim>& m, int corner)
00155         {assert(corner == 0); return *this;}
00156   Point rotateCenter(const RotMatrix<dim>& m) {return *this;}
00157   Point rotatePoint(const RotMatrix<dim>& m, const Point& p) {return rotate(m, p);}
00158 
00159   // The implementations of these lie in axisbox_funcs.h and
00160   // ball_funcs.h, to reduce include dependencies
00161   AxisBox<dim> boundingBox() const;
00162   Ball<dim> boundingSphere() const;
00163   Ball<dim> boundingSphereSloppy() const;
00164 
00165   // Member access
00166 
00168   CoordType operator[](const int i) const {assert(i >= 0 && i < dim); return m_elem[i];}
00170   CoordType& operator[](const int i)      {assert(i >= 0 && i < dim); return m_elem[i];}
00171 
00173   friend CoordType SquaredDistance<dim>(const Point& p1, const Point& p2);
00174 
00175 // FIXME instatiation problem when declared as friend
00176 //  template<template<class> class container>
00177 //  friend Point Barycenter(const container<Point>& c);
00178 
00180 
00186   friend Point<dim> Midpoint<dim>(const Point& p1, const Point& p2, CoordType dist);
00187 
00188   // 2D/3D stuff
00189 
00191   Point (CoordType x, CoordType y); // 2D only
00193   Point (CoordType x, CoordType y, CoordType z); // 3D only
00194 
00195   // Label the first three components of the vector as (x,y,z) for
00196   // 2D/3D convienience
00197 
00199   CoordType x() const   {assert(dim > 0); return m_elem[0];}
00201   CoordType& x()        {assert(dim > 0); return m_elem[0];}
00203   CoordType y() const   {assert(dim > 1); return m_elem[1];}
00205   CoordType& y()        {assert(dim > 1); return m_elem[1];}
00207   CoordType z() const   {assert(dim > 2); return m_elem[2];}
00209   CoordType& z()        {assert(dim > 2); return m_elem[2];}
00210 
00212   Point<2>& polar(CoordType r, CoordType theta);
00214   void asPolar(CoordType& r, CoordType& theta) const;
00215 
00217   Point<3>& polar(CoordType r, CoordType theta, CoordType z);
00219   void asPolar(CoordType& r, CoordType& theta, CoordType& z) const;
00221   Point<3>& spherical(CoordType r, CoordType theta, CoordType phi);
00223   void asSpherical(CoordType& r, CoordType& theta, CoordType& phi) const;
00224 
00225  private:
00226   CoordType m_elem[dim];
00227   bool m_valid;
00228 };
00229 
00230 } // namespace WFMath
00231 
00232 #include <wfmath/point_funcs.h>
00233 
00234 #endif  // WFMATH_POINT_H

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