1 #include <msp/geometry/boundingbox.h>
2 #include <msp/strings/format.h>
3 #include <msp/test/test.h>
7 class BoundingBoxTests: public Test::RegisteredTest<BoundingBoxTests>
10 typedef Geometry::BoundingBox<double, 3> BoundingBox3;
11 typedef LinAl::Vector<double, 3> Vector3;
15 static const char *get_name() { return "BoundingBox"; }
19 void simple_intersection();
20 void union_with_empty();
21 void intersection_with_empty();
29 void operator<<(LexicalConverter &conv, const Vector<double, 3> &vec)
31 conv.result(format("[%g %g %g]", vec.x, vec.y, vec.z));
36 BoundingBoxTests::BoundingBoxTests()
38 add(&BoundingBoxTests::simple_union, "Union");
39 add(&BoundingBoxTests::simple_intersection, "Intersection");
40 add(&BoundingBoxTests::union_with_empty, "Union with empty");
41 add(&BoundingBoxTests::intersection_with_empty, "Intersection with empty");
42 add(&BoundingBoxTests::substraction, "Substraction");
43 add(&BoundingBoxTests::special_cases, "Special cases");
46 void BoundingBoxTests::simple_union()
48 BoundingBox3 bbox1(Vector3(-1, 0, -1), Vector3(1, 2, 1));
49 BoundingBox3 bbox2(Vector3(0, -1, 0), Vector3(2, 1, 2));
50 BoundingBox3 bboxu = bbox1|bbox2;
51 EXPECT_EQUAL(bboxu.get_minimum_point(), Vector3(-1, -1, -1));
52 EXPECT_EQUAL(bboxu.get_maximum_point(), Vector3(2, 2, 2));
55 void BoundingBoxTests::simple_intersection()
57 BoundingBox3 bbox1(Vector3(-1, 0, -1), Vector3(1, 2, 1));
58 BoundingBox3 bbox2(Vector3(0, -1, 0), Vector3(2, 1, 2));
59 BoundingBox3 bboxi = bbox1&bbox2;
60 EXPECT_EQUAL(bboxi.get_minimum_point(), Vector3(0, 0, 0));
61 EXPECT_EQUAL(bboxi.get_maximum_point(), Vector3(1, 1, 1));
64 void BoundingBoxTests::union_with_empty()
66 BoundingBox3 bbox(Vector3(-1, -1, -1), Vector3(1, 1, 1));
67 BoundingBox3 bboxu = bbox|BoundingBox3();
68 EXPECT_EQUAL(bboxu.get_minimum_point(), bbox.get_minimum_point());
69 EXPECT_EQUAL(bboxu.get_maximum_point(), bbox.get_maximum_point());
72 void BoundingBoxTests::intersection_with_empty()
74 BoundingBox3 bbox(Vector3(-1, -1, -1), Vector3(1, 1, 1));
75 BoundingBox3 bboxi = bbox&BoundingBox3();
76 EXPECT(bboxi.is_empty());
79 void BoundingBoxTests::substraction()
81 BoundingBox3 bbox1(Vector3(-1, -1, -1), Vector3(1, 1, 1));
82 BoundingBox3 bbox2(Vector3(0, -2, -2), Vector3(2, 2, 2));
83 BoundingBox3 bboxs = bbox1&~bbox2;
84 EXPECT_EQUAL(bboxs.get_minimum_point(), Vector3(-1, -1, -1));
85 EXPECT_EQUAL(bboxs.get_maximum_point(), Vector3(0, 1, 1));
87 BoundingBox3 bbox3(Vector3(0, 0, -2), Vector3(2, 2, 2));
89 EXPECT_EQUAL(bboxs.get_minimum_point(), bbox1.get_minimum_point());
90 EXPECT_EQUAL(bboxs.get_maximum_point(), bbox1.get_maximum_point());
92 BoundingBox3 bbox4(Vector3(-2, -2, -2), Vector3(2, 2, 2));
94 EXPECT(bboxs.is_empty());
96 bboxs = bbox1&~BoundingBox3();
97 EXPECT_EQUAL(bboxs.get_minimum_point(), bbox1.get_minimum_point());
98 EXPECT_EQUAL(bboxs.get_maximum_point(), bbox1.get_maximum_point());
101 void BoundingBoxTests::special_cases()
103 BoundingBox3 bbox = BoundingBox3()|BoundingBox3();
104 EXPECT(bbox.is_empty());
106 bbox = BoundingBox3()|~BoundingBox3();
107 EXPECT(bbox.is_space());
109 bbox = ~BoundingBox3()|BoundingBox3();
110 EXPECT(bbox.is_space());
112 bbox = ~BoundingBox3()|~BoundingBox3();
113 EXPECT(bbox.is_space());
115 bbox = BoundingBox3()&BoundingBox3();
116 EXPECT(bbox.is_empty());
118 bbox = BoundingBox3()&~BoundingBox3();
119 EXPECT(bbox.is_empty());
121 bbox = ~BoundingBox3()&BoundingBox3();
122 EXPECT(bbox.is_empty());
124 bbox = ~BoundingBox3()&~BoundingBox3();
125 EXPECT(bbox.is_space());