WFMath  1.0.2
line.h
00001 // line.h (A segmented line in n-dimensional space)
00002 //
00003 //  The WorldForge Project
00004 //  Copyright (C) 2012  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_LINE_H
00027 #define WFMATH_LINE_H
00028 
00029 #include <wfmath/const.h>
00030 #include <wfmath/point.h>
00031 
00032 #include <vector>
00033 
00034 namespace WFMath {
00035 
00037 
00041 template<int dim = 3>
00042 class Line
00043 {
00044  public:
00046   Line() : m_points() {}
00048   Line(const Line<dim>& l) : m_points(l.m_points) {}
00050   explicit Line(const AtlasInType& a);
00052   ~Line() {}
00053 
00055   AtlasOutType toAtlas() const;
00057   void fromAtlas(const AtlasInType& a);
00058 
00060   Line& operator=(const Line& a);
00061 
00063   bool isEqualTo(const Line& s, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
00065   bool operator==(const Line& s) const  {return isEqualTo(s);}
00067   bool operator!=(const Line& s) const  {return !isEqualTo(s);}
00068 
00070   bool isValid() const {return m_points.size() > 1;}
00071 
00072   // Now we begin with the functions in the shape interface
00073 
00074   // Descriptive characteristics
00075 
00077 
00080   size_t numCorners() const {return m_points.size();}
00082   Point<dim> getCorner(size_t i) const {return m_points[i];}
00084   Point<dim> getCenter() const {return Barycenter(m_points);}
00085 
00086   // Add before i'th corner, zero is beginning, numCorners() is end
00087   bool addCorner(size_t i, const Point<dim>& p, CoordType = numeric_constants<CoordType>::epsilon())
00088   {m_points.insert(m_points.begin() + i, p); return true;}
00089 
00090   // Remove the i'th corner
00091   void removeCorner(size_t i) {m_points.erase(m_points.begin() + i);}
00092 
00093   bool moveCorner(size_t i,
00094                   const Point<dim>& p,
00095                   CoordType = numeric_constants<CoordType>::epsilon())
00096   {m_points[i] = p; return true;}
00097 
00098   // Movement functions
00099 
00101   Line& shift(const Vector<dim>& v); // Move the shape a certain distance
00103 
00106   Line& moveCornerTo(const Point<dim>& p, size_t corner)
00107         {return shift(p - getCorner(corner));}
00109 
00112   Line& moveCenterTo(const Point<dim>& p)
00113         {return shift(p - getCenter());}
00114 
00115 
00117 
00120   Line& rotateCorner(const RotMatrix<dim>& m, size_t corner)
00121         {return rotatePoint(m, getCorner(corner));}
00123 
00126   Line& rotateCenter(const RotMatrix<dim>& m)
00127         {return rotatePoint(m, getCenter());}
00129 
00133   Line& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p);
00134 
00135   AxisBox<dim> boundingBox() const {return BoundingBox(m_points);}
00136   Ball<dim> boundingSphere() const {return BoundingSphere(m_points);}
00137   Ball<dim> boundingSphereSloppy() const {return BoundingSphereSloppy(m_points);}
00138 
00139  private:
00140   std::vector<Point<dim> > m_points;
00141   typedef typename std::vector<Point<dim> >::iterator iterator;
00142   typedef typename std::vector<Point<dim> >::const_iterator const_iterator;
00143   typedef typename std::vector<Point<dim> >::size_type size_type;
00144 };
00145 
00146 template<int dim>
00147 inline Line<dim>& Line<dim>::operator=(const Line& rhs)
00148 {
00149     m_points = rhs.m_points;
00150     return *this;
00151 }
00152 
00153 
00154 } // namespace WFMath
00155 
00156 #endif  // WFMATH_LINE_H