Main Page | Namespace List | Class List | File List | Namespace Members | Class Members

rotbox.h

00001 // rotbox.h (A box with arbitrary orientation)
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 #ifndef WFMATH_ROT_BOX_H
00027 #define WFMATH_ROT_BOX_H
00028 
00029 #include <wfmath/const.h>
00030 #include <wfmath/vector.h>
00031 #include <wfmath/point.h>
00032 #include <wfmath/rotmatrix.h>
00033 #include <wfmath/axisbox.h>
00034 #include <wfmath/ball.h>
00035 #include <wfmath/intersect_decls.h>
00036 
00037 namespace WFMath {
00038 
00039 template<const int dim> class RotBox;
00040 
00041 template<const int dim>
00042 std::ostream& operator<<(std::ostream& os, const RotBox<dim>& r);
00043 template<const int dim>
00044 std::istream& operator>>(std::istream& is, RotBox<dim>& r);
00045 
00047 
00051 template<const int dim>
00052 class RotBox
00053 {
00054  public:
00056   RotBox() {}
00058 
00063   RotBox(const Point<dim>& p, const Vector<dim>& size,
00064          const RotMatrix<dim>& orientation) : m_corner0(p), m_size(size),
00065                 m_orient(orientation) {}
00067   RotBox(const RotBox& b) : m_corner0(b.m_corner0), m_size(b.m_size),
00068                 m_orient(b.m_orient) {}
00069 
00070   ~RotBox() {}
00071 
00072   friend std::ostream& operator<< <dim>(std::ostream& os, const RotBox& r);
00073   friend std::istream& operator>> <dim>(std::istream& is, RotBox& r);
00074 
00075   RotBox& operator=(const RotBox& s);
00076 
00077   bool isEqualTo(const RotBox& b, double epsilon = WFMATH_EPSILON) const;
00078 
00079   bool operator==(const RotBox& b) const        {return isEqualTo(b);}
00080   bool operator!=(const RotBox& b) const        {return !isEqualTo(b);}
00081 
00082   bool isValid() const {return m_corner0.isValid() && m_size.isValid()
00083         && m_orient.isValid();}
00084 
00085   // Descriptive characteristics
00086 
00087   int numCorners() const {return 1 << dim;}
00088   Point<dim> getCorner(int i) const;
00089   Point<dim> getCenter() const {return m_corner0 + Prod(m_size / 2, m_orient);}
00090 
00092   const Point<dim>& corner0() const             {return m_corner0;}
00094   Point<dim>& corner0()                         {return m_corner0;}
00096   const Vector<dim>& size() const               {return m_size;}
00098   Vector<dim>& size()                           {return m_size;}
00100   const RotMatrix<dim>& orientation() const     {return m_orient;}
00102   RotMatrix<dim>& orientation()                 {return m_orient;}
00103 
00104   // Movement functions
00105 
00106   RotBox& shift(const Vector<dim>& v)
00107         {m_corner0 += v; return *this;}
00108   RotBox& moveCornerTo(const Point<dim>& p, int corner)
00109         {return shift(p - getCorner(corner));}
00110   RotBox& moveCenterTo(const Point<dim>& p)
00111         {return shift(p - getCenter());}
00112 
00113   RotBox& rotateCorner(const RotMatrix<dim>& m, int corner)
00114         {rotatePoint(m, getCorner(corner)); return *this;}
00115   RotBox& rotateCenter(const RotMatrix<dim>& m)
00116         {rotatePoint(m, getCenter()); return *this;}
00117   RotBox& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00118         {m_orient = Prod(m_orient, m); m_corner0.rotate(m, p); return *this;}
00119 
00120   // 3D rotation functions
00121   RotBox<3>& rotateCorner(const Quaternion& q, int corner)
00122         {rotatePoint(q, getCorner(corner)); return *this;}
00123   RotBox<3>& rotateCenter(const Quaternion& q)
00124         {rotatePoint(q, getCenter()); return *this;}
00125   RotBox<3>& rotatePoint(const Quaternion& q, const Point<3>& p)
00126         {m_orient = m_orient.rotate(q); m_corner0.rotate(q, p); return *this;}
00127 
00128   // Intersection functions
00129 
00130   AxisBox<dim> boundingBox() const;
00131   Ball<dim> boundingSphere() const
00132         {return Ball<dim>(getCenter(), m_size.mag() / 2);}
00133   Ball<dim> boundingSphereSloppy() const
00134         {return Ball<dim>(getCenter(), m_size.sqrMag() / 2);}
00135 
00136   RotBox toParentCoords(const Point<dim>& origin,
00137       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00138         {return RotBox(m_corner0.toParentCoords(origin, rotation), m_size,
00139                 m_orient * rotation);}
00140   RotBox toParentCoords(const AxisBox<dim>& coords) const
00141         {return RotBox(m_corner0.toParentCoords(coords), m_size, m_orient);}
00142   RotBox toParentCoords(const RotBox<dim>& coords) const
00143         {return RotBox(m_corner0.toParentCoords(coords), m_size,
00144                 m_orient * coords.m_orient);}
00145 
00146   // toLocal is just like toParent, expect we reverse the order of
00147   // translation and rotation and use the opposite sense of the rotation
00148   // matrix
00149 
00150   RotBox toLocalCoords(const Point<dim>& origin,
00151       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00152         {return RotBox(m_corner0.toLocalCoords(origin, rotation), m_size,
00153                 rotation * m_orient);}
00154   RotBox toLocalCoords(const AxisBox<dim>& coords) const
00155         {return RotBox(m_corner0.toLocalCoords(coords), m_size, m_orient);}
00156   RotBox toLocalCoords(const RotBox<dim>& coords) const
00157         {return RotBox(m_corner0.toLocalCoords(coords), m_size,
00158                 coords.m_orient * m_orient);}
00159 
00160   // 3D only
00161   RotBox<3> toParentCoords(const Point<3>& origin, const Quaternion& rotation) const
00162         {return RotBox<3>(m_corner0.toParentCoords(origin, rotation), m_size,
00163                 m_orient.rotate(rotation));}
00164   RotBox<3> toLocalCoords(const Point<3>& origin, const Quaternion& rotation) const
00165         {return RotBox<3>(m_corner0.toLocalCoords(origin, rotation), m_size,
00166                 m_orient.rotate(rotation.inverse()));}
00167 
00168   friend bool Intersect<dim>(const RotBox& r, const Point<dim>& p, bool proper);
00169   friend bool Contains<dim>(const Point<dim>& p, const RotBox& r, bool proper);
00170 
00171   friend bool Intersect<dim>(const RotBox& r, const AxisBox<dim>& b, bool proper);
00172   friend bool Contains<dim>(const RotBox& r, const AxisBox<dim>& b, bool proper);
00173   friend bool Contains<dim>(const AxisBox<dim>& b, const RotBox& r, bool proper);
00174 
00175   friend bool Intersect<dim>(const RotBox& r, const Ball<dim>& b, bool proper);
00176   friend bool Contains<dim>(const RotBox& r, const Ball<dim>& b, bool proper);
00177   friend bool Contains<dim>(const Ball<dim>& b, const RotBox& r, bool proper);
00178 
00179   friend bool Intersect<dim>(const RotBox& r, const Segment<dim>& s, bool proper);
00180   friend bool Contains<dim>(const RotBox& r, const Segment<dim>& s, bool proper);
00181   friend bool Contains<dim>(const Segment<dim>& s, const RotBox& r, bool proper);
00182 
00183   friend bool Intersect<dim>(const RotBox& r1, const RotBox& r2, bool proper);
00184   friend bool Contains<dim>(const RotBox& outer, const RotBox& inner, bool proper);
00185 
00186   friend bool Intersect<dim>(const Polygon<dim>& p, const RotBox& r, bool proper);
00187   friend bool Contains<dim>(const Polygon<dim>& p, const RotBox& r, bool proper);
00188   friend bool Contains<dim>(const RotBox& r, const Polygon<dim>& p, bool proper);
00189 
00190  private:
00191 
00192   Point<dim> m_corner0;
00193   Vector<dim> m_size;
00194   RotMatrix<dim> m_orient;
00195 };
00196 
00197 } // namespace WFMath
00198 
00199 #include <wfmath/rotbox_funcs.h>
00200 
00201 #endif  // WFMATH_ROT_BOX_H

Generated on Wed Oct 13 21:00:48 2004 for WFMath by  doxygen 1.3.9.1