Skip to content

Commit

Permalink
fix(polygon): Respect tolerance when removing colinear vertices
Browse files Browse the repository at this point in the history
It seems that the function to remove colinear vertices was too aggressive in some cases and was not aggressive enough in other cases. This is because the criteria to evaluate whether a given vertex is colinear did not account for the distance between the neighboring vertices.

Now, a colinear vertex will only be removed if the distance between it and the line drawn between the two neighboring vertices is less than the tolerance.
  • Loading branch information
chriswmackey committed Oct 29, 2024
1 parent 620dc47 commit dfcaffa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions ladybug_geometry/geometry2d/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,10 @@ def remove_colinear_vertices(self, tolerance):
for i, _v in enumerate(self.vertices):
_v2, _v1 = self[i - 2 - skip], self[i - 1]
_a = _v2.determinant(_v1) + _v1.determinant(_v) + _v.determinant(_v2)
if abs(_a) >= tolerance: # triangle area > tolerance; not colinear
b_dist = _v.distance_to_point(_v2)
b_dist = tolerance if b_dist < tolerance else b_dist
tri_tol = (b_dist * tolerance) / 2 # area of triangle with tolerance height
if abs(_a) >= tri_tol: # triangle area > tolerance; not colinear
new_vertices.append(self[i - 1])
skip = 0
if is_first:
Expand All @@ -638,7 +641,10 @@ def remove_colinear_vertices(self, tolerance):
'There must be at least 3 vertices for a Polygon2D.'
_v2, _v1, _v = self[-2 - skip], self[-1], self[first_skip]
_a = _v2.determinant(_v1) + _v1.determinant(_v) + _v.determinant(_v2)
if abs(_a) >= tolerance: # triangle area > area tolerance; not colinear
b_dist = _v.distance_to_point(_v2)
b_dist = tolerance if b_dist < tolerance else b_dist
tri_tol = (b_dist * tolerance) / 2 # area of triangle with tolerance height
if abs(_a) >= tri_tol: # triangle area > area tolerance; not colinear
new_vertices.append(_v1)
return Polygon2D(new_vertices)

Expand Down
10 changes: 8 additions & 2 deletions ladybug_geometry/geometry3d/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -2891,7 +2891,10 @@ def _remove_colinear(self, pts_3d, pts_2d, tolerance):
for i, _v in enumerate(pts_2d):
_v2, _v1 = pts_2d[i - 2 - skip], pts_2d[i - 1]
_a = _v2.determinant(_v1) + _v1.determinant(_v) + _v.determinant(_v2)
if abs(_a) >= tolerance: # triangle area > area tolerance; not colinear
b_dist = _v.distance_to_point(_v2)
b_dist = tolerance if b_dist < tolerance else b_dist
tri_tol = (b_dist * tolerance) / 2 # area of triangle with tolerance height
if abs(_a) >= tri_tol: # triangle area > area tolerance; not colinear
new_vertices.append(pts_3d[i - 1])
skip = 0
if is_first:
Expand All @@ -2905,7 +2908,10 @@ def _remove_colinear(self, pts_3d, pts_2d, tolerance):
'There must be at least 3 vertices for a Face3D.'
_v2, _v1, _v = pts_2d[-2 - skip], pts_2d[-1], pts_2d[first_skip]
_a = _v2.determinant(_v1) + _v1.determinant(_v) + _v.determinant(_v2)
if abs(_a) >= tolerance: # triangle area > area tolerance; not colinear
b_dist = _v.distance_to_point(_v2)
b_dist = tolerance if b_dist < tolerance else b_dist
tri_tol = (b_dist * tolerance) / 2 # area of triangle with tolerance height
if abs(_a) >= tri_tol: # triangle area > area tolerance; not colinear
new_vertices.append(pts_3d[-1])
return new_vertices

Expand Down

0 comments on commit dfcaffa

Please sign in to comment.