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_SEGMENT_H
00027 #define WFMATH_SEGMENT_H
00028
00029 #include <wfmath/const.h>
00030 #include <wfmath/vector.h>
00031 #include <wfmath/point.h>
00032 #include <wfmath/axisbox.h>
00033 #include <wfmath/ball.h>
00034 #include <wfmath/intersect_decls.h>
00035
00036 namespace WFMath {
00037
00038 template<const int dim> class Segment;
00039
00040 template<const int dim>
00041 std::ostream& operator<<(std::ostream& os, const Segment<dim>& s);
00042 template<const int dim>
00043 std::istream& operator>>(std::istream& is, Segment<dim>& s);
00044
00046
00050 template<const int dim>
00051 class Segment
00052 {
00053 public:
00055 Segment() {}
00057 Segment(const Point<dim>& p1, const Point<dim>& p2) : m_p1(p1), m_p2(p2) {}
00059 Segment(const Segment& s) : m_p1(s.m_p1), m_p2(s.m_p2) {}
00060
00061 ~Segment() {}
00062
00063 friend std::ostream& operator<< <dim>(std::ostream& os, const Segment& s);
00064 friend std::istream& operator>> <dim>(std::istream& is, Segment& s);
00065
00066 Segment& operator=(const Segment& s)
00067 {m_p1 = s.m_p1; m_p2 = s.m_p2; return *this;}
00068
00069 bool isEqualTo(const Segment& s, double epsilon = WFMATH_EPSILON) const;
00070
00071 bool operator==(const Segment& b) const {return isEqualTo(b);}
00072 bool operator!=(const Segment& b) const {return !isEqualTo(b);}
00073
00074 bool isValid() const {return m_p1.isValid() && m_p2.isValid();}
00075
00076 bool operator< (const Segment& s) const;
00077
00078
00079
00080 int numCorners() const {return 2;}
00081 Point<dim> getCorner(int i) const {assert(i == 0 || i == 1); return i ? m_p2 : m_p1;}
00082 Point<dim> getCenter() const {return Midpoint(m_p1, m_p2);}
00083
00085 const Point<dim>& endpoint(const int i) const {return i ? m_p2 : m_p1;}
00087 Point<dim>& endpoint(const int i) {return i ? m_p2 : m_p1;}
00088
00089
00090
00091 Segment& shift(const Vector<dim>& v)
00092 {m_p1 += v; m_p2 += v; return *this;}
00093 Segment& moveCornerTo(const Point<dim>& p, int corner);
00094 Segment& moveCenterTo(const Point<dim>& p)
00095 {return shift(p - getCenter());}
00096
00097 Segment& rotateCorner(const RotMatrix<dim>& m, int corner);
00098 Segment& rotateCenter(const RotMatrix<dim>& m)
00099 {rotatePoint(m, getCenter()); return *this;}
00100 Segment<dim>& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00101 {m_p1.rotate(m, p); m_p2.rotate(m, p); return *this;}
00102
00103
00104
00105 AxisBox<dim> boundingBox() const {return AxisBox<dim>(m_p1, m_p2);}
00106 Ball<dim> boundingSphere() const
00107 {return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
00108 Ball<dim> boundingSphereSloppy() const
00109 {return Ball<dim>(getCenter(), SloppyDistance(m_p1, m_p2) / 2);}
00110
00111 friend bool Intersect<dim>(const Segment& s, const Point<dim>& p, bool proper);
00112 friend bool Contains<dim>(const Point<dim>& p, const Segment& s, bool proper);
00113
00114 friend bool Intersect<dim>(const Segment& s, const AxisBox<dim>& b, bool proper);
00115 friend bool Contains<dim>(const AxisBox<dim>& b, const Segment& s, bool proper);
00116
00117 friend bool Intersect<dim>(const Segment& s, const Ball<dim>& b, bool proper);
00118 friend bool Contains<dim>(const Ball<dim>& b, const Segment& s, bool proper);
00119
00120 friend bool Intersect<dim>(const Segment& s1, const Segment& s2, bool proper);
00121 friend bool Contains<dim>(const Segment& s1, const Segment& s2, bool proper);
00122
00123 friend bool Intersect<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00124 friend bool Contains<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00125 friend bool Contains<dim>(const Segment& s, const RotBox<dim>& r, bool proper);
00126
00127 friend bool Intersect<dim>(const Polygon<dim>& r, const Segment& s, bool proper);
00128 friend bool Contains<dim>(const Polygon<dim>& p, const Segment& s, bool proper);
00129 friend bool Contains<dim>(const Segment& s, const Polygon<dim>& p, bool proper);
00130
00131 private:
00132
00133 Point<dim> m_p1, m_p2;
00134 };
00135
00136 }
00137
00138 #include <wfmath/segment_funcs.h>
00139
00140 #endif // WFMATH_SEGMENT_H