diff --git a/tools/blender/tests/test_hull_grid.py b/tools/blender/tests/test_hull_grid.py index 537adc031..d9794e4ec 100644 --- a/tools/blender/tests/test_hull_grid.py +++ b/tools/blender/tests/test_hull_grid.py @@ -21,6 +21,9 @@ def rect(x0, y0, x1, y1): return [((x0, y0), (x1, y0), (x1, y1)), ((x0, y0), (x1, y1), (x0, y1))] +CORNER = [((1.99, 0.98), (2.0, 0.98), (2.0, 1.0))] # holds the bounding box at x 2 without covering a sample + + def occupied(cells, height): return {(i // height, i % height) for i, cell in enumerate(cells) if cell} @@ -53,6 +56,27 @@ class CellMappingTests(unittest.TestCase): self.assertEqual(near(-1.0, -1.0), (1, 2)) +class InsideTests(unittest.TestCase): + TRIANGLE = ((0, 0), (1, 0), (0, 1)) + + def test_inside_points_are_inside_for_both_windings(self): + for triangle in (self.TRIANGLE, self.TRIANGLE[::-1]): + for point in ((0.25, 0.25), (0.5, 0.25), (0.1, 0.8)): + self.assertTrue(hull_grid._inside(triangle, *point), (triangle, point)) + + def test_points_across_each_edge_are_outside_for_both_windings(self): + outside = ((0.5, -0.25), (0.75, 0.75), (-0.25, 0.5), (-0.5, -0.5), (1.5, 0.0), (0.0, 1.5), (2.0, 2.0)) + for triangle in (self.TRIANGLE, self.TRIANGLE[::-1]): + for point in outside: + self.assertFalse(hull_grid._inside(triangle, *point), (triangle, point)) + + def test_edges_and_corners_are_inside_for_both_windings(self): + on_edge = ((0.5, 0.0), (0.0, 0.5), (0.5, 0.5), (0.0, 0.0), (1.0, 0.0), (0.0, 1.0)) + for triangle in (self.TRIANGLE, self.TRIANGLE[::-1]): + for point in on_edge: + self.assertTrue(hull_grid._inside(triangle, *point), (triangle, point)) + + class RasteriseTests(unittest.TestCase): def test_eight_of_sixteen_samples_occupy_a_cell_and_seven_do_not(self): wing = rect(-2, -1, 0, 1) # cell (1, 0) in full; fixes the grid at 2 x 1 cells about the origin @@ -63,6 +87,23 @@ class RasteriseTests(unittest.TestCase): self.assertEqual((width, height, origin), (2, 1, (1.0, 0.0))) self.assertEqual(cells, [expected, True]) + def test_samples_sit_at_quarter_points_of_the_cell_on_each_axis(self): + wing = rect(-2, -1, 0, 1) + # Cell (0, 0) spans x 0..2, y -1..1; its sample rows are y = 0.75, 0.25, -0.25, -0.75 and columns x = 1.75 .. 0.25. + rows = hull_grid.rasterise(wing + rect(0, -0.3, 2, 0.3))[2] # y = 0.25 and -0.25: 8 samples + columns = hull_grid.rasterise(wing + rect(0.3, -1, 1.0, 1) + CORNER)[2] # x = 0.75 only: 4 samples + self.assertEqual((rows, columns), ([True, True], [False, True])) + + def test_a_sample_on_a_triangle_bound_counts(self): + wing = rect(-2, -1, 0, 1) + self.assertEqual(hull_grid.rasterise(wing + rect(0, 0.25, 2, 0.75))[2], [True, True]) # rows 0.75 and 0.25 + self.assertEqual(hull_grid.rasterise(wing + rect(0.25, -1, 0.75, 1) + CORNER)[2], [True, True]) # columns 0.75 and 0.25 + + def test_a_single_triangle_covers_only_its_own_half_of_its_bounding_box(self): + width, height, cells, _ = hull_grid.rasterise([((-2, -2), (2, -2), (-2, 2))]) + self.assertEqual((width, height), (2, 2)) + self.assertEqual(cells, [False, True, True, True]) + def test_a_sample_inside_two_triangles_counts_once(self): seven = rect(0, -1, 2, -0.5) + rect(0.5, -0.5, 2, 0) _, _, cells, _ = hull_grid.rasterise(rect(-2, -1, 0, 1) + seven + seven) @@ -108,20 +149,21 @@ class RasteriseTests(unittest.TestCase): self.assertEqual((width, height, len(cells), all(cells)), (1, 32, 32, True)) with self.assertRaises(ValueError) as caught: hull_grid.rasterise(rect(0, 0, 2, 66)) - self.assertIn("33", str(caught.exception)) + self.assertEqual(str(caught.exception), "The hull needs 33 cells long; a ship grid holds at most 32") with self.assertRaises(ValueError) as caught: hull_grid.rasterise(rect(0, 0, 66, 2)) - self.assertIn("33", str(caught.exception)) + self.assertEqual(str(caught.exception), "The hull needs 33 cells wide; a ship grid holds at most 32") def test_a_silhouette_with_no_area_is_refused(self): - with self.assertRaises(ValueError): - hull_grid.rasterise([]) - with self.assertRaises(ValueError): - hull_grid.rasterise([((0, 0), (1, 1), (2, 2))]) + for triangles in ([], [((0, 0), (1, 1), (2, 2))]): + with self.assertRaises(ValueError) as caught: + hull_grid.rasterise(triangles) + self.assertEqual(str(caught.exception), "The hull has no triangle with area to rasterise") def test_a_degenerate_triangle_does_not_widen_the_grid(self): - _, _, cells, _ = hull_grid.rasterise(rect(0, 0, 2, 2) + [((50, 50), (60, 60), (70, 70))]) - self.assertEqual(cells, [True]) + for line in (((50, 50), (60, 60), (70, 70)), ((51, 50), (53, 51), (55, 52)), ((50, 51), (51, 53), (52, 55))): + _, _, cells, _ = hull_grid.rasterise(rect(0, 0, 2, 2) + [line]) + self.assertEqual(cells, [True], line) if __name__ == "__main__":