"""Small geometry counterexamples for complete-face fitting and auditing.""" import copy import math from pathlib import Path import tempfile import unittest import xml.etree.ElementTree as ET import numpy as np import trimesh from collision_pieces import (connected_face_groups, fit_pieces, link_triangles, partition_triangles, shared_fitter) from verify_collision import (best_face_margins, coverage_selection, face_coverage_margins, verify_rim_cylinder) def box(size, position=(0, 0, 0), rpy=(0, 0, 0)): collision = ET.Element("collision") ET.SubElement(collision, "origin", xyz=" ".join(map(str, position)), rpy=" ".join(map(str, rpy))) geometry = ET.SubElement(collision, "geometry") ET.SubElement(geometry, "box", size=" ".join(map(str, size))) return collision def cylinder(radius, length, position=(0, 0, 0), rpy=(0, 0, 0)): collision = ET.Element("collision") ET.SubElement(collision, "origin", xyz=" ".join(map(str, position)), rpy=" ".join(map(str, rpy))) geometry = ET.SubElement(collision, "geometry") ET.SubElement(geometry, "cylinder", radius=str(radius), length=str(length)) return collision class CollisionPiecesTest(unittest.TestCase): def setUp(self): # The first face's centroid is below x=0.2, although two vertices cross # that boundary. The second face belongs to the other region. self.faces = np.array([ [[-0.2, 0.0, 0.0], [0.3, 0.2, 0.0], [0.4, 0.0, 0.1]], [[0.6, 0.0, 0.0], [0.8, 0.2, 0.0], [0.9, 0.0, 0.1]], ]) self.pieces = [ {"name": "near", "bounds_max": [0.2, math.inf, math.inf]}, {"name": "far", "bounds_min": [0.2, -math.inf, -math.inf]}, ] def test_separately_enclosed_vertices_do_not_cover_a_face(self): face = np.array([[[-2.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 2.0, 0.0]]]) boxes = [box((0.4, 0.4, 0.4), vertex) for vertex in face[0]] # Every vertex is well inside some box, while the connecting triangle # passes through empty space. A vertex-wise union test would pass. point_faces = np.repeat(face[0, :, None, :], 3, axis=1) np.testing.assert_allclose(best_face_margins(point_faces, boxes), 0.2) self.assertLess(best_face_margins(face, boxes)[0], 0.0) def test_fit_keeps_whole_faces_crossing_centroid_region_boundaries(self): np.testing.assert_array_equal(partition_triangles(self.faces, self.pieces), [0, 1]) fits = fit_pieces(self.faces, {"pieces": self.pieces, "padding": 0.001}) self.assertEqual([count for _, _, count in fits], [1, 1]) # Audit each assigned face against its own fitted body, independently # of the partitioner. Both out-of-region vertices must remain covered. for face, (_, fit, _) in zip(self.faces, fits): collider = shared_fitter().create_urdf_collision(fit) self.assertGreaterEqual(best_face_margins(face[None], [collider])[0], 0.001 - 1e-10) def test_half_open_boundary_has_one_owner(self): faces = np.array([ [[-1, 0, 0], [0, 1, 0], [1, 0, 1]], # centroid x=0 [[-2, 0, 0], [-1, 1, 0], [0, 0, 1]], ], dtype=float) pieces = [ {"name": "negative", "bounds_max": [0, math.inf, math.inf]}, {"name": "nonnegative", "bounds_min": [0, -math.inf, -math.inf]}, ] np.testing.assert_array_equal(partition_triangles(faces, pieces), [1, 0]) def test_disabled_piece_preserves_its_region_and_other_fits(self): original = fit_pieces(self.faces, {"pieces": self.pieces}) pieces = [self.pieces[0], {**self.pieces[1], "enabled": False}] filtered = fit_pieces(self.faces, {"pieces": pieces}) self.assertEqual([(name, count) for name, _, count in filtered], [("near", 1)]) np.testing.assert_array_equal(filtered[0][1].dimensions, original[0][1].dimensions) np.testing.assert_array_equal(filtered[0][1].transform, original[0][1].transform) # Disabling "far" must not silently excuse any face outside both regions. pieces[0] = {**pieces[0], "bounds_min": [-1, -math.inf, -math.inf]} unassigned = self.faces[0] - [2, 0, 0] with self.assertRaisesRegex(ValueError, "not assigned"): fit_pieces(np.array([*self.faces, unassigned]), {"pieces": pieces}) def test_audit_excludes_only_faces_assigned_to_disabled_piece(self): pieces = [self.pieces[0], {**self.pieces[1], "enabled": False}] retained, excluded = coverage_selection(self.faces, pieces, ["collision_near"]) np.testing.assert_array_equal(retained, [True, False]) self.assertEqual(excluded, {"far": 1}) # The retained face crosses the region boundary. Its farthest vertex # is still required when auditing coverage of the remaining collider. margins, _ = face_coverage_margins(self.faces[retained], [box((0.5, 1, 1))], 0.001) self.assertLess(margins[0], 0.0) with self.assertRaises((ValueError, RuntimeError)): coverage_selection(self.faces, pieces, ["collision_near", "collision_far_slice00"]) def test_rim_cylinder_uses_the_distal_circle_even_in_a_disabled_region(self): for direction in (-1, 1): with self.subTest(direction=direction): center = np.array([0.1, 0., -0.3]) end = 0.15 * direction rim = center + np.array([[.04, end, 0], [0, end, .04], [-.04, end, 0], [0, end, -.04]]) faces = np.array([rim[:3], [rim[0], rim[2], rim[3]], [center, center + [.08, 0, 0], center + [0, 0, .08]]]) piece = {"name": "body", "primitive": "cylinder", "rim_cylinder": {"start_y": 0, "end_y": end, "center_xz": [.1, -.3]}} cap = {"name": "cap", "enabled": False} lower_piece, upper_piece = (piece, cap) if end > 0 else (cap, piece) lower_piece["bounds_max"] = [np.inf, end / 2, np.inf] upper_piece["bounds_min"] = [-np.inf, end / 2, -np.inf] (_, fit, count), = fit_pieces(faces, {"pieces": [piece, cap]}) self.assertEqual(count, 1) np.testing.assert_allclose(fit.dimensions, [.04, .15]) np.testing.assert_allclose(fit.transform[:3, 2], [0, direction, 0]) np.testing.assert_allclose(fit.transform[:3, 3], [.1, end / 2, -.3]) collider = shared_fitter().create_urdf_collision(fit) verify_rim_cylinder(piece, collider, faces) collider.find("geometry/cylinder").set("radius", "0.08") with self.assertRaises(RuntimeError): verify_rim_cylinder(piece, collider, faces) def test_axial_layers_follow_a_taper_across_original_triangles(self): # Every side face spans all three layers. The middle radius therefore # has to come from intersections on the edges, not original vertices. ring = np.array([[1, 0, 0], [0, 0, 1], [-1, 0, 0], [0, 0, -1]]) wide = 2 * ring narrow = ring + [0, 1, 0] faces = [] for i in range(len(ring)): j = (i + 1) % len(ring) faces.extend([[wide[i], wide[j], narrow[i]], [wide[j], narrow[j], narrow[i]]]) settings = {"padding": 0.001, "pieces": [ {"name": "rim", "primitive": "cylinder", "axis": "y", "axial_layers": 3}]} fits = fit_pieces(np.array(faces), settings) self.assertEqual([name for name, _, _ in fits], ["rim_slice00", "rim_slice01", "rim_slice02"]) radii, lengths = np.array([fit.dimensions for _, fit, _ in fits]).T np.testing.assert_allclose(radii, np.array([2, 5 / 3, 4 / 3]) + 0.001) np.testing.assert_allclose(lengths, 1 / 3 + 0.002) def test_invalid_partitions_fail_instead_of_discarding_faces(self): invalid = { "overlap": [{"name": "all"}, {"name": "again"}], "disabled_overlap": [{"name": "all"}, {**self.pieces[1], "enabled": False}], "invalid_enabled": [self.pieces[0], {**self.pieces[1], "enabled": "false"}], "omission": [self.pieces[0]], "empty_piece": self.pieces + [{"name": "empty", "bounds_min": [10, 10, 10]}], "nan_bound": [{"name": "nan", "bounds_min": [math.nan, 0, 0]}], "duplicate_name": [{"name": "same"}, {"name": "same"}], "inverted_bounds": [{"name": "inverted", "bounds_min": [1, 0, 0], "bounds_max": [0, 1, 1]}], } for name, pieces in invalid.items(): with self.subTest(name=name), self.assertRaises(ValueError): partition_triangles(self.faces, pieces) def test_nonfinite_source_geometry_is_rejected_by_fit_and_audit(self): faces = self.faces.copy() faces[0, 1, 2] = math.nan with self.assertRaises(ValueError): fit_pieces(faces, {"pieces": self.pieces}) with self.assertRaises(RuntimeError): best_face_margins(faces, [box((10, 10, 10))]) def test_repeat_fit_preserves_explicit_names_order_and_geometry(self): settings = {"pieces": list(reversed(self.pieces)), "padding": 0.001} first = fit_pieces(self.faces, settings) second = fit_pieces(self.faces.copy(), copy.deepcopy(settings)) self.assertEqual([name for name, _, _ in first], ["far", "near"]) self.assertEqual([name for name, _, _ in second], ["far", "near"]) for (_, a, count_a), (_, b, count_b) in zip(first, second): self.assertEqual(count_a, count_b) self.assertEqual(a.kind, b.kind) np.testing.assert_array_equal(a.dimensions, b.dimensions) np.testing.assert_array_equal(a.transform, b.transform) def test_normal_groups_cover_complete_faces_with_stable_names_and_geometry(self): # Six explicitly oriented faces, supplied in an unrelated order. No # normal-classification helper is reused to determine the expectations. faces_by_name = { "skin_x_pos": [[0, 0, 0], [0, 1, 0], [0, 0, 1]], "skin_x_neg": [[2, 0, 0], [2, 0, 1], [2, 1, 0]], "skin_y_pos": [[0, 2, 0], [0, 2, 1], [1, 2, 0]], "skin_y_neg": [[0, 4, 0], [1, 4, 0], [0, 4, 1]], "skin_z_pos": [[0, 0, 3], [1, 0, 3], [0, 1, 3]], "skin_z_neg": [[0, 0, 5], [0, 1, 5], [1, 0, 5]], } expected_names = list(faces_by_name) faces = np.array([faces_by_name[expected_names[index]] for index in [5, 2, 0, 3, 4, 1]], dtype=float) settings = {"pieces": [{"name": "skin", "split_normals": True}], "padding": 0.001} first = fit_pieces(faces, settings) second = fit_pieces(faces[::-1].copy(), copy.deepcopy(settings)) self.assertEqual([name for name, _, _ in first], expected_names) self.assertEqual([name for name, _, _ in second], expected_names) self.assertEqual(sum(count for _, _, count in first), len(faces)) for (name, a, count_a), (_, b, count_b) in zip(first, second): self.assertEqual(count_a, 1) self.assertEqual(count_b, 1) np.testing.assert_array_equal(a.dimensions, b.dimensions) np.testing.assert_array_equal(a.transform, b.transform) collider = shared_fitter().create_urdf_collision(a) source_face = np.array([faces_by_name[name]], dtype=float) self.assertGreaterEqual(best_face_margins(source_face, [collider])[0], 0.001 - 1e-10) def test_normal_split_keeps_degenerate_faces_and_rejects_name_collisions(self): degenerates = { "line": [[-1, 0, 0], [0, 0, 0], [1, 0, 0]], "point": [[2, 3, 4], [2, 3, 4], [2, 3, 4]], } for kind, face in degenerates.items(): with self.subTest(degenerate=kind): faces = np.array([face], dtype=float) fits = fit_pieces(faces, {"alignment": "oriented", "padding": 0.001, "pieces": [{"name": "degenerate", "split_normals": True}]}) self.assertEqual([(name, count) for name, _, count in fits], [("degenerate_x_pos", 1)]) collider = shared_fitter().create_urdf_collision(fits[0][1]) self.assertGreaterEqual(best_face_margins(faces, [collider])[0], 0.001 - 1e-10) faces = np.array([ [[-1, 0, 0], [-1, 1, 0], [-1, 0, 1]], [[1, 0, 0], [1, 1, 0], [1, 0, 1]], ], dtype=float) pieces = [ {"name": "panel", "split_normals": True, "bounds_max": [0, math.inf, math.inf]}, {"name": "panel_x_pos", "bounds_min": [0, -math.inf, -math.inf]}, ] with self.subTest(derived_name_conflict=True), self.assertRaisesRegex(ValueError, "names collide"): fit_pieces(faces, {"pieces": pieces}) for invalid_flag in ("false", 1): with self.subTest(split_normals=invalid_flag), self.assertRaisesRegex(ValueError, "boolean"): fit_pieces(faces, {"pieces": [{"name": "panel", "split_normals": invalid_flag}]}) def test_components_preserve_original_faces_and_isolated_degenerates(self): # Interleave a connected pair with unrelated STL faces. Welding their # duplicated positions must not reorder or remove the source faces. faces = np.array([ [[10, 0, 0], [11, 0, 0], [10, 1, 0]], [[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[30, 0, 0], [30, 0, 0], [30, 0, 0]], [[1, 0, 0], [1, 1, 0], [0, 1, 0]], [[20, 0, 0], [21, 0, 0], [22, 0, 0]], ], dtype=float) expected_indices = [[0], [1, 3], [2], [4]] original = faces.copy() groups = connected_face_groups(faces) self.assertEqual(len(groups), len(expected_indices)) for group, indices in zip(groups, expected_indices): np.testing.assert_array_equal(group, faces[indices]) np.testing.assert_array_equal(faces, original) settings = {"pieces": [{"name": "shell", "split_components": True}], "padding": 0.001, "alignment": "oriented"} first = fit_pieces(faces, settings) second = fit_pieces(faces.copy(), copy.deepcopy(settings)) self.assertEqual([(name, count) for name, _, count in first], [("shell_part000", 1), ("shell_part001", 2), ("shell_part002", 1), ("shell_part003", 1)]) self.assertEqual(sum(count for _, _, count in first), len(faces)) for (name, a, count_a), (other_name, b, count_b), indices in zip( first, second, expected_indices): self.assertEqual((name, count_a), (other_name, count_b)) np.testing.assert_array_equal(a.dimensions, b.dimensions) np.testing.assert_array_equal(a.transform, b.transform) collider = shared_fitter().create_urdf_collision(a) self.assertTrue(np.all(best_face_margins(faces[indices], [collider]) >= 0.001 - 1e-10)) def test_component_split_rejects_invalid_flags_and_derived_name_collision(self): for invalid_flag in ("false", 1): with self.subTest(split_components=invalid_flag), self.assertRaisesRegex(ValueError, "boolean"): fit_pieces(self.faces, {"pieces": [{"name": "panel", "split_components": invalid_flag}]}) pieces = copy.deepcopy(self.pieces) pieces[0].update(name="panel", split_components=True) pieces[1]["name"] = "panel_part000" with self.assertRaisesRegex(ValueError, "names collide"): fit_pieces(self.faces, {"pieces": pieces}) def test_auto_fits_a_tilted_cylinder_and_keeps_complete_face_padding(self): # Build a closed, triangulated tube without using the fitting helpers. # Its known axis is oblique to every link axis, so a fixed-axis fit # cannot accidentally satisfy the orientation and coverage assertions. segments = 32 angles = np.arange(segments) * (2 * math.pi / segments) ring = np.column_stack([0.01 * np.cos(angles), 0.01 * np.sin(angles)]) vertices = np.concatenate([ np.column_stack([ring, np.full(segments, -0.05)]), np.column_stack([ring, np.full(segments, 0.05)]), [[0, 0, -0.05], [0, 0, 0.05]], ]) indices = [] for index in range(segments): following = (index + 1) % segments indices.extend([ [index, following, following + segments], [index, following + segments, index + segments], [2 * segments, following, index], [2 * segments + 1, index + segments, following + segments], ]) expected_axis = np.array([1.0, 2.0, 3.0]) / math.sqrt(14) radial_axis = np.array([2.0, -1.0, 0.0]) / math.sqrt(5) rotation = np.column_stack([radial_axis, np.cross(expected_axis, radial_axis), expected_axis]) transformed = vertices @ rotation.T + np.array([0.12, -0.07, 0.03]) faces = transformed[np.asarray(indices)] piece = {"name": "wire", "primitive": "auto", "axis": "auto", "allowed_primitives": ["box", "cylinder"]} settings = {"pieces": [piece], "alignment": "oriented", "padding": 0.001} [(name, fit, count)] = fit_pieces(faces, settings) self.assertEqual((name, fit.kind, count), ("wire", "cylinder", 4 * segments)) fitted_axis = fit.transform[:3, 2] self.assertGreater(abs(float(fitted_axis @ expected_axis)), 0.999) self.assertLess(float(np.max(np.abs(fitted_axis))), 0.95) collider = shared_fitter().create_urdf_collision(fit) self.assertTrue(np.all(best_face_margins(faces, [collider]) >= 0.001 - 1e-9)) # Changing the length unit must preserve the chosen shape, including # its physical padding. Compare axes up to their equivalent sign. [(scaled_name, scaled_fit, scaled_count)] = fit_pieces( faces * 10, {**settings, "padding": 0.01}) self.assertEqual((scaled_name, scaled_fit.kind, scaled_count), (name, fit.kind, count)) np.testing.assert_allclose(np.array(scaled_fit.dimensions) / 10, fit.dimensions, rtol=1e-4, atol=1e-8) np.testing.assert_allclose(scaled_fit.transform[:3, 3] / 10, fit.transform[:3, 3], rtol=1e-4, atol=1e-8) scaled_axis = scaled_fit.transform[:3, 2] aligned_axis = scaled_axis * np.sign(scaled_axis @ fitted_axis) np.testing.assert_allclose(aligned_axis, fitted_axis, rtol=1e-4, atol=1e-4) for invalid in ({"allowed_primitives": []}, {"axis": "diagonal"}): with self.subTest(invalid=invalid), self.assertRaises(ValueError): fit_pieces(faces, {**settings, "pieces": [{**piece, **invalid}]}) def test_rotated_translated_box_margin_matches_local_geometry(self): local = np.array([[[0.8, 0, 0], [0, 0.1, 0.2], [-0.6, -0.2, 0]]]) # Rz(pi/2) Rx(pi/2), calculated explicitly rather than using the # verifier's origin parser to manufacture its own expected result. rotation = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) position = np.array([3, -2, 1]) world = local @ rotation.T + position collider = box((2, 0.8, 0.6), position, (math.pi / 2, 0, math.pi / 2)) np.testing.assert_allclose(best_face_margins(world, [collider]), [0.1], atol=1e-12) def test_rotated_cylinder_checks_radial_and_end_cap_margins(self): local = np.array([ [[0.4, 0, 0], [0, 0.2, 0.8], [-0.2, 0, 0]], [[0.1, 0, 0.95], [0, 0.2, 0.8], [-0.2, 0, 0.5]], ]) rotation = np.array([[0, 0, 1], [0, 1, 0], [-1, 0, 0]]) position = np.array([-2, 3, 4]) world = local @ rotation.T + position collider = cylinder(0.5, 2, position, (0, math.pi / 2, 0)) expected = [0.1, 0.05] np.testing.assert_allclose(best_face_margins(world, [collider]), expected, atol=1e-12) np.testing.assert_allclose(best_face_margins(world, [collider], batch_size=1), expected, atol=1e-12) def test_source_visual_scale_and_origin_are_applied_before_partitioning(self): vertices = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float) with tempfile.TemporaryDirectory(prefix="xiaozhao-piece-test-") as directory: mesh_path = Path(directory) / "triangle.stl" trimesh.Trimesh(vertices=vertices, faces=[[0, 1, 2]], process=False).export(mesh_path) link = ET.Element("link", name="fixture") visual = ET.SubElement(link, "visual") ET.SubElement(visual, "origin", xyz="1 2 3", rpy=f"0 0 {math.pi / 2}") geometry = ET.SubElement(visual, "geometry") ET.SubElement(geometry, "mesh", filename=str(mesh_path), scale="2 3 4") expected = np.array([[[1, 4, 3], [-2, 2, 3], [1, 2, 7]]]) np.testing.assert_allclose(link_triangles(link), expected, atol=1e-12) def test_invalid_primitive_dimensions_and_origins_fail_audit(self): invalid = [box((1, 0, 1)), box((math.nan, 1, 1)), cylinder(-1, 2), cylinder(1, math.inf), box((1, 1, 1), rpy=(math.nan, 0, 0))] for collider in invalid: with self.subTest(collider=ET.tostring(collider)), self.assertRaises(RuntimeError): best_face_margins(self.faces, [collider]) class AxialSliceCoverageTest(unittest.TestCase): """Independent fixtures: no generation/partition/clipping helper is used.""" def slices(self, gap=0.0, padding=0.1): pieces = [cylinder(1.1, 1 + 2 * padding - gap, (0, 0, center)) for center in (-0.5, 0.5)] for index, piece in enumerate(pieces): piece.set("name", f"collision_test_ring_slice{index:02d}") return pieces def crossing_triangle(self): return np.array([[[-0.6, 0.0, -0.8], [0.6, 0.0, 0.8], [0.1, 0.4, 0.6]]]) def test_complete_crossing_triangle_is_proved_and_counted_once(self): faces, pieces = self.crossing_triangle(), self.slices() self.assertLess(best_face_margins(faces, pieces)[0], 0.1) margins, rescued = face_coverage_margins(faces, pieces, 0.1) self.assertEqual(rescued, 1) self.assertGreaterEqual(margins[0], 0.1 - 1e-12) # Choosing a CLI margin below fitting padding must keep the proof valid. for margin in (0.0, 0.05): with self.subTest(required_margin=margin): margins, rescued = face_coverage_margins(faces, pieces, margin) self.assertEqual(rescued, 1) self.assertGreaterEqual(margins[0], margin - 1e-12) def test_vertex_coverage_cannot_hide_a_real_seam_gap(self): faces = self.crossing_triangle() # The two physical cylinders have an actual 0.1 m gap. Every original # vertex nevertheless has 0.1 m clearance in some cylinder. pieces = self.slices(gap=0.3) vertex_faces = np.repeat(faces[0, :, None, :], 3, axis=1) self.assertTrue(np.all(best_face_margins(vertex_faces, pieces) >= 0.1)) margins, rescued = face_coverage_margins(faces, pieces, 0.1) self.assertEqual(rescued, 0) self.assertLess(margins[0], 0.1 - 1e-9) def test_padding_seam_and_sub_nanometer_tolerance_are_not_silent_gaps(self): faces = self.crossing_triangle() # A 6 nm missing band at the required inset must be rejected; only the # documented 1 nm point-to-surface comparison tolerance is allowed. for gap in (0.01, 6e-9): with self.subTest(gap=gap): margins, rescued = face_coverage_margins(faces, self.slices(gap=gap), 0.1) self.assertEqual(rescued, 0) self.assertLess(margins[0], 0.1 - 1e-9) def test_radial_hole_between_covered_original_vertices_is_rejected(self): pieces = self.slices() pieces[0].find("geometry/cylinder").set("radius", "0.35") faces = np.array([[[0.1, 0, -0.8], [0.9, 0, 0.8], [0.8, 0.1, 0.7]]]) vertex_faces = np.repeat(faces[0, :, None, :], 3, axis=1) self.assertTrue(np.all(best_face_margins(vertex_faces, pieces) >= 0.1 - 1e-12)) margins, rescued = face_coverage_margins(faces, pieces, 0.1) self.assertEqual(rescued, 0) self.assertLess(margins[0], 0.1 - 1e-9) def test_only_explicit_contiguous_same_group_cylinder_names_enable_union(self): invalid_names = [ ["ordinary_lower", "ordinary_upper"], ["collision_ring_slice0", "collision_ring_slice1"], ["collision_ring_slice00", "collision_ring_slice02"], ["collision_first_slice00", "collision_second_slice01"], ] for names in invalid_names: pieces = self.slices() for piece, name in zip(pieces, names): piece.set("name", name) with self.subTest(names=names): margins, rescued = face_coverage_margins(self.crossing_triangle(), pieces, 0.1) self.assertEqual(rescued, 0) np.testing.assert_array_equal(margins, best_face_margins(self.crossing_triangle(), pieces)) def test_rotated_translated_group_uses_each_actual_radial_center(self): faces, pieces = self.crossing_triangle(), self.slices() # Explicit Ry(pi/2): common cylinder axis is now world X. Give the # second slice a distinct radial center, larger than numerical noise. rotation = np.array([[0, 0, 1], [0, 1, 0], [-1, 0, 0]]) translation = np.array([3., -2., 4.]) world_faces = faces @ rotation.T + translation for index, (piece, axial_center) in enumerate(zip(pieces, (-0.5, 0.5))): center = rotation @ np.array([0.02 * index, 0., axial_center]) + translation piece.find("origin").set("xyz", " ".join(map(str, center))) piece.find("origin").set("rpy", f"0 {math.pi / 2} 0") margins, rescued = face_coverage_margins(world_faces, pieces, 0.1) self.assertEqual(rescued, 1) self.assertGreaterEqual(margins[0], 0.1 - 1e-12) def test_plain_whole_face_and_degenerate_line_keep_their_proofs(self): pieces = self.slices() whole_face = np.array([[[0, 0, -0.8], [0.2, 0, -0.6], [0, 0.1, -0.3]]]) margins, rescued = face_coverage_margins(whole_face, pieces, 0.1) self.assertEqual(rescued, 0) np.testing.assert_array_equal(margins, best_face_margins(whole_face, pieces)) line = np.array([[[0, 0, -0.8], [0, 0, 0.8], [0, 0, 0.8]]]) margins, rescued = face_coverage_margins(line, pieces, 0.1) self.assertEqual(rescued, 1) self.assertGreaterEqual(margins[0], 0.1 - 1e-12) margins, rescued = face_coverage_margins(line, self.slices(gap=0.3), 0.1) self.assertEqual(rescued, 0) self.assertLess(margins[0], 0.1 - 1e-9) if __name__ == "__main__": unittest.main()