00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
00129
00130
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
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
00160
00161 AxisBox<dim> boundingBox() const;
00162 Ball<dim> boundingSphere() const;
00163 Ball<dim> boundingSphereSloppy() const;
00164
00165
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
00176
00177
00178
00180
00186 friend Point<dim> Midpoint<dim>(const Point& p1, const Point& p2, CoordType dist);
00187
00188
00189
00191 Point (CoordType x, CoordType y);
00193
00194
00195
00196
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 }
00231
00232 #include <wfmath/point_funcs.h>
00233
00234 #endif // WFMATH_POINT_H