OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
Point.hpp
1 #ifndef POINT_HPP
2 #define POINT_HPP
3 
4 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
5 #include <boost/fusion/include/at_c.hpp>
6 #include <boost/fusion/container/vector.hpp>
7 #include <boost/fusion/include/vector.hpp>
8 #include <boost/fusion/container/vector/vector_fwd.hpp>
9 #include <boost/fusion/include/vector_fwd.hpp>
10 #include "boost/multi_array.hpp"
11 #include "memory_ly/Encap.hpp"
12 #include "Point_operators.hpp"
13 
14 template<typename T>
16 {
17  typedef T type;
18 };
19 
27 template<unsigned int dim ,typename T> class Point
28 {
29  public:
30 
32  typedef int is_vtk_writable;
33 
35  typedef T coord_type;
36 
38  typedef boost::fusion::vector<T[dim]> type;
39 
41  typedef int yes_is_point;
42 
45 
47  static const unsigned int x = 0;
48 
49  typedef typename native_encapsulated_type<T[dim]>::type type_native;
50 
56  template<typename orig, typename exp1, typename exp2, unsigned int op> __device__ __host__ Point(const point_expression_op<orig,exp1,exp2,op> & p_exp)
57  {
58  this->operator=(p_exp);
59  }
60 
66  __device__ __host__ inline Point(const Point<dim,T> && p)
67  {
68  for(size_t i = 0; i < dim ; i++)
69  {get(i) = p.get(i);}
70  }
71 
77  __device__ __host__ inline Point(const Point<dim,T> & p)
78  {
79  for(size_t i = 0; i < dim ; i++)
80  {get(i) = p.get(i);}
81  }
82 
88  __device__ __host__ inline Point(const T (&p)[dim])
89  {
90  for(size_t i = 0; i < dim ; i++)
91  {get(i) = p[i];}
92  }
93 
99  __device__ __host__ inline Point(T d)
100  {
101  this->operator=(d);
102  }
103 
109  template <typename S> __device__ __host__ inline Point(const Point<dim,S> & p)
110  {
111  for (size_t i = 0 ; i < dim ; i++)
112  {get(i) = static_cast<S>(p.get(i));}
113  }
114 
120  template <unsigned int d, typename M> inline Point(const encapc<d,Point<dim,T>,M> & p)
121  {
122  for (size_t i = 0 ; i < dim ; i++)
123  get(i) = p.template get<0>()[i];
124  }
125 
131  template <typename vmpl> inline __device__ __host__ Point(const openfpm::detail::multi_array::sub_array_openfpm<T,1,vmpl> & mar)
132  {
133  for (unsigned int i = 0 ; i < dim ; i++)
134  {get(i) = mar[i];}
135  }
136 
142  template <typename vmpl> inline __device__ __host__ Point(const openfpm::detail::multi_array::const_sub_array_openfpm<T,1,vmpl> & mar)
143  {
144  for (unsigned int i = 0 ; i < dim ; i++)
145  {get(i) = mar[i];}
146  }
147 
155  __device__ __host__ inline Point(std::initializer_list<T> p1)
156  {
157  size_t i = 0;
158  for(T x : p1)
159  {get(i) = x;i++;}
160  }
161 
163  __device__ __host__ inline Point()
164  {}
165 
172  __device__ __host__ inline const T & get(unsigned int i) const
173  {
174  return boost::fusion::at_c<x>(data)[i];
175  }
176 
184  inline T get_vtk(size_t i) const
185  {
186  return boost::fusion::at_c<x>(data)[i];
187  }
188 
195  __device__ __host__ inline T& get(unsigned int i)
196  {
197  return boost::fusion::at_c<x>(data)[i];
198  }
199 
208  __device__ __host__ inline T& operator[](unsigned int i)
209  {
210  return get(i);
211  }
212 
221  __device__ __host__ inline const T& operator[](unsigned int i) const
222  {
223  return get(i);
224  }
225 
231  __device__ __host__ T norm()
232  {
233  T n = 0.0;
234 
235  for (size_t i = 0 ; i < dim ; i++)
236  n+=get(i) * get(i);
237 
238  return sqrt(n);
239  }
240 
250  __device__ __host__ T distance(const Point<dim,T> & q) const
251  {
252  T tot = 0.0;
253 
254  for (size_t i = 0 ; i < dim ; i++)
255  tot += (this->get(i) - q.get(i)) * (this->get(i) - q.get(i));
256 
257  return sqrt(tot);
258  }
259 
269  T distance2(const Point<dim,T> & q) const
270  {
271  T tot = 0.0;
272 
273  for (size_t i = 0 ; i < dim ; i++)
274  tot += (this->get(i) - q.get(i)) * (this->get(i) - q.get(i));
275 
276  return tot;
277  }
278 
279 
284  __device__ __host__ inline void zero()
285  {
286  for (size_t i = 0 ; i < dim ; i++)
287  {
288  get(i) = 0;
289  }
290  }
291 
296  inline void one()
297  {
298  for (size_t i = 0 ; i < dim ; i++)
299  {
300  get(i) = 1;
301  }
302  }
303 
309  inline static Point<dim,T> zero_p()
310  {
311  Point<dim,T> p;
312 
313  for (size_t i = 0 ; i < dim ; i++)
314  {
315  p.get(i) = 0;
316  }
317 
318  return p;
319  }
320 
326  std::string toPointString() const
327  {
328  std::stringstream ps;
329 
330  for (size_t i = 0 ; i < dim ; i++)
331  ps << "x[" << i << "]=" << get(i) << " ";
332 
333  ps << "\n";
334 
335  return ps.str();
336  }
337 
343  void swap(Point<dim,T> & p)
344  {
345  for (size_t i = 0 ; i < dim ; i++)
346  {
347  T tmp = get(i);
348  get(i) = p.get(i);
349  p.get(i) = tmp;
350  }
351  }
352 
360  __device__ __host__ inline bool operator==(const Point<dim,T> & p) const
361  {
362  for (size_t i = 0 ; i < dim ; i++)
363  {
364  if (p.get(i) != get(i))
365  {return false;}
366  }
367 
368  return true;
369  }
370 
378  __device__ __host__ inline bool operator!=(const Point<dim,T> & p) const
379  {
380  return !this->operator==(p);
381  }
382 
388  std::string to_string() const
389  {
390  return toString();
391  }
392 
398  std::string toString() const
399  {
400  std::string str;
401 
402  for (size_t i = 0 ; i < dim - 1 ; i++)
403  {
404  // coverty[dead_error_line]
405  str += std::to_string(static_cast<double>(get(i))) + " ";
406  }
407  str += std::to_string(static_cast<double>(get(dim-1)));
408 
409  return str;
410  }
411 
419  T & value(size_t i)
420  {
421  return get(i);
422  }
423 
431  __device__ __host__ inline T value(size_t i) const
432  {
433  return get(i);
434  }
435 
441  T (&asArray())[dim]
442  {
443  return boost::fusion::at_c<x>(data);
444  }
445 
451  template<typename A> Point<dim,A> convertPoint() const
452  {
453  Point<dim,A> p;
454 
455  for (size_t i = 0; i < dim ; i++)
456  p.get(i) = static_cast<A>(get(i));
457 
458  return p;
459  }
460 
461 
463  static bool noPointers()
464  {
465  return true;
466  }
467 
471 
479  template<typename orig, typename exp1, typename exp2, unsigned int op> __device__ __host__ Point<dim,T> & operator=(const point_expression_op<orig,exp1,exp2,op> & p_exp)
480  {
481  p_exp.init();
482 
483  for (size_t i = 0; i < dim ; i++)
484  {get(i) = p_exp.value(i);}
485 
486  return *this;
487  }
488 
496  template<typename any>
497  __device__ __host__ Point<dim,T> & operator=(const point_expression<any> & p_exp)
498  {
499  p_exp.init();
500 
501  for (size_t i = 0; i < dim ; i++)
502  get(i) = p_exp.value(i);
503 
504  return *this;
505  }
506 
514  __device__ __host__ Point<dim,T> & operator=(const T (& p)[dim])
515  {
516  for (size_t i = 0; i < dim ; i++)
517  get(i) = p[i];
518 
519  return *this;
520  }
521 
531  template<typename T1, typename check = typename std::enable_if<std::is_const<T1>::value == false>::type> __device__ __host__ Point<dim,T> & operator=(const point_expression<const T1[dim]> & p_exp)
532  {
533  p_exp.init();
534 
535  for (size_t i = 0; i < dim ; i++)
536  get(i) = p_exp.value(i);
537 
538  return *this;
539  }
540 
548  template<typename aT> __device__ __host__ inline Point<dim,T> operator/(const aT (&ar)[dim])
549  {
550  Point<dim,T> result;
551 
552  for (size_t i = 0 ; i < dim ; i++)
553  result.get(i) = get(i) / ar[i];
554 
555  return result;
556  }
557 
565  template<typename aT> __device__ __host__ inline Point<dim,T> operator/=(const aT c)
566  {
567  for (size_t i = 0 ; i < dim ; i++)
568  get(i) = get(i) / c;
569 
570  return *this;
571  }
572 
573 
581  __device__ __host__ Point<dim,T> & operator=(T d)
582  {
583  for (size_t i = 0; i < dim ; i++)
584  get(i) = d;
585 
586  return *this;
587  }
588 
596  __device__ __host__ inline Point<dim,T> & operator=(const Point<dim,T> & p)
597  {
598  for (size_t i = 0 ; i < dim ; i++)
599  get(i) = p.get(i);
600 
601  return *this;
602  }
603 
611  __device__ __host__ inline Point<dim,T> & operator-=(const Point<dim,T> & p)
612  {
613  for (size_t i = 0 ; i < dim ; i++)
614  get(i) -= p.get(i);
615 
616  return *this;
617  }
618 
626  __device__ __host__ inline Point<dim,T> & operator+=(const Point<dim,T> & p)
627  {
628  for (size_t i = 0 ; i < dim ; i++)
629  {get(i) += p.get(i);}
630 
631  return *this;
632  }
633 
639  __device__ __host__ inline void init() const
640  {}
641 
644 
646  static const unsigned int max_prop = 1;
647  static const unsigned int max_prop_real = 1;
648 
650  static const unsigned int dims = dim;
651 
653  static const unsigned int nvals = dim;
654 };
655 
663 template <unsigned int N, typename T> std::string toPointString(const T (&p)[N] )
664 {
665  std::stringstream ps;
666 
667  for (size_t i = 0 ; i < N ; i++)
668  ps << "x[" << i << "]=" << p[i] << " ";
669 
670  ps << "\n";
671 
672  return ps.str();
673 }
674 
682 template <unsigned int N, typename T, typename Mem> std::string toPointString(const encapc<1,Point<N,T>,Mem> & p )
683 {
684  std::stringstream ps;
685 
686  for (size_t i = 0 ; i < N ; i++)
687  ps << "x[" << i << "]=" << p.template get<Point<N,T>::x>()[i] << " ";
688 
689  ps << "\n";
690 
691  return ps.str();
692 }
693 
694 
696 
697 template<unsigned int dim, typename T> using VectorS = Point<dim,T>;
698 
704 template<typename T, bool is_point = is_Point<T>::value>
705 struct rank_gen
706 {
707  typedef boost::mpl::int_<std::rank<T>::value> type;
708 };
709 
710 template<typename T>
711 struct rank_gen<T,true>
712 {
713  typedef boost::mpl::int_<1> type;
714 };
715 
716 #endif
T distance2(const Point< dim, T > &q) const
It calculate the square distance between 2 points.
Definition: Point.hpp:269
__device__ __host__ T value(size_t i) const
Return the value at coordinate i.
Definition: Point.hpp:431
__device__ __host__ Point< dim, T > & operator+=(const Point< dim, T > &p)
Sum two points.
Definition: Point.hpp:626
__device__ __host__ Point(const Point< dim, T > &p)
Point constructor from point.
Definition: Point.hpp:77
void swap(Point< dim, T > &p)
exchange the data of two points
Definition: Point.hpp:343
__device__ __host__ Point(const T(&p)[dim])
Constructor from an array.
Definition: Point.hpp:88
static bool noPointers()
This structure has no internal pointers.
Definition: Point.hpp:463
__device__ __host__ Point(const openfpm::detail::multi_array::const_sub_array_openfpm< T, 1, vmpl > &mar)
Point constructor from multi array.
Definition: Point.hpp:142
__device__ __host__ Point(std::initializer_list< T > p1)
Constructor from a list.
Definition: Point.hpp:155
int is_vtk_writable
Indicate that this object is vtk writable.
Definition: Point.hpp:32
__device__ __host__ Point(const Point< dim, S > &p)
Point constructor.
Definition: Point.hpp:109
__device__ __host__ void init() const
Do nothing stub operation.
Definition: Point.hpp:639
std::string to_string() const
Return the string with the point coordinate.
Definition: Point.hpp:388
__device__ __host__ Point< dim, T > & operator=(const T(&p)[dim])
Fill the point with the value specified in the array.
Definition: Point.hpp:514
__device__ __host__ bool operator!=(const Point< dim, T > &p) const
Check if two points match.
Definition: Point.hpp:378
Unknown operation specialization.
static const unsigned int max_prop
The point has one property.
Definition: Point.hpp:646
__device__ __host__ Point< dim, T > & operator=(const point_expression< any > &p_exp)
Fill the vector property with the evaluated expression.
Definition: Point.hpp:497
__device__ __host__ Point(T d)
Constructor from scalar.
Definition: Point.hpp:99
__device__ __host__ Point()
Default contructor.
Definition: Point.hpp:163
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:27
Main class that encapsulate a constant number used in a point expression.
__device__ __host__ Point< dim, T > & operator=(const point_expression< const T1[dim]> &p_exp)
Fill the vector property with the evaluated expression.
Definition: Point.hpp:531
T & value(size_t i)
Return the reference to the value at coordinate i.
Definition: Point.hpp:419
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:398
static Point< dim, T > zero_p()
Create a point set to zero.
Definition: Point.hpp:309
__device__ __host__ T norm()
norm of the vector
Definition: Point.hpp:231
std::string toPointString() const
Convert the point into a string.
Definition: Point.hpp:326
T(& asArray())[dim]
Return the coordinated of the point as reference array.
Definition: Point.hpp:441
__device__ __host__ Point(const point_expression_op< orig, exp1, exp2, op > &p_exp)
Evaluate the expression and save the result on the point.
Definition: Point.hpp:56
T get_vtk(size_t i) const
Get coordinate.
Definition: Point.hpp:184
__device__ __host__ const T & value(const int k) const
Evaluate the expression.
type data
structure that store the data of the point
Definition: Point.hpp:44
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
__device__ __host__ Point< dim, T > & operator=(const point_expression_op< orig, exp1, exp2, op > &p_exp)
Fill the vector with the evaluated expression.
Definition: Point.hpp:479
void one()
Set to one the point coordinate.
Definition: Point.hpp:296
__device__ __host__ Point(const openfpm::detail::multi_array::sub_array_openfpm< T, 1, vmpl > &mar)
Point constructor from multi array.
Definition: Point.hpp:131
boost::fusion::vector< T[dim]> type
boost fusion that store the point
Definition: Point.hpp:38
__device__ __host__ Point< dim, T > operator/=(const aT c)
divide each component by a constant
Definition: Point.hpp:565
__device__ __host__ Point< dim, T > operator/(const aT(&ar)[dim])
divide each component by an array
Definition: Point.hpp:548
static const unsigned int dims
expose the dimension
Definition: Point.hpp:650
__device__ __host__ void zero()
Set to zero the point coordinate.
Definition: Point.hpp:284
static const unsigned int x
Property id of the point.
Definition: Point.hpp:47
Point< dim, A > convertPoint() const
Definition: Point.hpp:451
__device__ __host__ T & get(unsigned int i)
Get coordinate.
Definition: Point.hpp:195
__device__ __host__ Point< dim, T > & operator-=(const Point< dim, T > &p)
Subtract two points.
Definition: Point.hpp:611
__device__ __host__ Point(const Point< dim, T > &&p)
Point constructor from point.
Definition: Point.hpp:66
Point(const encapc< d, Point< dim, T >, M > &p)
Point constructor.
Definition: Point.hpp:120
static const unsigned int nvals
expose the dimension with a different name
Definition: Point.hpp:653
__device__ __host__ Point< dim, T > & operator=(const Point< dim, T > &p)
operator= between points
Definition: Point.hpp:596
__device__ __host__ T distance(const Point< dim, T > &q) const
It calculate the distance between 2 points.
Definition: Point.hpp:250
__device__ __host__ bool operator==(const Point< dim, T > &p) const
Check if two points match.
Definition: Point.hpp:360
__device__ __host__ Point< dim, T > & operator=(T d)
Fill the vector property with some value.
Definition: Point.hpp:581
int yes_is_point
Indicate that is a Point.
Definition: Point.hpp:41
__device__ __host__ void init() const
This function must be called before value.
like std::rank but it also work for openfpm structures like Point where it return 1
Definition: Point.hpp:705
__device__ __host__ const T & operator[](unsigned int i) const
Get the component i.
Definition: Point.hpp:221
__device__ __host__ T & operator[](unsigned int i)
Get the component i.
Definition: Point.hpp:208
T coord_type
coordinate type
Definition: Point.hpp:35