WFMath 1.0.2
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., 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
32namespace WFMath {
33
34template<int dim>
35std::ostream& operator<<(std::ostream& os, const Segment<dim>& s);
36template<int dim>
37std::istream& operator>>(std::istream& is, Segment<dim>& s);
38
40
44template<int dim = 3>
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) : m_p1(s.m_p1), m_p2(s.m_p2) {}
54
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
68 bool isValid() const {return m_p1.isValid() && m_p2.isValid();}
69
70 // Descriptive characteristics
71
72 size_t numCorners() const {return 2;}
73 Point<dim> getCorner(size_t i) const {return i ? m_p2 : m_p1;}
74 Point<dim> getCenter() const {return Midpoint(m_p1, m_p2);}
75
77 const Point<dim>& endpoint(const int i) const {return i ? m_p2 : m_p1;}
79 Point<dim>& endpoint(const int i) {return i ? m_p2 : m_p1;}
80
81 // Movement functions
82
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);
86 Segment& moveCenterTo(const Point<dim>& p)
87 {return shift(p - getCenter());}
88
89 Segment& rotateCorner(const RotMatrix<dim>& m, size_t corner);
90 Segment& rotateCenter(const RotMatrix<dim>& m)
91 {rotatePoint(m, getCenter()); return *this;}
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
102 AxisBox<dim> boundingBox() const {return AxisBox<dim>(m_p1, m_p2);}
103 Ball<dim> boundingSphere() const
104 {return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
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));}
112 Segment toParentCoords(const AxisBox<dim>& coords) const
113 {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(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));}
125 Segment toLocalCoords(const AxisBox<dim>& coords) const
126 {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(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
161template<int dim>
162inline 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
A dim dimensional point.
Definition point.h:96
Point & rotate(const RotMatrix< dim > &m, const Point &p)
Rotate about point p.
Definition point.h:146
A line segment embedded in dim dimensions.
Definition segment.h:46
const Point< dim > & endpoint(const int i) const
get one end of the segment
Definition segment.h:77
Segment()
construct an uninitialized segment
Definition segment.h:49
Segment(const Point< dim > &p1, const Point< dim > &p2)
construct a segment with endpoints p1 and p2
Definition segment.h:51
Segment(const Segment &s)
construct a copy of a segment
Definition segment.h:53
Point< dim > & endpoint(const int i)
get one end of the segment
Definition segment.h:79
A dim dimensional vector.
Definition vector.h:121
Generic library namespace.
Definition atlasconv.h:45
bool Equal(const C &c1, const C &c2, CoordType epsilon=numeric_constants< CoordType >::epsilon())
Test for equality up to precision epsilon.
Definition const.h:158
float CoordType
Basic floating point type.
Definition const.h:140
Point< dim > Midpoint(const Point< dim > &p1, const Point< dim > &p2, CoordType dist=0.5)
Definition point_funcs.h:240