segment.h

00001 // segment.h (A line segment)
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_SEGMENT_H
00027 #define WFMATH_SEGMENT_H
00028 
00029 #include <wfmath/const.h>
00030 #include <wfmath/vector.h>
00031 #include <wfmath/point.h>
00032 #include <wfmath/axisbox.h>
00033 #include <wfmath/rotbox.h>
00034 #include <wfmath/ball.h>
00035 #include <wfmath/intersect_decls.h>
00036 
00037 namespace WFMath {
00038 
00039 template<const int dim> class Segment;
00040 
00041 template<const int dim>
00042 std::ostream& operator<<(std::ostream& os, const Segment<dim>& s);
00043 template<const int dim>
00044 std::istream& operator>>(std::istream& is, Segment<dim>& s);
00045 
00047 
00051 template<const int dim>
00052 class Segment
00053 {
00054  public:
00056   Segment() {}
00058   Segment(const Point<dim>& p1, const Point<dim>& p2) : m_p1(p1), m_p2(p2) {}
00060   Segment(const Segment& s) : m_p1(s.m_p1), m_p2(s.m_p2) {}
00061 
00062   ~Segment() {}
00063 
00064   friend std::ostream& operator<< <dim>(std::ostream& os, const Segment& s);
00065   friend std::istream& operator>> <dim>(std::istream& is, Segment& s);
00066 
00067   Segment& operator=(const Segment& s)
00068         {m_p1 = s.m_p1; m_p2 = s.m_p2; return *this;}
00069 
00070   bool isEqualTo(const Segment& s, double epsilon = WFMATH_EPSILON) const;
00071 
00072   bool operator==(const Segment& b) const       {return isEqualTo(b);}
00073   bool operator!=(const Segment& b) const       {return !isEqualTo(b);}
00074 
00075   bool isValid() const {return m_p1.isValid() && m_p2.isValid();}
00076 
00077   // Descriptive characteristics
00078 
00079   int numCorners() const {return 2;}
00080   Point<dim> getCorner(int i) const {assert(i == 0 || i == 1); return i ? m_p2 : m_p1;}
00081   Point<dim> getCenter() const {return Midpoint(m_p1, m_p2);}
00082 
00084   const Point<dim>& endpoint(const int i) const {return i ? m_p2 : m_p1;}
00086   Point<dim>& endpoint(const int i)             {return i ? m_p2 : m_p1;}
00087 
00088   // Movement functions
00089 
00090   Segment& shift(const Vector<dim>& v)
00091         {m_p1 += v; m_p2 += v; return *this;}
00092   Segment& moveCornerTo(const Point<dim>& p, int corner);
00093   Segment& moveCenterTo(const Point<dim>& p)
00094         {return shift(p - getCenter());}
00095 
00096   Segment& rotateCorner(const RotMatrix<dim>& m, int corner);
00097   Segment& rotateCenter(const RotMatrix<dim>& m)
00098         {rotatePoint(m, getCenter()); return *this;}
00099   Segment<dim>& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00100         {m_p1.rotate(m, p); m_p2.rotate(m, p); return *this;}
00101 
00102   // 3D rotation functions
00103   Segment<3>& rotateCorner(const Quaternion& q, int corner);
00104   Segment<3>& rotateCenter(const Quaternion& q)
00105         {rotatePoint(q, getCenter()); return *this;}
00106   Segment<3>& rotatePoint(const Quaternion& q, const Point<3>& p)
00107         {m_p1.rotate(q, p); m_p2.rotate(q, p); return *this;}
00108 
00109   // Intersection functions
00110 
00111   AxisBox<dim> boundingBox() const {return AxisBox<dim>(m_p1, m_p2);}
00112   Ball<dim> boundingSphere() const
00113         {return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
00114   Ball<dim> boundingSphereSloppy() const
00115         {return Ball<dim>(getCenter(), SloppyDistance(m_p1, m_p2) / 2);}
00116 
00117   Segment toParentCoords(const Point<dim>& origin,
00118       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00119         {return Segment(m_p1.toParentCoords(origin, rotation),
00120                 m_p2.toParentCoords(origin, rotation));}
00121   Segment toParentCoords(const AxisBox<dim>& coords) const
00122         {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
00123   Segment toParentCoords(const RotBox<dim>& coords) const
00124         {return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
00125 
00126   // toLocal is just like toParent, expect we reverse the order of
00127   // translation and rotation and use the opposite sense of the rotation
00128   // matrix
00129 
00130   Segment toLocalCoords(const Point<dim>& origin,
00131       const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
00132         {return Segment(m_p1.toLocalCoords(origin, rotation),
00133                 m_p2.toLocalCoords(origin, rotation));}
00134   Segment toLocalCoords(const AxisBox<dim>& coords) const
00135         {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
00136   Segment toLocalCoords(const RotBox<dim>& coords) const
00137         {return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
00138 
00139   // 3D only
00140   Segment<3> toParentCoords(const Point<3>& origin, const Quaternion& rotation) const
00141         {return Segment<3>(m_p1.toParentCoords(origin, rotation),
00142                 m_p2.toParentCoords(origin, rotation));}
00143   Segment<3> toLocalCoords(const Point<3>& origin, const Quaternion& rotation) const
00144         {return Segment<3>(m_p1.toLocalCoords(origin, rotation),
00145                 m_p2.toLocalCoords(origin, rotation));}
00146 
00147   friend bool Intersect<dim>(const Segment& s, const Point<dim>& p, bool proper);
00148   friend bool Contains<dim>(const Point<dim>& p, const Segment& s, bool proper);
00149 
00150   friend bool Intersect<dim>(const Segment& s, const AxisBox<dim>& b, bool proper);
00151   friend bool Contains<dim>(const AxisBox<dim>& b, const Segment& s, bool proper);
00152 
00153   friend bool Intersect<dim>(const Segment& s, const Ball<dim>& b, bool proper);
00154   friend bool Contains<dim>(const Ball<dim>& b, const Segment& s, bool proper);
00155 
00156   friend bool Intersect<dim>(const Segment& s1, const Segment& s2, bool proper);
00157   friend bool Contains<dim>(const Segment& s1, const Segment& s2, bool proper);
00158 
00159   friend bool Intersect<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00160   friend bool Contains<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00161   friend bool Contains<dim>(const Segment& s, const RotBox<dim>& r, bool proper);
00162 
00163   friend bool Intersect<dim>(const Polygon<dim>& r, const Segment& s, bool proper);
00164   friend bool Contains<dim>(const Polygon<dim>& p, const Segment& s, bool proper);
00165   friend bool Contains<dim>(const Segment& s, const Polygon<dim>& p, bool proper);
00166 
00167  private:
00168 
00169   Point<dim> m_p1, m_p2;
00170 };
00171 
00172 } // namespace WFMath
00173 
00174 #include <wfmath/segment_funcs.h>
00175 
00176 #endif  // WFMATH_SEGMENT_H

Generated on Mon Jul 23 16:08:46 2007 for WFMath by  doxygen 1.5.2