WFMath 1.0.2
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
34namespace WFMath {
35
37
41template<int dim = 3>
42class 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
146template<int dim>
147inline 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
A dim dimensional axis-aligned box.
Definition const.h:48
A dim dimensional line.
Definition line.h:43
Line & rotateCenter(const RotMatrix< dim > &m)
shape: rotate the shape while holding the center fixed
Definition line.h:126
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
size_t numCorners() const
shape: return the number of corners in the shape.
Definition line.h:80
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
bool operator!=(const Line &s) const
generic: check if two classes are not equal, up to tolerance WFMATH_EPSILON
Definition line.h:67
Point< dim > getCorner(size_t i) const
shape: return the position of the i'th corner, where 0 <= i < numCorners()
Definition line.h:82
Point< dim > getCenter() const
shape: return the position of the center of the shape
Definition line.h:84
Line & rotateCorner(const RotMatrix< dim > &m, size_t corner)
shape: rotate the shape while holding the given corner fixed
Definition line.h:120
Line & shift(const Vector< dim > &v)
shape: move the shape by an amount given by the Vector v
Definition line_funcs.h:51
void fromAtlas(const AtlasInType &a)
Set the line's value to that given by an Atlas object.
Definition atlasconv.h:369
AtlasOutType toAtlas() const
Create an Atlas object from the line.
Definition atlasconv.h:391
bool operator==(const Line &s) const
generic: check if two classes are equal, up to tolerance WFMATH_EPSILON
Definition line.h:65
Line & moveCenterTo(const Point< dim > &p)
shape: move the shape, moving the center to the Point p
Definition line.h:112
bool isValid() const
generic: returns true if the class instance has been initialized
Definition line.h:70
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
A dim dimensional point.
Definition point.h:96
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition rotmatrix.h:87
Generic library namespace.
Definition atlasconv.h:45
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
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:207
float CoordType
Basic floating point type.
Definition const.h:140
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
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