axisbox.h

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

Generated on Fri May 4 15:45:27 2007 for WFMath by  doxygen 1.5.1