Library of Assembled Shared Sources
simple_polygon_2d.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2022 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::prim::SimplePolygon2D
46 * @brief convex or concave polygon in 2D (not selfintersecting, no holes)
47 * @author Bram de Greve [BdG]
48 *
49 * @warning SimplePolygon2D only ASSUMES it's simple. there's no guarantee at any time.
50 * It's your own responsibility to keep it simple. We do it this way because
51 * it's just to costly to check it at every access to the polygon. However, we
52 * provide some methods to check it yourself.
53 */
54
55#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_SIMPLE_POLYGON_2D_H
56#define LASS_GUARDIAN_OF_INCLUSION_PRIM_SIMPLE_POLYGON_2D_H
57
58#include "prim_common.h"
59#include "degenerate_policy.h"
60#include "orientation.h"
61#include "line_segment_2d.h"
62
63namespace lass
64{
65namespace prim
66{
67
68template
69<
70 typename T,
71 class DegeneratePolicy = NoDegenerate
72>
73class SimplePolygon2D
74{
75public:
76
77 typedef SimplePolygon2D<T, NoDegenerate> TSelf;
78 typedef DegeneratePolicy TDegeneratePolicy;
79
80 typedef Point2D<T> TPoint;
81 typedef Point2DH<T> TPointH;
82 typedef typename TPoint::TVector TVector;
83 typedef LineSegment2D<T> TLineSegment;
84
85 typedef typename TPoint::TValue TValue;
86 typedef typename TPoint::TParam TParam;
87 typedef typename TPoint::TReference TReference;
88 typedef typename TPoint::TConstReference TConstReference;
89 typedef typename TPoint::TNumTraits TNumTraits;
90
91 enum { dimension = TPoint::dimension }; /**< number of dimensions */
92
93 template <typename U> struct Rebind
94 {
95 typedef SimplePolygon2D<U, NoDegenerate> Type;
96 };
97
98 SimplePolygon2D();
99 template <typename InputIterator>
100 SimplePolygon2D(InputIterator iFirstVertex, InputIterator iLastVertex);
101 SimplePolygon2D(std::initializer_list<TPoint> init);
102
103 const TPoint& operator[](size_t iIndexOfVertex) const;
104 TPoint& operator[](size_t iIndexOfVertex);
105 const TPoint& at(int iIndexOfVertex) const;
106 TPoint& at(int iIndexOfVertex);
107 const TLineSegment edge(int iIndexOfTailVertex) const;
108 const TVector vector(int iIndexOfTailVertex) const;
109
110 void add(const TPoint& iVertex);
111 void insert(int iIndexOfVertex, const TPoint& iVertex);
112 void erase(int iIndexOfVertex);
113
114 bool isEmpty() const;
115 size_t size() const;
116
117 const TValue signedArea() const;
118 const TValue area() const;
119 const TValue perimeter() const;
120 const TPointH vertexCentroid() const;
121 const TPointH surfaceCentroid() const;
122
123 bool isSimple() const;
124 bool isConvex() const;
126
127 bool isReflex(int iIndexOfVertex) const;
128
129 Side classify(const TPoint& iP) const;
130 bool contains(const TPoint& iP) const;
131
132 void flip();
134 bool isValid() const;
135
136private:
137
138 bool isInRange(size_t iIndexOfVertex) const;
139
140 typedef std::vector<TPoint> TVertices;
141
142 TVertices vertices_;
143};
144
145template <typename T, typename DP, typename PP>
146bool intersects(const SimplePolygon2D<T, DP>& poly, const LineSegment2D<T, PP>& segment);
147
148template <typename T, typename DP, typename PP>
149bool intersects(const LineSegment2D<T, PP>& segment, const SimplePolygon2D<T, DP>& poly);
150
151template <typename T, typename DP1, typename DP2>
152bool intersects(const SimplePolygon2D<T, DP1>& a, const SimplePolygon2D<T, DP2>& b);
153
154
155template <typename T, class DP>
156io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const SimplePolygon2D<T, DP>& iPolygon);
157
158template <typename T, class DP>
159std::ostream& operator<<(std::ostream& ioOStream, const SimplePolygon2D<T, DP>& iPolygon);
160
161template <typename T, class DP>
163 const SimplePolygon2D<T, DP>& iPolygon);
164
165
166
167/** C = A \ B */
168template <typename T, class DP>
169bool set_difference(const SimplePolygon2D<T, DP>& iPolygonA,const SimplePolygon2D<T, DP>& iPolygonB, std::vector<SimplePolygon2D<T, DP> >& oPolygonsC);
170
171/** C = A U B */
172template <typename T, class DP>
173bool set_union(const SimplePolygon2D<T, DP>& iPolygonA,const SimplePolygon2D<T, DP>& iPolygonB, std::vector<SimplePolygon2D<T, DP> >& oPolygonsC);
174
175/** C = (A U B) \ (A \ B) \ (B \ A) */
176template <typename T, class DP>
177bool set_intersect(const SimplePolygon2D<T, DP>& iPolygonA,const SimplePolygon2D<T, DP>& iPolygonB, std::vector<SimplePolygon2D<T, DP> >& oPolygonsC);
178
179
180}
181
182}
183
184#include "simple_polygon_2d.inl"
185
186#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_SIMPLE_POLYGON_2D
187#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
189#endif
190
191#ifdef LASS_GUARDIAN_OF_INCLUSION_PRIM_AABB_2D_H
193#endif
194
195#ifdef LASS_GUARDIAN_OF_INCLUSION_PRIM_RAY_2D_H
197#endif
198
199#endif
200
201// EOF
Output stream for writing a selection of geometric primitives to matlab M files.
Output stream for writing a selection of geometric primitives to XML files.
convex or concave polygon in 2D (not selfintersecting, no holes)
const TPointH vertexCentroid() const
return the barycenter of all vertices.
bool isSimple() const
return true if polygon is simple, false if not.
void flip()
flip orientation of polygon.
size_t size() const
return number of vertices
void erase(int iIndexOfVertex)
remove the vertex at(iIndex)
bool isValid() const
a simple polygon is valid if it is not degenerate.
const TValue perimeter() const
return sum of the lengths of all edges
const TPoint & at(int iIndexOfVertex) const
return vertex of polygon by its index, but wrap around the bounds.
const TValue area() const
return area of the polygons surface.
const TPointH surfaceCentroid() const
return the centroid of the filled polygon.
bool isReflex(int iIndexOfVertex) const
return true if inner angle of vertex is reflex (is > 180 degrees).
bool isEmpty() const
return true if polygon has no vertices
Orientation orientation() const
return orientation of polygon.
bool isConvex() const
return true if polygon is convex, false if not.
TPoint & at(int iIndexOfVertex)
return vertex of polygon by its index, but wrap around the bounds.
TPoint & operator[](size_t iIndexOfVertex)
return vertex of polygon by its index, not wrapped, no bounds check.
void insert(int iIndexOfVertex, const TPoint &iVertex)
insert a vertex at iIndex (so it will sit before the current at(iIndex)).
void add(const TPoint &iVertex)
add a point at the "end" of the vertex list.
const TLineSegment edge(int iIndexOfTailVertex) const
return the edge of the polygon between vertices at(iIndex) and at(iIndex + 1).
const TVector vector(int iIndexOfTailVertex) const
return the vector between vertices at(iIndex) and at(iIndex + 1)\
const TValue signedArea() const
return signed polygon area.
const TPoint & operator[](size_t iIndexOfVertex) const
return vertex of polygon by its index, not wrapped, no bounds check.
void fixDegenerate()
fixes degenerate polygons as far as possible.
bool contains(const TPoint &iP) const
return true when a point is inside or on a polygon.
set of geometrical primitives
Definition aabb_2d.h:81
Side
Different sides of a surface.
Definition side.h:79
bool set_union(const SimplePolygon2D< T, DP > &iPolygonA, const SimplePolygon2D< T, DP > &iPolygonB, std::vector< SimplePolygon2D< T, DP > > &oPolygonsC)
C = A U B.
bool set_intersect(const SimplePolygon2D< T, DP > &iPolygonA, const SimplePolygon2D< T, DP > &iPolygonB, std::vector< SimplePolygon2D< T, DP > > &oPolygonsC)
C = (A U B) \ (A \ B) \ (B \ A)
bool set_difference(const SimplePolygon2D< T, DP > &iPolygonA, const SimplePolygon2D< T, DP > &iPolygonB, std::vector< SimplePolygon2D< T, DP > > &oPolygonsC)
C = A \ B.
Orientation
enumeration of clockwise versus counterclockwise
Definition orientation.h:58
Library for Assembled Shared Sources.
Definition config.h:53
This is the default policy.
homogenous 2D Point
Definition point_2dh.h:63