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#include <iosfwd>
36
37namespace WFMath {
38
39template<int dim>
40bool Intersection(const AxisBox<dim>& a1, const AxisBox<dim>& a2, AxisBox<dim>& out);
41template<int dim>
42AxisBox<dim> Union(const AxisBox<dim>& a1, const AxisBox<dim>& a2);
43
44template<int dim>
45std::ostream& operator<<(std::ostream& os, const AxisBox<dim>& m);
46template<int dim>
47std::istream& operator>>(std::istream& is, AxisBox<dim>& m);
48
50template<int dim, template<class, class> class container>
51AxisBox<dim> BoundingBox(const container<AxisBox<dim>, std::allocator<AxisBox<dim> > >& c);
52
54template<int dim, template<class, class> class container>
55AxisBox<dim> BoundingBox(const container<Point<dim>, std::allocator<Point<dim> > >& c);
56
58
62template<int dim = 3>
64{
65 public:
67 AxisBox() = default;
69 AxisBox(const Point<dim>& p1, const Point<dim>& p2, bool ordered = false) :
70 m_low(), m_high()
71 {setCorners(p1, p2, ordered);}
73 AxisBox(const AxisBox& a) = default;
75 explicit AxisBox(const AtlasInType& a);
76
77 friend std::ostream& operator<< <dim>(std::ostream& os, const AxisBox& a);
78 friend std::istream& operator>> <dim>(std::istream& is, AxisBox& a);
79
81 AtlasOutType toAtlas() const;
83 void fromAtlas(const AtlasInType& a);
84
85 AxisBox& operator=(const AxisBox& a) = default;
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
182template<int dim>
183inline 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
A dim dimensional axis-aligned box.
Definition: axisbox.h:64
AxisBox(const Point< dim > &p1, const Point< dim > &p2, bool ordered=false)
Construct a box with opposite corners p1 and p2.
Definition: axisbox.h:69
void fromAtlas(const AtlasInType &a)
Set the box's value to that given by an Atlas object.
Definition: atlasconv.h:215
const Point< dim > & highCorner() const
Get a reference to corner (2^dim)-1.
Definition: axisbox.h:103
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:107
const Point< dim > & lowCorner() const
Get a reference to corner 0.
Definition: axisbox.h:100
CoordType upperBound(const int axis) const
Get the upper bound of the box on the i'th axis.
Definition: axisbox.h:109
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