diff --git a/ladybug_geometry/geometry2d/polygon.py b/ladybug_geometry/geometry2d/polygon.py index 31ab515b..fe9af131 100644 --- a/ladybug_geometry/geometry2d/polygon.py +++ b/ladybug_geometry/geometry2d/polygon.py @@ -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]) + \ diff --git a/tests/face3d_test.py b/tests/face3d_test.py index d36d69c9..f6b7b95a 100644 --- a/tests/face3d_test.py +++ b/tests/face3d_test.py @@ -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) diff --git a/tests/json/union_faces.json b/tests/json/union_faces.json new file mode 100644 index 00000000..50a33dc9 --- /dev/null +++ b/tests/json/union_faces.json @@ -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]}}] \ No newline at end of file