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);
57 friend std::ostream& operator<< <2>(std::ostream& os,
const Polygon& p);
58 friend std::istream&
operator>> <2>(std::istream& is,
Polygon& p);
70 bool operator==(
const Polygon& p)
const {
return isEqualTo(p);}
71 bool operator!=(
const Polygon& p)
const {
return !isEqualTo(p);}
77 size_t numCorners()
const {
return m_points.size();}
78 Point<2> getCorner(
size_t i)
const {
return m_points[i];}
87 {m_points.insert(m_points.begin() + i, p);
return true;}
90 void removeCorner(
size_t i) {m_points.erase(m_points.begin() + i);}
94 {m_points[i] = p;
return true;}
97 void clear() {m_points.clear();}
99 const Point<2>& operator[](
size_t i)
const {
return m_points[i];}
100 Point<2>& operator[](
size_t i) {
return m_points[i];}
102 void resize(std::vector<
Point<2> >::size_type size) {m_points.resize(size);}
108 {
return shift(p - getCorner(corner));}
110 {
return shift(p - getCenter());}
113 {rotatePoint(m, getCorner(corner));
return *
this;}
115 {rotatePoint(m, getCenter());
return *
this;}
138 friend bool Intersect<2>(
const Polygon& r,
const Point<2>& p,
bool proper);
139 friend bool Contains<2>(
const Point<2>& p,
const Polygon& r,
bool proper);
145 friend bool Intersect<2>(
const Polygon& p,
const Ball<2>& b,
bool proper);
146 friend bool Contains<2>(
const Polygon& p,
const Ball<2>& b,
bool proper);
147 friend bool Contains<2>(
const Ball<2>& b,
const Polygon& p,
bool proper);
153 friend bool Intersect<2>(
const Polygon& p,
const RotBox<2>& r,
bool proper);
157 friend bool Intersect<2>(
const Polygon& p1,
const Polygon& p2,
bool proper);
158 friend bool Contains<2>(
const Polygon& outer,
const Polygon& inner,
bool proper);
161 std::vector<Point<2> > m_points;
162 typedef std::vector<Point<2> >::iterator theIter;
163 typedef std::vector<Point<2> >::const_iterator theConstIter;
171 WFMATH_POLY2REORIENT_NONE,
172 WFMATH_POLY2REORIENT_CLEAR_AXIS2,
173 WFMATH_POLY2REORIENT_CLEAR_BOTH_AXES,
174 WFMATH_POLY2REORIENT_MOVE_AXIS2_TO_AXIS1,
175 WFMATH_POLY2REORIENT_SCALE1_CLEAR2
184 : m_type(type), m_scale(scale) {}
187 void reorient(
Polygon<2>& poly,
size_t skip = std::numeric_limits<size_t>::max())
const;
190 Poly2ReorientType m_type;
200 bool o1_is_line, o2_is_line;
235 void shift(
const Vector<dim>& v) {
if(m_origin.isValid()) m_origin += v;}
247 {
Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(origin, rotation);
248 p.m_axes[0].rotate(rotation); p.m_axes[1].rotate(rotation);
return p;}
250 {
Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(coords);
return p;}
252 {
Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(coords);
254 p.m_axes[1].rotate(coords.
orientation());
return p;}
262 {
Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(origin, rotation);
263 p.m_axes[0] = rotation * p.m_axes[0];
264 p.m_axes[1] = rotation * p.m_axes[1];
return p;}
266 {
Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(coords);
return p;}
268 {
Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(coords);
270 p.m_axes[1] = coords.
orientation() * p.m_axes[1];
return p;}
274 {
Poly2Orient p(*
this); p.m_origin = m_origin.toParentCoords(origin, rotation);
275 p.m_axes[0].rotate(rotation); p.m_axes[0].rotate(rotation);
return p;}
277 {
Poly2Orient p(*
this); p.m_origin = m_origin.toLocalCoords(origin, rotation);
278 p.m_axes[0].rotate(rotation.
inverse());
279 p.m_axes[0].rotate(rotation.
inverse());
return p;}
292 friend int Intersect<dim>(
const Poly2Orient<dim> &,
const Poly2Orient<dim> &,
304 template<
int dim = 3>
308 Polygon() : m_orient(), m_poly() {}
309 Polygon(
const Polygon& p) : m_orient(p.m_orient), m_poly(p.m_poly) {}
313 friend std::ostream& operator<< <dim>(std::ostream& os,
const Polygon& p);
314 friend std::istream&
operator>> <dim>(std::istream& is,
Polygon& p);
317 {m_orient = p.m_orient; m_poly = p.m_poly;
return *
this;}
321 bool operator==(
const Polygon& p)
const {
return isEqualTo(p);}
322 bool operator!=(
const Polygon& p)
const {
return !isEqualTo(p);}
324 bool isValid()
const {
return m_poly.isValid();}
328 size_t numCorners()
const {
return m_poly.numCorners();}
329 Point<dim> getCorner(
size_t i)
const {
return m_orient.convert(m_poly[i]);}
330 Point<dim> getCenter()
const {
return m_orient.convert(m_poly.getCenter());}
340 void removeCorner(
size_t i);
354 {m_orient.shift(v);
return *
this;}
356 {
return shift(p - getCorner(corner));}
358 {
return shift(p - getCenter());}
361 {m_orient.rotate2(m, m_poly[corner]);
return *
this;}
363 {
if(m_poly.numCorners() > 0)
364 m_orient.rotate2(m, m_poly.getCenter());
367 {m_orient.rotate(m, p);
return *
this;}
371 {m_orient.rotate2(q, m_poly[corner]);
return *
this;}
373 {
if(m_poly.numCorners() > 0)
374 m_orient.rotate2(q, m_poly.getCenter());
377 {m_orient.rotate(q, p);
return *
this;}
387 {
Polygon p(*
this); p.m_orient = m_orient.toParentCoords(origin, rotation);
return p;}
389 {
Polygon p(*
this); p.m_orient = m_orient.toParentCoords(coords);
return p;}
391 {
Polygon p(*
this); p.m_orient = m_orient.toParentCoords(coords);
return p;}
399 {
Polygon p(*
this); p.m_orient = m_orient.toLocalCoords(origin, rotation);
return p;}
401 {
Polygon p(*
this); p.m_orient = m_orient.toLocalCoords(coords);
return p;}
403 {
Polygon p(*
this); p.m_orient = m_orient.toLocalCoords(coords);
return p;}
407 {
Polygon<3> p(*
this); p.m_orient = m_orient.toParentCoords(origin, rotation);
return p;}
409 {
Polygon<3> p(*
this); p.m_orient = m_orient.toLocalCoords(origin, rotation);
return p;}
418 friend bool Intersect<dim>(
const Polygon& p,
const Ball<dim>& b,
bool proper);
419 friend bool Contains<dim>(
const Polygon& p,
const Ball<dim>& b,
bool proper);
420 friend bool Contains<dim>(
const Ball<dim>& b,
const Polygon& p,
bool proper);
430 friend bool Intersect<dim>(
const Polygon& p1,
const Polygon& p2,
bool proper);
431 friend bool Contains<dim>(
const Polygon& outer,
const Polygon& inner,
bool proper);
443 bool succ = m_orient.expand(p, p2, epsilon);
445 m_poly.addCorner(i, p2, epsilon);
452 m_poly.removeCorner(i);
464 if(!try_orient.expand(p, p2, epsilon))
467 r.reorient(m_poly, i);
469 m_orient = try_orient;
476 #endif // WFMATH_POLYGON_H Generic library namespace.
A dim dimensional axis-aligned box.
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
double CoordType
Basic floating point type.
A dim dimensional box, lying at an arbitrary angle.
A polygon, all of whose points lie in a plane, embedded in dim dimensions.
Ball< dim > BoundingSphereSloppy(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get a bounding sphere for a set of points
Quaternion inverse() const
returns the inverse of the Quaternion
Polygon(const AtlasInType &a)
Construct a polygon from an object passed by Atlas.
const RotMatrix< dim > & orientation() const
returns the orientation of the box
The 2D specialization of the Polygon<> template.
A line segment embedded in dim dimensions.
Ball< dim > BoundingSphere(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get the minimal bounding sphere for a set of points
Point< dim > Barycenter(const container< Point< dim >, std::allocator< Point< dim > > > &c)
Find the center of a set of points, all weighted equally.
AxisBox< dim > BoundingBox(const container< AxisBox< dim >, std::allocator< AxisBox< dim > > > &c)
Get the axis-aligned bounding box for a set of boxes.