wfmath  1.0.3
A math library for the Worldforge system.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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() = default;
48  Line(const Line<dim>& l) = default;
50  explicit Line(const AtlasInType& a);
52  ~Line() = default;
53 
55  AtlasOutType toAtlas() const;
57  void fromAtlas(const AtlasInType& a);
58 
60  Line& operator=(const Line& a) = default;
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,
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 
147 } // namespace WFMath
148 
149 #endif // WFMATH_LINE_H
WFMath::Line::getCorner
Point< dim > getCorner(size_t i) const
shape: return the position of the i'th corner, where 0 <= i < numCorners()
Definition: line.h:82
WFMath::Line::operator==
bool operator==(const Line &s) const
generic: check if two classes are equal, up to tolerance WFMATH_EPSILON
Definition: line.h:65
WFMath::Line
A dim dimensional line.
Definition: line.h:42
WFMath::Line::isValid
bool isValid() const
generic: returns true if the class instance has been initialized
Definition: line.h:70
WFMath::numeric_constants::epsilon
static FloatType epsilon()
This is the attempted precision of the library.
WFMath::Line::rotateCenter
Line & rotateCenter(const RotMatrix< dim > &m)
shape: rotate the shape while holding the center fixed
Definition: line.h:126
WFMath::RotMatrix
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition: const.h:53
WFMath::Line::moveCenterTo
Line & moveCenterTo(const Point< dim > &p)
shape: move the shape, moving the center to the Point p
Definition: line.h:112
WFMath::BoundingSphere
Ball< dim > BoundingSphere(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get the minimal bounding sphere for a set of points
Definition: ball_funcs.h:57
WFMath::Barycenter
Point< dim > Barycenter(const container< Point< dim >, std::allocator< Point< dim > > > &c)
Find the center of a set of points, all weighted equally.
Definition: point_funcs.h:186
WFMath::BoundingBox
AxisBox< dim > BoundingBox(const container< AxisBox< dim >, std::allocator< AxisBox< dim > > > &c)
Get the axis-aligned bounding box for a set of boxes.
Definition: axisbox_funcs.h:130
WFMath::Line::rotatePoint
Line & rotatePoint(const RotMatrix< dim > &m, const Point< dim > &p)
shape: rotate the shape while holding the Point p fixed.
Definition: line_funcs.h:61
WFMath
Generic library namespace.
Definition: shape.h:41
WFMath::AxisBox
A dim dimensional axis-aligned box.
Definition: axisbox.h:63
WFMath::CoordType
double CoordType
Basic floating point type.
Definition: const.h:140
WFMath::numeric_constants
Definition: const.h:64
WFMath::Line::rotateCorner
Line & rotateCorner(const RotMatrix< dim > &m, size_t corner)
shape: rotate the shape while holding the given corner fixed
Definition: line.h:120
WFMath::Line::numCorners
size_t numCorners() const
shape: return the number of corners in the shape.
Definition: line.h:80
WFMath::Line::shift
Line & shift(const Vector< dim > &v)
shape: move the shape by an amount given by the Vector v
Definition: line_funcs.h:51
WFMath::AtlasOutType
Definition: atlasconv.h:67
WFMath::AtlasInType
Definition: atlasconv.h:53
WFMath::BoundingSphereSloppy
Ball< dim > BoundingSphereSloppy(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get a bounding sphere for a set of points
Definition: ball_funcs.h:92
WFMath::Line::moveCornerTo
Line & moveCornerTo(const Point< dim > &p, size_t corner)
shape: move the shape, moving the given corner to the Point p
Definition: line.h:106
WFMath::Line::getCenter
Point< dim > getCenter() const
shape: return the position of the center of the shape
Definition: line.h:84
WFMath::Line::fromAtlas
void fromAtlas(const AtlasInType &a)
Set the line's value to that given by an Atlas object.
Definition: atlasconv.h:412
WFMath::Line::toAtlas
AtlasOutType toAtlas() const
Create an Atlas object from the line.
Definition: atlasconv.h:434
WFMath::Line::operator!=
bool operator!=(const Line &s) const
generic: check if two classes are not equal, up to tolerance WFMATH_EPSILON
Definition: line.h:67
WFMath::Line::isEqualTo
bool isEqualTo(const Line &s, CoordType epsilon=numeric_constants< CoordType >::epsilon()) const
generic: check if two classes are equal, up to a given tolerance
Definition: line_funcs.h:34
WFMath::Point
A dim dimensional point.
Definition: const.h:50