26 #ifndef WFMATH_POLYGON_H 27 #define WFMATH_POLYGON_H 29 #include <wfmath/const.h> 30 #include <wfmath/axisbox.h> 31 #include <wfmath/ball.h> 32 #include <wfmath/quaternion.h> 38 template<
int dim>
class Polygon;
41 std::ostream& operator<<(std::ostream& os, const Polygon<dim>& r);
43 std::istream& operator>>(std::istream& is, Polygon<dim>& r);
53 explicit Polygon(
const AtlasInType& a) : m_points() {fromAtlas(a);}
57 friend std::ostream& operator<< <2>(std::ostream& os,
const Polygon& p);
58 friend std::istream&
operator>> <2>(std::istream& is,
Polygon& p);
62 AtlasOutType toAtlas()
const;
64 void fromAtlas(
const AtlasInType& a);
67 {m_points = p.m_points;
return *
this;}
69 bool isEqualTo(
const Polygon& p,
CoordType epsilon = numeric_constants<CoordType>::epsilon())
const;
71 bool operator==(
const Polygon& p)
const {
return isEqualTo(p);}
72 bool operator!=(
const Polygon& p)
const {
return !isEqualTo(p);}
78 size_t numCorners()
const {
return m_points.size();}
79 Point<2> getCorner(
size_t i)
const {
return m_points[i];}
87 bool addCorner(
size_t i,
const Point<2>& p,
CoordType = numeric_constants<CoordType>::epsilon())
88 {m_points.insert(m_points.begin() + i, p);
return true;}
91 void removeCorner(
size_t i) {m_points.erase(m_points.begin() + i);}
94 bool moveCorner(
size_t i,
const Point<2>& p,
CoordType = numeric_constants<CoordType>::epsilon())
95 {m_points[i] = p;
return true;}
98 void clear() {m_points.clear();}
100 const Point<2>& operator[](
size_t i)
const {
return m_points[i];}
101 Point<2>& operator[](
size_t i) {
return m_points[i];}
103 void resize(std::vector<
Point<2> >::size_type size) {m_points.resize(size);}
109 {
return shift(p - getCorner(corner));}
111 {
return shift(p - getCenter());}
114 {rotatePoint(m, getCorner(corner));
return *
this;}
116 {rotatePoint(m, getCenter());
return *
this;}
139 friend bool Intersect<2>(
const Polygon& r,
const Point<2>& p,
bool proper);
140 friend bool Contains<2>(
const Point<2>& p,
const Polygon& r,
bool proper);
146 friend bool Intersect<2>(
const Polygon& p,
const Ball<2>& b,
bool proper);
147 friend bool Contains<2>(
const Polygon& p,
const Ball<2>& b,
bool proper);
148 friend bool Contains<2>(
const Ball<2>& b,
const Polygon& p,
bool proper);
154 friend bool Intersect<2>(
const Polygon& p,
const RotBox<2>& r,
bool proper);
158 friend bool Intersect<2>(
const Polygon& p1,
const Polygon& p2,
bool proper);
159 friend bool Contains<2>(
const Polygon& outer,
const Polygon& inner,
bool proper);
162 std::vector<Point<2> > m_points;
163 typedef std::vector<Point<2> >::iterator theIter;
164 typedef std::vector<Point<2> >::const_iterator theConstIter;
172 _WFMATH_POLY2REORIENT_NONE,
173 _WFMATH_POLY2REORIENT_CLEAR_AXIS2,
174 _WFMATH_POLY2REORIENT_CLEAR_BOTH_AXES,
175 _WFMATH_POLY2REORIENT_MOVE_AXIS2_TO_AXIS1,
176 _WFMATH_POLY2REORIENT_SCALE1_CLEAR2
177 } _Poly2ReorientType;
184 _Poly2Reorient(_Poly2ReorientType type,
CoordType scale = 0.0)
185 : m_type(type), m_scale(scale) {}
188 void reorient(
Polygon<2>& poly,
size_t skip = std::numeric_limits<size_t>::max())
const;
191 _Poly2ReorientType m_type;
195 template<
int dim>
class _Poly2Orient;
197 struct _Poly2OrientIntersectData {
201 bool o1_is_line, o2_is_line;
209 int _Intersect(
const _Poly2Orient<dim> &,
const _Poly2Orient<dim> &,
210 _Poly2OrientIntersectData &);
217 _Poly2Orient() : m_origin() {}
218 _Poly2Orient(
const _Poly2Orient& p) : m_origin() {operator=(p);}
221 _Poly2Orient& operator=(
const _Poly2Orient& p);
234 _Poly2Reorient reduce(
const Polygon<2>& poly,
size_t skip = std::numeric_limits<size_t>::max());
236 void shift(
const Vector<dim>& v) {
if(m_origin.isValid()) m_origin += v;}
246 _Poly2Orient toParentCoords(
const Point<dim>& origin,
248 {_Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(origin, rotation);
249 p.m_axes[0].rotate(rotation); p.m_axes[1].rotate(rotation);
return p;}
250 _Poly2Orient toParentCoords(
const AxisBox<dim>& coords)
const 251 {_Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(coords);
return p;}
252 _Poly2Orient toParentCoords(
const RotBox<dim>& coords)
const 253 {_Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(coords);
255 p.m_axes[1].rotate(coords.
orientation());
return p;}
261 _Poly2Orient toLocalCoords(
const Point<dim>& origin,
263 {_Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(origin, rotation);
264 p.m_axes[0] = rotation * p.m_axes[0];
265 p.m_axes[1] = rotation * p.m_axes[1];
return p;}
266 _Poly2Orient toLocalCoords(
const AxisBox<dim>& coords)
const 267 {_Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(coords);
return p;}
268 _Poly2Orient toLocalCoords(
const RotBox<dim>& coords)
const 269 {_Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(coords);
271 p.m_axes[1] = coords.
orientation() * p.m_axes[1];
return p;}
274 _Poly2Orient<3> toParentCoords(
const Point<3>& origin,
const Quaternion& rotation)
const 275 {_Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(origin, rotation);
276 p.m_axes[0].rotate(rotation); p.m_axes[0].rotate(rotation);
return p;}
277 _Poly2Orient<3> toLocalCoords(
const Point<3>& origin,
const Quaternion& rotation)
const 278 {_Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(origin, rotation);
279 p.m_axes[0].rotate(rotation.
inverse());
280 p.m_axes[0].rotate(rotation.
inverse());
return p;}
293 friend int _Intersect<dim>(
const _Poly2Orient<dim> &,
const _Poly2Orient<dim> &,
294 _Poly2OrientIntersectData &);
305 template<
int dim = 3>
309 Polygon() : m_orient(), m_poly() {}
310 Polygon(
const Polygon& p) : m_orient(p.m_orient), m_poly(p.m_poly) {}
314 friend std::ostream& operator<< <dim>(std::ostream& os,
const Polygon& p);
315 friend std::istream&
operator>> <dim>(std::istream& is,
Polygon& p);
318 {m_orient = p.m_orient; m_poly = p.m_poly;
return *
this;}
320 bool isEqualTo(
const Polygon& p2,
CoordType epsilon = numeric_constants<CoordType>::epsilon())
const;
322 bool operator==(
const Polygon& p)
const {
return isEqualTo(p);}
323 bool operator!=(
const Polygon& p)
const {
return !isEqualTo(p);}
325 bool isValid()
const {
return m_poly.isValid();}
329 size_t numCorners()
const {
return m_poly.numCorners();}
330 Point<dim> getCorner(
size_t i)
const {
return m_orient.convert(m_poly[i]);}
331 Point<dim> getCenter()
const {
return m_orient.convert(m_poly.getCenter());}
338 bool addCorner(
size_t i,
const Point<dim>& p,
CoordType epsilon = numeric_constants<CoordType>::epsilon());
341 void removeCorner(
size_t i);
347 bool moveCorner(
size_t i,
const Point<dim>& p,
CoordType epsilon = numeric_constants<CoordType>::epsilon());
350 void clear() {m_poly.clear(); m_orient = _Poly2Orient<dim>();}
355 {m_orient.shift(v);
return *
this;}
357 {
return shift(p - getCorner(corner));}
359 {
return shift(p - getCenter());}
362 {m_orient.rotate2(m, m_poly[corner]);
return *
this;}
364 {
if(m_poly.numCorners() > 0)
365 m_orient.rotate2(m, m_poly.getCenter());
368 {m_orient.rotate(m, p);
return *
this;}
372 {m_orient.rotate2(q, m_poly[corner]);
return *
this;}
374 {
if(m_poly.numCorners() > 0)
375 m_orient.rotate2(q, m_poly.getCenter());
378 {m_orient.rotate(q, p);
return *
this;}
388 {
Polygon p(*
this); p.m_orient = m_orient.toParentCoords(origin, rotation);
return p;}
390 {
Polygon p(*
this); p.m_orient = m_orient.toParentCoords(coords);
return p;}
392 {
Polygon p(*
this); p.m_orient = m_orient.toParentCoords(coords);
return p;}
400 {
Polygon p(*
this); p.m_orient = m_orient.toLocalCoords(origin, rotation);
return p;}
402 {
Polygon p(*
this); p.m_orient = m_orient.toLocalCoords(coords);
return p;}
404 {
Polygon p(*
this); p.m_orient = m_orient.toLocalCoords(coords);
return p;}
408 {
Polygon<3> p(*
this); p.m_orient = m_orient.toParentCoords(origin, rotation);
return p;}
410 {
Polygon<3> p(*
this); p.m_orient = m_orient.toLocalCoords(origin, rotation);
return p;}
419 friend bool Intersect<dim>(
const Polygon& p,
const Ball<dim>& b,
bool proper);
420 friend bool Contains<dim>(
const Polygon& p,
const Ball<dim>& b,
bool proper);
421 friend bool Contains<dim>(
const Ball<dim>& b,
const Polygon& p,
bool proper);
431 friend bool Intersect<dim>(
const Polygon& p1,
const Polygon& p2,
bool proper);
432 friend bool Contains<dim>(
const Polygon& outer,
const Polygon& inner,
bool proper);
436 _Poly2Orient<dim> m_orient;
444 bool succ = m_orient.expand(p, p2, epsilon);
446 m_poly.addCorner(i, p2, epsilon);
453 m_poly.removeCorner(i);
454 _Poly2Reorient r = m_orient.reduce(m_poly);
461 _Poly2Orient<dim> try_orient = m_orient;
462 _Poly2Reorient r = try_orient.reduce(m_poly, i);
465 if(!try_orient.expand(p, p2, epsilon))
468 r.reorient(m_poly, i);
470 m_orient = try_orient;
477 #endif // WFMATH_POLYGON_H Generic library namespace.
Definition: atlasconv.h:45
A dim dimensional axis-aligned box.
Definition: axisbox.h:62
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition: const.h:53
A dim dimensional box, lying at an arbitrary angle.
Definition: const.h:52
A polygon, all of whose points lie in a plane, embedded in dim dimensions.
Definition: const.h:51
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
Quaternion inverse() const
returns the inverse of the Quaternion
Polygon(const AtlasInType &a)
Construct a polygon from an object passed by Atlas.
Definition: polygon.h:53
const RotMatrix< dim > & orientation() const
returns the orientation of the box
Definition: rotbox.h:101
float CoordType
Basic floating point type.
Definition: const.h:140
The 2D specialization of the Polygon<> template.
Definition: polygon.h:47
A line segment embedded in dim dimensions.
Definition: const.h:54
A normalized quaterion.
Definition: quaternion.h:35
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
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
A dim dimensional ball.
Definition: ball.h:34
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