]> git.tdb.fi Git - libs/math.git/blob - tests/boundingbox.cpp
Always explicitly construct values of type T
[libs/math.git] / tests / boundingbox.cpp
1 #include <msp/geometry/boundingbox.h>
2 #include <msp/strings/format.h>
3 #include <msp/test/test.h>
4
5 using namespace Msp;
6
7 class BoundingBoxTests: public Test::RegisteredTest<BoundingBoxTests>
8 {
9 private:
10         typedef Geometry::BoundingBox<double, 3> BoundingBox3;
11         typedef LinAl::Vector<double, 3> Vector3;
12 public:
13         BoundingBoxTests();
14
15         static const char *get_name() { return "BoundingBox"; }
16
17 private:
18         void simple_union();
19         void simple_intersection();
20         void union_with_empty();
21         void intersection_with_empty();
22         void substraction();
23         void special_cases();
24 };
25
26 namespace Msp {
27 namespace LinAl {
28
29 void operator<<(LexicalConverter &conv, const Vector<double, 3> &vec)
30 {
31         conv.result(format("[%g %g %g]", vec.x, vec.y, vec.z));
32 }
33
34 } }
35
36 BoundingBoxTests::BoundingBoxTests()
37 {
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");
44 }
45
46 void BoundingBoxTests::simple_union()
47 {
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));
53 }
54
55 void BoundingBoxTests::simple_intersection()
56 {
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));
62 }
63
64 void BoundingBoxTests::union_with_empty()
65 {
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());
70 }
71
72 void BoundingBoxTests::intersection_with_empty()
73 {
74         BoundingBox3 bbox(Vector3(-1, -1, -1), Vector3(1, 1, 1));
75         BoundingBox3 bboxi = bbox&BoundingBox3();
76         EXPECT(bboxi.is_empty());
77 }
78
79 void BoundingBoxTests::substraction()
80 {
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));
86
87         BoundingBox3 bbox3(Vector3(0, 0, -2), Vector3(2, 2, 2));
88         bboxs = bbox1&~bbox3;
89         EXPECT_EQUAL(bboxs.get_minimum_point(), bbox1.get_minimum_point());
90         EXPECT_EQUAL(bboxs.get_maximum_point(), bbox1.get_maximum_point());
91
92         BoundingBox3 bbox4(Vector3(-2, -2, -2), Vector3(2, 2, 2));
93         bboxs = bbox1&~bbox4;
94         EXPECT(bboxs.is_empty());
95
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());
99 }
100
101 void BoundingBoxTests::special_cases()
102 {
103         BoundingBox3 bbox = BoundingBox3()|BoundingBox3();
104         EXPECT(bbox.is_empty());
105
106         bbox = BoundingBox3()|~BoundingBox3();
107         EXPECT(bbox.is_space());
108
109         bbox = ~BoundingBox3()|BoundingBox3();
110         EXPECT(bbox.is_space());
111
112         bbox = ~BoundingBox3()|~BoundingBox3();
113         EXPECT(bbox.is_space());
114
115         bbox = BoundingBox3()&BoundingBox3();
116         EXPECT(bbox.is_empty());
117
118         bbox = BoundingBox3()&~BoundingBox3();
119         EXPECT(bbox.is_empty());
120
121         bbox = ~BoundingBox3()&BoundingBox3();
122         EXPECT(bbox.is_empty());
123
124         bbox = ~BoundingBox3()&~BoundingBox3();
125         EXPECT(bbox.is_space());
126 }