OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
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 
20 template<unsigned int dim ,typename T> class Point
21 {
22  public:
23 
24  typedef T coord_type;
25 
27  typedef boost::fusion::vector<T[dim]> type;
28 
31 
33  static const unsigned int x = 0;
34 
42  inline T get(int i) const
43  {
44  return boost::fusion::at_c<x>(data)[i];
45  }
46 
54  inline T& get(int i)
55  {
56  return boost::fusion::at_c<x>(data)[i];
57  }
58 
65  inline T& operator[](size_t i)
66  {
67  return get(i);
68  }
69 
75  inline Point<dim,T> & operator=(const Point<dim,T> & p)
76  {
77  for (size_t i = 0 ; i < dim ; i++)
78  {
79  get(i) = p.get(i);
80  }
81 
82  return *this;
83  }
84 
90  inline Point<dim,T> operator*(T c)
91  {
92  Point<dim,T> result;
93 
94  for (size_t i = 0 ; i < dim ; i++)
95  {
96  result.get(i) = get(i) * c;
97  }
98 
99  return result;
100  }
101 
107  template<typename aT> inline Point<dim,T> operator*(const Point<dim,aT> & p)
108  {
109  Point<dim,T> result;
110 
111  for (size_t i = 0 ; i < dim ; i++)
112  {
113  result.get(i) = get(i) * p.get(i);
114  }
115 
116  return result;
117  }
118 
125  {
126  for (size_t i = 0 ; i < dim ; i++)
127  {
128  get(i) -= p.get(i);
129  }
130 
131  return *this;
132  }
133 
140  {
141  for (size_t i = 0 ; i < dim ; i++)
142  {
143  get(i) += p.get(i);
144  }
145 
146  return *this;
147  }
148 
154  T norm()
155  {
156  T n = 0.0;
157 
158  for (size_t i = 0 ; i < dim ; i++)
159  {
160  n+=get(i) * get(i);
161  }
162 
163  return sqrt(n);
164  }
165 
171  template<typename aT> inline Point<dim,T> operator+(const Point<dim,aT> & p)
172  {
173  Point<dim,T> result;
174 
175  for (size_t i = 0 ; i < dim ; i++)
176  {
177  result.get(i) = get(i) + p.get(i);
178  }
179 
180  return result;
181  }
182 
188  template<typename aT> inline Point<dim,T> operator/(const aT (&ar)[dim])
189  {
190  Point<dim,T> result;
191 
192  for (size_t i = 0 ; i < dim ; i++)
193  {
194  result.get(i) = get(i) / ar[i];
195  }
196 
197  return result;
198  }
199 
205  template<typename aT> inline Point<dim,T> operator/(const aT c)
206  {
207  Point<dim,T> result;
208 
209  for (size_t i = 0 ; i < dim ; i++)
210  {
211  result.get(i) = get(i) / c;
212  }
213 
214  return result;
215  }
216 
225  {
226  Point<dim,T> result;
227 
228  for (size_t i = 0 ; i < dim ; i++)
229  {
230  result.get(i) = get(i) - p.get(i);
231  }
232 
233  return result;
234  }
235 
240  inline void zero()
241  {
242  for (size_t i = 0 ; i < dim ; i++)
243  {
244  get(i) = 0;
245  }
246  }
247 
252  inline void one()
253  {
254  for (size_t i = 0 ; i < dim ; i++)
255  {
256  get(i) = 1;
257  }
258  }
259 
265  inline static Point<dim,T> zero_p()
266  {
267  Point<dim,T> p;
268 
269  for (size_t i = 0 ; i < dim ; i++)
270  {
271  p.get(i) = 0;
272  }
273 
274  return p;
275  }
276 
282  std::string toPointString() const
283  {
284  std::stringstream ps;
285 
286  for (size_t i = 0 ; i < dim ; i++)
287  ps << "x[" << i << "]=" << get(i) << " ";
288 
289  ps << "\n";
290 
291  return ps.str();
292  }
293 
299  std::string toString() const
300  {
301  std::string str;
302 
303  for (size_t i = 0 ; i < dim - 1 ; i++)
304  {
305  str += std::to_string(get(i)) + " ";
306  }
307  str += std::to_string(get(dim-1));
308 
309  return str;
310  }
311 
317  inline Point(const Point<dim,T> && p)
318  {
319  for(size_t i = 0; i < dim ; i++)
320  {get(i) = p.get(i);}
321  }
322 
328  inline Point(const Point<dim,T> & p)
329  {
330  for(size_t i = 0; i < dim ; i++)
331  {get(i) = p.get(i);}
332  }
333 
339  inline Point(const T (&p)[dim])
340  {
341  for(size_t i = 0; i < dim ; i++)
342  {get(i) = p[i];}
343  }
344 
350  template <typename S> inline Point(const Point<dim,S> & p)
351  {
352  for (size_t i = 0 ; i < dim ; i++)
353  get(i) = static_cast<S>(p.get(i));
354  }
355 
361  template <unsigned int d, typename M> inline Point(const encapc<d,Point<dim,T>,M> & p)
362  {
363  for (size_t i = 0 ; i < dim ; i++)
364  get(i) = p.template get<0>()[i];
365  }
366 
372  inline Point(std::initializer_list<T> p1)
373  {
374  size_t i = 0;
375  for(T x : p1)
376  {get(i) = x;i++;}
377  }
378 
380  inline Point()
381  {}
382 
388  T & value(size_t i)
389  {
390  return get(i);
391  }
392 
398  T value(size_t i) const
399  {
400  return get(i);
401  }
402 
408  T (&asArray())[dim]
409  {
410  return boost::fusion::at_c<x>(data);
411  }
412 
418  template<typename A> Point<dim,A> convertPoint() const
419  {
420  Point<dim,A> p;
421 
422  for (size_t i = 0; i < dim ; i++)
423  p.get(i) = static_cast<A>(get(i));
424 
425  return p;
426  }
427 
429  static bool noPointers()
430  {
431  return true;
432  }
433 
434  static const unsigned int max_prop = 1;
435  static const unsigned int dims = dim;
436 };
437 
445 template <unsigned int N, typename T> std::string toPointString(const T (&p)[N] )
446 {
447  std::stringstream ps;
448 
449  for (size_t i = 0 ; i < N ; i++)
450  ps << "x[" << i << "]=" << p[i] << " ";
451 
452  ps << "\n";
453 
454  return ps.str();
455 }
456 
464 template <unsigned int N, typename T, typename Mem> std::string toPointString(const encapc<1,Point<N,T>,Mem> & p )
465 {
466  std::stringstream ps;
467 
468  for (size_t i = 0 ; i < N ; i++)
469  ps << "x[" << i << "]=" << p.template get<Point<N,T>::x>()[i] << " ";
470 
471  ps << "\n";
472 
473  return ps.str();
474 }
475 
476 #endif
Point(const Point< dim, T > &&p)
Point constructor from point.
Definition: Point.hpp:317
Point()
Default contructor.
Definition: Point.hpp:380
static bool noPointers()
This structure has no internal pointers.
Definition: Point.hpp:429
Point< dim, T > operator-(const Point< dim, T > &p)
Operator subtraction.
Definition: Point.hpp:224
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:299
T & operator[](size_t i)
Get the component i.
Definition: Point.hpp:65
Point< dim, T > operator+(const Point< dim, aT > &p)
Sum each components.
Definition: Point.hpp:171
Point< dim, T > operator/(const aT(&ar)[dim])
divide each component
Definition: Point.hpp:188
Point(const Point< dim, S > &p)
Point constructor.
Definition: Point.hpp:350
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:20
T & value(size_t i)
Return the reference to the value at coordinate i.
Definition: Point.hpp:388
Point< dim, T > & operator+=(const Point< dim, T > &p)
Sum each components.
Definition: Point.hpp:139
static Point< dim, T > zero_p()
Create a point set to zero.
Definition: Point.hpp:265
Point(const T(&p)[dim])
Constructor from an array.
Definition: Point.hpp:339
Point< dim, T > operator/(const aT c)
divide each component
Definition: Point.hpp:205
Point< dim, T > & operator=(const Point< dim, T > &p)
operator= between points
Definition: Point.hpp:75
T(& asArray())[dim]
Return the coordinated of the point as reference array.
Definition: Point.hpp:408
T get(int i) const
Get coordinate.
Definition: Point.hpp:42
type data
structure that store the data of the point
Definition: Point.hpp:30
T value(size_t i) const
Return the value at coordinate i.
Definition: Point.hpp:398
void one()
Set to one the point coordinate.
Definition: Point.hpp:252
Point(const Point< dim, T > &p)
Point constructor from point.
Definition: Point.hpp:328
Point< dim, T > & operator-=(const Point< dim, T > &p)
Subtract each components.
Definition: Point.hpp:124
this structure encapsulate an object of the grid
Definition: Encap.hpp:209
boost::fusion::vector< T[dim]> type
boost fusion that store the point
Definition: Point.hpp:27
T norm()
Definition: Point.hpp:154
static const unsigned int x
Property id of the point.
Definition: Point.hpp:33
Point< dim, A > convertPoint() const
Definition: Point.hpp:418
Point< dim, T > operator*(const Point< dim, aT > &p)
Multiply each components.
Definition: Point.hpp:107
Point(const encapc< d, Point< dim, T >, M > &p)
Point constructor.
Definition: Point.hpp:361
void zero()
Set to zero the point coordinate.
Definition: Point.hpp:240
std::string toPointString() const
Convert the point into a string.
Definition: Point.hpp:282
Point< dim, T > operator*(T c)
Multiply each components by a constant.
Definition: Point.hpp:90
Point(std::initializer_list< T > p1)
Constructor from a list.
Definition: Point.hpp:372