OpenFPM  5.2.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() const
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 
246  __device__ __host__ T norm1() const
247  {
248  T n = 0.0;
249 
250  for (size_t i = 0 ; i < dim ; i++)
251  n+=abs(get(i));
252 
253  return n;
254  }
255 
265  __device__ __host__ T distance(const Point<dim,T> & q) const
266  {
267  T tot = 0.0;
268 
269  for (size_t i = 0 ; i < dim ; i++)
270  tot += (this->get(i) - q.get(i)) * (this->get(i) - q.get(i));
271 
272  return sqrt(tot);
273  }
274 
284  T distance2(const Point<dim,T> & q) const
285  {
286  T tot = 0.0;
287 
288  for (size_t i = 0 ; i < dim ; i++)
289  tot += (this->get(i) - q.get(i)) * (this->get(i) - q.get(i));
290 
291  return tot;
292  }
293 
294 
299  __device__ __host__ inline void zero()
300  {
301  for (size_t i = 0 ; i < dim ; i++)
302  {
303  get(i) = 0;
304  }
305  }
306 
311  inline void one()
312  {
313  for (size_t i = 0 ; i < dim ; i++)
314  {
315  get(i) = 1;
316  }
317  }
318 
324  inline static Point<dim,T> zero_p()
325  {
326  Point<dim,T> p;
327 
328  for (size_t i = 0 ; i < dim ; i++)
329  {
330  p.get(i) = 0;
331  }
332 
333  return p;
334  }
335 
341  std::string toPointString() const
342  {
343  std::stringstream ps;
344 
345  for (size_t i = 0 ; i < dim ; i++)
346  ps << "x[" << i << "]=" << get(i) << " ";
347 
348  ps << "\n";
349 
350  return ps.str();
351  }
352 
358  void swap(Point<dim,T> & p)
359  {
360  for (size_t i = 0 ; i < dim ; i++)
361  {
362  T tmp = get(i);
363  get(i) = p.get(i);
364  p.get(i) = tmp;
365  }
366  }
367 
375  __device__ __host__ inline bool operator==(const Point<dim,T> & p) const
376  {
377  for (size_t i = 0 ; i < dim ; i++)
378  {
379  if (p.get(i) != get(i))
380  {return false;}
381  }
382 
383  return true;
384  }
385 
393  __device__ __host__ inline bool operator!=(const Point<dim,T> & p) const
394  {
395  return !this->operator==(p);
396  }
397 
403  std::string to_string() const
404  {
405  return toString();
406  }
407 
413  std::string toString() const
414  {
415  std::string str;
416 
417  for (size_t i = 0 ; i < dim - 1 ; i++)
418  {
419  // coverty[dead_error_line]
420  str += std::to_string(static_cast<double>(get(i))) + " ";
421  }
422  str += std::to_string(static_cast<double>(get(dim-1)));
423 
424  return str;
425  }
426 
434  __device__ __host__ T & value(size_t i)
435  {
436  return get(i);
437  }
438 
446  __device__ __host__ inline T value(size_t i) const
447  {
448  return get(i);
449  }
450 
456  T (&asArray())[dim]
457  {
458  return boost::fusion::at_c<x>(data);
459  }
460 
466  template<typename A> Point<dim,A> convertPoint() const
467  {
468  Point<dim,A> p;
469 
470  for (size_t i = 0; i < dim ; i++)
471  p.get(i) = static_cast<A>(get(i));
472 
473  return p;
474  }
475 
476 
478  static bool noPointers()
479  {
480  return true;
481  }
482 
486 
494  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)
495  {
496  p_exp.init();
497 
498  for (size_t i = 0; i < dim ; i++)
499  {get(i) = p_exp.value(i);}
500 
501  return *this;
502  }
503 
511  template<typename any>
512  __device__ __host__ Point<dim,T> & operator=(const point_expression<any> & p_exp)
513  {
514  p_exp.init();
515 
516  for (size_t i = 0; i < dim ; i++)
517  get(i) = p_exp.value(i);
518 
519  return *this;
520  }
521 
529  __device__ __host__ Point<dim,T> & operator=(const T (& p)[dim])
530  {
531  for (size_t i = 0; i < dim ; i++)
532  get(i) = p[i];
533 
534  return *this;
535  }
536 
546  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)
547  {
548  p_exp.init();
549 
550  for (size_t i = 0; i < dim ; i++)
551  get(i) = p_exp.value(i);
552 
553  return *this;
554  }
555 
563  template<typename aT> __device__ __host__ inline Point<dim,T> operator/(const aT (&ar)[dim])
564  {
565  Point<dim,T> result;
566 
567  for (size_t i = 0 ; i < dim ; i++)
568  result.get(i) = get(i) / ar[i];
569 
570  return result;
571  }
572 
580  template<typename aT> __device__ __host__ inline Point<dim,T> operator/=(const aT c)
581  {
582  for (size_t i = 0 ; i < dim ; i++)
583  get(i) = get(i) / c;
584 
585  return *this;
586  }
587 
588 
596  __device__ __host__ Point<dim,T> & operator=(T d)
597  {
598  for (size_t i = 0; i < dim ; i++)
599  get(i) = d;
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 
641  __device__ __host__ inline Point<dim,T> & operator+=(const Point<dim,T> & p)
642  {
643  for (size_t i = 0 ; i < dim ; i++)
644  {get(i) += p.get(i);}
645 
646  return *this;
647  }
648 
654  __device__ __host__ inline void init() const
655  {}
656 
659 
661  static const unsigned int max_prop = 1;
662  static const unsigned int max_prop_real = 1;
663 
665  static const unsigned int dims = dim;
666 
668  static const unsigned int nvals = dim;
669 };
670 
678 template <unsigned int N, typename T> std::string toPointString(const T (&p)[N] )
679 {
680  std::stringstream ps;
681 
682  for (size_t i = 0 ; i < N ; i++)
683  ps << "x[" << i << "]=" << p[i] << " ";
684 
685  ps << "\n";
686 
687  return ps.str();
688 }
689 
697 template <unsigned int N, typename T, typename Mem> std::string toPointString(const encapc<1,Point<N,T>,Mem> & p )
698 {
699  std::stringstream ps;
700 
701  for (size_t i = 0 ; i < N ; i++)
702  ps << "x[" << i << "]=" << p.template get<Point<N,T>::x>()[i] << " ";
703 
704  ps << "\n";
705 
706  return ps.str();
707 }
708 
709 
711 
712 template<unsigned int dim, typename T> using VectorS = Point<dim,T>;
713 
719 template<typename T, bool is_point = is_Point<T>::value>
720 struct rank_gen
721 {
722  typedef boost::mpl::int_<std::rank<T>::value> type;
723 };
724 
725 template<typename T>
726 struct rank_gen<T,true>
727 {
728  typedef boost::mpl::int_<1> type;
729 };
730 
731 template<unsigned int dim, typename T>
732 std::ostream& operator<<(std::ostream& os, const Point<dim, T>& p)
733 {
734  for (unsigned int i = 0; i < dim; i++)
735  {
736  os << p.get(i);
737  if(i < dim - 1)
738  os << " ";
739  }
740  return os;
741 }
742 template<typename T>
743 std::ostream& operator<<(std::ostream& os, const Point<1, T>& p)
744 {
745  os << p.get(0);
746  return os;
747 }
748 
749 #endif
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:28
static bool noPointers()
This structure has no internal pointers.
Definition: Point.hpp:478
__device__ __host__ T & value(size_t i)
Return the reference to the value at coordinate i.
Definition: Point.hpp:434
void one()
Set to one the point coordinate.
Definition: Point.hpp:311
int yes_is_point
Indicate that is a Point.
Definition: Point.hpp:41
T coord_type
coordinate type
Definition: Point.hpp:35
std::string toPointString() const
Convert the point into a string.
Definition: Point.hpp:341
__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:546
static const unsigned int x
Property id of the point.
Definition: Point.hpp:47
__device__ __host__ T & operator[](unsigned int i)
Get the component i.
Definition: Point.hpp:208
__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
int is_vtk_writable
Indicate that this object is vtk writable.
Definition: Point.hpp:32
__device__ __host__ bool operator==(const Point< dim, T > &p) const
Check if two points match.
Definition: Point.hpp:375
__device__ __host__ void zero()
Set to zero the point coordinate.
Definition: Point.hpp:299
__device__ __host__ T distance(const Point< dim, T > &q) const
It calculate the distance between 2 points.
Definition: Point.hpp:265
__device__ __host__ Point< dim, T > operator/=(const aT c)
divide each component by a constant
Definition: Point.hpp:580
boost::fusion::vector< T[dim]> type
boost fusion that store the point
Definition: Point.hpp:38
__device__ __host__ Point(const openfpm::detail::multi_array::sub_array_openfpm< T, 1, vmpl > &mar)
Point constructor from multi array.
Definition: Point.hpp:131
T get_vtk(size_t i) const
Get coordinate.
Definition: Point.hpp:184
static const unsigned int nvals
expose the dimension with a different name
Definition: Point.hpp:668
__device__ __host__ Point(const Point< dim, S > &p)
Point constructor.
Definition: Point.hpp:109
__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:494
__device__ __host__ Point(const T(&p)[dim])
Constructor from an array.
Definition: Point.hpp:88
Point(const encapc< d, Point< dim, T >, M > &p)
Point constructor.
Definition: Point.hpp:120
__device__ __host__ Point(std::initializer_list< T > p1)
Constructor from a list.
Definition: Point.hpp:155
__device__ __host__ const T & operator[](unsigned int i) const
Get the component i.
Definition: Point.hpp:221
static Point< dim, T > zero_p()
Create a point set to zero.
Definition: Point.hpp:324
__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__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
__device__ __host__ Point< dim, T > & operator=(const T(&p)[dim])
Fill the point with the value specified in the array.
Definition: Point.hpp:529
__device__ __host__ Point< dim, T > & operator=(const Point< dim, T > &p)
operator= between points
Definition: Point.hpp:611
__device__ __host__ bool operator!=(const Point< dim, T > &p) const
Check if two points match.
Definition: Point.hpp:393
__device__ __host__ Point< dim, T > & operator-=(const Point< dim, T > &p)
Subtract two points.
Definition: Point.hpp:626
__device__ __host__ Point< dim, T > & operator=(const point_expression< any > &p_exp)
Fill the vector property with the evaluated expression.
Definition: Point.hpp:512
__device__ __host__ Point()
Default contructor.
Definition: Point.hpp:163
T distance2(const Point< dim, T > &q) const
It calculate the square distance between 2 points.
Definition: Point.hpp:284
std::string to_string() const
Return the string with the point coordinate.
Definition: Point.hpp:403
__device__ __host__ Point(const Point< dim, T > &p)
Point constructor from point.
Definition: Point.hpp:77
static const unsigned int dims
expose the dimension
Definition: Point.hpp:665
__device__ __host__ T value(size_t i) const
Return the value at coordinate i.
Definition: Point.hpp:446
__device__ __host__ T norm1() const
L1 norm of the vector.
Definition: Point.hpp:246
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:413
__device__ __host__ Point(T d)
Constructor from scalar.
Definition: Point.hpp:99
__device__ __host__ Point< dim, T > & operator+=(const Point< dim, T > &p)
Sum two points.
Definition: Point.hpp:641
void swap(Point< dim, T > &p)
exchange the data of two points
Definition: Point.hpp:358
T(& asArray())[dim]
Return the coordinated of the point as reference array.
Definition: Point.hpp:456
__device__ __host__ Point(const Point< dim, T > &&p)
Point constructor from point.
Definition: Point.hpp:66
__device__ __host__ T norm() const
norm of the vector
Definition: Point.hpp:231
static const unsigned int max_prop
The point has one property.
Definition: Point.hpp:661
__device__ __host__ Point< dim, T > operator/(const aT(&ar)[dim])
divide each component by an array
Definition: Point.hpp:563
Point< dim, A > convertPoint() const
Definition: Point.hpp:466
type data
structure that store the data of the point
Definition: Point.hpp:44
__device__ __host__ void init() const
Do nothing stub operation.
Definition: Point.hpp:654
__device__ __host__ Point< dim, T > & operator=(T d)
Fill the vector property with some value.
Definition: Point.hpp:596
__device__ __host__ T & get(unsigned int i)
Get coordinate.
Definition: Point.hpp:195
Unknown operation specialization.
Main class that encapsulate a constant number used in a point expression.
__device__ __host__ void init() const
This function must be called before value.
__device__ __host__ const T & value(const int k) const
Evaluate the expression.
like std::rank but it also work for openfpm structures like Point where it return 1
Definition: Point.hpp:721