OpenFPM  5.2.0
Project that contain the implementation of distributed structures
Box_unit_tests.cpp
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/Shape/Box.hpp"
12 #include "Space/Ghost.hpp"
13 
14 #define N_RANDOM_POINT 1024
15 
16 BOOST_AUTO_TEST_SUITE( spacebox_test )
17 
18 BOOST_AUTO_TEST_CASE( spacebox_use)
19 {
20  std::cout << "Box unit test start" << "\n";
21 
23 
24  float spacing[2] = {0.1,0.1};
25 
26  {
27  Box<2,float> sp({1.0,1.0},{2.0,2.0});
28  sp.rescale(spacing);
29 
30  BOOST_REQUIRE_CLOSE(sp.getLow(0),1.0,0.0001);
31  BOOST_REQUIRE_CLOSE(sp.getLow(1),1.0,0.0001);
32  BOOST_REQUIRE_CLOSE(sp.getHigh(0),1.1,0.0001);
33  BOOST_REQUIRE_CLOSE(sp.getHigh(1),1.1,0.0001);
34  }
35 
37 
38  {
39  Box<2,float> sp({1.0,1.0},{2.0,2.0});
40  sp.mul(spacing);
41  sp.expand(spacing);
42 
43  BOOST_REQUIRE_CLOSE(sp.getLow(0),0.1,0.0001);
44  BOOST_REQUIRE_CLOSE(sp.getLow(1),0.1,0.0001);
45  BOOST_REQUIRE_CLOSE(sp.getHigh(0),0.3,0.0001);
46  BOOST_REQUIRE_CLOSE(sp.getHigh(1),0.3,0.0001);
47  }
48 
50 
51  {
52  Box<2,float> sp1({1.0,1.0},{2.0,2.0});
53  Box<2,float> sp2({0.5,0.5},{1.5,1.5});
54  Box<2,float> sp3;
55 
56  bool inte = sp1.Intersect(sp2,sp3);
57 
58  BOOST_REQUIRE_EQUAL(inte,true);
59  BOOST_REQUIRE_EQUAL(sp3.getLow(0),1.0);
60  BOOST_REQUIRE_EQUAL(sp3.getLow(1),1.0);
61  BOOST_REQUIRE_EQUAL(sp3.getHigh(0),1.5);
62  BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.5);
63 
65 
66  sp1.set({0.0,0.0},{1.0,1.0});
67  sp2.set({0.2,-0.5},{0.4,1.5});
68 
69  inte = sp1.Intersect(sp2,sp3);
70 
71  BOOST_REQUIRE_EQUAL(inte,true);
72  BOOST_REQUIRE_EQUAL(sp3.getLow(0),0.2f);
73  BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.0f);
74  BOOST_REQUIRE_EQUAL(sp3.getHigh(0),0.4f);
75  BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.0f);
76 
77  sp1.set({0.0,0.0},{1.0,1.0});
78  sp2.set({0.2,0.2},{0.4,0.4});
79 
80  inte = sp1.Intersect(sp2,sp3);
81 
82  BOOST_REQUIRE_EQUAL(inte,true);
83  BOOST_REQUIRE_EQUAL(sp3.getLow(0),0.2f);
84  BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.2f);
85  BOOST_REQUIRE_EQUAL(sp3.getHigh(0),0.4f);
86  BOOST_REQUIRE_EQUAL(sp3.getHigh(1),0.4f);
87 
88  sp1.set({0.0,0.0},{1.0,1.0});
89  sp2.set({1.2,0.0},{2.4,1.0});
90 
91  inte = sp1.Intersect(sp2,sp3);
92 
93  BOOST_REQUIRE_EQUAL(inte,false);
94 
95 
96  // Box line intersection
97 
98  sp1.set({0.0,0.0},{1.0,1.0});
99  sp2.set({0.5,0.1},{0.5,1.1});
100 
101  inte = sp1.Intersect(sp2,sp3);
102 
103  BOOST_REQUIRE_EQUAL(inte,true);
104  BOOST_REQUIRE_EQUAL(sp3.getLow(0),0.5f);
105  BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.1f);
106  BOOST_REQUIRE_EQUAL(sp3.getHigh(0),0.5f);
107  BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.0f);
108 
109  // Box Box with line result
110 
111  sp1.set({0.0,0.0},{1.0,1.0});
112  sp2.set({1.0,0.0},{1.5,1.5});
113 
114  inte = sp1.Intersect(sp2,sp3);
115 
116  BOOST_REQUIRE_EQUAL(inte,true);
117  BOOST_REQUIRE_EQUAL(sp3.getLow(0),1.0f);
118  BOOST_REQUIRE_EQUAL(sp3.getLow(1),0.0f);
119  BOOST_REQUIRE_EQUAL(sp3.getHigh(0),1.0f);
120  BOOST_REQUIRE_EQUAL(sp3.getHigh(1),1.0f);
121 
122 
123  sp1.set({0.0,0.0},{1.0,1.0});
124  Ghost<2,float> g(0.5);
125 
126  sp1.enlarge(g);
127 
128  BOOST_REQUIRE_EQUAL(sp1.getLow(0),-0.5f);
129  BOOST_REQUIRE_EQUAL(sp1.getLow(1),-0.5f);
130  BOOST_REQUIRE_EQUAL(sp1.getHigh(0),1.5f);
131  BOOST_REQUIRE_EQUAL(sp1.getHigh(1),1.5f);
132  }
133 
135 
136  Box<3,float> sp_box({0.0,0.0,0.0},{1.0,1.0,1.0});
137 
138  for (int i = 0 ; i < N_RANDOM_POINT ; i++)
139  {
140  Point<3,float> p = sp_box.rnd();
141 
142  BOOST_REQUIRE_EQUAL(sp_box.isInside(p),true);
143  }
144 
146 
147  // Create random points outside the space box and check
148 
149  Box<3,float> sp_box_out({1.1,1.1,1.1},{2.1,2.1,2.1});
150 
151  for (int i = 0 ; i < N_RANDOM_POINT ; i++)
152  {
153  Point<3,float> p = sp_box_out.rnd();
154 
155  BOOST_REQUIRE_EQUAL(sp_box.isInside(p),false);
156  }
157 
158  std::cout << "Box unit test stop" << "\n";
159 }
160 
161 
162 BOOST_AUTO_TEST_SUITE_END()
163 
164 
This class represent an N-dimensional box.
Definition: Box.hpp:60
void mul(T(&sp)[dim])
multiply box p1,p2 points with the coefficients defined in sp
Definition: Box.hpp:1490
__device__ __host__ T getLow(int i) const
get the i-coordinate of the low bound interval of the box
Definition: Box.hpp:555
__device__ __host__ bool Intersect(const Box< dim, T > &b, Box< dim, T > &b_out) const
Intersect.
Definition: Box.hpp:94
__device__ __host__ T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:566
void rescale(T(&sp)[dim])
Re-scale box with the coefficient defined in sp.
Definition: Box.hpp:1466
Definition: Ghost.hpp:40