wfmath  1.0.3
A math library for the Worldforge system.
segment.h
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 
40 
44 template<int dim = 3>
45 class Segment
46 {
47  public:
49  Segment() : m_p1{}, m_p2{} {}
51  Segment(const Point<dim>& p1, const Point<dim>& p2) : m_p1(p1), m_p2(p2) {}
53  Segment(const Segment& s) = default;
54 
55  ~Segment() = default;
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) = default;
61 
62  bool isEqualTo(const Segment& s, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
63 
64  bool operator==(const Segment& b) const {return isEqualTo(b);}
65  bool operator!=(const Segment& b) const {return !isEqualTo(b);}
66 
67  bool isValid() const {return m_p1.isValid() && m_p2.isValid();}
68 
69  // Descriptive characteristics
70 
71  size_t numCorners() const {return 2;}
72  Point<dim> getCorner(size_t i) const {return i ? m_p2 : m_p1;}
73  Point<dim> getCenter() const {return Midpoint(m_p1, m_p2);}
74 
76  const Point<dim>& endpoint(const int i) const {return i ? m_p2 : m_p1;}
78  Point<dim>& endpoint(const int i) {return i ? m_p2 : m_p1;}
79 
80  // Movement functions
81 
82  Segment& shift(const Vector<dim>& v)
83  {m_p1 += v; m_p2 += v; return *this;}
84  Segment& moveCornerTo(const Point<dim>& p, size_t corner);
85  Segment& moveCenterTo(const Point<dim>& p)
86  {return shift(p - getCenter());}
87 
88  Segment& rotateCorner(const RotMatrix<dim>& m, size_t corner);
89  Segment& rotateCenter(const RotMatrix<dim>& m)
90  {rotatePoint(m, getCenter()); return *this;}
91  Segment<dim>& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
92  {m_p1.rotate(m, p); m_p2.rotate(m, p); return *this;}
93 
94  // 3D rotation functions
95  Segment& rotateCorner(const Quaternion& q, size_t corner);
96  Segment& rotateCenter(const Quaternion& q);
97  Segment& rotatePoint(const Quaternion& q, const Point<dim>& p);
98 
99  // Intersection functions
100 
101  AxisBox<dim> boundingBox() const {return AxisBox<dim>(m_p1, m_p2);}
102  Ball<dim> boundingSphere() const
103  {return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
104  Ball<dim> boundingSphereSloppy() const
105  {return Ball<dim>(getCenter(), SloppyDistance(m_p1, m_p2) / 2);}
106 
107  Segment toParentCoords(const Point<dim>& origin,
108  const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
109  {return Segment(m_p1.toParentCoords(origin, rotation),
110  m_p2.toParentCoords(origin, rotation));}
111  Segment toParentCoords(const AxisBox<dim>& coords) const
112  {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
113  Segment toParentCoords(const RotBox<dim>& coords) const
114  {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
115 
116  // toLocal is just like toParent, expect we reverse the order of
117  // translation and rotation and use the opposite sense of the rotation
118  // matrix
119 
120  Segment toLocalCoords(const Point<dim>& origin,
121  const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
122  {return Segment(m_p1.toLocalCoords(origin, rotation),
123  m_p2.toLocalCoords(origin, rotation));}
124  Segment toLocalCoords(const AxisBox<dim>& coords) const
125  {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
126  Segment toLocalCoords(const RotBox<dim>& coords) const
127  {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
128 
129  // 3D only
130  Segment toParentCoords(const Point<dim>& origin,
131  const Quaternion& rotation) const;
132  Segment toLocalCoords(const Point<dim>& origin,
133  const Quaternion& rotation) const;
134 
135  friend bool Intersect<dim>(const Segment& s, const Point<dim>& p, bool proper);
136  friend bool Contains<dim>(const Point<dim>& p, const Segment& s, bool proper);
137 
138  friend bool Intersect<dim>(const Segment& s, const AxisBox<dim>& b, bool proper);
139  friend bool Contains<dim>(const AxisBox<dim>& b, const Segment& s, bool proper);
140 
141  friend bool Intersect<dim>(const Segment& s, const Ball<dim>& b, bool proper);
142  friend bool Contains<dim>(const Ball<dim>& b, const Segment& s, bool proper);
143 
144  friend bool Intersect<dim>(const Segment& s1, const Segment& s2, bool proper);
145  friend bool Contains<dim>(const Segment& s1, const Segment& s2, bool proper);
146 
147  friend bool Intersect<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
148  friend bool Contains<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
149  friend bool Contains<dim>(const Segment& s, const RotBox<dim>& r, bool proper);
150 
151  friend bool Intersect<dim>(const Polygon<dim>& r, const Segment& s, bool proper);
152  friend bool Contains<dim>(const Polygon<dim>& p, const Segment& s, bool proper);
153  friend bool Contains<dim>(const Segment& s, const Polygon<dim>& p, bool proper);
154 
155  private:
156 
157  Point<dim> m_p1, m_p2;
158 };
159 
160 template<int dim>
161 inline bool Segment<dim>::isEqualTo(const Segment<dim>& s,
162  CoordType epsilon) const
163 {
164  return Equal(m_p1, s.m_p1, epsilon)
165  && Equal(m_p2, s.m_p2, epsilon);
166 }
167 
168 } // namespace WFMath
169 
170 #endif // WFMATH_SEGMENT_H
WFMath::Midpoint
Point< dim > Midpoint(const Point< dim > &p1, const Point< dim > &p2, CoordType dist=0.5)
Definition: point_funcs.h:219
WFMath::Segment::endpoint
const Point< dim > & endpoint(const int i) const
get one end of the segment
Definition: segment.h:76
WFMath::Segment::Segment
Segment()
construct an uninitialized segment
Definition: segment.h:49
WFMath::Segment::Segment
Segment(const Segment &s)=default
construct a copy of a segment
WFMath::Segment
A line segment embedded in dim dimensions.
Definition: const.h:54
WFMath
Generic library namespace.
Definition: shape.h:41
WFMath::Vector
A dim dimensional vector.
Definition: const.h:55
WFMath::CoordType
double CoordType
Basic floating point type.
Definition: const.h:140
WFMath::numeric_constants
Definition: const.h:65
WFMath::Segment::endpoint
Point< dim > & endpoint(const int i)
get one end of the segment
Definition: segment.h:78
WFMath::Segment::Segment
Segment(const Point< dim > &p1, const Point< dim > &p2)
construct a segment with endpoints p1 and p2
Definition: segment.h:51
WFMath::Point
A dim dimensional point.
Definition: const.h:50