Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

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/ball.h>
00034 #include <wfmath/intersect_decls.h>
00035 
00036 namespace WFMath {
00037 
00038 template<const int dim> class Segment;
00039 
00040 template<const int dim>
00041 std::ostream& operator<<(std::ostream& os, const Segment<dim>& s);
00042 template<const int dim>
00043 std::istream& operator>>(std::istream& is, Segment<dim>& s);
00044 
00046 
00050 template<const int dim>
00051 class Segment
00052 {
00053  public:
00055   Segment() {}
00057   Segment(const Point<dim>& p1, const Point<dim>& p2) : m_p1(p1), m_p2(p2) {}
00059   Segment(const Segment& s) : m_p1(s.m_p1), m_p2(s.m_p2) {}
00060 
00061   ~Segment() {}
00062 
00063   friend std::ostream& operator<< <dim>(std::ostream& os, const Segment& s);
00064   friend std::istream& operator>> <dim>(std::istream& is, Segment& s);
00065 
00066   Segment& operator=(const Segment& s)
00067         {m_p1 = s.m_p1; m_p2 = s.m_p2; return *this;}
00068 
00069   bool isEqualTo(const Segment& s, double epsilon = WFMATH_EPSILON) const;
00070 
00071   bool operator==(const Segment& b) const       {return isEqualTo(b);}
00072   bool operator!=(const Segment& b) const       {return !isEqualTo(b);}
00073 
00074   bool isValid() const {return m_p1.isValid() && m_p2.isValid();}
00075 
00076   bool operator< (const Segment& s) const;
00077 
00078   // Descriptive characteristics
00079 
00080   int numCorners() const {return 2;}
00081   Point<dim> getCorner(int i) const {assert(i == 0 || i == 1); return i ? m_p2 : m_p1;}
00082   Point<dim> getCenter() const {return Midpoint(m_p1, m_p2);}
00083 
00085   const Point<dim>& endpoint(const int i) const {return i ? m_p2 : m_p1;}
00087   Point<dim>& endpoint(const int i)             {return i ? m_p2 : m_p1;}
00088 
00089   // Movement functions
00090 
00091   Segment& shift(const Vector<dim>& v)
00092         {m_p1 += v; m_p2 += v; return *this;}
00093   Segment& moveCornerTo(const Point<dim>& p, int corner);
00094   Segment& moveCenterTo(const Point<dim>& p)
00095         {return shift(p - getCenter());}
00096 
00097   Segment& rotateCorner(const RotMatrix<dim>& m, int corner);
00098   Segment& rotateCenter(const RotMatrix<dim>& m)
00099         {rotatePoint(m, getCenter()); return *this;}
00100   Segment<dim>& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
00101         {m_p1.rotate(m, p); m_p2.rotate(m, p); return *this;}
00102 
00103   // Intersection functions
00104 
00105   AxisBox<dim> boundingBox() const {return AxisBox<dim>(m_p1, m_p2);}
00106   Ball<dim> boundingSphere() const
00107         {return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
00108   Ball<dim> boundingSphereSloppy() const
00109         {return Ball<dim>(getCenter(), SloppyDistance(m_p1, m_p2) / 2);}
00110 
00111   friend bool Intersect<dim>(const Segment& s, const Point<dim>& p, bool proper);
00112   friend bool Contains<dim>(const Point<dim>& p, const Segment& s, bool proper);
00113 
00114   friend bool Intersect<dim>(const Segment& s, const AxisBox<dim>& b, bool proper);
00115   friend bool Contains<dim>(const AxisBox<dim>& b, const Segment& s, bool proper);
00116 
00117   friend bool Intersect<dim>(const Segment& s, const Ball<dim>& b, bool proper);
00118   friend bool Contains<dim>(const Ball<dim>& b, const Segment& s, bool proper);
00119 
00120   friend bool Intersect<dim>(const Segment& s1, const Segment& s2, bool proper);
00121   friend bool Contains<dim>(const Segment& s1, const Segment& s2, bool proper);
00122 
00123   friend bool Intersect<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00124   friend bool Contains<dim>(const RotBox<dim>& r, const Segment& s, bool proper);
00125   friend bool Contains<dim>(const Segment& s, const RotBox<dim>& r, bool proper);
00126 
00127   friend bool Intersect<dim>(const Polygon<dim>& r, const Segment& s, bool proper);
00128   friend bool Contains<dim>(const Polygon<dim>& p, const Segment& s, bool proper);
00129   friend bool Contains<dim>(const Segment& s, const Polygon<dim>& p, bool proper);
00130 
00131  private:
00132 
00133   Point<dim> m_p1, m_p2;
00134 };
00135 
00136 } // namespace WFMath
00137 
00138 #include <wfmath/segment_funcs.h>
00139 
00140 #endif  // WFMATH_SEGMENT_H

Generated on Wed May 28 09:20:32 2003 for WFMath by doxygen1.3-rc3