1 // segment.h (A line segment)
2 //
3 //  The WorldForge Project
4 //  Copyright (C) 2000, 2001  The WorldForge Project
5 //
6 //  This program is free software; you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation; either version 2 of the License, or
9 //  (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20 //  For information about WorldForge and its authors, please contact
21 //  the Worldforge Web Site at http://www.worldforge.org.
22 //
23 
24 // Author: Ron Steinke
25 
26 #ifndef WFMATH_SEGMENT_H
27 #define WFMATH_SEGMENT_H
28 
29 #include <wfmath/point.h>
30 #include <wfmath/intersect_decls.h>
31 
32 namespace WFMath {
33 
34 template<int dim>
35 std::ostream& operator<<(std::ostream& os, const Segment<dim>& s);
36 template<int dim>
37 std::istream& operator>>(std::istream& is, Segment<dim>& s);
38 
39 /// A line segment embedded in dim dimensions
40 /**
41  * This class implements the full shape interface, as described in
42  * the fake class Shape.
43  **/
44 template<int dim = 3>
45 class Segment
46 {
47  public:
48   /// construct an uninitialized segment
Segment()49   Segment() :m_p1(), m_p2() {}
50   /// construct a segment with endpoints p1 and p2
Segment(const Point<dim> & p1,const Point<dim> & p2)51   Segment(const Point<dim>& p1, const Point<dim>& p2) : m_p1(p1), m_p2(p2) {}
52   /// construct a copy of a segment
Segment(const Segment & s)53   Segment(const Segment& s) : m_p1(s.m_p1), m_p2(s.m_p2) {}
54 
~Segment()55   ~Segment() {}
56 
57   friend std::ostream& operator<< <dim>(std::ostream& os, const Segment& s);
58   friend std::istream& operator>> <dim>(std::istream& is, Segment& s);
59 
60   Segment& operator=(const Segment& s)
61 	{m_p1 = s.m_p1; m_p2 = s.m_p2; return *this;}
62 
63   bool isEqualTo(const Segment& s, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
64 
65   bool operator==(const Segment& b) const	{return isEqualTo(b);}
66   bool operator!=(const Segment& b) const	{return !isEqualTo(b);}
67 
isValid()68   bool isValid() const {return m_p1.isValid() && m_p2.isValid();}
69 
70   // Descriptive characteristics
71 
numCorners()72   size_t numCorners() const {return 2;}
getCorner(size_t i)73   Point<dim> getCorner(size_t i) const {return i ? m_p2 : m_p1;}
getCenter()74   Point<dim> getCenter() const {return Midpoint(m_p1, m_p2);}
75 
76   /// get one end of the segment
endpoint(const int i)77   const Point<dim>& endpoint(const int i) const	{return i ? m_p2 : m_p1;}
78   /// get one end of the segment
endpoint(const int i)79   Point<dim>& endpoint(const int i)		{return i ? m_p2 : m_p1;}
80 
81   // Movement functions
82 
shift(const Vector<dim> & v)83   Segment& shift(const Vector<dim>& v)
84 	{m_p1 += v; m_p2 += v; return *this;}
85   Segment& moveCornerTo(const Point<dim>& p, size_t corner);
moveCenterTo(const Point<dim> & p)86   Segment& moveCenterTo(const Point<dim>& p)
87 	{return shift(p - getCenter());}
88 
89   Segment& rotateCorner(const RotMatrix<dim>& m, size_t corner);
rotateCenter(const RotMatrix<dim> & m)90   Segment& rotateCenter(const RotMatrix<dim>& m)
91 	{rotatePoint(m, getCenter()); return *this;}
rotatePoint(const RotMatrix<dim> & m,const Point<dim> & p)92   Segment<dim>& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
93 	{m_p1.rotate(m, p); m_p2.rotate(m, p); return *this;}
94 
95   // 3D rotation functions
96   Segment& rotateCorner(const Quaternion& q, size_t corner);
97   Segment& rotateCenter(const Quaternion& q);
98   Segment& rotatePoint(const Quaternion& q, const Point<dim>& p);
99 
100   // Intersection functions
101 
boundingBox()102   AxisBox<dim> boundingBox() const {return AxisBox<dim>(m_p1, m_p2);}
boundingSphere()103   Ball<dim> boundingSphere() const
104 	{return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
boundingSphereSloppy()105   Ball<dim> boundingSphereSloppy() const
106 	{return Ball<dim>(getCenter(), SloppyDistance(m_p1, m_p2) / 2);}
107 
108   Segment toParentCoords(const Point<dim>& origin,
109       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
110         {return Segment(m_p1.toParentCoords(origin, rotation),
111 		m_p2.toParentCoords(origin, rotation));}
toParentCoords(const AxisBox<dim> & coords)112   Segment toParentCoords(const AxisBox<dim>& coords) const
113         {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
toParentCoords(const RotBox<dim> & coords)114   Segment toParentCoords(const RotBox<dim>& coords) const
115         {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
116 
117   // toLocal is just like toParent, expect we reverse the order of
118   // translation and rotation and use the opposite sense of the rotation
119   // matrix
120 
121   Segment toLocalCoords(const Point<dim>& origin,
122       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
123         {return Segment(m_p1.toLocalCoords(origin, rotation),
124 		m_p2.toLocalCoords(origin, rotation));}
toLocalCoords(const AxisBox<dim> & coords)125   Segment toLocalCoords(const AxisBox<dim>& coords) const
126         {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
toLocalCoords(const RotBox<dim> & coords)127   Segment toLocalCoords(const RotBox<dim>& coords) const
128         {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
129 
130   // 3D only
131   Segment toParentCoords(const Point<dim>& origin,
132                          const Quaternion& rotation) const;
133   Segment toLocalCoords(const Point<dim>& origin,
134                         const Quaternion& rotation) const;
135 
136   friend bool Intersect<dim>(const Segment& s, const Point<dim>& p, bool proper);
137   friend bool Contains<dim>(const Point<dim>& p, const Segment& s, bool proper);
138 
139   friend bool Intersect<dim>(const Segment& s, const AxisBox<dim>& b, bool proper);
140   friend bool Contains<dim>(const AxisBox<dim>& b, const Segment& s, bool proper);
141 
142   friend bool Intersect<dim>(const Segment& s, const Ball<dim>& b, bool proper);
143   friend bool Contains<dim>(const Ball<dim>& b, const Segment& s, bool proper);
144 
145   friend bool Intersect<dim>(const Segment& s1, const Segment& s2, bool proper);
146   friend bool Contains<dim>(const Segment& s1, const Segment& s2, bool proper);
147 
148   friend bool Intersect<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
149   friend bool Contains<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
150   friend bool Contains<dim>(const Segment& s, const RotBox<dim>& r, bool proper);
151 
152   friend bool Intersect<dim>(const Polygon<dim>& r, const Segment& s, bool proper);
153   friend bool Contains<dim>(const Polygon<dim>& p, const Segment& s, bool proper);
154   friend bool Contains<dim>(const Segment& s, const Polygon<dim>& p, bool proper);
155 
156  private:
157 
158   Point<dim> m_p1, m_p2;
159 };
160 
161 template<int dim>
isEqualTo(const Segment<dim> & s,CoordType epsilon)162 inline bool Segment<dim>::isEqualTo(const Segment<dim>& s,
163                                     CoordType epsilon) const
164 {
165   return Equal(m_p1, s.m_p1, epsilon)
166       && Equal(m_p2, s.m_p2, epsilon);
167 }
168 
169 } // namespace WFMath
170 
171 #endif  // WFMATH_SEGMENT_H
172