OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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 "Grid/Encap.hpp"
12 #include "Point_operators.hpp"
13 
14 
22 template<unsigned int dim ,typename T> class Point
23 {
24  public:
25 
27  typedef int is_vtk_writable;
28 
30  typedef T coord_type;
31 
33  typedef boost::fusion::vector<T[dim]> type;
34 
37 
39  static const unsigned int x = 0;
40 
41 
47  template<typename orig, typename exp1, typename exp2, unsigned int op> Point(const point_expression_op<orig,exp1,exp2,op> & p_exp)
48  {
49  this->operator=(p_exp);
50  }
51 
57  inline Point(const Point<dim,T> && p)
58  {
59  for(size_t i = 0; i < dim ; i++)
60  {get(i) = p.get(i);}
61  }
62 
68  inline Point(const Point<dim,T> & p)
69  {
70  for(size_t i = 0; i < dim ; i++)
71  {get(i) = p.get(i);}
72  }
73 
79  inline Point(const T (&p)[dim])
80  {
81  for(size_t i = 0; i < dim ; i++)
82  {get(i) = p[i];}
83  }
84 
90  inline Point(T d)
91  {
92  this->operator=(d);
93  }
94 
100  template <typename S> inline Point(const Point<dim,S> & p)
101  {
102  for (size_t i = 0 ; i < dim ; i++)
103  get(i) = static_cast<S>(p.get(i));
104  }
105 
111  template <unsigned int d, typename M> inline Point(const encapc<d,Point<dim,T>,M> & p)
112  {
113  for (size_t i = 0 ; i < dim ; i++)
114  get(i) = p.template get<0>()[i];
115  }
116 
124  inline Point(std::initializer_list<T> p1)
125  {
126  size_t i = 0;
127  for(T x : p1)
128  {get(i) = x;i++;}
129  }
130 
132  inline Point()
133  {}
134 
142  inline const T & get(size_t i) const
143  {
144  return boost::fusion::at_c<x>(data)[i];
145  }
146 
154  inline T get_vtk(size_t i) const
155  {
156  return boost::fusion::at_c<x>(data)[i];
157  }
158 
166  inline T& get(size_t i)
167  {
168  return boost::fusion::at_c<x>(data)[i];
169  }
170 
179  inline T& operator[](size_t i)
180  {
181  return get(i);
182  }
183 
192  inline const T& operator[](size_t i) const
193  {
194  return get(i);
195  }
196 
202  T norm()
203  {
204  T n = 0.0;
205 
206  for (size_t i = 0 ; i < dim ; i++)
207  n+=get(i) * get(i);
208 
209  return sqrt(n);
210  }
211 
221  T distance(const Point<dim,T> & q)
222  {
223  T tot = 0.0;
224 
225  for (size_t i = 0 ; i < dim ; i++)
226  tot += (this->get(i) - q.get(i)) * (this->get(i) - q.get(i));
227 
228  return sqrt(tot);
229  }
230 
240  T distance2(const Point<dim,T> & q)
241  {
242  T tot = 0.0;
243 
244  for (size_t i = 0 ; i < dim ; i++)
245  tot += (this->get(i) - q.get(i)) * (this->get(i) - q.get(i));
246 
247  return tot;
248  }
249 
250 
255  inline void zero()
256  {
257  for (size_t i = 0 ; i < dim ; i++)
258  {
259  get(i) = 0;
260  }
261  }
262 
267  inline void one()
268  {
269  for (size_t i = 0 ; i < dim ; i++)
270  {
271  get(i) = 1;
272  }
273  }
274 
280  inline static Point<dim,T> zero_p()
281  {
282  Point<dim,T> p;
283 
284  for (size_t i = 0 ; i < dim ; i++)
285  {
286  p.get(i) = 0;
287  }
288 
289  return p;
290  }
291 
297  std::string toPointString() const
298  {
299  std::stringstream ps;
300 
301  for (size_t i = 0 ; i < dim ; i++)
302  ps << "x[" << i << "]=" << get(i) << " ";
303 
304  ps << "\n";
305 
306  return ps.str();
307  }
308 
314  void swap(Point<dim,T> & p)
315  {
316  for (size_t i = 0 ; i < dim ; i++)
317  {
318  T tmp = get(i);
319  get(i) = p.get(i);
320  p.get(i) = tmp;
321  }
322  }
323 
331  inline bool operator==(const Point<dim,T> & p)
332  {
333  for (size_t i = 0 ; i < dim ; i++)
334  {
335  if (p.get(i) != get(i))
336  return false;
337  }
338 
339  return true;
340  }
341 
349  inline bool operator!=(const Point<dim,T> & p)
350  {
351  return !this->operator==(p);
352 
353  return true;
354  }
355 
361  std::string to_string() const
362  {
363  return toString();
364  }
365 
371  std::string toString() const
372  {
373  std::string str;
374 
375  for (size_t i = 0 ; i < dim - 1 ; i++)
376  {
377  str += std::to_string(static_cast<double>(get(i))) + " ";
378  }
379  str += std::to_string(static_cast<double>(get(dim-1)));
380 
381  return str;
382  }
383 
391  T & value(size_t i)
392  {
393  return get(i);
394  }
395 
403  inline T value(size_t i) const
404  {
405  return get(i);
406  }
407 
413  T (&asArray())[dim]
414  {
415  return boost::fusion::at_c<x>(data);
416  }
417 
423  template<typename A> Point<dim,A> convertPoint() const
424  {
425  Point<dim,A> p;
426 
427  for (size_t i = 0; i < dim ; i++)
428  p.get(i) = static_cast<A>(get(i));
429 
430  return p;
431  }
432 
433 
435  static bool noPointers()
436  {
437  return true;
438  }
439 
443 
451  template<typename orig, typename exp1, typename exp2, unsigned int op> Point<dim,T> & operator=(const point_expression_op<orig,exp1,exp2,op> & p_exp)
452  {
453  p_exp.init();
454 
455  for (size_t i = 0; i < dim ; i++)
456  get(i) = p_exp.value(i);
457 
458  return *this;
459  }
460 
468  Point<dim,T> & operator=(const point_expression<T[dim]> & p_exp)
469  {
470  p_exp.init();
471 
472  for (size_t i = 0; i < dim ; i++)
473  get(i) = p_exp.value(i);
474 
475  return *this;
476  }
477 
485  Point<dim,T> & operator=(const T (& p)[dim])
486  {
487  for (size_t i = 0; i < dim ; i++)
488  get(i) = p[i];
489 
490  return *this;
491  }
492 
502  template<typename T1, typename check = typename std::enable_if<std::is_const<T1>::value == false>::type> Point<dim,T> & operator=(const point_expression<const T1[dim]> & p_exp)
503  {
504  p_exp.init();
505 
506  for (size_t i = 0; i < dim ; i++)
507  get(i) = p_exp.value(i);
508 
509  return *this;
510  }
511 
519  template<typename aT> inline Point<dim,T> operator/(const aT (&ar)[dim])
520  {
521  Point<dim,T> result;
522 
523  for (size_t i = 0 ; i < dim ; i++)
524  result.get(i) = get(i) / ar[i];
525 
526  return result;
527  }
528 
536  template<typename aT> inline Point<dim,T> operator/=(const aT c)
537  {
538  for (size_t i = 0 ; i < dim ; i++)
539  get(i) = get(i) / c;
540 
541  return *this;
542  }
543 
544 
553  {
554  for (size_t i = 0; i < dim ; i++)
555  get(i) = d;
556 
557  return *this;
558  }
559 
567  inline Point<dim,T> & operator=(const Point<dim,T> & p)
568  {
569  for (size_t i = 0 ; i < dim ; i++)
570  get(i) = p.get(i);
571 
572  return *this;
573  }
574 
583  {
584  for (size_t i = 0 ; i < dim ; i++)
585  get(i) -= p.get(i);
586 
587  return *this;
588  }
589 
598  {
599  for (size_t i = 0 ; i < dim ; i++)
600  get(i) += p.get(i);
601 
602  return *this;
603  }
604 
610  inline void init() const
611  {}
612 
615 
617  static const unsigned int max_prop = 1;
618  static const unsigned int max_prop_real = 1;
619 
621  static const unsigned int dims = dim;
622 
624  static const unsigned int nvals = dim;
625 };
626 
634 template <unsigned int N, typename T> std::string toPointString(const T (&p)[N] )
635 {
636  std::stringstream ps;
637 
638  for (size_t i = 0 ; i < N ; i++)
639  ps << "x[" << i << "]=" << p[i] << " ";
640 
641  ps << "\n";
642 
643  return ps.str();
644 }
645 
653 template <unsigned int N, typename T, typename Mem> std::string toPointString(const encapc<1,Point<N,T>,Mem> & p )
654 {
655  std::stringstream ps;
656 
657  for (size_t i = 0 ; i < N ; i++)
658  ps << "x[" << i << "]=" << p.template get<Point<N,T>::x>()[i] << " ";
659 
660  ps << "\n";
661 
662  return ps.str();
663 }
664 
665 
667 
668 template<unsigned int dim, typename T> using VectorS = Point<dim,T>;
669 
670 #endif
Point< dim, T > & operator=(T d)
Fill the vector property with some value.
Definition: Point.hpp:552
Point< dim, T > & operator=(const T(&p)[dim])
Fill the point with the value specified in the array.
Definition: Point.hpp:485
Point(const Point< dim, T > &&p)
Point constructor from point.
Definition: Point.hpp:57
Point< dim, T > & operator=(const point_expression_op< orig, exp1, exp2, op > &p_exp)
Fill the vector with the evaluated expression.
Definition: Point.hpp:451
Point()
Default contructor.
Definition: Point.hpp:132
Point< dim, T > & operator=(const point_expression< const T1[dim]> &p_exp)
Fill the vector property with the evaluated expression.
Definition: Point.hpp:502
void swap(Point< dim, T > &p)
exchange the data of two points
Definition: Point.hpp:314
static bool noPointers()
This structure has no internal pointers.
Definition: Point.hpp:435
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:371
int is_vtk_writable
Indicate that this object is vtk writable.
Definition: Point.hpp:27
T & operator[](size_t i)
Get the component i.
Definition: Point.hpp:179
Point< dim, T > operator/(const aT(&ar)[dim])
divide each component by an array
Definition: Point.hpp:519
Unknown operation specialization.
static const unsigned int max_prop
The point has one property.
Definition: Point.hpp:617
Point(const Point< dim, S > &p)
Point constructor.
Definition: Point.hpp:100
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
Main class that encapsulate a constant number used in a point expression.
T & value(size_t i)
Return the reference to the value at coordinate i.
Definition: Point.hpp:391
Point< dim, T > & operator+=(const Point< dim, T > &p)
Sum two points.
Definition: Point.hpp:597
static Point< dim, T > zero_p()
Create a point set to zero.
Definition: Point.hpp:280
bool operator!=(const Point< dim, T > &p)
Check if two points match.
Definition: Point.hpp:349
Point(const T(&p)[dim])
Constructor from an array.
Definition: Point.hpp:79
Point< dim, T > & operator=(const Point< dim, T > &p)
operator= between points
Definition: Point.hpp:567
void init() const
Do nothing stub operation.
Definition: Point.hpp:610
T(& asArray())[dim]
Return the coordinated of the point as reference array.
Definition: Point.hpp:413
const T & operator[](size_t i) const
Get the component i.
Definition: Point.hpp:192
type data
structure that store the data of the point
Definition: Point.hpp:36
T value(size_t i) const
Return the value at coordinate i.
Definition: Point.hpp:403
void one()
Set to one the point coordinate.
Definition: Point.hpp:267
Point< dim, T > operator/=(const aT c)
divide each component by a constant
Definition: Point.hpp:536
T get_vtk(size_t i) const
Get coordinate.
Definition: Point.hpp:154
Point(const Point< dim, T > &p)
Point constructor from point.
Definition: Point.hpp:68
Point< dim, T > & operator-=(const Point< dim, T > &p)
Subtract two points.
Definition: Point.hpp:582
Point(T d)
Constructor from scalar.
Definition: Point.hpp:90
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
boost::fusion::vector< T[dim]> type
boost fusion that store the point
Definition: Point.hpp:33
Point< dim, T > & operator=(const point_expression< T[dim]> &p_exp)
Fill the vector property with the evaluated expression.
Definition: Point.hpp:468
T norm()
norm of the vector
Definition: Point.hpp:202
static const unsigned int dims
expose the dimension
Definition: Point.hpp:621
static const unsigned int x
Property id of the point.
Definition: Point.hpp:39
Point< dim, A > convertPoint() const
Definition: Point.hpp:423
T distance2(const Point< dim, T > &q)
It calculate the square distance between 2 points.
Definition: Point.hpp:240
Point(const encapc< d, Point< dim, T >, M > &p)
Point constructor.
Definition: Point.hpp:111
static const unsigned int nvals
expose the dimension with a different name
Definition: Point.hpp:624
std::string to_string() const
Return the string with the point coordinate.
Definition: Point.hpp:361
bool operator==(const Point< dim, T > &p)
Check if two points match.
Definition: Point.hpp:331
Point(const point_expression_op< orig, exp1, exp2, op > &p_exp)
Evaluate the expression and save the result on the point.
Definition: Point.hpp:47
void zero()
Set to zero the point coordinate.
Definition: Point.hpp:255
T distance(const Point< dim, T > &q)
It calculate the distance between 2 points.
Definition: Point.hpp:221
std::string toPointString() const
Convert the point into a string.
Definition: Point.hpp:297
T coord_type
coordinate type
Definition: Point.hpp:30
Point(std::initializer_list< T > p1)
Constructor from a list.
Definition: Point.hpp:124