Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(polygon): Fix bug in Polygon2D.remove_colinear_vertices #395

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ladybug_geometry/geometry2d/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,16 +608,18 @@ def remove_colinear_vertices(self, tolerance):
"""
if len(self.vertices) == 3:
return self # Polygon2D cannot have fewer than 3 vertices
new_vertices = []
skip = 0
new_vertices = [] # list to hold the new vertices
skip = 0 # track the number of vertices being skipped/removed
# loop through vertices and remove all cases of colinear verts
for i, _v in enumerate(self.vertices):
_a = self[i - 2].determinant(self[i - 1]) + self[i - 1].determinant(_v) + \
_v.determinant(self[i - 2])
_a = self[i - 2 - skip].determinant(self[i - 1]) + self[i - 1].determinant(_v) + \
_v.determinant(self[i - 2 - skip])
if abs(_a) >= tolerance:
new_vertices.append(self[i - 1])
skip = 0
else:
skip += 1
# catch case of last two vertices being equal but distinct from first point
if skip != 0 and self.vertices[-2].is_equivalent(self.vertices[-1], tolerance):
pts_2d = self.vertices
_a = pts_2d[-3].determinant(pts_2d[-1]) + \
Expand Down
11 changes: 11 additions & 0 deletions tests/face3d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,17 @@ def test_join_coplanar_faces():
assert len(joined_faces[0].holes) == 5


def test_join_coplanar_faces_advanced():
"""Test the join_coplanar_faces method."""
geo_file = './tests/json/union_faces.json'
with open(geo_file, 'r') as fp:
geo_dict = json.load(fp)
join_faces = [Face3D.from_dict(fd) for fd in geo_dict]

join_face = Face3D.join_coplanar_faces(join_faces, 0.001)
assert sum(f.area for f in join_faces) == pytest.approx(join_face[0].area, rel=1e-3)


def test_extract_all_from_stl():
file_path = 'tests/stl/cube_binary.stl'
faces = Face3D.extract_all_from_stl(file_path)
Expand Down
1 change: 1 addition & 0 deletions tests/json/union_faces.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type": "Face3D", "boundary": [[6.96093151743763, 1.2384060214181818, 2.9999999999999987], [8.290021124356151, 1.2384060214181818, 2.9999999999999987], [8.290021124356151, -0.7441439785816331, 2.9999999999999987], [6.960931517437629, -0.7441439785816288, 2.9999999999999987], [6.96093151743763, 1.2384060214181818, 2.9999999999999987]], "plane": {"type": "Plane", "n": [0.0, 0.0, -1.0], "o": [6.96093151743763, 1.2384060214181818, 2.9999999999999987], "x": [1.0, 0.0, 0.0]}}, {"type": "Face3D", "boundary": [[7.642481517437625, -0.7441439785816324, 2.9999999999999987], [10.001931517437628, -0.7441439785816344, 2.9999999999999987], [10.001931517437628, -3.302593978581496, 2.9999999999999987], [7.642481517437624, -3.302593978581496, 2.9999999999999987]], "plane": {"type": "Plane", "n": [0.0, 0.0, -1.0], "o": [7.642481517437625, -0.7441439785816324, 2.9999999999999987], "x": [1.0, 0.0, 0.0]}}, {"type": "Face3D", "boundary": [[6.859931517437632, 1.2384060214181867, 2.9999999999999987], [6.96093151743763, 1.2384060214181818, 2.9999999999999987], [6.960931517437629, -0.7441439785816288, 2.9999999999999987], [7.642481517437625, -0.7441439785816324, 2.9999999999999987], [7.642481517437624, -3.302593978581496, 2.9999999999999987], [7.001931517437624, -3.302593978581496, 2.9999999999999987], [7.001931517437626, -1.1455939785817102, 2.9999999999999987], [6.616831517437697, -1.1455939785817089, 2.9999999999999987], [6.6168315174376975, 1.3584060214181886, 2.9999999999999987], [6.859931517437632, 1.3584060214181877, 2.9999999999999987]], "plane": {"type": "Plane", "n": [0.0, 0.0, -1.0], "o": [6.859931517437632, 1.2384060214181867, 2.9999999999999987], "x": [1.0, 0.0, 0.0]}}, {"type": "Face3D", "boundary": [[10.001931517437628, 1.238406021418177, 2.9999999999999987], [10.001931517437628, -0.7441439785816344, 2.9999999999999987], [8.290021124356151, -0.7441439785816331, 2.9999999999999987], [8.290021124356151, 1.2384060214181818, 2.9999999999999987], [10.001931517437628, 1.238406021418177, 2.9999999999999987]], "plane": {"type": "Plane", "n": [0.0, 0.0, -1.0], "o": [10.001931517437628, 1.238406021418177, 2.9999999999999987], "x": [1.0, 0.0, 0.0]}}, {"type": "Face3D", "boundary": [[6.96093151743763, 1.2384060214181818, 2.9999999999999987], [6.859931517437632, 1.2384060214181867, 2.9999999999999987], [6.859931517437632, 1.3584060214181877, 2.9999999999999987], [10.001931517437628, 1.3584060214181772, 2.9999999999999987], [10.001931517437628, 1.238406021418177, 2.9999999999999987], [6.96093151743763, 1.2384060214181818, 2.9999999999999987]], "plane": {"type": "Plane", "n": [0.0, 0.0, -1.0], "o": [6.96093151743763, 1.2384060214181818, 2.9999999999999987], "x": [1.0, 0.0, 0.0]}}]
Loading