| 1 | /* |
| 2 | * SpaceBox_unit_tests.hpp |
| 3 | * |
| 4 | * Created on: May 8, 2015 |
| 5 | * Author: i-bird |
| 6 | */ |
| 7 | #define BOOST_TEST_DYN_LINK |
| 8 | #include <boost/test/unit_test.hpp> |
| 9 | |
| 10 | |
| 11 | #include "Space/SpaceBox.hpp" |
| 12 | |
| 13 | #define N_RANDOM_POINT 1024 |
| 14 | |
| 15 | BOOST_AUTO_TEST_SUITE( spacebox_test ) |
| 16 | |
| 17 | BOOST_AUTO_TEST_CASE( spacebox_use) |
| 18 | { |
| 19 | std::cout << "SpaceBox unit test start" << "\n" ; |
| 20 | |
| 21 | //! [Definition of a spacebox and rescale] |
| 22 | |
| 23 | float spacing[2] = {0.1,0.1}; |
| 24 | |
| 25 | { |
| 26 | SpaceBox<2,float> sp({1.0,1.0},{2.0,2.0}); |
| 27 | sp.rescale(spacing); |
| 28 | |
| 29 | BOOST_REQUIRE_CLOSE(sp.getLow(0),1.0,0.0001); |
| 30 | BOOST_REQUIRE_CLOSE(sp.getLow(1),1.0,0.0001); |
| 31 | BOOST_REQUIRE_CLOSE(sp.getHigh(0),1.1,0.0001); |
| 32 | BOOST_REQUIRE_CLOSE(sp.getHigh(1),1.1,0.0001); |
| 33 | } |
| 34 | |
| 35 | //! [Definition of a spacebox and rescale] |
| 36 | |
| 37 | { |
| 38 | SpaceBox<2,float> sp({1.0,1.0},{2.0,2.0}); |
| 39 | sp.mul(spacing); |
| 40 | sp.expand(spacing); |
| 41 | |
| 42 | BOOST_REQUIRE_CLOSE(sp.getLow(0),0.1,0.0001); |
| 43 | BOOST_REQUIRE_CLOSE(sp.getLow(1),0.1,0.0001); |
| 44 | BOOST_REQUIRE_CLOSE(sp.getHigh(0),0.3,0.0001); |
| 45 | BOOST_REQUIRE_CLOSE(sp.getHigh(1),0.3,0.0001); |
| 46 | } |
| 47 | |
| 48 | //! [Definition of a spacebox and intersection between them] |
| 49 | |
| 50 | { |
| 51 | SpaceBox<2,float> sp1({1.0,1.0},{2.0,2.0}); |
| 52 | SpaceBox<2,float> sp2({0.5,0.5},{1.5,1.5}); |
| 53 | SpaceBox<2,float> sp3; |
| 54 | |
| 55 | bool inte = sp1.Intersect(sp2,sp3); |
| 56 | |
| 57 | BOOST_REQUIRE_EQUAL(inte,true); |
| 58 | BOOST_REQUIRE_EQUAL(sp3.getLow(0),1.0); |
| 59 | BOOST_REQUIRE_EQUAL(sp3.getLow(1),1.0); |
| 60 | BOOST_REQUIRE_EQUAL(sp3.getHigh(0),1.5); |
| 61 | BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.5); |
| 62 | |
| 63 | //! [Definition of a spacebox and intersection between them] |
| 64 | |
| 65 | sp1.set({0.0,0.0},{1.0,1.0}); |
| 66 | sp2.set({0.2,-0.5},{0.4,1.5}); |
| 67 | |
| 68 | inte = sp1.Intersect(sp2,sp3); |
| 69 | |
| 70 | BOOST_REQUIRE_EQUAL(inte,true); |
| 71 | BOOST_REQUIRE_EQUAL(sp3.getLow(0),0.2f); |
| 72 | BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.0f); |
| 73 | BOOST_REQUIRE_EQUAL(sp3.getHigh(0),0.4f); |
| 74 | BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.0f); |
| 75 | |
| 76 | sp1.set({0.0,0.0},{1.0,1.0}); |
| 77 | sp2.set({0.2,0.2},{0.4,0.4}); |
| 78 | |
| 79 | inte = sp1.Intersect(sp2,sp3); |
| 80 | |
| 81 | BOOST_REQUIRE_EQUAL(inte,true); |
| 82 | BOOST_REQUIRE_EQUAL(sp3.getLow(0),0.2f); |
| 83 | BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.2f); |
| 84 | BOOST_REQUIRE_EQUAL(sp3.getHigh(0),0.4f); |
| 85 | BOOST_REQUIRE_EQUAL(sp3.getHigh(1),0.4f); |
| 86 | |
| 87 | sp1.set({0.0,0.0},{1.0,1.0}); |
| 88 | sp2.set({1.2,0.0},{2.4,1.0}); |
| 89 | |
| 90 | inte = sp1.Intersect(sp2,sp3); |
| 91 | |
| 92 | BOOST_REQUIRE_EQUAL(inte,false); |
| 93 | |
| 94 | |
| 95 | // Box line intersection |
| 96 | |
| 97 | sp1.set({0.0,0.0},{1.0,1.0}); |
| 98 | sp2.set({0.5,0.1},{0.5,1.1}); |
| 99 | |
| 100 | inte = sp1.Intersect(sp2,sp3); |
| 101 | |
| 102 | BOOST_REQUIRE_EQUAL(inte,true); |
| 103 | BOOST_REQUIRE_EQUAL(sp3.getLow(0),0.5f); |
| 104 | BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.1f); |
| 105 | BOOST_REQUIRE_EQUAL(sp3.getHigh(0),0.5f); |
| 106 | BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.0f); |
| 107 | |
| 108 | // Box Box with line result |
| 109 | |
| 110 | sp1.set({0.0,0.0},{1.0,1.0}); |
| 111 | sp2.set({1.0,0.0},{1.5,1.5}); |
| 112 | |
| 113 | inte = sp1.Intersect(sp2,sp3); |
| 114 | |
| 115 | BOOST_REQUIRE_EQUAL(inte,true); |
| 116 | BOOST_REQUIRE_EQUAL(sp3.getLow(0),1.0f); |
| 117 | BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.0f); |
| 118 | BOOST_REQUIRE_EQUAL(sp3.getHigh(0),1.0f); |
| 119 | BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.0f); |
| 120 | |
| 121 | |
| 122 | sp1.set({0.0,0.0},{1.0,1.0}); |
| 123 | Ghost<2,float> g(0.5); |
| 124 | |
| 125 | sp1.enlarge(g); |
| 126 | |
| 127 | BOOST_REQUIRE_EQUAL(sp1.getLow(0),-0.5f); |
| 128 | BOOST_REQUIRE_EQUAL(sp1.getLow(1),-0.5f); |
| 129 | BOOST_REQUIRE_EQUAL(sp1.getHigh(0),1.5f); |
| 130 | BOOST_REQUIRE_EQUAL(sp1.getHigh(1),1.5f); |
| 131 | } |
| 132 | |
| 133 | //! [Create random points inside the SpaceBox] |
| 134 | |
| 135 | SpaceBox<3,float> sp_box({0.0,0.0,0.0},{1.0,1.0,1.0}); |
| 136 | |
| 137 | for (int i = 0 ; i < N_RANDOM_POINT ; i++) |
| 138 | { |
| 139 | Point<3,float> p = sp_box.rnd(); |
| 140 | |
| 141 | BOOST_REQUIRE_EQUAL(sp_box.isInside(p),true); |
| 142 | } |
| 143 | |
| 144 | //! [Create random points inside the SpaceBox] |
| 145 | |
| 146 | // Create random points outside the space box and check |
| 147 | |
| 148 | SpaceBox<3,float> sp_box_out({1.1,1.1,1.1},{2.1,2.1,2.1}); |
| 149 | |
| 150 | for (int i = 0 ; i < N_RANDOM_POINT ; i++) |
| 151 | { |
| 152 | Point<3,float> p = sp_box_out.rnd(); |
| 153 | |
| 154 | BOOST_REQUIRE_EQUAL(sp_box.isInside(p),false); |
| 155 | } |
| 156 | |
| 157 | std::cout << "SpaceBox unit test stop" << "\n" ; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | BOOST_AUTO_TEST_SUITE_END() |
| 162 | |
| 163 | |
| 164 | |