|
| 1 | +from LoopStructural.datatypes import BoundingBox |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +def test_create_bounding_box(): |
| 6 | + bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1]) |
| 7 | + assert bbox.origin[0] == 0 |
| 8 | + assert bbox.origin[1] == 0 |
| 9 | + assert bbox.origin[2] == 0 |
| 10 | + assert bbox.maximum[0] == 1 |
| 11 | + assert bbox.maximum[1] == 1 |
| 12 | + assert bbox.maximum[2] == 1 |
| 13 | + assert bbox.dimensions == 3 |
| 14 | + assert np.all(bbox.bb == np.array([[0, 0, 0], [1, 1, 1]])) |
| 15 | + assert bbox.valid is True |
| 16 | + |
| 17 | + |
| 18 | +def test_create_bounding_box_2d(): |
| 19 | + bbox = BoundingBox(origin=[0, 0], maximum=[1, 1], dimensions=2) |
| 20 | + assert bbox.origin[0] == 0 |
| 21 | + assert bbox.origin[1] == 0 |
| 22 | + assert bbox.maximum[0] == 1 |
| 23 | + assert bbox.maximum[1] == 1 |
| 24 | + assert bbox.dimensions == 2 |
| 25 | + assert np.all(bbox.bb == np.array([[0, 0], [1, 1]])) |
| 26 | + assert bbox.valid is True |
| 27 | + |
| 28 | + |
| 29 | +def test_create_bounding_box_from_points(): |
| 30 | + bbox = BoundingBox(dimensions=3) |
| 31 | + bbox.fit(np.array([[0, 0, 0], [1, 1, 1]])) |
| 32 | + assert bbox.origin[0] == 0 |
| 33 | + assert bbox.origin[1] == 0 |
| 34 | + assert bbox.origin[2] == 0 |
| 35 | + assert bbox.maximum[0] == 1 |
| 36 | + assert bbox.maximum[1] == 1 |
| 37 | + assert bbox.maximum[2] == 1 |
| 38 | + assert np.all(bbox.bb == np.array([[0, 0, 0], [1, 1, 1]])) |
| 39 | + assert bbox.valid is True |
| 40 | + |
| 41 | + |
| 42 | +def test_create_bounding_box_from_points_2d(): |
| 43 | + bbox = BoundingBox(dimensions=2) |
| 44 | + bbox.fit(np.array([[0, 0], [1, 1]])) |
| 45 | + assert bbox.origin[0] == 0 |
| 46 | + assert bbox.origin[1] == 0 |
| 47 | + assert bbox.maximum[0] == 1 |
| 48 | + assert bbox.maximum[1] == 1 |
| 49 | + assert np.all(bbox.bb == np.array([[0, 0], [1, 1]])) |
| 50 | + assert bbox.valid is True |
| 51 | + |
| 52 | + |
| 53 | +def test_create_3d_bounding_box_from_2d_points(): |
| 54 | + bbox = BoundingBox(dimensions=3) |
| 55 | + try: |
| 56 | + bbox.fit(np.array([[0, 0], [1, 1]])) |
| 57 | + except Exception as e: |
| 58 | + assert str(e) == "locations array is 2D but bounding box is 3" |
| 59 | + else: |
| 60 | + assert False |
| 61 | + |
| 62 | + |
| 63 | +def test_create_with_buffer(): |
| 64 | + bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1]) |
| 65 | + bbox = bbox.with_buffer(0.2) |
| 66 | + assert bbox.origin[0] == -0.2 |
| 67 | + assert bbox.origin[1] == -0.2 |
| 68 | + assert bbox.origin[2] == -0.2 |
| 69 | + assert bbox.maximum[0] == 1.2 |
| 70 | + assert bbox.maximum[1] == 1.2 |
| 71 | + assert bbox.maximum[2] == 1.2 |
| 72 | + assert np.all(bbox.bb == np.array([[-0.2, -0.2, -0.2], [1.2, 1.2, 1.2]])) |
| 73 | + assert bbox.valid is True |
| 74 | + |
| 75 | + |
| 76 | +def test_is_inside(): |
| 77 | + bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1]) |
| 78 | + assert np.all(bbox.is_inside(np.array([0.5, 0.5, 0.5]))) |
| 79 | + assert not np.all(bbox.is_inside(np.array([0.5, 0.5, 1.5]))) |
| 80 | + assert not np.all(bbox.is_inside(np.array([0.5, 0.5, -0.5]))) |
| 81 | + assert not np.all(bbox.is_inside(np.array([0.5, 1.5, 0.5]))) |
| 82 | + assert not np.all(bbox.is_inside(np.array([0.5, -0.5, 0.5]))) |
| 83 | + assert not np.all(bbox.is_inside(np.array([1.5, 0.5, 0.5]))) |
| 84 | + assert not np.all(bbox.is_inside(np.array([-0.5, 0.5, 0.5]))) |
| 85 | + |
| 86 | + |
| 87 | +def test_regular_grid_3d(): |
| 88 | + bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1]) |
| 89 | + print(bbox.dimensions) |
| 90 | + grid = bbox.regular_grid((10, 10, 10)) |
| 91 | + assert grid.shape == (10 * 10 * 10, 3) |
| 92 | + |
| 93 | + |
| 94 | +def test_regular_grid_2d(): |
| 95 | + bbox = BoundingBox(origin=[0, 0], maximum=[1, 1], dimensions=2) |
| 96 | + print(bbox.dimensions) |
| 97 | + grid = bbox.regular_grid((10, 10)) |
| 98 | + assert grid.shape == (10 * 10, 2) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + test_create_bounding_box() |
| 103 | + test_create_bounding_box_from_points() |
| 104 | + test_create_with_buffer() |
| 105 | + test_is_inside() |
| 106 | + test_regular_grid_3d() |
| 107 | + test_regular_grid_2d() |
0 commit comments