WFMath
1.0.1
|
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<int dim> 00039 bool Intersection(const AxisBox<dim>& a1, const AxisBox<dim>& a2, AxisBox<dim>& out); 00040 template<int dim> 00041 AxisBox<dim> Union(const AxisBox<dim>& a1, const AxisBox<dim>& a2); 00042 00043 template<int dim> 00044 std::ostream& operator<<(std::ostream& os, const AxisBox<dim>& m); 00045 template<int dim> 00046 std::istream& operator>>(std::istream& is, AxisBox<dim>& m); 00047 00049 template<int dim, template<class, class> class container> 00050 AxisBox<dim> BoundingBox(const container<AxisBox<dim>, std::allocator<AxisBox<dim> > >& c); 00051 00053 template<int dim, template<class, class> class container> 00054 AxisBox<dim> BoundingBox(const container<Point<dim>, std::allocator<Point<dim> > >& c); 00055 00057 00061 template<int dim = 3> 00062 class AxisBox 00063 { 00064 public: 00066 AxisBox() : m_low(), m_high() {} 00068 AxisBox(const Point<dim>& p1, const Point<dim>& p2, bool ordered = false) : 00069 m_low(), m_high() 00070 {setCorners(p1, p2, ordered);} 00072 AxisBox(const AxisBox& a) : m_low(a.m_low), m_high(a.m_high) {} 00074 explicit AxisBox(const AtlasInType& a); 00075 00076 friend std::ostream& operator<< <dim>(std::ostream& os, const AxisBox& a); 00077 friend std::istream& operator>> <dim>(std::istream& is, AxisBox& a); 00078 00080 AtlasOutType toAtlas() const; 00082 void fromAtlas(const AtlasInType& a); 00083 00084 AxisBox& operator=(const AxisBox& a) 00085 {m_low = a.m_low; m_high = a.m_high; return *this;} 00086 00087 bool isEqualTo(const AxisBox& b, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const; 00088 bool operator==(const AxisBox& a) const {return isEqualTo(a);} 00089 bool operator!=(const AxisBox& a) const {return !isEqualTo(a);} 00090 00091 bool isValid() const {return m_low.isValid() && m_high.isValid();} 00092 00093 // Descriptive characteristics 00094 00095 size_t numCorners() const {return 1 << dim;} 00096 Point<dim> getCorner(size_t i) const; 00097 Point<dim> getCenter() const {return Midpoint(m_low, m_high);} 00098 00100 const Point<dim>& lowCorner() const {return m_low;} 00101 Point<dim>& lowCorner() {return m_low;} 00103 const Point<dim>& highCorner() const {return m_high;} 00104 Point<dim>& highCorner() {return m_high;} 00105 00107 CoordType lowerBound(const int axis) const {return m_low[axis];} 00109 CoordType upperBound(const int axis) const {return m_high[axis];} 00110 00112 00117 AxisBox& setCorners(const Point<dim>& p1, const Point<dim>& p2, 00118 bool ordered = false); 00119 00120 // Movement functions 00121 00122 AxisBox& shift(const Vector<dim>& v) 00123 {m_low += v; m_high += v; return *this;} 00124 AxisBox& moveCornerTo(const Point<dim>& p, size_t corner) 00125 {return shift(p - getCorner(corner));} 00126 AxisBox& moveCenterTo(const Point<dim>& p) 00127 {return shift(p - getCenter());} 00128 00129 // No rotation functions, this shape can't rotate 00130 00131 // Intersection functions 00132 00133 AxisBox boundingBox() const {return *this;} 00134 Ball<dim> boundingSphere() const; 00135 Ball<dim> boundingSphereSloppy() const; 00136 00137 AxisBox toParentCoords(const Point<dim>& origin) const 00138 {return AxisBox(m_low.toParentCoords(origin), m_high.toParentCoords(origin), true);} 00139 AxisBox toParentCoords(const AxisBox<dim>& coords) const 00140 {return AxisBox(m_low.toParentCoords(coords), m_high.toParentCoords(coords), true);} 00141 00142 // toLocal is just like toParent, expect we reverse the order of 00143 // translation and rotation and use the opposite sense of the rotation 00144 // matrix 00145 00146 AxisBox toLocalCoords(const Point<dim>& origin) const 00147 {return AxisBox(m_low.toLocalCoords(origin), m_high.toLocalCoords(origin), true);} 00148 AxisBox toLocalCoords(const AxisBox<dim>& coords) const 00149 {return AxisBox(m_low.toLocalCoords(coords), m_high.toLocalCoords(coords), true);} 00150 00152 friend bool Intersection<dim>(const AxisBox& a1, const AxisBox& a2, AxisBox& out); 00154 friend AxisBox Union<dim>(const AxisBox& a1, const AxisBox& a2); 00155 00156 friend bool Intersect<dim>(const AxisBox& b, const Point<dim>& p, bool proper); 00157 friend bool Contains<dim>(const Point<dim>& p, const AxisBox& b, bool proper); 00158 00159 friend bool Intersect<dim>(const AxisBox& b1, const AxisBox& b2, bool proper); 00160 friend bool Contains<dim>(const AxisBox& outer, const AxisBox& inner, bool proper); 00161 00162 friend bool Intersect<dim>(const Ball<dim>& b, const AxisBox& a, bool proper); 00163 friend bool Contains<dim>(const Ball<dim>& b, const AxisBox& a, bool proper); 00164 friend bool Contains<dim>(const AxisBox& a, const Ball<dim>& b, bool proper); 00165 00166 friend bool Intersect<dim>(const Segment<dim>& s, const AxisBox& b, bool proper); 00167 friend bool Contains<dim>(const Segment<dim>& s, const AxisBox& b, bool proper); 00168 00169 friend bool Intersect<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper); 00170 friend bool Contains<dim>(const RotBox<dim>& r, const AxisBox& b, bool proper); 00171 friend bool Contains<dim>(const AxisBox& b, const RotBox<dim>& r, bool proper); 00172 00173 friend bool Intersect<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper); 00174 friend bool Contains<dim>(const Polygon<dim>& p, const AxisBox& b, bool proper); 00175 friend bool Contains<dim>(const AxisBox& b, const Polygon<dim>& p, bool proper); 00176 00177 private: 00178 00179 Point<dim> m_low, m_high; 00180 }; 00181 00182 template<int dim> 00183 inline bool AxisBox<dim>::isEqualTo(const AxisBox<dim>& b, CoordType epsilon) const 00184 { 00185 return Equal(m_low, b.m_low, epsilon) 00186 && Equal(m_high, b.m_high, epsilon); 00187 } 00188 00189 } // namespace WFMath 00190 00191 #endif // WFMATH_AXIS_BOX_H