Skip to content

Commit a33ff26

Browse files
CHANGE the triangulation_earclip test are added as a separate file, testing only the algorithm
1 parent 9e862be commit a33ff26

File tree

4 files changed

+122
-89
lines changed

4 files changed

+122
-89
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5555
* Changed `compas.datastructures.Mesh` to take additional `**kwargs`, instead of only `name=None` specifically.
5656
* Moved registration of `ping` and `remote_shutdown` of the RPC server to `compas.rpc.Server.__init__()`.
5757
* Moved `FileWatcherService` to `compas.rpc.services.watcher` so it can be reused.
58-
* Changed `compas.geometry.earclip_polygon` algorithm because the current one does not work in several cases.
59-
* Changed the docstrings in `compas.geometry.earclip_polygon` are corrected. The orientation to the XY Frame has been removed since it is handled by the `polygon.to_vertices_and_faces` method. Additionally, the test_polygon.py file has been modified with a new test for the ear_clip called `test_polygon_to_vertices_and_faces`. The `object` has been added to all `Ear` classes.
60-
* Changed the docstrings in `compas.geometry.earclip_polygon` is merged back, meaning the polygon is transformed to XY frame.
58+
* Changed `compas.geometry.earclip_polygon` algorithm changed to a new one that better handles different cases.
6159

6260
### Removed
6361

src/compas/geometry/triangulation_earclip.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ def triangulate(self):
202202
If no more Ears were found for triangulation.
203203
204204
"""
205+
206+
if self.length < 3:
207+
raise ValueError("Polygon must have at least 3 vertices.")
208+
elif self.length == 3:
209+
self.triangles.append([0, 1, 2])
210+
return self.triangles
211+
205212
indexes = list(range(self.length))
206213
self.find_ears()
207214

tests/compas/geometry/test_polygon.py

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -87,89 +87,3 @@ def test_polygon__setitem__():
8787
assert polygon[4] == point
8888
assert isinstance(polygon[4], Point)
8989
assert polygon.lines[-2].end == point
90-
91-
92-
def test_polygon_to_vertices_and_faces():
93-
94-
points = [
95-
[332.639635, -3824, 1435.771272],
96-
[422.117559, -3301, 1563.558991],
97-
[642.370911, -3534, 1878.113376],
98-
[505.286143, -3908, 1682.336037],
99-
[477.180897, -3578, 1642.197587],
100-
[448.502075, -3634, 1601.239985],
101-
[441.619158, -3998, 1591.41016],
102-
[339.522553, -4195, 1445.601096],
103-
[189.245526, -3981, 1230.983261],
104-
[186.377644, -3672, 1226.887501],
105-
[278.149874, -3619, 1357.951828],
106-
[332.639635, -3414, 1435.771272],
107-
[192.686985, -3362, 1235.898173],
108-
[136.476494, -3688, 1155.621273],
109-
[63.05871, -4035, 1050.769811],
110-
[-51.083001, -3865, 887.758554],
111-
[-2.329003, -3686, 957.386478],
112-
[86.575344, -3695, 1084.355045],
113-
[69.368051, -3436, 1059.780484],
114-
[74.530239, -3315, 1067.152852],
115-
[219.071501, -3154, 1273.579167],
116-
[277.576297, -3227, 1357.132676],
117-
[336.081094, -3052, 1440.686184],
118-
[458.252875, -3120, 1615.16557],
119-
[718.083001, -3144, 1986.241446],
120-
[595.337643, -3295, 1810.942908],
121-
[459.973604, -3252, 1617.623026],
122-
[369.348527, -3265, 1488.197003],
123-
[377.952174, -3452, 1500.484283],
124-
]
125-
126-
polygon = Polygon(points)
127-
polygon.points.reverse()
128-
129-
vertices, faces = polygon.to_vertices_and_faces(True)
130-
131-
assert list(polygon) == vertices
132-
assert faces == [
133-
[2, 3, 4],
134-
[5, 6, 7],
135-
[7, 8, 9],
136-
[12, 13, 14],
137-
[14, 15, 16],
138-
[17, 18, 19],
139-
[19, 20, 21],
140-
[21, 22, 23],
141-
[23, 24, 25],
142-
[27, 28, 0],
143-
[1, 2, 4],
144-
[7, 9, 10],
145-
[14, 16, 17],
146-
[23, 25, 26],
147-
[0, 1, 4],
148-
[12, 14, 17],
149-
[21, 23, 26],
150-
[0, 4, 5],
151-
[12, 17, 19],
152-
[21, 26, 27],
153-
[0, 5, 7],
154-
[11, 12, 19],
155-
[19, 21, 27],
156-
[0, 7, 10],
157-
[11, 19, 27],
158-
[0, 10, 11],
159-
[11, 27, 0],
160-
]
161-
162-
# test polygon with self-coinciding vertices
163-
self_intersecting_polygon = Polygon(
164-
[
165-
[-1, -1, 0],
166-
[-1, 0, 0],
167-
[-1, 1, 0],
168-
[0, 0, 0],
169-
[0, 0, 0],
170-
]
171-
)
172-
173-
with pytest.raises(IndexError):
174-
vertices,
175-
faces = self_intersecting_polygon.to_vertices_and_faces(True)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import pytest
2+
import compas
3+
4+
5+
def test_earclip_polygon_triangle():
6+
7+
points = [
8+
[0, 0, 0],
9+
[1, 0, 0],
10+
[1, 1, 0],
11+
]
12+
13+
polygon = compas.geometry.Polygon(points)
14+
faces = compas.geometry.earclip_polygon(polygon)
15+
assert faces == [[0, 1, 2]]
16+
17+
18+
def test_earclip_polygon_square():
19+
20+
points = [
21+
[0, 0, 0],
22+
[1, 0, 0],
23+
[1, 1, 0],
24+
[0, 1, 0],
25+
]
26+
27+
polygon = compas.geometry.Polygon(points)
28+
faces = compas.geometry.earclip_polygon(polygon)
29+
assert faces == [[3, 0, 1], [1, 2, 3]]
30+
31+
32+
def test_earclip_polygon_wrong_winding():
33+
34+
points = [
35+
[332.639635, -3824, 1435.771272],
36+
[422.117559, -3301, 1563.558991],
37+
[642.370911, -3534, 1878.113376],
38+
[505.286143, -3908, 1682.336037],
39+
[477.180897, -3578, 1642.197587],
40+
[448.502075, -3634, 1601.239985],
41+
[441.619158, -3998, 1591.41016],
42+
[339.522553, -4195, 1445.601096],
43+
[189.245526, -3981, 1230.983261],
44+
[186.377644, -3672, 1226.887501],
45+
[278.149874, -3619, 1357.951828],
46+
[332.639635, -3414, 1435.771272],
47+
[192.686985, -3362, 1235.898173],
48+
[136.476494, -3688, 1155.621273],
49+
[63.05871, -4035, 1050.769811],
50+
[-51.083001, -3865, 887.758554],
51+
[-2.329003, -3686, 957.386478],
52+
[86.575344, -3695, 1084.355045],
53+
[69.368051, -3436, 1059.780484],
54+
[74.530239, -3315, 1067.152852],
55+
[219.071501, -3154, 1273.579167],
56+
[277.576297, -3227, 1357.132676],
57+
[336.081094, -3052, 1440.686184],
58+
[458.252875, -3120, 1615.16557],
59+
[718.083001, -3144, 1986.241446],
60+
[595.337643, -3295, 1810.942908],
61+
[459.973604, -3252, 1617.623026],
62+
[369.348527, -3265, 1488.197003],
63+
[377.952174, -3452, 1500.484283],
64+
]
65+
66+
polygon = compas.geometry.Polygon(points)
67+
polygon.points.reverse()
68+
69+
faces = compas.geometry.earclip_polygon(polygon)
70+
71+
assert faces == [
72+
[2, 3, 4],
73+
[5, 6, 7],
74+
[7, 8, 9],
75+
[12, 13, 14],
76+
[14, 15, 16],
77+
[17, 18, 19],
78+
[19, 20, 21],
79+
[21, 22, 23],
80+
[23, 24, 25],
81+
[27, 28, 0],
82+
[1, 2, 4],
83+
[7, 9, 10],
84+
[14, 16, 17],
85+
[23, 25, 26],
86+
[0, 1, 4],
87+
[12, 14, 17],
88+
[21, 23, 26],
89+
[0, 4, 5],
90+
[12, 17, 19],
91+
[21, 26, 27],
92+
[0, 5, 7],
93+
[11, 12, 19],
94+
[19, 21, 27],
95+
[0, 7, 10],
96+
[11, 19, 27],
97+
[0, 10, 11],
98+
[11, 27, 0],
99+
]
100+
101+
102+
def test_earclip_polygon_coincident_points():
103+
self_intersecting_polygon = compas.geometry.Polygon(
104+
[
105+
[-1, -1, 0],
106+
[-1, 0, 0],
107+
[-1, 1, 0],
108+
[0, 0, 0],
109+
[0, 0, 0],
110+
]
111+
)
112+
113+
with pytest.raises(IndexError):
114+
compas.geometry.earclip_polygon(self_intersecting_polygon)

0 commit comments

Comments
 (0)