Skip to content

Commit 5c1b7b9

Browse files
committed
fix(polyline): Improve the removing of colinear vertices in polylines
1 parent d328e0b commit 5c1b7b9

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

ladybug_geometry/geometry2d/polyline.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ def remove_colinear_vertices(self, tolerance):
153153
skip = 0 # track the number of vertices being skipped/removed
154154
# loop through vertices and remove all cases of colinear verts
155155
for i, _v in enumerate(self.vertices[1:-1]):
156-
_a = self[i - skip].determinant(_v) + _v.determinant(self[i + 2]) + \
157-
self[i + 2].determinant(self[i - skip])
158-
if abs(_a) >= tolerance:
156+
_v2, _v1 = self[i - skip], self[i + 2]
157+
_a = _v2.determinant(_v) + _v.determinant(_v1) + _v1.determinant(_v2)
158+
b_dist = _v1.distance_to_point(_v2)
159+
b_dist = tolerance if b_dist < tolerance else b_dist
160+
tri_tol = (b_dist * tolerance) / 2 # area of triangle with tolerance height
161+
if abs(_a) >= tri_tol: # triangle area > tolerance; not colinear
159162
new_vertices.append(_v)
160163
skip = 0
161164
else:

ladybug_geometry/geometry3d/polyline.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,21 @@ def remove_colinear_vertices(self, tolerance):
136136
before it is considered colinear.
137137
"""
138138
if len(self.vertices) == 3:
139-
return self # Polyline3D cannot have fewer than 3 vertices
139+
return self # Polyline3D cannot have fewer than 3 vertices
140140
new_vertices = [self.vertices[0]] # first vertex is always ok
141+
skip = 0 # track the number of vertices being skipped/removed
142+
# loop through vertices and remove all cases of colinear verts
141143
for i, _v in enumerate(self.vertices[1:-1]):
142-
if (self[i] - _v).cross(self[i + 2] - _v).magnitude >= tolerance:
144+
_v2, _v1 = self[i - skip], self[i + 2]
145+
_a = (_v2 - _v).cross(_v1 - _v).magnitude
146+
b_dist = _v1.distance_to_point(_v2)
147+
b_dist = tolerance if b_dist < tolerance else b_dist
148+
tri_tol = (b_dist * tolerance) / 2 # area of triangle with tolerance height
149+
if abs(_a) >= tri_tol: # triangle area > tolerance; not colinear
143150
new_vertices.append(_v)
151+
skip = 0
152+
else:
153+
skip += 1
144154
new_vertices.append(self[-1]) # last vertex is always ok
145155
_new_poly = Polyline3D(new_vertices)
146156
self._transfer_properties(_new_poly)

0 commit comments

Comments
 (0)