WFMath 0.3.11
|
00001 // axisbox.h (An axis-aligned box) 00002 // 00003 // The WorldForge Project 00004 // Copyright (C) 2000, 2001 The WorldForge Project 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 // 00020 // For information about WorldForge and its authors, please contact 00021 // the Worldforge Web Site at http://www.worldforge.org. 00022 // 00023 00024 // Author: Ron Steinke 00025 00026 // The implementation of this class is based on the geometric 00027 // parts of the Tree and Placement classes from stage/shepherd/sylvanus 00028 00029 #ifndef WFMATH_AXIS_BOX_H 00030 #define WFMATH_AXIS_BOX_H 00031 00032 #include <wfmath/intersect_decls.h> 00033 00034 #include <iosfwd> 00035 00036 namespace WFMath { 00037 00038 template<const int dim> 00039 bool Intersection(const AxisBox<dim>& a1, const AxisBox<dim>& a2, AxisBox<dim>& out); 00040 template<const int dim> 00041 AxisBox<dim> Union(const AxisBox<dim>& a1, const AxisBox<dim>& a2); 00042 00043 template<const int dim> 00044 std::ostream& operator<<(std::ostream& os, const AxisBox<dim>& m); 00045 template<const int dim> 00046 std::istream& operator>>(std::istream& is, AxisBox<dim>& m); 00047 00048 #ifndef WFMATH_NO_TEMPLATES_AS_TEMPLATE_PARAMETERS 00049 00050 template<const int dim, template<class, class> class container> 00051 AxisBox<dim> BoundingBox(const container<AxisBox<dim>, std::allocator<AxisBox<dim> > >& c); 00052 00054 template<const int dim, template<class, class> class container> 00055 AxisBox<dim> BoundingBox(const container<Point<dim>, std::allocator<Point<dim> > >& c); 00056 #endif 00057 00059 00063 template<const int dim = 3> 00064 class AxisBox 00065 { 00066 public: 00068 AxisBox() {} 00070 AxisBox(const Point<dim>& p1, const Point<dim>& p2, bool ordered = false) 00071 {setCorners(p1, p2, ordered);} 00073 AxisBox(const AxisBox& a) : m_low(a.m_low), m_high(a.m_high) {} 00075 explicit AxisBox(const AtlasInType& a); 00076 00077 friend std::ostream& operator<< <dim>(std::ostream& os, const AxisBox& a); 00078 friend std::istream& operator>> <dim>(std::istream& is, AxisBox& a); 00079 00081 AtlasOutType toAtlas() const; 00083 void fromAtlas(const AtlasInType& a); 00084 00085 AxisBox& operator=(const AxisBox& a) 00086 {m_low = a.m_low; m_high = a.m_high; return *this;} 00087 00088 bool isEqualTo(const AxisBox& b, double epsilon = WFMATH_EPSILON) const; 00089 bool operator==(const AxisBox& a) const {return isEqualTo(a);} 00090 bool operator!=(const AxisBox& a) const {return !isEqualTo(a);} 00091 00092 bool isValid() const {return m_low.isValid() && m_high.isValid();} 00093 00094 // Descriptive characteristics 00095 00096 int numCorners() const {return 1 << dim;} 00097 Point<dim> getCorner(int i) const; 00098 Point<dim> getCenter() const {return Midpoint(m_low, m_high);} 00099 00101 const Point<dim>& lowCorner() const {return m_low;} 00102 Point<dim>& lowCorner() {return m_low;} 00104 const Point<dim>& highCorner() const {return m_high;} 00105 Point<dim>& highCorner() {return m_high;} 00106 00108 CoordType lowerBound(const int axis) const {return m_low[axis];} 00110 CoordType upperBound(const int axis) const {return m_high[axis];} 00111 00113 00118 AxisBox& setCorners(const Point<dim>& p1, const Point<dim>& p2, 00119 bool ordered = false); 00120 00121 // Movement functions 00122 00123 AxisBox& shift(const Vector<dim>& v) 00124 {m_low += v; m_high += v; return *this;} 00125 AxisBox& moveCornerTo(const Point<dim>& p, int corner) 00126 {return shift(p - getCorner(corner));} 00127 AxisBox& moveCenterTo(const Point<dim>& p) 00128 {return shift(p - getCenter());} 00129 00130 // No rotation functions, this shape can't rotate 00131 00132 // Intersection functions 00133 00134 AxisBox boundingBox() const {return *this;} 00135 Ball<dim> boundingSphere() const; 00136 Ball<dim> boundingSphereSloppy() const; 00137 00138 AxisBox toParentCoords(const Point<dim>& origin) const 00139 {return AxisBox(m_low.toParentCoords(origin), m_high.toParentCoords(origin), true);} 00140 AxisBox toParentCoords(const AxisBox<dim>& coords) const 00141 {return AxisBox(m_low.toParentCoords(coords), m_high.toParentCoords(coords), true);} 00142 00143 // toLocal is just like toParent, expect we reverse the order of 00144 // translation and rotation and use the opposite sense of the rotation 00145 // matrix 00146 00147 AxisBox toLocalCoords(const Point<dim>& origin) const 00148 {return AxisBox(m_low.toLocalCoords(origin), m_high.toLocalCoords(origin), true);} 00149 AxisBox toLocalCoords(const AxisBox<dim>& coords) const 00150 {return AxisBox(m_low.toLocalCoords(coords), m_high.toLocalCoords(coords), true);} 00151 00153 friend bool Intersection<dim>(const AxisBox& a1, const AxisBox& a2, AxisBox& out); 00155 friend AxisBox Union<dim>(const AxisBox& a1, const AxisBox& a2); 00156 00157 friend bool Intersect<dim>(const AxisBox& b, const Point<dim>& p, bool proper); 00158 friend bool Contains<dim>(const Point<dim>& p, const AxisBox& b, bool proper); 00159 00160 friend bool Intersect<dim>(const AxisBox& b1, const AxisBox& b2, bool proper); 00161 friend bool Contains<dim>(const AxisBox& outer, const AxisBox& inner, bool proper); 00162 00163 friend bool Intersect<dim>(const Ball<dim>& b, const AxisBox& a, bool proper); 00164 friend bool Contains<dim>(const Ball<dim>& b, const AxisBox& a, bool proper); 00165 friend bool Contains<dim>(const AxisBox& a, const Ball<dim>& b, bool proper); 00166 00167 friend bool Intersect<dim>(const Segment<dim>& s, const AxisBox& b, bool proper); 00168 friend bool Contains<dim>(const Segment<dim>& s, const AxisBox& b, bool proper); 00169 00170 friend bool Intersect<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper); 00171 friend bool Contains<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper); 00172 friend bool Contains<dim>(const AxisBox& b, const RotBox<dim>& r, bool proper); 00173 00174 friend bool Intersect<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper); 00175 friend bool Contains<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper); 00176 friend bool Contains<dim>(const AxisBox& b, const Polygon<dim>& p, bool proper); 00177 00178 private: 00179 00180 Point<dim> m_low, m_high; 00181 }; 00182 00183 } // namespace WFMath 00184 00185 #endif // WFMATH_AXIS_BOX_H