WFMath  1.0.2
axisbox.h
1 // axisbox.h (An axis-aligned box)
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 // The implementation of this class is based on the geometric
27 // parts of the Tree and Placement classes from stage/shepherd/sylvanus
28 
29 #ifndef WFMATH_AXIS_BOX_H
30 #define WFMATH_AXIS_BOX_H
31 
32 #include <wfmath/intersect_decls.h>
33 
34 #include <iosfwd>
35 
36 namespace WFMath {
37 
38 template<int dim>
39 bool Intersection(const AxisBox<dim>& a1, const AxisBox<dim>& a2, AxisBox<dim>& out);
40 template<int dim>
41 AxisBox<dim> Union(const AxisBox<dim>& a1, const AxisBox<dim>& a2);
42 
43 template<int dim>
44 std::ostream& operator<<(std::ostream& os, const AxisBox<dim>& m);
45 template<int dim>
46 std::istream& operator>>(std::istream& is, AxisBox<dim>& m);
47 
49 template<int dim, template<class, class> class container>
50 AxisBox<dim> BoundingBox(const container<AxisBox<dim>, std::allocator<AxisBox<dim> > >& c);
51 
53 template<int dim, template<class, class> class container>
54 AxisBox<dim> BoundingBox(const container<Point<dim>, std::allocator<Point<dim> > >& c);
55 
57 
61 template<int dim = 3>
62 class AxisBox
63 {
64  public:
66  AxisBox() : m_low(), m_high() {}
68  AxisBox(const Point<dim>& p1, const Point<dim>& p2, bool ordered = false) :
69  m_low(), m_high()
70  {setCorners(p1, p2, ordered);}
72  AxisBox(const AxisBox& a) : m_low(a.m_low), m_high(a.m_high) {}
74  explicit AxisBox(const AtlasInType& a);
75 
76  friend std::ostream& operator<< <dim>(std::ostream& os, const AxisBox& a);
77  friend std::istream& operator>> <dim>(std::istream& is, AxisBox& a);
78 
80  AtlasOutType toAtlas() const;
82  void fromAtlas(const AtlasInType& a);
83 
84  AxisBox& operator=(const AxisBox& a)
85  {m_low = a.m_low; m_high = a.m_high; return *this;}
86 
87  bool isEqualTo(const AxisBox& b, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
88  bool operator==(const AxisBox& a) const {return isEqualTo(a);}
89  bool operator!=(const AxisBox& a) const {return !isEqualTo(a);}
90 
91  bool isValid() const {return m_low.isValid() && m_high.isValid();}
92 
93  // Descriptive characteristics
94 
95  size_t numCorners() const {return 1 << dim;}
96  Point<dim> getCorner(size_t i) const;
97  Point<dim> getCenter() const {return Midpoint(m_low, m_high);}
98 
100  const Point<dim>& lowCorner() const {return m_low;}
101  Point<dim>& lowCorner() {return m_low;}
103  const Point<dim>& highCorner() const {return m_high;}
104  Point<dim>& highCorner() {return m_high;}
105 
107  CoordType lowerBound(const int axis) const {return m_low[axis];}
109  CoordType upperBound(const int axis) const {return m_high[axis];}
110 
112 
117  AxisBox& setCorners(const Point<dim>& p1, const Point<dim>& p2,
118  bool ordered = false);
119 
120  // Movement functions
121 
122  AxisBox& shift(const Vector<dim>& v)
123  {m_low += v; m_high += v; return *this;}
124  AxisBox& moveCornerTo(const Point<dim>& p, size_t corner)
125  {return shift(p - getCorner(corner));}
126  AxisBox& moveCenterTo(const Point<dim>& p)
127  {return shift(p - getCenter());}
128 
129  // No rotation functions, this shape can't rotate
130 
131  // Intersection functions
132 
133  AxisBox boundingBox() const {return *this;}
134  Ball<dim> boundingSphere() const;
135  Ball<dim> boundingSphereSloppy() const;
136 
137  AxisBox toParentCoords(const Point<dim>& origin) const
138  {return AxisBox(m_low.toParentCoords(origin), m_high.toParentCoords(origin), true);}
139  AxisBox toParentCoords(const AxisBox<dim>& coords) const
140  {return AxisBox(m_low.toParentCoords(coords), m_high.toParentCoords(coords), true);}
141 
142  // toLocal is just like toParent, expect we reverse the order of
143  // translation and rotation and use the opposite sense of the rotation
144  // matrix
145 
146  AxisBox toLocalCoords(const Point<dim>& origin) const
147  {return AxisBox(m_low.toLocalCoords(origin), m_high.toLocalCoords(origin), true);}
148  AxisBox toLocalCoords(const AxisBox<dim>& coords) const
149  {return AxisBox(m_low.toLocalCoords(coords), m_high.toLocalCoords(coords), true);}
150 
152  friend bool Intersection<dim>(const AxisBox& a1, const AxisBox& a2, AxisBox& out);
154  friend AxisBox Union<dim>(const AxisBox& a1, const AxisBox& a2);
155 
156  friend bool Intersect<dim>(const AxisBox& b, const Point<dim>& p, bool proper);
157  friend bool Contains<dim>(const Point<dim>& p, const AxisBox& b, bool proper);
158 
159  friend bool Intersect<dim>(const AxisBox& b1, const AxisBox& b2, bool proper);
160  friend bool Contains<dim>(const AxisBox& outer, const AxisBox& inner, bool proper);
161 
162  friend bool Intersect<dim>(const Ball<dim>& b, const AxisBox& a, bool proper);
163  friend bool Contains<dim>(const Ball<dim>& b, const AxisBox& a, bool proper);
164  friend bool Contains<dim>(const AxisBox& a, const Ball<dim>& b, bool proper);
165 
166  friend bool Intersect<dim>(const Segment<dim>& s, const AxisBox& b, bool proper);
167  friend bool Contains<dim>(const Segment<dim>& s, const AxisBox& b, bool proper);
168 
169  friend bool Intersect<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper);
170  friend bool Contains<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper);
171  friend bool Contains<dim>(const AxisBox& b, const RotBox<dim>& r, bool proper);
172 
173  friend bool Intersect<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper);
174  friend bool Contains<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper);
175  friend bool Contains<dim>(const AxisBox& b, const Polygon<dim>& p, bool proper);
176 
177  private:
178 
179  Point<dim> m_low, m_high;
180 };
181 
182 template<int dim>
183 inline bool AxisBox<dim>::isEqualTo(const AxisBox<dim>& b, CoordType epsilon) const
184 {
185  return Equal(m_low, b.m_low, epsilon)
186  && Equal(m_high, b.m_high, epsilon);
187 }
188 
189 } // namespace WFMath
190 
191 #endif // WFMATH_AXIS_BOX_H
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
AxisBox(const Point< dim > &p1, const Point< dim > &p2, bool ordered=false)
Construct a box with opposite corners p1 and p2.
Definition: axisbox.h:68
Generic library namespace.
Definition: atlasconv.h:45
const Point< dim > & lowCorner() const
Get a reference to corner 0.
Definition: axisbox.h:100
A dim dimensional axis-aligned box.
Definition: axisbox.h:62
CoordType upperBound(const int axis) const
Get the upper bound of the box on the i&#39;th axis.
Definition: axisbox.h:109
AtlasOutType toAtlas() const
Create an Atlas object from the box.
Definition: atlasconv.h:225
AxisBox & setCorners(const Point< dim > &p1, const Point< dim > &p2, bool ordered=false)
Set the box to have opposite corners p1 and p2.
Definition: axisbox_funcs.h:72
A dim dimensional box, lying at an arbitrary angle.
Definition: const.h:52
AxisBox(const AxisBox &a)
Construct a copy of a box.
Definition: axisbox.h:72
A polygon, all of whose points lie in a plane, embedded in dim dimensions.
Definition: const.h:51
Point< dim > Midpoint(const Point< dim > &p1, const Point< dim > &p2, CoordType dist=0.5)
Definition: point_funcs.h:240
A dim dimensional vector.
Definition: const.h:55
const Point< dim > & highCorner() const
Get a reference to corner (2^dim)-1.
Definition: axisbox.h:103
float CoordType
Basic floating point type.
Definition: const.h:140
CoordType lowerBound(const int axis) const
Get the lower bound of the box on the i&#39;th axis.
Definition: axisbox.h:107
AxisBox()
Construct an uninitialized box.
Definition: axisbox.h:66
void fromAtlas(const AtlasInType &a)
Set the box&#39;s value to that given by an Atlas object.
Definition: atlasconv.h:191
A line segment embedded in dim dimensions.
Definition: const.h:54
A dim dimensional point.
Definition: const.h:50
A dim dimensional ball.
Definition: ball.h:34
AxisBox< dim > BoundingBox(const container< AxisBox< dim >, std::allocator< AxisBox< dim > > > &c)
Get the axis-aligned bounding box for a set of boxes.
Definition: axisbox_funcs.h:130