wfmath  1.0.3
A math library for the Worldforge system.
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., 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 // 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 #include <wfmath/point.h>
34 
35 namespace WFMath {
36 
37 template<int dim>
38 bool Intersection(const AxisBox<dim>& a1, const AxisBox<dim>& a2, AxisBox<dim>& out);
39 template<int dim>
40 AxisBox<dim> Union(const AxisBox<dim>& a1, const AxisBox<dim>& a2);
41 
42 template<int dim>
43 std::ostream& operator<<(std::ostream& os, const AxisBox<dim>& m);
44 template<int dim>
45 std::istream& operator>>(std::istream& is, AxisBox<dim>& m);
46 
48 template<int dim, template<class, class> class container>
49 AxisBox<dim> BoundingBox(const container<AxisBox<dim>, std::allocator<AxisBox<dim> > >& c);
50 
52 template<int dim, template<class, class> class container>
53 AxisBox<dim> BoundingBox(const container<Point<dim>, std::allocator<Point<dim> > >& c);
54 
56 
60 template<int dim = 3>
61 class AxisBox
62 {
63  public:
65  AxisBox() = default;
67  AxisBox(const Point<dim>& p1, const Point<dim>& p2, bool ordered = false) :
68  m_low(), m_high()
69  {setCorners(p1, p2, ordered);}
71  AxisBox(const AxisBox& a) = default;
73  explicit AxisBox(const AtlasInType& a);
74 
75  friend std::ostream& operator<< <dim>(std::ostream& os, const AxisBox& a);
76  friend std::istream& operator>> <dim>(std::istream& is, AxisBox& a);
77 
79  AtlasOutType toAtlas() const;
81  void fromAtlas(const AtlasInType& a);
82 
83  AxisBox& operator=(const AxisBox& a) = default;
84 
85  bool isEqualTo(const AxisBox& b, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
86  bool operator==(const AxisBox& a) const {return isEqualTo(a);}
87  bool operator!=(const AxisBox& a) const {return !isEqualTo(a);}
88 
89  bool isValid() const {return m_low.isValid() && m_high.isValid();}
90 
91  // Descriptive characteristics
92 
93  size_t numCorners() const {return 1 << dim;}
94  Point<dim> getCorner(size_t i) const;
95  Point<dim> getCenter() const {return Midpoint(m_low, m_high);}
96 
98  const Point<dim>& lowCorner() const {return m_low;}
99  Point<dim>& lowCorner() {return m_low;}
101  const Point<dim>& highCorner() const {return m_high;}
102  Point<dim>& highCorner() {return m_high;}
103 
105  CoordType lowerBound(const int axis) const {return m_low[axis];}
107  CoordType upperBound(const int axis) const {return m_high[axis];}
108 
110 
115  AxisBox& setCorners(const Point<dim>& p1, const Point<dim>& p2,
116  bool ordered = false);
117 
118  // Movement functions
119 
120  AxisBox& shift(const Vector<dim>& v)
121  {m_low += v; m_high += v; return *this;}
122  AxisBox& moveCornerTo(const Point<dim>& p, size_t corner)
123  {return shift(p - getCorner(corner));}
124  AxisBox& moveCenterTo(const Point<dim>& p)
125  {return shift(p - getCenter());}
126 
127  // No rotation functions, this shape can't rotate
128 
129  // Intersection functions
130 
131  AxisBox boundingBox() const {return *this;}
132  Ball<dim> boundingSphere() const;
133  Ball<dim> boundingSphereSloppy() const;
134 
135  AxisBox toParentCoords(const Point<dim>& origin) const
136  {return AxisBox(m_low.toParentCoords(origin), m_high.toParentCoords(origin), true);}
137  AxisBox toParentCoords(const AxisBox<dim>& coords) const
138  {return AxisBox(m_low.toParentCoords(coords), m_high.toParentCoords(coords), true);}
139 
140  // toLocal is just like toParent, expect we reverse the order of
141  // translation and rotation and use the opposite sense of the rotation
142  // matrix
143 
144  AxisBox toLocalCoords(const Point<dim>& origin) const
145  {return AxisBox(m_low.toLocalCoords(origin), m_high.toLocalCoords(origin), true);}
146  AxisBox toLocalCoords(const AxisBox<dim>& coords) const
147  {return AxisBox(m_low.toLocalCoords(coords), m_high.toLocalCoords(coords), true);}
148 
150  friend bool Intersection<dim>(const AxisBox& a1, const AxisBox& a2, AxisBox& out);
152  friend AxisBox Union<dim>(const AxisBox& a1, const AxisBox& a2);
153 
154  friend bool Intersect<dim>(const AxisBox& b, const Point<dim>& p, bool proper);
155  friend bool Contains<dim>(const Point<dim>& p, const AxisBox& b, bool proper);
156 
157  friend bool Intersect<dim>(const AxisBox& b1, const AxisBox& b2, bool proper);
158  friend bool Contains<dim>(const AxisBox& outer, const AxisBox& inner, bool proper);
159 
160  friend bool Intersect<dim>(const Ball<dim>& b, const AxisBox& a, bool proper);
161  friend bool Contains<dim>(const Ball<dim>& b, const AxisBox& a, bool proper);
162  friend bool Contains<dim>(const AxisBox& a, const Ball<dim>& b, bool proper);
163 
164  friend bool Intersect<dim>(const Segment<dim>& s, const AxisBox& b, bool proper);
165  friend bool Contains<dim>(const Segment<dim>& s, const AxisBox& b, bool proper);
166 
167  friend bool Intersect<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper);
168  friend bool Contains<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper);
169  friend bool Contains<dim>(const AxisBox& b, const RotBox<dim>& r, bool proper);
170 
171  friend bool Intersect<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper);
172  friend bool Contains<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper);
173  friend bool Contains<dim>(const AxisBox& b, const Polygon<dim>& p, bool proper);
174 
175  private:
176 
177  Point<dim> m_low, m_high;
178 };
179 
180 template<int dim>
181 inline bool AxisBox<dim>::isEqualTo(const AxisBox<dim>& b, CoordType epsilon) const
182 {
183  return Equal(m_low, b.m_low, epsilon)
184  && Equal(m_high, b.m_high, epsilon);
185 }
186 
187 } // namespace WFMath
188 
189 #endif // WFMATH_AXIS_BOX_H
A dim dimensional axis-aligned box.
Definition: axisbox.h:62
AxisBox(const Point< dim > &p1, const Point< dim > &p2, bool ordered=false)
Construct a box with opposite corners p1 and p2.
Definition: axisbox.h:67
void fromAtlas(const AtlasInType &a)
Set the box's value to that given by an Atlas object.
Definition: atlasconv.h:215
const Point< dim > & lowCorner() const
Get a reference to corner 0.
Definition: axisbox.h:98
AxisBox()=default
Construct an uninitialized box.
AxisBox(const AxisBox &a)=default
Construct a copy of a box.
CoordType lowerBound(const int axis) const
Get the lower bound of the box on the i'th axis.
Definition: axisbox.h:105
CoordType upperBound(const int axis) const
Get the upper bound of the box on the i'th axis.
Definition: axisbox.h:107
const Point< dim > & highCorner() const
Get a reference to corner (2^dim)-1.
Definition: axisbox.h:101
AtlasOutType toAtlas() const
Create an Atlas object from the box.
Definition: atlasconv.h:257
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
Generic library namespace.
Definition: shape.h:41
double CoordType
Basic floating point type.
Definition: const.h:140
AxisBox< dim > BoundingBox(const container< AxisBox< dim >, std::allocator< AxisBox< dim > > > &c)
Get the axis-aligned bounding box for a set of boxes.
Point< dim > Midpoint(const Point< dim > &p1, const Point< dim > &p2, CoordType dist=0.5)
Definition: point_funcs.h:219