OpenFPM  5.2.0
Project that contain the implementation of distributed structures
Box.hpp
1 
2 #ifndef BOX_HPP_
3 #define BOX_HPP_
4 
5 
6 #include "Space/Shape/Sphere.hpp"
7 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
8 #include "Grid/grid_key.hpp"
9 #include "memory_ly/Encap.hpp"
10 #include <sstream>
11 
12 #define PERIODIC 1
13 #define NON_PERIODIC 0
14 
20 enum Base
21 {
22  UP,
23  DOWN
24 };
25 
58 template<unsigned int dim , typename T>
59 class Box
60 {
61 public:
62 
64  typedef boost::fusion::vector<T[dim],T[dim]> type;
66  typedef T btype;
67 
69  typedef int yes_is_box;
70 
73 
75  static const unsigned int p1 = 0;
77  static const unsigned int p2 = 1;
79  static const unsigned int max_prop = 2;
80 
82  static const unsigned int dims = dim;
83 
94  __device__ __host__ bool Intersect(const Box<dim,T> & b, Box<dim,T> & b_out) const
95  {
96  // check if p1 of b is smaller than
97 
98  for (size_t i = 0 ; i < dim ; i++)
99  {
100  if (getLow(i) <= b.getLow(i))
101  b_out.setLow(i,b.getLow(i));
102  else if (getLow(i) <= b.getHigh(i))
103  b_out.setLow(i,getLow(i));
104  else
105  return false;
106 
107  if (getHigh(i) >= b.getHigh(i))
108  b_out.setHigh(i,b.getHigh(i));
109  else if (getHigh(i) >= b.getLow(i))
110  b_out.setHigh(i,getHigh(i));
111  else
112  return false;
113  }
114  return true;
115  }
116 
117 
118 
129  template<typename Mem>
130  __device__ __host__
131  bool Intersect(const encapc<1,Box<dim,T>,Mem> & e_b, Box<dim,T> & b_out) const
132  {
133  return Intersect(Box<dim,T>(e_b),b_out);
134  }
135 
163  template <typename distance> bool Intersect(Sphere<dim,T> & sphere)
164  {
165  // distance functor
166  distance dist;
167 
168  // Get the nearest point of the box from the center of the sphere
169  typename distance::ResultType distance_r = 0;
170 
171  for (size_t i = 0 ; i < dim ; i++)
172  {
173 
174  // if the center of the sphere on dimension i is not in the i box interval
175  // do not accumulate, otherwise accumulate from the nearest point on that
176  // dimension
177  if (boost::fusion::at_c<p1>(data)[i] < sphere.center(i))
178  {
179  // accumulate the distance from p1
180  distance_r += dist.accum_dist(sphere.center(i),boost::fusion::at_c<p1>(data)[i],i);
181  }
182  else if ( boost::fusion::at_c<p2>(data)[i] <= sphere.center(i))
183  {
184  // accumulate the distance from p2
185  distance_r += dist.accum_dist(sphere.center(i),boost::fusion::at_c<p2>(data)[i],i);
186  }
187  }
188 
189  // return if there is intersection
190  return distance_r < sphere.radius();
191  }
192 
193  /* \brief return the minimum distance between two boxes
194  *
195  * Consider two box in space like in the figure below. Given two points P and Q in the two boxes 1 and 2.
196  * The minimum distance, is the distance that satify min( dist(P,Q) ). There P and Q is the standard euclidean distance
197  *
198  * \verbatim b box 2
199  *
200  * \verbatim
201 
202 
203 
204  _____ p2
205  | |
206  | | Box 2
207  |p1__|
208  /
209  /
210  / <----------- min distance
211  /
212  _____/
213  | p2|
214  | | Box 1
215  |____|
216  p1
217 
218  \endverbatim
219  *
220  *
221  */
222  T min_distance(Box<dim,T> & b) const
223  {
224  Point<dim,T> dist;
225 
226  for (unsigned int i = 0 ; i < dim ; i++)
227  {
228  if (getHigh(i) <= b.getLow(i))
229  {dist.get(i) = b.getLow(i) - getHigh(i);}
230  else if (b.getHigh(i) <= getLow(i))
231  {dist.get(i) = getLow(i) - b.getHigh(i);}
232  else
233  {
234  T d1 = fabs(getHigh(i) - b.getLow(i) );
235  T d2 = fabs(getLow(i) - b.getLow(i));
236 
237  if (d1 <= d2)
238  {
239  T d3 = fabs(getHigh(i) - b.getHigh(i));
240  dist.get(i) = (d3 < d1)?d3:d1;
241  }
242  else
243  {
244  T d3 = fabs(getHigh(i) - b.getHigh(i));
245  dist.get(i) = (d3 < d1)?d3:d2;
246  }
247  }
248  }
249 
250  return dist.norm();
251  }
252 
261  template<unsigned int b> T getBase(const unsigned int i) const
262  {
263  return boost::fusion::at_c<b>(data)[i];
264  }
265 
272  Box<dim-1,T> getSubBox()
273  {
274  return Box<dim-1,T>(data);
275  }
276 
286  __device__ __host__ Box<dim,T> & operator=(const Box<dim,T> & box)
287  {
288  for(size_t i = 0 ; i < dim ; i++)
289  {
290  setLow(i,box.getLow(i));
291  setHigh(i,box.getHigh(i));
292  }
293 
294  // return itself
295  return *this;
296  }
297 
298  public:
299 
301  __device__ __host__ Box()
302  {}
303 
310  Box(const Point<dim,T> & p1, const Point<dim,T> & p2)
311  {
312  setP1(p1);
313  setP2(p2);
314  }
315 
322  Box(std::initializer_list<T> p1, std::initializer_list<T> p2)
323  {
324  set(p1,p2);
325  }
326 
333  inline Box(T * low, T * high)
334  {
335  // copy all the data
336 
337  for (int i = 0 ; i < dim ; i++)
338  {
339  // p1 is low p2 is high
340 
341  boost::fusion::at_c<Box::p1>(data)[i] = low[i];
342  boost::fusion::at_c<Box::p2>(data)[i] = high[i];
343  }
344  }
345 
351  __device__ __host__ inline Box(const Box<dim,T> & box)
352  {
353  // we copy the data
354 
355  for (size_t i = 0 ; i < dim ; i++)
356  {
357  boost::fusion::at_c<p1>(data)[i] = boost::fusion::at_c<p1>(box.data)[i];
358  boost::fusion::at_c<p2>(data)[i] = boost::fusion::at_c<p2>(box.data)[i];
359  }
360  }
361 
367  explicit inline Box(type box_data)
368  {
369  // we copy the data
370 
371  for (size_t i = 0 ; i < dim ; i++)
372  {
373  boost::fusion::at_c<p1>(data)[i] = boost::fusion::at_c<p1>(box_data)[i];
374  boost::fusion::at_c<p2>(data)[i] = boost::fusion::at_c<p2>(box_data)[i];
375  }
376  }
377 
383  inline Box(T (& box_data)[dim])
384  {
385  // we copy the data
386 
387  for (size_t i = 0 ; i < dim ; i++)
388  {
389  boost::fusion::at_c<p1>(data)[i] = 0;
390  boost::fusion::at_c<p2>(data)[i] = box_data[i];
391  }
392  }
393 
400  inline Box(const grid_key_dx<dim> & key1, const grid_key_dx<dim> & key2)
401  {
402  for (size_t i = 0 ; i < dim ; i++)
403  {
404  setLow(i,key1.get(i));
405  setHigh(i,key2.get(i));
406  }
407  }
408 
414  template<unsigned int dimS>
415  __device__ __host__ inline Box(boost::fusion::vector<T[dimS],T[dimS]> & box_data)
416  {
417  // we copy the data
418 
419  for (size_t i = 0 ; i < dim ; i++)
420  {
421  boost::fusion::at_c<p1>(data)[i] = boost::fusion::at_c<p1>(box_data)[i];
422  boost::fusion::at_c<p2>(data)[i] = boost::fusion::at_c<p2>(box_data)[i];
423  }
424  }
425 
431  template<typename Mem>
432  __device__ __host__
433  inline Box(const encapc<1,Box<dim,T>,Mem> & b)
434  {
435  // we copy the data
436 
437  for (size_t i = 0 ; i < dim ; i++)
438  {
439  boost::fusion::at_c<p1>(data)[i] = b.template get<p1>()[i];
440  boost::fusion::at_c<p2>(data)[i] = b.template get<p2>()[i];
441  }
442  }
443 
449  template <typename S>
450  __device__ __host__
451  inline Box(const Box<dim,S> & b)
452  {
453  for (size_t i = 0 ; i < dim ; i++)
454  {
455  setLow(i,b.getLow(i));
456  setHigh(i,b.getHigh(i));
457  }
458  }
459 
467  inline Box<dim,T> & operator/=(const Point<dim,T> & p)
468  {
469  for (size_t i = 0 ; i < dim ; i++)
470  {
471  setLow(i, getLow(i)/p.get(i));
472  setHigh(i, getHigh(i)/p.get(i));
473  }
474  return *this;
475  }
476 
485  {
486  Box<dim,T> ret;
487 
488  for (size_t i = 0 ; i < dim ; i++)
489  {
490  ret.setLow(i, getLow(i)*p.get(i));
491  ret.setHigh(i, getHigh(i)*p.get(i));
492  }
493  return ret;
494  }
495 
505  inline void set(std::initializer_list<T> p1, std::initializer_list<T> p2)
506  {
507  size_t i = 0;
508  for(T x : p1)
509  {
510  setLow(i,x);
511  i++;
512  if (i >= dim)
513  break;
514  }
515 
516  i = 0;
517  for(T x : p2)
518  {
519  setHigh(i,x);
520  i++;
521  if (i >= dim)
522  break;
523  }
524  }
525 
532  __device__ __host__ inline void setLow(int i, T val)
533  {
534  boost::fusion::at_c<p1>(data)[i] = val;
535  }
536 
543  __device__ __host__ inline void setHigh(int i, T val)
544  {
545  boost::fusion::at_c<p2>(data)[i] = val;
546  }
547 
555  __device__ __host__ inline T getLow(int i) const
556  {
557  return boost::fusion::at_c<p1>(data)[i];
558  }
559 
566  __device__ __host__ inline T getHigh(int i) const
567  {
568  return boost::fusion::at_c<p2>(data)[i];
569  }
570 
576  inline void setP1(const grid_key_dx<dim> & p1)
577  {
578  for (size_t i = 0 ; i < dim ; i++)
579  setLow(i,p1.get(i));
580  }
581 
587  inline void setP2(const grid_key_dx<dim> & p2)
588  {
589  for (size_t i = 0 ; i < dim ; i++)
590  setHigh(i,p2.get(i));
591  }
592 
598  inline void setP1(const Point<dim,T> & p1)
599  {
600  for (size_t i = 0 ; i < dim ; i++)
601  setLow(i,p1.get(i));
602  }
603 
609  inline void setP2(const Point<dim,T> & p2)
610  {
611  for (size_t i = 0 ; i < dim ; i++)
612  setHigh(i,p2.get(i));
613  }
614 
623  {
624  return *this;
625  }
626 
634  const Box<dim,T> & getBox() const
635  {
636  return *this;
637  }
638 
646  {
647  return data;
648  }
649 
656  {
657  // grid key to return
658  grid_key_dx<dim> ret(boost::fusion::at_c<p1>(data));
659 
660  return ret;
661  }
662 
669  {
670  // grid key to return
671  grid_key_dx<dim> ret(boost::fusion::at_c<p2>(data));
672 
673  return ret;
674  }
675 
682  {
683  // grid key to return
684  grid_key_dx<dim,int> ret(boost::fusion::at_c<p1>(data));
685 
686  return ret;
687  }
688 
695  {
696  // grid key to return
697  grid_key_dx<dim,int> ret(boost::fusion::at_c<p2>(data));
698 
699  return ret;
700  }
701 
707  inline Point<dim,T> getP1() const
708  {
709  // grid key to return
710  Point<dim,T> ret(boost::fusion::at_c<p1>(data));
711 
712  return ret;
713  }
714 
721  inline Point<dim,T> getP2() const
722  {
723  // grid key to return
724  Point<dim,T> ret(boost::fusion::at_c<p2>(data));
725 
726  return ret;
727  }
728 
736  inline Box<dim,T> & operator-=(const Point<dim,T> & p)
737  {
738  for (size_t i = 0 ; i < dim ; i++)
739  {
740  boost::fusion::at_c<p2>(data)[i] -= p.get(i);
741  boost::fusion::at_c<p1>(data)[i] -= p.get(i);
742  }
743 
744  return *this;
745  }
746 
754  inline Box<dim,T> & operator+=(const Point<dim,T> & p)
755  {
756  for (size_t i = 0 ; i < dim ; i++)
757  {
758  boost::fusion::at_c<p2>(data)[i] += p.get(i);
759  boost::fusion::at_c<p1>(data)[i] += p.get(i);
760  }
761 
762  return *this;
763  }
764 
772  inline Box<dim,T> operator+(const Point<dim,T> & p) const
773  {
774  Box<dim,T> b;
775 
776  for (size_t i = 0 ; i < dim ; i++)
777  {
778  b.setHigh(i,boost::fusion::at_c<p2>(data)[i] + p.get(i));
779  b.setLow(i,boost::fusion::at_c<p1>(data)[i] + p.get(i));
780  }
781 
782  return b;
783  }
784 
792  inline void expand(T (& exp)[dim])
793  {
794  for (size_t i = 0 ; i < dim ; i++)
795  {
796  boost::fusion::at_c<p2>(data)[i] = boost::fusion::at_c<p2>(data)[i] + exp[i];
797  }
798  }
799 
822  void enlarge(const Box<dim,T> & gh)
823  {
824  typedef ::Box<dim,T> g;
825 
826  for (size_t j = 0 ; j < dim ; j++)
827  {
828  setLow(j,this->template getBase<g::p1>(j) + gh.template getBase<g::p1>(j));
829  setHigh(j,this->template getBase<g::p2>(j) + gh.template getBase<g::p2>(j));
830  }
831  }
832 
855  template<typename S> inline void enlarge_fix_P1(Box<dim,S> & gh)
856  {
857  typedef ::Box<dim,T> g;
858 
859  for (size_t j = 0 ; j < dim ; j++)
860  {
861  setHigh(j,this->template getBase<g::p2>(j) + gh.template getBase<g::p2>(j) - gh.template getBase<g::p1>(j));
862  }
863  }
864 
870  void invalidate()
871  {
872  for (size_t j = 0 ; j < dim ; j++)
873  {
874  setLow(j,this->getHigh(j)+1);
875  }
876  }
877 
878 
888  void magnify(T mg)
889  {
890  typedef ::Box<dim,T> g;
891 
892  for (size_t j = 0 ; j < dim ; j++)
893  {
894  setLow(j,mg * this->template getBase<g::p1>(j));
895  setHigh(j,mg * this->template getBase<g::p2>(j));
896  }
897  }
898 
906  inline void magnify_fix_P1(T mg)
907  {
908  typedef ::Box<dim,T> g;
909 
910  for (size_t j = 0 ; j < dim ; j++)
911  {
912  setHigh(j,this->template getBase<g::p1>(j) + mg * (this->template getBase<g::p2>(j) - this->template getBase<g::p1>(j)));
913  }
914  }
915 
921  inline void shrinkP2(T sh)
922  {
923  for (size_t j = 0 ; j < dim ; j++)
924  {
925  setHigh(j,this->getHigh(j) - sh);
926  }
927  }
928 
934  inline void enlargeP2(T sh)
935  {
936  for (size_t j = 0 ; j < dim ; j++)
937  {
938  setHigh(j,this->getHigh(j) + sh);
939  }
940  }
941 
947  inline void enclose(const Box<dim,T> & en)
948  {
949  for (size_t j = 0 ; j < dim ; j++)
950  {
951  if (getLow(j) > en.getLow(j))
952  setLow(j,en.getLow(j));
953 
954  if (getHigh(j) < en.getHigh(j))
955  setHigh(j,en.getHigh(j));
956  }
957  }
958 
967  inline void contained(const Box<dim,T> & en, const bool reset_p1 = true)
968  {
969  for (size_t j = 0 ; j < dim ; j++)
970  {
971  if (getHigh(j) > (en.getHigh(j) - en.getLow(j)))
972  setHigh(j,en.getHigh(j) - en.getLow(j));
973 
974  if (reset_p1 == true)
975  setLow(j,0);
976  }
977  }
978 
982  inline void zero()
983  {
984  for (size_t j = 0 ; j < dim ; j++)
985  {
986  setHigh(j,0);
987  setLow(j,0);
988  }
989  }
990 
998  inline bool isContained(const Box<dim,T> & b) const
999  {
1000  bool isc = true;
1001 
1002  isc &= isInside(b.getP1());
1003  isc &= isInside(b.getP2());
1004 
1005  return isc;
1006  }
1007 
1015  inline
1016  __host__ __device__ bool isInside(const Point<dim,T> & p) const
1017  {
1018  // check if bound
1019 
1020  for (size_t i = 0 ; i < dim ; i++)
1021  {
1022  // if outside the region return false
1023  if ( boost::fusion::at_c<Point<dim,T>::x>(p.data)[i] < boost::fusion::at_c<Box<dim,T>::p1>(this->data)[i]
1024  || boost::fusion::at_c<Point<dim,T>::x>(p.data)[i] > boost::fusion::at_c<Box<dim,T>::p2>(this->data)[i])
1025  {
1026  // Out of bound
1027 
1028  return false;
1029  }
1030  }
1031 
1032  // In bound
1033 
1034  return true;
1035  }
1036 
1045  __device__ __host__ inline bool isInsideNP(const Point<dim,T> & p) const
1046  {
1047  // check if bound
1048 
1049  for (size_t i = 0 ; i < dim ; i++)
1050  {
1051  // if outside the region return false
1052  if ( boost::fusion::at_c<Point<dim,T>::x>(p.data)[i] < boost::fusion::at_c<Box<dim,T>::p1>(this->data)[i]
1053  || boost::fusion::at_c<Point<dim,T>::x>(p.data)[i] >= boost::fusion::at_c<Box<dim,T>::p2>(this->data)[i])
1054  {
1055  // Out of bound
1056 
1057 
1058 
1059  return false;
1060  }
1061  }
1062 
1063  // In bound
1064 
1065  return true;
1066  }
1067 
1076  template<typename bc_type>
1077  __device__ __host__ inline bool isInsideNP_with_border(const Point<dim,T> & p, const Box<dim,T> & border, const bc_type (& bc)[dim]) const
1078  {
1079  // check if bound
1080 
1081  for (size_t i = 0 ; i < dim ; i++)
1082  {
1083  // if outside the region return false
1084  if ( p.get(i) < this->getLow(i)
1085  || (bc[i] == NON_PERIODIC && ((this->getHigh(i) != border.getHigh(i) && p.get(i) >= this->getHigh(i)) || (this->getHigh(i) == border.getHigh(i) && p.get(i) > this->getHigh(i)) ) )
1086  || (bc[i] == PERIODIC && p.get(i) >= this->getHigh(i)))
1087  {
1088  // Out of bound
1089  return false;
1090  }
1091  }
1092 
1093  // In bound
1094 
1095  return true;
1096  }
1097 
1104  inline bool isInsideNB(const Point<dim,T> & p) const
1105  {
1106  // check if bound
1107 
1108  for (size_t i = 0 ; i < dim ; i++)
1109  {
1110  // if outside the region return false
1111  if ( boost::fusion::at_c<Point<dim,T>::x>(p.data)[i] <= boost::fusion::at_c<Box<dim,T>::p1>(this->data)[i]
1112  || boost::fusion::at_c<Point<dim,T>::x>(p.data)[i] >= boost::fusion::at_c<Box<dim,T>::p2>(this->data)[i])
1113  {
1114  // Out of bound
1115 
1116  return false;
1117  }
1118  }
1119 
1120  // In bound
1121 
1122  return true;
1123  }
1124 
1132  inline bool isInside(const T (&p)[dim]) const
1133  {
1134  // check if bound
1135 
1136  for (size_t i = 0 ; i < dim ; i++)
1137  {
1138  // if outside the region return false
1139  if ( p[i] < boost::fusion::at_c<Box<dim,T>::p1>(this->data)[i]
1140  || p[i] > boost::fusion::at_c<Box<dim,T>::p2>(this->data)[i])
1141  {
1142  // Out of bound
1143 
1144  return false;
1145  }
1146  }
1147 
1148  // In bound
1149 
1150  return true;
1151  }
1152 
1159  template<typename KeyType>
1160  __device__ __host__ inline bool isInsideKey(const KeyType & k) const
1161  {
1162  // check if bound
1163 
1164  for (size_t i = 0 ; i < dim ; i++)
1165  {
1166  // if outside the region return false
1167  if ( k.get(i) < boost::fusion::at_c<Box<dim,T>::p1>(this->data)[i]
1168  || k.get(i) > boost::fusion::at_c<Box<dim,T>::p2>(this->data)[i])
1169  {
1170  // Out of bound
1171 
1172  return false;
1173  }
1174  }
1175 
1176  // In bound
1177 
1178  return true;
1179  }
1180 
1186  inline bool isValid() const
1187  {
1188  for (size_t i = 0 ; i < dim ; i++)
1189  {
1190  if (getLow(i) > getHigh(i))
1191  return false;
1192  }
1193 
1194  return true;
1195  }
1196 
1202  inline bool isValidN() const
1203  {
1204  for (size_t i = 0 ; i < dim ; i++)
1205  {
1206  if (getLow(i) >= getHigh(i))
1207  return false;
1208  }
1209 
1210  return true;
1211  }
1212 
1217  inline void floorP1()
1218  {
1219  for (size_t i = 0 ; i < dim ; i++)
1220  {
1221  setLow(i,std::floor(getLow(i)));
1222  }
1223  }
1224 
1229  inline void floorP2()
1230  {
1231  for (size_t i = 0 ; i < dim ; i++)
1232  {
1233  setHigh(i,std::floor(getHigh(i)));
1234  }
1235  }
1236 
1241  inline void ceilP1()
1242  {
1243  for (size_t i = 0 ; i < dim ; i++)
1244  {
1245  setLow(i,std::ceil(getLow(i)));
1246  }
1247  }
1248 
1253  inline void ceilP2()
1254  {
1255  for (size_t i = 0 ; i < dim ; i++)
1256  {
1257  setHigh(i,std::ceil(getHigh(i)));
1258  }
1259  }
1260 
1266  inline void shrinkP2(const Point<dim,T> & p)
1267  {
1268  for (size_t i = 0 ; i < dim ; i++)
1269  setHigh(i,getHigh(i) - p.get(i));
1270  }
1271 
1277  void swap(Box<dim,T> & b)
1278  {
1279  for (size_t i = 0 ; i < dim ; i++)
1280  {
1281  T tmp_l = getLow(i);
1282  T tmp_h = getHigh(i);
1283 
1284  setLow(i,b.getLow(i));
1285  setHigh(i,b.getHigh(i));
1286 
1287  b.setLow(i,tmp_l);
1288  b.setHigh(i,tmp_h);
1289  }
1290  }
1291 
1299  template <typename Mem>
1300  inline bool isInside(const encapc<1,Point<dim,T>,Mem> & p)
1301  {
1302  // check if bound
1303 
1304  for (size_t i = 0 ; i < dim ; i++)
1305  {
1306  // if outside the region return false
1307  if ( p.template get<Point<dim,T>::x>()[i] < boost::fusion::at_c<Box<dim,T>::p1>(this->data)[i]
1308  || p.template get<Point<dim,T>::x>()[i] > boost::fusion::at_c<Box<dim,T>::p2>(this->data)[i])
1309  {
1310  // Out of bound
1311 
1312  return false;
1313  }
1314  }
1315 
1316  // In bound
1317 
1318  return true;
1319  }
1320 
1325  inline T getRcut() const
1326  {
1327  T r_cut = 0;
1328  for (size_t i = 0 ; i < dim ; i++)
1329  r_cut = std::max(r_cut, getHigh(i));
1330 
1331  return r_cut;
1332  }
1333 
1339  inline T getVolume() const
1340  {
1341  T vol = 1.0;
1342 
1343  for (size_t i = 0 ; i < dim ; i++)
1344  vol *= (getHigh(i) - getLow(i));
1345 
1346  return vol;
1347  }
1348 
1354  inline T getVolumeKey() const
1355  {
1356  T vol = 1.0;
1357 
1358  for (size_t i = 0 ; i < dim ; i++)
1359  vol *= (getHigh(i) - getLow(i) + 1.0);
1360 
1361  return vol;
1362  }
1363 
1376  inline static T getVolumeKey(const T (&p1)[dim], const T(&p2)[dim])
1377  {
1378  T vol = 1.0;
1379 
1380  for (size_t i = 0 ; i < dim ; i++)
1381  vol *= (p2[i] - p1[i] + 1.0);
1382 
1383  return vol;
1384  }
1385 
1387  static bool noPointers()
1388  {
1389  return true;
1390  }
1391 
1397  inline Point<dim,T> middle() const
1398  {
1399  Point<dim,T> p;
1400 
1401  for (size_t i = 0 ; i < dim ; i++)
1402  p.get(i) = (getLow(i) + getHigh(i))/2;
1403 
1404  return p;
1405  }
1406 
1412  std::string toString() const
1413  {
1414  std::stringstream str;
1415 
1416  for (size_t i = 0 ; i < dim ; i++)
1417  str << "x[" << i << "]=" << getLow(i) << " ";
1418 
1419  str << " | ";
1420 
1421  for (size_t i = 0 ; i < dim ; i++)
1422  str << "x[" << i << "]=" << getHigh(i) << " ";
1423 
1424  return str.str();
1425  }
1426 
1434  bool operator==(const Box<dim,T> & b) const
1435  {
1436  for (size_t i = 0 ; i < dim ; i++)
1437  {
1438  if (getLow(i) != b.getLow(i))
1439  return false;
1440 
1441  if (getHigh(i) != b.getHigh(i))
1442  return false;
1443  }
1444 
1445  return true;
1446  }
1447 
1455  bool operator!=(const Box<dim,T> & b) const
1456  {
1457  return ! this->operator==(b);
1458  }
1459 
1466  void rescale(T (& sp)[dim])
1467  {
1468  for (size_t d = 0 ; d < dim ; d++)
1469  setHigh(d,getLow(d) + (getHigh(d) -getLow(d)) * sp[d]);
1470  }
1471 
1478  template<typename S> void rescale(S (& sp)[dim])
1479  {
1480  for (size_t d = 0 ; d < dim ; d++)
1481  setHigh(d,getLow(d) + (getHigh(d) -getLow(d)) * sp[d]);
1482  }
1483 
1490  void mul(T (& sp)[dim])
1491  {
1492  for (size_t d = 0 ; d < dim ; d++)
1493  {
1494  setLow(d,getLow(d) * sp[d]);
1495  setHigh(d,getHigh(d) * sp[d]);
1496  }
1497  }
1498 
1505  template<typename S> void mul(S (& sp)[dim])
1506  {
1507  for (size_t d = 0 ; d < dim ; d++)
1508  {
1509  setLow(d,getLow(d) * sp[d]);
1510  setHigh(d,getHigh(d) * sp[d]);
1511  }
1512  }
1513 
1520  {
1521  Point<dim,T> p;
1522 
1523  for (size_t i = 0 ; i < dim ; i++)
1524  p.get(i) = ((T)rand())/(T)RAND_MAX * (getHigh(i) - getLow(i)) + getLow(i);
1525 
1526  return p;
1527  }
1528 };
1529 
1530 #endif
Header file containing functions for creating a filled 3D sphere of defined radius.
This class represent an N-dimensional box.
Definition: Box.hpp:60
void enlarge_fix_P1(Box< dim, S > &gh)
Enlarge the box with ghost margin keeping fix the point P1.
Definition: Box.hpp:855
bool isInside(const encapc< 1, Point< dim, T >, Mem > &p)
Check if the point is inside the region.
Definition: Box.hpp:1300
boost::fusion::vector< T[dim], T[dim]> type
boost fusion that store the point
Definition: Box.hpp:64
void expand(T(&exp)[dim])
expand the box by a vector
Definition: Box.hpp:792
Point< dim, T > getP1() const
Get the point p1.
Definition: Box.hpp:707
Box(const grid_key_dx< dim > &key1, const grid_key_dx< dim > &key2)
constructor from 2 grid_key_dx
Definition: Box.hpp:400
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__ Box(const Box< dim, T > &box)
Box constructor from a box.
Definition: Box.hpp:351
static const unsigned int p1
Low point.
Definition: Box.hpp:75
void setP2(const Point< dim, T > &p2)
Set the point P2 of the box.
Definition: Box.hpp:609
__device__ __host__ bool Intersect(const Box< dim, T > &b, Box< dim, T > &b_out) const
Intersect.
Definition: Box.hpp:94
std::string toString() const
Produce a string from the object.
Definition: Box.hpp:1412
void ceilP1()
Apply the ceil operation to the point P1.
Definition: Box.hpp:1241
void mul(S(&sp)[dim])
multiply box p1,p2 points with the coefficients defined in sp
Definition: Box.hpp:1505
Box(type box_data)
Box constructor from vector::fusion.
Definition: Box.hpp:367
void zero()
Set p1 and p2 to 0.
Definition: Box.hpp:982
Box(std::initializer_list< T > p1, std::initializer_list< T > p2)
Constructor from initializer list.
Definition: Box.hpp:322
Box(T *low, T *high)
Box constructor from a box.
Definition: Box.hpp:333
__device__ __host__ T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:566
__host__ __device__ bool isInside(const Point< dim, T > &p) const
Check if the point is inside the box.
Definition: Box.hpp:1016
Box(const Point< dim, T > &p1, const Point< dim, T > &p2)
Constructor from two points.
Definition: Box.hpp:310
void setP2(const grid_key_dx< dim > &p2)
Set the point P2 of the box.
Definition: Box.hpp:587
Box< dim, T > & operator/=(const Point< dim, T > &p)
Divide component wise each box points with a point.
Definition: Box.hpp:467
void ceilP2()
Apply the ceil operation to the point P2.
Definition: Box.hpp:1253
grid_key_dx< dim > getKP1() const
Get the point p1 as grid_key_dx.
Definition: Box.hpp:655
void shrinkP2(T sh)
Shrink moving p2 of sh quantity (on each direction)
Definition: Box.hpp:921
void setP1(const Point< dim, T > &p1)
Set the point P1 of the box.
Definition: Box.hpp:598
__device__ __host__ Box< dim, T > & operator=(const Box< dim, T > &box)
Operator= between boxes.
Definition: Box.hpp:286
void magnify_fix_P1(T mg)
Magnify the box by a factor keeping fix the point P1.
Definition: Box.hpp:906
Box< dim, T > & operator-=(const Point< dim, T > &p)
Translate the box.
Definition: Box.hpp:736
void shrinkP2(const Point< dim, T > &p)
Shrink the point P2 by one vector.
Definition: Box.hpp:1266
T getBase(const unsigned int i) const
Get the coordinate of the bounding point.
Definition: Box.hpp:261
static T getVolumeKey(const T(&p1)[dim], const T(&p2)[dim])
Get the volume spanned by the Box as grid_key_dx_iterator_sub.
Definition: Box.hpp:1376
Point< dim, T > getP2() const
Get the point p2.
Definition: Box.hpp:721
__device__ __host__ Box(boost::fusion::vector< T[dimS], T[dimS]> &box_data)
Box constructor from vector::fusion of higher dimension.
Definition: Box.hpp:415
void rescale(S(&sp)[dim])
Re-scale box with the coefficient defined in sp.
Definition: Box.hpp:1478
void invalidate()
Invalidate the box.
Definition: Box.hpp:870
T btype
type of the box
Definition: Box.hpp:66
void set(std::initializer_list< T > p1, std::initializer_list< T > p2)
Constructor from initializer list.
Definition: Box.hpp:505
int yes_is_box
Indicate that this is a box.
Definition: Box.hpp:69
__device__ __host__ bool isInsideNP(const Point< dim, T > &p) const
Check if the point is inside the region excluding the positive part.
Definition: Box.hpp:1045
__device__ __host__ Box()
default constructor
Definition: Box.hpp:301
grid_key_dx< dim, int > getKP1int() const
Get the point p1 as grid_key_dx.
Definition: Box.hpp:681
void enlarge(const Box< dim, T > &gh)
Enlarge the box with ghost margin.
Definition: Box.hpp:822
grid_key_dx< dim > getKP2() const
Get the point p12 as grid_key_dx.
Definition: Box.hpp:668
grid_key_dx< dim, int > getKP2int() const
Get the point p12 as grid_key_dx.
Definition: Box.hpp:694
static const unsigned int dims
dimensionality of the box
Definition: Box.hpp:82
bool isValid() const
Check if the Box is a valid box P2 >= P1.
Definition: Box.hpp:1186
T getVolumeKey() const
Get the volume spanned by the Box P1 and P2 interpreted as grid key.
Definition: Box.hpp:1354
T getRcut() const
Get the worst extension.
Definition: Box.hpp:1325
void magnify(T mg)
Magnify the box.
Definition: Box.hpp:888
void contained(const Box< dim, T > &en, const bool reset_p1=true)
Refine the box to be contained in the given box and itself.
Definition: Box.hpp:967
type data
It store the two point bounding the box.
Definition: Box.hpp:72
__device__ __host__ Box(const encapc< 1, Box< dim, T >, Mem > &b)
Box constructor from encapsulated box.
Definition: Box.hpp:433
bool isInside(const T(&p)[dim]) const
Check if the point is inside the region (Border included)
Definition: Box.hpp:1132
Box< dim, T > & operator+=(const Point< dim, T > &p)
Translate the box.
Definition: Box.hpp:754
void floorP2()
Apply the ceil operation to the point P2.
Definition: Box.hpp:1229
__device__ __host__ void setHigh(int i, T val)
set the high interval of the box
Definition: Box.hpp:543
Box< dim, T > operator+(const Point< dim, T > &p) const
Translate the box.
Definition: Box.hpp:772
static const unsigned int p2
High point.
Definition: Box.hpp:77
void enclose(const Box< dim, T > &en)
Refine the box to enclose the given box and itself.
Definition: Box.hpp:947
__device__ __host__ bool Intersect(const encapc< 1, Box< dim, T >, Mem > &e_b, Box< dim, T > &b_out) const
Intersect.
Definition: Box.hpp:131
Box< dim-1, T > getSubBox()
Get the the box of dimensionality dim-1 (it eliminate the last dimension)
Definition: Box.hpp:272
Box< dim, T > operator*(const Point< dim, T > &p)
Multiply component wise each box points with a point.
Definition: Box.hpp:484
bool operator==(const Box< dim, T > &b) const
Compare two boxes.
Definition: Box.hpp:1434
void setP1(const grid_key_dx< dim > &p1)
Set the point P1 of the box.
Definition: Box.hpp:576
Point< dim, T > middle() const
Return the middle point of the box.
Definition: Box.hpp:1397
__device__ __host__ bool isInsideKey(const KeyType &k) const
Check if the point is inside the region (Border included)
Definition: Box.hpp:1160
type & getVector()
Get the internal boost::fusion::vector that store the data.
Definition: Box.hpp:645
__device__ __host__ bool isInsideNP_with_border(const Point< dim, T > &p, const Box< dim, T > &border, const bc_type(&bc)[dim]) const
Check if the point is inside the region excluding the positive part.
Definition: Box.hpp:1077
bool isInsideNB(const Point< dim, T > &p) const
Check if the point is inside the region excluding the borders.
Definition: Box.hpp:1104
__device__ __host__ void setLow(int i, T val)
set the low interval of the box
Definition: Box.hpp:532
Box(T(&box_data)[dim])
Box constructor from an array reference.
Definition: Box.hpp:383
bool operator!=(const Box< dim, T > &b) const
Compare two boxes.
Definition: Box.hpp:1455
const Box< dim, T > & getBox() const
Get the box enclosing this Box.
Definition: Box.hpp:634
Point< dim, T > rnd()
Generates a random point inside the box.
Definition: Box.hpp:1519
void enlargeP2(T sh)
Shrink moving p2 of sh quantity (on each direction)
Definition: Box.hpp:934
void floorP1()
Apply the ceil operation to the point P1.
Definition: Box.hpp:1217
__device__ __host__ Box(const Box< dim, S > &b)
constructor from a Box of different type
Definition: Box.hpp:451
bool Intersect(Sphere< dim, T > &sphere)
Check if the sphere intersect the box.
Definition: Box.hpp:163
bool isContained(const Box< dim, T > &b) const
Check if the box is contained.
Definition: Box.hpp:998
T getVolume() const
Get the volume of the box.
Definition: Box.hpp:1339
void swap(Box< dim, T > &b)
exchange the data of two boxes
Definition: Box.hpp:1277
static bool noPointers()
This structure has no internal pointers.
Definition: Box.hpp:1387
static const unsigned int max_prop
Maximum number of properties.
Definition: Box.hpp:79
bool isValidN() const
Check if the Box is a valid box P2 > P1.
Definition: Box.hpp:1202
Box< dim, T > & getBox()
Get the box enclosing this Box.
Definition: Box.hpp:622
void rescale(T(&sp)[dim])
Re-scale box with the coefficient defined in sp.
Definition: Box.hpp:1466
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:28
This class implement the Sphere concept in an N-dimensional space.
Definition: Sphere.hpp:24
__device__ __host__ T center(unsigned int i)
Get the component i of the center.
Definition: Sphere.hpp:44
__device__ __host__ T radius() const
Get the radius of the sphere.
Definition: Sphere.hpp:115
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition: grid_key.hpp:503