Proto  3.2
Proto_Point.H
Go to the documentation of this file.
1 #pragma once
2 #ifndef _PROTO_POINT_
3 #define _PROTO_POINT_
4 
5 #include <iostream>
6 #include <sstream>
7 #include <array>
8 #include <set>
9 
10 #include "Proto_Accel.H"
11 #include "Proto_Face.H"
12 #include "Proto_PAssert.H"
13 
14 namespace Proto
15 {
16 
17 #ifdef PR_HDF5
18  class HDF5Handler;
19 #endif
20  /// Integer Valued Vector
21  /**
22  An element \f$\mathbf{p}\in\mathbb{Z}^{DIM}\f$
23  */
24  class Point
25  {
26 #ifdef PR_HDF5
27  friend class Proto::HDF5Handler;
28 #endif
29  public:
30  //////////////////////////////////////////////////////////////////////////////////////////
31  /** @name Constructors */
32  ///@{
33  /// Default Constructor
34  /**
35  Creates the zero Point <code>(0,0,...,0)</code>
36  */
37  ACCEL_DECORATION inline Point();
38 
39  /// C-Array Constructor
40  /**
41 
42  Allows "literal" construction of points using a C-Array
43 
44  Example:
45  @code
46  using namespace Proto;
47  //DIM=2
48  Point p = Point({-5,13}); //creates the point (-5,13)
49  //NB: This will not compile when DIM != 2
50  @endcode
51 
52  \param a_tuple integer array representing a Point
53  */
54  ACCEL_DECORATION inline Point(const int (&a_tuple)[DIM]);
55 
56  /// Variadic Constructor
57  /**
58  Builds a Point from the first DIM arguments given to this. Any trailing arguments are ignored.
59  This constructor is very useful for building dimensionally independent test code.
60 
61  Example:
62  @code
63  Point p(1,2,3,4,5,6);
64  if (DIM == 1)
65  {
66  std::cout << p << std::endl; //prints (1)
67  } else if (DIM == 2) {
68  std::cout << p << std::endl; //prints (1, 2)
69  } else if (DIM == 3) {
70  std::cout << p << std::endl; //prints (1, 2, 3)
71  } //etc.
72  @endcode
73 
74  \param args At least DIM integer arguments representing a Point
75  */
76  template<typename... vals>
77  ACCEL_DECORATION inline explicit Point(vals... args) { unpack(0, args...); }
78 
79  /// Copy Constructor
80  /**
81  Creates a new point by copying an existing one.
82 
83  \param a_pt A Point.
84  */
85  ACCEL_DECORATION inline Point(const Point& a_pt);
86 
87  ///@}
88  //////////////////////////////////////////////////////////////////////////////////////////
89  /** @name Static Methods */
90  ///@{
91 
92  /// Get Ones
93  /**
94  Returns the unit Point <code>scale*(1,1,...,1)</code>
95 
96  \param a_scale (Optional) value to scale the vector by (default: 1)
97  */
98  ACCEL_DECORATION inline static Point Ones(int a_scale=1);
99 
100  /// Get Unit Point
101  /**
102  Returns the Point <code>(1,1,...,1)</code>
103  */
104  ACCEL_DECORATION inline static Point Unit() { return Ones(1); }
105 
106  /// Get Zeros
107  /**
108  Returns the zero Point <code>(0,0,...,0)</code>
109  */
110  ACCEL_DECORATION inline static Point Zeros();
111 
112  /// Get Zeros (Pedantic Spelling)
113  /**
114  Identical to Zeros() but with alternative spelling.
115  */
116  ACCEL_DECORATION inline static Point Zeroes() { return Zeros(); }
117 
118  /// Get Zeros (Alternate Spelling)
119  /**
120  Identical to Zeros() but with alternative spelling.
121  */
122  ACCEL_DECORATION inline static Point Zero() { return Zeros(); }
123 
124  /// Get Basis
125  /**
126  Returns an axis-aligned Point \f$\bf{p}: \bf{p}[k] = scale*\delta_{dir}^k\f$.
127 
128  \param a_dir A coordinate axis.
129  \param a_scale (Optional) value to scale the unit vector by (default: 1)
130  */
131  ACCEL_DECORATION inline static Point Basis(int a_dir, int a_scale=1);
132 
133  /// Get Basis (Side Argument)
134  /**
135  Returns an axis-aligned Point \f$\bf{p}: \bf{p}[k] = s*\delta_{dir}^k\f$.
136  Where \f$ s = 1 \f$ if <code> a_side = Side::Hi </code> and
137  where \f$ s = -1 \f$ if <code> a_side = Side::Lo </code>.
138 
139  \param a_dir A spatial axis (0 for x, 1 for y, etc.)
140  \param a_side Low or high side. Low amounts to scaling by -1.
141  */
142  ACCEL_DECORATION inline static Point Basis(int a_dir, const Side::LoHiSide a_side);
143 
144  ACCEL_DECORATION inline static Point X() { return Basis(0); }
145  ACCEL_DECORATION inline static Point Y() { return Basis(1); }
146  ACCEL_DECORATION inline static Point Z() { return Basis(2); }
147 
148  inline static std::set<Point> Directions();
149  inline static std::set<Point> DirectionsOfCodim(int a_codim);
150 
151 
152  ///@}
153  //////////////////////////////////////////////////////////////////////////////////////////
154  /** @name Accessors */
155  ///@{
156 
157  /// Get Component (Const)
158  /**
159  Returns a const reference to the a_index component of *this
160 
161  \param a_index Index in [0,DIM)
162  */
163  ACCEL_DECORATION inline const int& operator[](unsigned int a_index) const;
164 
165  /// Get Component (Non-Const)
166  /**
167  Returns a mutable reference to the a_index component of *this
168 
169  \param a_index Index in [0,DIM)
170  */
171  ACCEL_DECORATION inline int& operator[](unsigned int a_index);
172 
173  ///@}
174  //////////////////////////////////////////////////////////////////////////////////////////
175  /** @name Operators */
176  ///@{
177 
178  /// Asignment Operator
179  /**
180  Performs copy assignment.
181 
182  \param a_rhs Another Point
183  */
184  ACCEL_DECORATION inline Point& operator=(const Point& a_rhs);
185 
186  /// Less-Than Operator
187  /**
188  Used to define an ordering for placing Points in maps etc.
189  Uses Lexical comparison. Note that Chombo lexLT is also lexical
190  but uses the dimensions in the opposite way.
191 
192  \param a_rhs Another Point
193  */
194  ACCEL_DECORATION inline bool operator<(const Point& a_rhs) const;
195 
196  /// Equality Operator
197  /**
198  Equality is checked componentwise.
199 
200  \param a_pt Another Point
201  */
202  ACCEL_DECORATION inline bool operator==(const Point& a_pt) const;
203 
204  /// Inequality Operator
205  /**
206  Equality is checked componentwise.
207 
208  \param a_pt Another Point
209  */
210  ACCEL_DECORATION inline bool operator!=(const Point& a_pt) const;
211 
212  /// Componentwise Point Addition Operator
213  /**
214  Creates the new Point by adding <code>*this</code> and <code>a_rhs</code> componentwise.
215 
216  \param a_rhs Another Point
217  */
218  ACCEL_DECORATION inline Point operator+(const Point& a_rhs) const;
219 
220  /// Componentwise Point Subtraction Operator
221  /**
222  Creates the new Point by subtracting <code>*this</code>
223  and <code>a_rhs</code> componentwise.
224 
225  \param a_rhs Another Point
226  */
227  ACCEL_DECORATION inline Point operator-(const Point& a_rhs) const;
228 
229  /// Componentwise Point Multiplication Operator
230  /**
231  Creates the new Point by multiplying <code>*this</code>
232  and <code>a_rhs</code> componentwise.
233 
234  \param a_pt Another Point
235  */
236  ACCEL_DECORATION inline Point operator*(const Point& a_pt) const;
237 
238  /// Componentwise Point Division Operator
239  /**
240  Creates the new Point by dividing <code>*this</code>
241  by <code>a_rhs</code> componentwise. Quotients are rounded down.
242  Division of any component by 0 yields an error.
243 
244  \param a_pt Another Point
245  */
246  ACCEL_DECORATION inline Point operator/(const Point& a_pt) const;
247 
248  /// Componentwise Point Modulus Operator
249  /**
250  Creates the new Point by taking the modulus of <code>*this</code>
251  by <code>a_rhs</code> componentwise. Quotients are rounded down.
252  Modulo by 0 yields an error.
253 
254  \param a_pt Another Point
255  */
256  ACCEL_DECORATION inline Point operator%(const Point& a_pt) const;
257 
258  ///Scalar Addition Operator
259  /**
260  Creates a new Point by adding nref to each component of <code>*this</code>.
261 
262  \param a_value An Integer scalar
263  */
264  inline Point operator+(int a_value) const;
265 
266  ///Scalar Subtraction Operator
267  /**
268  Creates a new Point by subtracting nref to each component of <code>*this</code>.
269 
270  \param a_value An Integer scalar
271  */
272  inline Point operator-(int a_value) const;
273 
274  /// Scalar Multiplication Operator
275  /**
276  Creates a new Point by multiplying each component of <code>*this</code> by <code>a_value</code>.
277 
278  \param a_value An Integer scalar
279  */
280  inline Point operator*(int a_value) const;
281 
282  /// Scalar Division Operator
283  /**
284  Creates a new Point by dividing each component of <code>*this</code> by <code>a_value</code>.
285  Quotients are rounded down. Division of any component by 0 yields an error.
286 
287  \param a_value An Integer scalar
288  */
289  inline Point operator/(int a_value) const;
290 
291  /// Scalar Modulus Operator
292  /**
293  Creates a new Point by taking the modulus of each component of <code>*this</code> by <code>a_value</code>.
294  Quotients are rounded down. Modulo of any component by 0 yields an error.
295 
296  \param a_value An Integer scalar
297  */
298  inline Point operator%(int a_value) const;
299 
300  /// In Place Componentwise Addition Operator
301  /**
302  Adds another Point componentwise to <code>*this</code>.
303 
304  \param a_pt Another Point
305  */
306  ACCEL_DECORATION inline void operator+=(const Point& a_pt);
307 
308  /// In Place Componentwise Subtraction Operator
309  /**
310  Subtracts another Point componentwise from <code>*this</code>.
311 
312  \param a_pt Another Point
313  */
314  ACCEL_DECORATION inline void operator-=(const Point& a_pt);
315 
316  /// In Place Componentwise Multiplication Operator
317  /**
318  Multiplies <code>*this</code> by another Point componentwise.
319 
320  \param a_pt Another Point
321  */
322  ACCEL_DECORATION inline void operator*=(const Point& a_pt);
323 
324  /// In Place Componentwise Division Operator
325  /**
326  Divides <code>*this</code> by another Point componentwise.
327  Quotients are rounded down. Division of any component by 0 yields an error.
328 
329  \param a_pt Another Point
330  */
331  ACCEL_DECORATION inline void operator/=(const Point& a_pt);
332 
333  /// In Place Componentwise Modulus Operator
334  /**
335  Computes the modulus of <code>*this</code> by another Point componentwise.
336  Quotients are rounded down. Module of any component by 0 yields an error.
337 
338  \param a_pt Another Point
339  */
340  ACCEL_DECORATION inline void operator%=(const Point& a_pt);
341 
342  /// In Place Scalar Addition Operator
343  /**
344  Adds an integer to every component of <code>*this</code>.
345 
346  \param a_n An integer.
347  */
348  inline void operator+=(int a_n);
349 
350  /// In Place Scalar Subtraction Operator
351  /**
352  Subtracts an integer from every component of <code>*this</code>.
353 
354  \param a_n An integer.
355  */
356  inline void operator-=(int a_n);
357 
358  /// In Place Scalar Multiplication Operator
359  /**
360  Multiplies every component of <code>*this</code> by an integer.
361 
362  \param a_n An integer.
363  */
364  inline void operator*=(int a_n);
365 
366  /// In Place Scalar Division Operator
367  /**
368  Divides every component of <code>*this</code> by an integer.
369  Quotients are rounded down. Division by 0 yields an error.
370 
371  \param a_n An integer.
372  */
373  inline void operator/=(int a_n);
374 
375  /// In Place Scalar Modulus Operator
376  /**
377  Computes the modulus of every component of *this by an integer.
378  Modulo of 0 results in an error.
379 
380  \param a_n An integer.
381  */
382  inline void operator%=(int a_n);
383 
384  /// Compute Dot Product
385  inline int dot(const Point& a_rhs) const;
386 
387  /// Coarsen Operator
388  /**
389  Returns a new Point coarsened by a_refRatio.
390  This function is identical to any of the division operators.
391 
392  \param a_refRatio A non-zero refinement ratio.
393  */
394  inline Point coarsen(unsigned int a_refRatio) const;
395 
396  /// Sum Operator
397  /**
398  Computes the sum of the entries in *this
399  */
401  inline int sum() const;
402 
403  /// Absolute Value Sum Operator
404  /**
405  Computes the sum of the absolute values of the entries in *this
406  */
407  inline int sumAbs() const;
408 
409  inline int min() const;
410 
411  inline int max() const;
412 
413  inline Point abs() const;
414  /// Isotropic Query
415  /**
416  Checks if all elements of *this are equal.
417  */
418  inline bool isotropic() const;
419 
420  /// Query Codimension
421  /**
422  Returns the codimensionality of *this (e.g. the number of num-zero entries)
423  */
424  inline int codim() const;
425 
426  /// Parallel Unit Vectors
427  /**
428  Returns an array of Point representing the basis vectors
429  of the smallest vector space containing *this. If the ith element
430  of *this is non-zero, the ith element of the output will be Basis(i).
431  Otherwise, the ith element of the output will be Zeros().
432  */
433  inline std::array<Point, DIM> parallelUnit() const;
434 
435  /// Perpendicular Unit Vectors
436  /**
437  Returns an array of Point representing the basis vectors
438  of the largest vector space not containing *this. If the ith element
439  of *this is zero, the ith element of the output will be Basis(i).
440  Otherwise, the ith element of the output will be Zeros().
441  */
442  inline std::array<Point, DIM> perpUnit() const;
443 
444  inline int firstNonZeroIndex() const;
445 
446  inline int lastNonZeroIndex() const;
447 
448  /// Coordinates of non-zero entries
449  inline std::vector<int> coords() const;
450 
451  /// Coordinates of zero entries
452  inline std::vector<int> perpCoords() const;
453 
454  /// Scaled Unit Vectors
455  inline std::vector<Point> split() const;
456 
457  /// Returns a vector of Points each of which are this dotted into a subset of unit vectors
458  /// The zero vector is omitted
459  inline std::vector<Point> subset() const;
460 
461  /// Convert Point to Direction
462  /** A "direction" is defined as a Point with values limited to 1, 0, and -1.
463  */
464  inline Point dir() const;
465  /// Linear Size
466  /**
467  Returns the size of *this in bytes
468  */
469  inline size_t linearSize() const;
470 
471  /// Linear Out
472  /**
473  Writes from *this into a linear buffer of size <code>this->linearSize()</code>.
474 
475  \param a_buf A buffer
476  */
477  inline void linearOut(char* a_buf) const;
478 
479  /// Linear In
480  /**
481  Reads to *this from a linear buffer of size <code>this->linearSize()</code>.
482 
483  \param a_buf A buffer
484  */
485  inline void linearIn(const char* a_buf);
486 
487  ///@}
488  //////////////////////////////////////////////////////////////////////////////////////////
489  /** @name Utility */
490  ///@{
491 
492  /// Print Function
493  /**
494  Output is formatted: <code>(p1,p2,...)</code>
495  */
496  inline void print() const;
497  inline std::string str() const;
498  ///@}
499 
500 
501 
502  int m_tuple[DIM]; ///<Integer coordinates of the Point
503 
504  template<typename... vars>
505  ACCEL_DECORATION inline void unpack(int D, int i, vars... args)
506  {
507  if (D < DIM)
508  {
509  m_tuple[D] = i;
510  unpack(D+1, args...);
511  }
512  }
513 
514  }; //end class Point
515 
516  template<>
517  ACCEL_DECORATION inline void Point::unpack(int D, int i)
518  {
519  if (D < DIM)
520  {
521  m_tuple[D] = i;
522  }
523  }
524 
525  //////////////////////////////////////////////////////////////////////////////////////////
526  /** @name External Operators */
527  ///@{
528 
529  /// Stream Operator
530  inline std::ostream& operator <<(std::ostream& a_os, const Point& a_pt);
531  /// Stream Operator
532  inline std::istream& operator >>(std::istream& a_os, Point& a_pt);
533 
534  /// Premultiplication by scalar
535  inline Point operator*(int a_scale, Point a_pt) { return (a_pt*a_scale); }
536 
537  /// Unary Negation
538  inline Point operator-(Point a_pt) { return a_pt*(-1); }
539 
540  inline Point minPoint(Point a_p1, Point a_p2)
541  {
542  Point p;
543  for (int ii = 0; ii < DIM; ii++)
544  {
545  p[ii] = std::min(a_p1[ii], a_p2[ii]);
546  }
547  return p;
548  }
549 
550  inline Point maxPoint(Point a_p1, Point a_p2)
551  {
552  Point p;
553  for (int ii = 0; ii < DIM; ii++)
554  {
555  p[ii] = std::max(a_p1[ii], a_p2[ii]);
556  }
557  return p;
558  }
559 
560  inline Point absMaxPoint(Point a_p1, Point a_p2)
561  {
562  Point p;
563  for (int ii = 0; ii < DIM; ii++)
564  {
565  p[ii] = std::max(abs(a_p1[ii]), abs(a_p2[ii]));
566  }
567  return p;
568  }
569 
570  ///@}
571 } //end namespace Proto
572 #endif //end include guard
ACCEL_DECORATION bool operator!=(const Point &a_pt) const
Inequality Operator.
ACCEL_DECORATION bool operator==(const Point &a_pt) const
Equality Operator.
ACCEL_DECORATION Point operator%(const Point &a_pt) const
Componentwise Point Modulus Operator.
std::vector< Point > subset() const
Point coarsen(unsigned int a_refRatio) const
Coarsen Operator.
LoHiSide
Side Enum.
Definition: Proto_Face.H:23
Point dir() const
Convert Point to Direction.
int dot(const Point &a_rhs) const
Compute Dot Product.
std::vector< int > coords() const
Coordinates of non-zero entries.
std::istream & operator>>(std::istream &a_is, Box &a_box)
OStream Operator.
Definition: Proto_Box.H:962
ACCEL_DECORATION Point()
int max() const
static std::set< Point > DirectionsOfCodim(int a_codim)
int min() const
ACCEL_DECORATION void operator/=(const Point &a_pt)
In Place Componentwise Division Operator.
void linearOut(char *a_buf) const
Linear Out.
void linearIn(const char *a_buf)
Linear In.
static ACCEL_DECORATION Point Basis(int a_dir, int a_scale=1)
Get Basis.
Point abs() const
ACCEL_DECORATION bool operator<(const Point &a_rhs) const
Less-Than Operator.
Point maxPoint(Point a_p1, Point a_p2)
Definition: Proto_Point.H:550
ACCEL_DECORATION Point operator/(const Point &a_pt) const
Componentwise Point Division Operator.
ACCEL_DECORATION void operator%=(const Point &a_pt)
In Place Componentwise Modulus Operator.
int lastNonZeroIndex() const
std::vector< int > perpCoords() const
Coordinates of zero entries.
Point minPoint(Point a_p1, Point a_p2)
Definition: Proto_Point.H:540
static ACCEL_DECORATION Point Zero()
Get Zeros (Alternate Spelling)
Definition: Proto_Point.H:122
std::string str() const
ACCEL_DECORATION Point operator-(const Point &a_rhs) const
Componentwise Point Subtraction Operator.
size_t linearSize() const
Linear Size.
std::vector< Point > split() const
Scaled Unit Vectors.
ACCEL_DECORATION Point operator+(const Point &a_rhs) const
Componentwise Point Addition Operator.
#define ACCEL_DECORATION
Definition: Proto_Accel.H:12
ACCEL_DECORATION int sum() const
Sum Operator.
ACCEL_DECORATION void unpack(int D, int i, vars... args)
Definition: Proto_Point.H:505
ACCEL_DECORATION void operator-=(const Point &a_pt)
In Place Componentwise Subtraction Operator.
bool isotropic() const
Isotropic Query.
static ACCEL_DECORATION Point X()
Definition: Proto_Point.H:144
static ACCEL_DECORATION Point Ones(int a_scale=1)
Get Ones.
Point absMaxPoint(Point a_p1, Point a_p2)
Definition: Proto_Point.H:560
static std::set< Point > Directions()
static ACCEL_DECORATION Point Y()
Definition: Proto_Point.H:145
static ACCEL_DECORATION Point Z()
Definition: Proto_Point.H:146
static ACCEL_DECORATION Point Zeros()
Get Zeros.
Definition: Proto_Array.H:17
std::array< Point, DIM > perpUnit() const
Perpendicular Unit Vectors.
std::array< Point, DIM > parallelUnit() const
Parallel Unit Vectors.
std::ostream & operator<<(std::ostream &stream, const Array< T, N > &arr)
Ostream operator.
Integer Valued Vector.
Definition: Proto_Point.H:24
int sumAbs() const
Absolute Value Sum Operator.
ACCEL_DECORATION void operator*=(const Point &a_pt)
In Place Componentwise Multiplication Operator.
ACCEL_DECORATION void operator+=(const Point &a_pt)
In Place Componentwise Addition Operator.
int codim() const
Query Codimension.
ACCEL_DECORATION Point(vals... args)
Variadic Constructor.
Definition: Proto_Point.H:77
static ACCEL_DECORATION Point Zeroes()
Get Zeros (Pedantic Spelling)
Definition: Proto_Point.H:116
ACCEL_DECORATION Point & operator=(const Point &a_rhs)
Asignment Operator.
ACCEL_DECORATION const int & operator[](unsigned int a_index) const
Get Component (Const)
static ACCEL_DECORATION Point Unit()
Get Unit Point.
Definition: Proto_Point.H:104
void print() const
Print Function.
int m_tuple[DIM]
Integer coordinates of the Point.
Definition: Proto_Point.H:502
ACCEL_DECORATION Point operator*(const Point &a_pt) const
Componentwise Point Multiplication Operator.
int firstNonZeroIndex() const