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 
48 /// Get the axis-aligned bounding box for a set of boxes
49 template<int dim, template<class, class> class container>
50 AxisBox<dim> BoundingBox(const container<AxisBox<dim>, std::allocator<AxisBox<dim> > >& c);
51 
52 /// Get the axis-aligned bounding box for a set of points
53 template<int dim, template<class, class> class container>
54 AxisBox<dim> BoundingBox(const container<Point<dim>, std::allocator<Point<dim> > >& c);
55 
56 /// A dim dimensional axis-aligned box
57 /**
58  * This class implements the full shape interface, as described in
59  * the fake class Shape, with the exception of the rotation functions.
60  **/
61 template<int dim = 3>
62 class AxisBox
63 {
64  public:
65   /// Construct an uninitialized box
AxisBox()66   AxisBox() : m_low(), m_high() {}
67   /// Construct a box with opposite corners p1 and p2
68   AxisBox(const Point<dim>& p1, const Point<dim>& p2, bool ordered = false) :
m_low()69     m_low(), m_high()
70     {setCorners(p1, p2, ordered);}
71   /// Construct a copy of a box
AxisBox(const AxisBox & a)72   AxisBox(const AxisBox& a) : m_low(a.m_low), m_high(a.m_high) {}
73   /// Construct a box from an object passed by Atlas
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 
79   /// Create an Atlas object from the box
80   AtlasOutType toAtlas() const;
81   /// Set the box's value to that given by an Atlas object
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 
isValid()91   bool isValid() const {return m_low.isValid() && m_high.isValid();}
92 
93   // Descriptive characteristics
94 
numCorners()95   size_t numCorners() const {return 1 << dim;}
96   Point<dim> getCorner(size_t i) const;
getCenter()97   Point<dim> getCenter() const {return Midpoint(m_low, m_high);}
98 
99   /// Get a reference to corner 0
lowCorner()100   const Point<dim>& lowCorner() const {return m_low;}
lowCorner()101   Point<dim>& lowCorner() {return m_low;}
102   /// Get a reference to corner (2^dim)-1
highCorner()103   const Point<dim>& highCorner() const {return m_high;}
highCorner()104   Point<dim>& highCorner() {return m_high;}
105 
106   /// Get the lower bound of the box on the i'th axis
lowerBound(const int axis)107   CoordType lowerBound(const int axis) const	{return m_low[axis];}
108   /// Get the upper bound of the box on the i'th axis
upperBound(const int axis)109   CoordType upperBound(const int axis) const	{return m_high[axis];}
110 
111   /// Set the box to have opposite corners p1 and p2
112   /**
113    * The 'ordered' variable may be set to true if p1[i] <= p2[i] for all
114    * i. It is always safe to leave 'ordered' as false, it is a speed
115    * optimization primarily intended for use inside the library.
116    **/
117   AxisBox& setCorners(const Point<dim>& p1, const Point<dim>& p2,
118   bool ordered = false);
119 
120   // Movement functions
121 
shift(const Vector<dim> & v)122   AxisBox& shift(const Vector<dim>& v)
123   {m_low += v; m_high += v; return *this;}
moveCornerTo(const Point<dim> & p,size_t corner)124   AxisBox& moveCornerTo(const Point<dim>& p, size_t corner)
125   {return shift(p - getCorner(corner));}
moveCenterTo(const Point<dim> & p)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 
boundingBox()133   AxisBox boundingBox() const {return *this;}
134   Ball<dim> boundingSphere() const;
135   Ball<dim> boundingSphereSloppy() const;
136 
toParentCoords(const Point<dim> & origin)137   AxisBox toParentCoords(const Point<dim>& origin) const
138         {return AxisBox(m_low.toParentCoords(origin), m_high.toParentCoords(origin), true);}
toParentCoords(const AxisBox<dim> & coords)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 
toLocalCoords(const Point<dim> & origin)146   AxisBox toLocalCoords(const Point<dim>& origin) const
147         {return AxisBox(m_low.toLocalCoords(origin), m_high.toLocalCoords(origin), true);}
toLocalCoords(const AxisBox<dim> & coords)148   AxisBox toLocalCoords(const AxisBox<dim>& coords) const
149         {return AxisBox(m_low.toLocalCoords(coords), m_high.toLocalCoords(coords), true);}
150 
151   /// Return true if the boxes intersect, and set 'out' to their intersection
152   friend bool Intersection<dim>(const AxisBox& a1, const AxisBox& a2, AxisBox& out);
153   /// Get the minimal box that contains a1 and a2
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>
isEqualTo(const AxisBox<dim> & b,CoordType epsilon)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
192