WFMath  1.0.1
line.h
1 // line.h (A segmented line in n-dimensional space)
2 //
3 // The WorldForge Project
4 // Copyright (C) 2012 The WorldForge Project
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20 // For information about WorldForge and its authors, please contact
21 // the Worldforge Web Site at http://www.worldforge.org.
22 //
23 
24 // Author: Ron Steinke
25 
26 #ifndef WFMATH_LINE_H
27 #define WFMATH_LINE_H
28 
29 #include <wfmath/const.h>
30 #include <wfmath/point.h>
31 
32 #include <vector>
33 
34 namespace WFMath {
35 
37 
41 template<int dim = 3>
42 class Line
43 {
44  public:
46  Line() : m_points() {}
48  Line(const Line<dim>& l) : m_points(l.m_points) {}
50  explicit Line(const AtlasInType& a);
52  ~Line() {}
53 
55  AtlasOutType toAtlas() const;
57  void fromAtlas(const AtlasInType& a);
58 
60  Line& operator=(const Line& a);
61 
63  bool isEqualTo(const Line& s, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
65  bool operator==(const Line& s) const {return isEqualTo(s);}
67  bool operator!=(const Line& s) const {return !isEqualTo(s);}
68 
70  bool isValid() const {return m_points.size() > 1;}
71 
72  // Now we begin with the functions in the shape interface
73 
74  // Descriptive characteristics
75 
77 
80  size_t numCorners() const {return m_points.size();}
82  Point<dim> getCorner(size_t i) const {return m_points[i];}
84  Point<dim> getCenter() const {return Barycenter(m_points);}
85 
86  // Add before i'th corner, zero is beginning, numCorners() is end
87  bool addCorner(size_t i, const Point<dim>& p, CoordType = numeric_constants<CoordType>::epsilon())
88  {m_points.insert(m_points.begin() + i, p); return true;}
89 
90  // Remove the i'th corner
91  void removeCorner(size_t i) {m_points.erase(m_points.begin() + i);}
92 
93  bool moveCorner(size_t i,
94  const Point<dim>& p,
95  CoordType = numeric_constants<CoordType>::epsilon())
96  {m_points[i] = p; return true;}
97 
98  // Movement functions
99 
101  Line& shift(const Vector<dim>& v); // Move the shape a certain distance
103 
106  Line& moveCornerTo(const Point<dim>& p, size_t corner)
107  {return shift(p - getCorner(corner));}
109 
113  {return shift(p - getCenter());}
114 
115 
117 
120  Line& rotateCorner(const RotMatrix<dim>& m, size_t corner)
121  {return rotatePoint(m, getCorner(corner));}
123 
127  {return rotatePoint(m, getCenter());}
129 
133  Line& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p);
134 
135  AxisBox<dim> boundingBox() const {return BoundingBox(m_points);}
136  Ball<dim> boundingSphere() const {return BoundingSphere(m_points);}
137  Ball<dim> boundingSphereSloppy() const {return BoundingSphereSloppy(m_points);}
138 
139  private:
140  std::vector<Point<dim> > m_points;
141  typedef typename std::vector<Point<dim> >::iterator iterator;
142  typedef typename std::vector<Point<dim> >::const_iterator const_iterator;
143  typedef typename std::vector<Point<dim> >::size_type size_type;
144 };
145 
146 template<int dim>
147 inline Line<dim>& Line<dim>::operator=(const Line& rhs)
148 {
149  m_points = rhs.m_points;
150  return *this;
151 }
152 
153 
154 } // namespace WFMath
155 
156 #endif // WFMATH_LINE_H