• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

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

Generated for WFMath by  doxygen 1.7.1