1 | |
2 | #ifndef SPACEBOX_HPP_ |
3 | #define SPACEBOX_HPP_ |
4 | |
5 | #include "Shape/Point.hpp" |
6 | #include "Shape/Box.hpp" |
7 | #include <boost/fusion/include/vector.hpp> |
8 | #include "memory_ly/Encap.hpp" |
9 | #include "Ghost.hpp" |
10 | #include <stdlib.h> /* srand, rand */ |
11 | |
12 | /** \brief This class represent an N-dimensional box |
13 | * |
14 | * \param T type of space ... double float int size_t |
15 | * \param N dimensionality of the Box |
16 | * |
17 | * ### Definition of a spacebox and rescale |
18 | * \snippet SpaceBox_unit_tests.hpp Definition of a spacebox and rescale |
19 | * ### Definition of a spaceboxes and intersection between them |
20 | * \snippet SpaceBox_unit_tests.hpp Definition of a spacebox and intersection between them |
21 | * ### Create random points inside the SpaceBox |
22 | * \snippet SpaceBox_unit_tests.hpp Create random points inside the SpaceBox |
23 | * |
24 | */ |
25 | template<unsigned int dim, typename T> |
26 | class SpaceBox : public Box<dim,T> |
27 | { |
28 | public: |
29 | |
30 | /*! \brief Define the box from a box shape |
31 | * |
32 | * Define the box from a box shape |
33 | * |
34 | * \param b is the box |
35 | * \return itself |
36 | * |
37 | */ |
38 | |
39 | inline SpaceBox<dim,T> & operator=(const Box<dim,T> & b) |
40 | { |
41 | // for each dimension set high and low |
42 | |
43 | for (size_t d = 0 ; d < dim ; d++) |
44 | {this->setLow(d,b.getLow(d));} |
45 | |
46 | for (size_t d = 0 ; d < dim ; d++) |
47 | {this->setHigh(d,b.getHigh(d));} |
48 | |
49 | return *this; |
50 | } |
51 | |
52 | /*! \brief constructor from a Box of different type |
53 | * |
54 | * \param b box |
55 | * |
56 | */ |
57 | template <typename S>inline SpaceBox(const Box<dim,S> & b) |
58 | { |
59 | for (size_t d = 0 ; d < dim ; d++) |
60 | {this->setLow(d,b.getLow(d));} |
61 | |
62 | for (size_t d = 0 ; d < dim ; d++) |
63 | {this->setHigh(d,b.getHigh(d));} |
64 | } |
65 | |
66 | /*! \brief constructor from a SpaceBox |
67 | * |
68 | * constructor from a SpaceBox |
69 | * |
70 | * \param b is the SpaceBox |
71 | * |
72 | */ |
73 | SpaceBox(const SpaceBox<dim,T> & b) |
74 | :Box<dim,T>(b) |
75 | { |
76 | } |
77 | |
78 | /*! \brief constructor from a box |
79 | * |
80 | * constructor from a box |
81 | * |
82 | * \param b is the box |
83 | * |
84 | */ |
85 | |
86 | explicit SpaceBox(const Box<dim,T> & b) |
87 | :Box<dim,T>(b) |
88 | { |
89 | } |
90 | |
91 | /*! \brief Constructor from a Box |
92 | * |
93 | * \param box Box (Encapsulated) |
94 | * |
95 | */ |
96 | |
97 | template<unsigned int dim_s,typename Mem> SpaceBox(const encapc<dim_s,Box<dim,T>,Mem> & box) |
98 | { |
99 | // for each dimension set high and low |
100 | |
101 | for (size_t d = 0 ; d < dim ; d++) |
102 | {this->setLow(d,box.template get<Box<dim,T>::p1>()[d]);} |
103 | |
104 | for (size_t d = 0 ; d < dim ; d++) |
105 | {this->setHigh(d,box.template get<Box<dim,T>::p2>()[d]);} |
106 | } |
107 | |
108 | /*! \brief Constructor from a Box |
109 | * |
110 | * \param box box (Encapsulated) |
111 | * |
112 | */ |
113 | template<unsigned int dim_s,typename Mem> SpaceBox(const encapc<dim_s,SpaceBox<dim,T>,Mem> & box) |
114 | { |
115 | // for each dimension set high and low |
116 | |
117 | for (size_t d = 0 ; d < dim ; d++) |
118 | {this->setLow(d,box.template get<Box<dim,T>::p1>()[d]);} |
119 | |
120 | for (size_t d = 0 ; d < dim ; d++) |
121 | {this->setHigh(d,box.template get<Box<dim,T>::p2>()[d]);} |
122 | } |
123 | |
124 | /*! \brief Constructor from initializer list |
125 | * |
126 | * Constructor from initializer list |
127 | * |
128 | * \param p1 Low point, initialize as a list example {0.0,0.0,0.0} |
129 | * \param p2 High point, initialized as a list example {1.0,1.0,1.0} |
130 | * |
131 | */ |
132 | |
133 | SpaceBox(std::initializer_list<T> p1, std::initializer_list<T> p2) |
134 | { |
135 | // for each dimension set high and low |
136 | |
137 | size_t i = 0; |
138 | for(T x : p1) |
139 | {this->setLow(i,x);i++;} |
140 | |
141 | i = 0; |
142 | for(T x : p2) |
143 | {this->setHigh(i,x);i++;} |
144 | } |
145 | |
146 | /*! \brief Re-scale the space box with the coefficient defined in sp |
147 | * |
148 | * \param sp |
149 | * |
150 | */ |
151 | |
152 | void rescale(T (& sp)[dim]) |
153 | { |
154 | for (size_t d = 0 ; d < dim ; d++) |
155 | {this->setHigh(d,this->getLow(d) + (this->getHigh(d) -this->getLow(d)) * sp[d]);} |
156 | } |
157 | |
158 | /*! \brief Re-scale the space box with the coefficient defined in sp |
159 | * |
160 | * \param sp |
161 | * |
162 | */ |
163 | |
164 | template<typename S> void rescale(S (& sp)[dim]) |
165 | { |
166 | for (size_t d = 0 ; d < dim ; d++) |
167 | {this->setHigh(d,this->getLow(d) + (this->getHigh(d) -this->getLow(d)) * sp[d]);} |
168 | } |
169 | |
170 | /*! \brief multiply the space box with the coefficient defined in sp |
171 | * |
172 | * It rescale the domain where the space box live |
173 | * |
174 | * \param sp coefficents |
175 | * |
176 | */ |
177 | |
178 | void mul(T (& sp)[dim]) |
179 | { |
180 | for (size_t d = 0 ; d < dim ; d++) |
181 | {this->setLow(d,this->getLow(d) * sp[d]);} |
182 | |
183 | for (size_t d = 0 ; d < dim ; d++) |
184 | {this->setHigh(d,this->getHigh(d) * sp[d]);} |
185 | } |
186 | |
187 | /*! \brief multiply the space box with the coefficient defined in sp |
188 | * |
189 | * It rescale the domain where the space box live |
190 | * |
191 | * \param sp coefficents |
192 | * |
193 | */ |
194 | |
195 | template<typename S> void mul(S (& sp)[dim]) |
196 | { |
197 | for (size_t d = 0 ; d < dim ; d++) |
198 | {this->setLow(d,this->getLow(d) * sp[d]);} |
199 | |
200 | for (size_t d = 0 ; d < dim ; d++) |
201 | {this->setHigh(d,this->getHigh(d) * sp[d]);} |
202 | } |
203 | |
204 | /*! \brief Generate a random point inside the box |
205 | * |
206 | * \return a random point inside the box |
207 | * |
208 | */ |
209 | Point<dim,T> rnd() |
210 | { |
211 | Point<dim,T> p; |
212 | |
213 | for (size_t i = 0 ; i < dim ; i++) |
214 | p.get(i) = ((T)rand())/RAND_MAX * (this->getHigh(i) - this->getLow(i)) + this->getLow(i); |
215 | |
216 | return p; |
217 | } |
218 | |
219 | //! Default constructor |
220 | SpaceBox<dim,T>() {} |
221 | }; |
222 | |
223 | |
224 | ///// Unfortunately it seem that nvcc it specialize incorrectly this data structure so we have to specialize for the broken cases |
225 | |
226 | template<unsigned int dim, typename St> |
227 | struct is_typedef_and_data_same<true,SpaceBox<dim,St>> |
228 | { |
229 | enum |
230 | { |
231 | value = 1 |
232 | }; |
233 | }; |
234 | |
235 | |
236 | #endif |
237 | |