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/point.h>
00030 #include <wfmath/intersect_decls.h>
00031
00032 namespace WFMath {
00033
00034 template<int dim>
00035 std::ostream& operator<<(std::ostream& os, const Segment<dim>& s);
00036 template<int dim>
00037 std::istream& operator>>(std::istream& is, Segment<dim>& s);
00038
00040
00044 template<int dim = 3>
00045 class Segment
00046 {
00047 public:
00049 Segment() :m_p1(), m_p2() {}
00051 Segment(const Point<dim>& p1, const Point<dim>& p2) : m_p1(p1), m_p2(p2) {}
00053 Segment(const Segment& s) : m_p1(s.m_p1), m_p2(s.m_p2) {}
00054
00055 ~Segment() {}
00056
00057 friend std::ostream& operator<< <dim>(std::ostream& os, const Segment& s);
00058 friend std::istream& operator>> <dim>(std::istream& is, Segment& s);
00059
00060 Segment& operator=(const Segment& s)
00061 {m_p1 = s.m_p1; m_p2 = s.m_p2; return *this;}
00062
00063 bool isEqualTo(const Segment& s, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
00064
00065 bool operator==(const Segment& b) const {return isEqualTo(b);}
00066 bool operator!=(const Segment& b) const {return !isEqualTo(b);}
00067
00068 bool isValid() const {return m_p1.isValid() && m_p2.isValid();}
00069
00070
00071
00072 size_t numCorners() const {return 2;}
00073 Point<dim> getCorner(size_t i) const {return i ? m_p2 : m_p1;}
00074 Point<dim> getCenter() const {return Midpoint(m_p1, m_p2);}
00075
00077 const Point<dim>& endpoint(const int i) const {return i ? m_p2 : m_p1;}
00079 Point<dim>& endpoint(const int i) {return i ? m_p2 : m_p1;}
00080
00081
00082
00083 Segment& shift(const Vector<dim>& v)
00084 {m_p1 += v; m_p2 += v; return *this;}
00085 Segment& moveCornerTo(const Point<dim>& p, size_t corner);
00086 Segment& moveCenterTo(const Point<dim>& p)
00087 {return shift(p - getCenter());}
00088
00089 Segment& rotateCorner(const RotMatrix<dim>& m, size_t corner);
00090 Segment& rotateCenter(const RotMatrix<dim>& m)
00091 {rotatePoint(m, getCenter()); return *this;}
00092 Segment<dim>& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00093 {m_p1.rotate(m, p); m_p2.rotate(m, p); return *this;}
00094
00095
00096 Segment& rotateCorner(const Quaternion& q, size_t corner);
00097 Segment& rotateCenter(const Quaternion& q);
00098 Segment& rotatePoint(const Quaternion& q, const Point<dim>& p);
00099
00100
00101
00102 AxisBox<dim> boundingBox() const {return AxisBox<dim>(m_p1, m_p2);}
00103 Ball<dim> boundingSphere() const
00104 {return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
00105 Ball<dim> boundingSphereSloppy() const
00106 {return Ball<dim>(getCenter(), SloppyDistance(m_p1, m_p2) / 2);}
00107
00108 Segment toParentCoords(const Point<dim>& origin,
00109 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00110 {return Segment(m_p1.toParentCoords(origin, rotation),
00111 m_p2.toParentCoords(origin, rotation));}
00112 Segment toParentCoords(const AxisBox<dim>& coords) const
00113 {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
00114 Segment toParentCoords(const RotBox<dim>& coords) const
00115 {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
00116
00117
00118
00119
00120
00121 Segment toLocalCoords(const Point<dim>& origin,
00122 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00123 {return Segment(m_p1.toLocalCoords(origin, rotation),
00124 m_p2.toLocalCoords(origin, rotation));}
00125 Segment toLocalCoords(const AxisBox<dim>& coords) const
00126 {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
00127 Segment toLocalCoords(const RotBox<dim>& coords) const
00128 {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
00129
00130
00131 Segment toParentCoords(const Point<dim>& origin,
00132 const Quaternion& rotation) const;
00133 Segment toLocalCoords(const Point<dim>& origin,
00134 const Quaternion& rotation) const;
00135
00136 friend bool Intersect<dim>(const Segment& s, const Point<dim>& p, bool proper);
00137 friend bool Contains<dim>(const Point<dim>& p, const Segment& s, bool proper);
00138
00139 friend bool Intersect<dim>(const Segment& s, const AxisBox<dim>& b, bool proper);
00140 friend bool Contains<dim>(const AxisBox<dim>& b, const Segment& s, bool proper);
00141
00142 friend bool Intersect<dim>(const Segment& s, const Ball<dim>& b, bool proper);
00143 friend bool Contains<dim>(const Ball<dim>& b, const Segment& s, bool proper);
00144
00145 friend bool Intersect<dim>(const Segment& s1, const Segment& s2, bool proper);
00146 friend bool Contains<dim>(const Segment& s1, const Segment& s2, bool proper);
00147
00148 friend bool Intersect<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00149 friend bool Contains<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00150 friend bool Contains<dim>(const Segment& s, const RotBox<dim>& r, bool proper);
00151
00152 friend bool Intersect<dim>(const Polygon<dim>& r, const Segment& s, bool proper);
00153 friend bool Contains<dim>(const Polygon<dim>& p, const Segment& s, bool proper);
00154 friend bool Contains<dim>(const Segment& s, const Polygon<dim>& p, bool proper);
00155
00156 private:
00157
00158 Point<dim> m_p1, m_p2;
00159 };
00160
00161 template<int dim>
00162 inline bool Segment<dim>::isEqualTo(const Segment<dim>& s,
00163 CoordType epsilon) const
00164 {
00165 return Equal(m_p1, s.m_p1, epsilon)
00166 && Equal(m_p2, s.m_p2, epsilon);
00167 }
00168
00169 }
00170
00171 #endif // WFMATH_SEGMENT_H