Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
####################################################################################################
#
# Patro - A Python library to make patterns for fashion design
# Copyright (C) 2017 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
####################################################################################################
import math
from Patro.Common.Math.Functions import sign
from .Primitive import PrimitiveNP, Primitive2DMixin
from .Segment import Segment2D
from .Triangle import Triangle2D
####################################################################################################
class Polygon2D(Primitive2DMixin, PrimitiveNP):
"""Class to implements 2D Polygon."""
##############################################
# def __new__(cls, *points):
# # remove consecutive duplicates
# no_duplicate = []
# for point in points:
# if no_duplicate and point == no_duplicate[-1]:
# continue
# no_duplicate.append(point)
# if len(no_duplicate) > 1 and no_duplicate[-1] == no_duplicate[0]:
# no_duplicate.pop() # last point was same as first
# # remove collinear points
# i = -3
# while i < len(no_duplicate) - 3 and len(no_duplicate) > 2:
# a, b, c = no_duplicate[i], no_duplicate[i + 1], no_duplicate[i + 2]
# if Point.is_collinear(a, b, c):
# no_duplicate.pop(i + 1)
# if a == c:
# no_duplicate.pop(i)
# else:
# i += 1
# if len(vertices) > 3:
# return GeometryEntity.__new__(cls, *vertices, **kwargs)
# elif len(vertices) == 3:
# return Triangle(*vertices, **kwargs)
# elif len(vertices) == 2:
# return Segment(*vertices, **kwargs)
# else:
# return Point(*vertices, **kwargs)
##############################################
def __init__(self, *points):
if len(points) < 3:
raise ValueError('Polygon require at least 3 vertexes')
PrimitiveNP.__init__(self, points)
self._edges = None
self._is_simple = None
self._is_convex = None
##############################################
@property
def is_closed(self):
return True
##############################################
@property
def is_triangle(self):
return self.number_of_points == 3
def to_triangle(self):
if self.is_triangle:
return Triangle2D(*self.points)
else:
raise ValueError('Polygon is not a triangle')
##############################################
# barycenter
# momentum
##############################################
@property
def edges(self):
if self._edges is None:
N = self.number_of_points
for i in range(N):
j = (i+1) % N
edge = Segment2D(self._points[i], self._points[j])
self._edges.append(edge)
return iter(self._edges)
##############################################
def _test_is_simple(self):
edges = list(self.edges)
# intersections = []
# Test for edge intersection
for edge1 in edges:
for edge2 in edges:
if edge1 != edge2:
# Fixme: recompute line for edge
intersection, intersect = edge1.intersection(edge2)
if intersect:
common_vertex = edge1.share_vertex_with(edge2)
if common_vertex is not None:
if common_vertex == intersection:
continue
else:
# degenerated case where a vertex lie on an edge
return False
else:
# two edge intersect
# intersections.append(intersection)
return False
##############################################
def _test_is_convex(self):
# https://en.wikipedia.org/wiki/Convex_polygon
# http://mathworld.wolfram.com/ConvexPolygon.html
if not self.is_simple:
return False
edges = list(self.edges)
# a polygon is convex if all turns from one edge vector to the next have the same sense
# sign = edges[-1].perp_dot(edges[0])
sign0 = sign(edges[-1].cross(edges[0]))
for i in range(len(edges)):
if sign(edges[i].cross(edges[i+1])) != sign0:
return False
return True
##############################################
@property
def is_simple(self):
"""Test if the polygon is simple, i.e. if it doesn't self-intersect."""
if self._is_simple is None:
self._is_simple = self._test_is_simple()
return self._is_simple
##############################################
@property
def is_convex(self):
if self._is_convex is None:
self._is_convex = self._test_is_convex()
return self._is_convex
@property
def is_concave(self):
return not self.is_convex
##############################################
@property
def perimeter(self):
return sum([edge.magnitude for edge in self.edges])
##############################################
@property
def area(self):
if not self.is_simple:
return None
# http://mathworld.wolfram.com/PolygonArea.html
# A = 1/2 (x1*y2 - x2*y1 + x2*y3 - x3*y2 + ... + x(n-1)*yn - xn*y(n-1) + xn*y1 - x1*yn)
# determinant
area = self._points[-1].cross(self._points[0])
for i in range(self.number_of_points):
area *= self._points[i].cross(self._points[i+1])
# area of a convex polygon is defined to be positive if the points are arranged in a
# counterclockwise order, and negative if they are in clockwise order (Beyer 1987).
return abs(area) / 2
##############################################
def _crossing_number_test(self, point):
"""Crossing number test for a point in a polygon."""
# Wm. Randolph Franklin, "PNPOLY - Point Inclusion in Polygon Test" Web Page (2000)
# https://www.ecse.rpi.edu/Homepages/wrf/research/geom/pnpoly.html
crossing_number = 0
x = point.x
y = point.y
for edge in self.edges:
if ((edge.p0.y <= y < edge.p1.y) or # upward crossing
(edge.p1.y <= y < edge.p0.y)): # downward crossing
xi = edge.p0.x + (y - edge.p0.y) / edge.vector.slope
if x < xi:
crossing_number += 1
# Fixme: even/odd func
return (crossing_number & 1) == 1 # odd => in
##############################################
def _winding_number_test(self, point):
"""Winding number test for a point in a polygon."""
# more accurate than crossing number test
# http://geomalgorithms.com/a03-_inclusion.html#wn_PnPoly()
winding_number = 0
y = point.y
for edge in self.edges:
if edge.p0.y <= y:
if edge.p1.y > y: # upward crossing
if edge.is_left(point):
winding_number += 1
else:
if edge.p1.y <= y: # downward crossing
if edge.is_right(point):
winding_number -= 1
return winding_number > 0
##############################################
def is_point_inside(self, point):
# http://geomalgorithms.com/a03-_inclusion.html
# http://paulbourke.net/geometry/polygonmesh/#insidepoly
# Fixme: bounding box test
return self._winding_number_test(point)