Point.h
Go to the documentation of this file.
1 /*
2  This file is part of the Ristra wonton project.
3  Please see the license file at the root of this repository, or at:
4  https://github.com/laristra/wonton/blob/master/LICENSE
5 */
6 
8 // Copyright 2016 Los Alamos National Laboratory //
9 // //
10 // Original Author: Paul Henning //
11 // TSM, ASCI Problem Setup Team //
12 // Applied Physics Division //
13 // Los Alamos National Laboratory //
14 // phenning@lanl.gov //
15 // //
16 // Modified for GK by: Brian Jean //
17 // TSM, ASCI Problem Setup Team //
18 // Applied Physics Division //
19 // Los Alamos National Laboratory //
20 // 505.665.6374 //
21 // baj@lanl.gov //
22 // //
23 // //
24 // Modified for Wonton by: Rao Garimella, rao@lanl.gov //
25 // //
27 
28 #ifndef WONTON_POINT_H_
29 #define WONTON_POINT_H_
30 
31 #include <cassert>
32 #include <iostream>
33 #include <vector>
34 #include "wonton/support/Vector.h"
35 #include "wonton/support/wonton.h"
36 
37 namespace Wonton {
38 
39 const int X = 0;
40 const int Y = 1;
41 const int Z = 2;
42 
49 template <int D>
50 class Point {
51  private:
52  double m_loc[D] {};
53 
54  public:
55 
58  Point() {
59  for (int i = 0; i < D; i++)
60  m_loc[i] = 0.0;
61  }
62 
68  Point(const std::vector<double> &v) {
69  assert(v.size() == D);
70  for (int i = 0; i < D; i++)
71  m_loc[i] = v[i];
72  }
73 
79  Point(const double& x, const double& y, const double& z) {
80  assert(D == 3);
81  m_loc[0] = x;
82  m_loc[1] = y;
83  m_loc[2] = z;
84  }
85 
91  Point(const double& x, const double& y) {
92  assert(D == 2);
93  m_loc[0] = x;
94  m_loc[1] = y;
95  }
96 
102  Point(const double& x) {
103  assert(D == 1);
104  m_loc[0] = x;
105  }
106 
109  explicit Point(const Vector<D>& v) {
110  for (int i = 0; i < D; i++)
111  m_loc[i] = v[i];
112  }
113 
116  Point(const Point<D>& rhs) {
117  for (int i = 0; i < D; i++)
118  m_loc[i] = rhs[i];
119  }
120 
123  const double& operator[](const int& i) const {
124  return m_loc[i];
125  }
126 
129  double& operator[](const int& i) {
130  return m_loc[i];
131  }
132 
135  Point<D> operator-() const {
136  Point<D> p;
137  for (int i = 0; i < D; i++) p.m_loc[i] = -m_loc[i];
138  return p;
139  }
140 
144  for (int i = 0; i < D; i++) m_loc[i] += v[i];
145  return *this;
146  }
147 
151  for (int i = 0; i < D; i++) m_loc[i] += p[i];
152  return *this;
153  }
154 
156  Point<D>& operator+=(std::vector<double> const& v) {
157  assert(v.size() == D);
158  for (int i = 0; i < D; i++)
159  m_loc[i] += v[i];
160  return *this;
161  }
162 
165  Point<D>& operator*=(double s) {
166  for (int i = 0; i < D; i++) m_loc[i] *= s;
167  return *this;
168  }
169 
172  Point<D>& operator/=(double s) {
173  for (int i = 0; i < D; i++) m_loc[i] /= s;
174  return *this;
175  }
176 
178  std::istream& readFromStream(std::istream& is) {
179  for (int i = 0; i < D; i++)
180  is >> m_loc[i];
181  return is;
182  }
183 
185  std::ostream& writeToStream(std::ostream& os) const {
186  for (int i = 0; i < D; i++) {
187  if (i > 0) os << ' ';
188  os << m_loc[i];
189  }
190  return os;
191  }
192 
196  Vector<D> v;
197  for (int i = 0; i < D; i++)
198  v[i] = m_loc[i];
199  return v;
200  }
201 
204  Vector<D> asV() const {
205  Vector<D> v;
206  for (int i = 0; i < D; i++)
207  v[i] = m_loc[i];
208  return v;
209  }
210 };
211 
213 typedef Point<3> Point3;
215 typedef Point<2> Point2;
217 typedef Point<1> Point1;
218 
219 template <int D>
220 std::ostream& operator<<(std::ostream& os, const Point<D>& p) {
221  return p.writeToStream(os);
222 }
223 
224 
225 template <int D>
227 Point<D> operator+(const Point<D>& p, const Vector<D>& v) {
228  return Point<D>(p) += v;
229 }
230 
231 template <int D>
233 Point<D> operator+(const Point<D>& p1, const Point<D>& p2) {
234  return Point<D>(p1) += p2;
235 }
236 
237 template <int D>
239 Vector<D> operator-(const Point<D>& p1, const Point<D>& p2) {
240  Vector<D> v;
241  for (int i = 0; i < D; i++) v[i] = p1[i] - p2[i];
242  return v;
243 }
244 
245 template <int D>
247 Point<D> operator*(const Point<D>& p, double s) {
248  return Point<D>(p) *= s;
249 }
250 
251 template <int D>
253 Point<D> operator*(double s, const Point<D>& p) {
254  return Point<D>(p) *= s;
255 }
256 
257 template <int D>
259 Point<D> operator/(const Point<D>& p, double s) {
260  return Point<D>(p) /= s;
261 }
262 
263 template <int D>
265 bool operator==(const Point<D>& p1, const Point<D>& p2) {
266  for (int i = 0; i < D; i++)
267  if (p1[i] != p2[i])
268  return false;
269  return true;
270 }
271 
272 template <int D>
274 bool approxEq(const Point<D>& p1, const Point<D>& p2, double tol = 1.0e-8) {
275  // This uses the L_infty norm to avoid nearby subtractions
276  for (int i = 0; i < D; i++)
277  if (p1[i] < (p2[i] - tol) || p1[i] > (p2[i] + tol))
278  return false;
279  return true;
280 }
281 
282 template <int D>
284 bool operator<(const Point<D>& p1, const Point<D>& p2) {
285  if (approxEq(p1, p2))
286  return false;
287  else
288  for (int i = 0; i < D; ++i) {
289  if (p1[i] < p2[i])
290  return true;
291  else if (p2[i] < p1[i])
292  return false;
293  }
294  return false;
295 }
296 
297 
299 Point2 ToCylindrical(const Point3& p) {
300  Point2 result;
301 
302  result[0] = sqrt(p[0]*p[0]+p[1]*p[1]);
303  result[1] = p[2];
304  return result;
305 }
306 
308 Point3 createP3(double x, double y, double z) {
309  Point3 p;
310 
311  p[0] = x;
312  p[1] = y;
313  p[2] = z;
314 
315  return p;
316 }
317 
319 Point2 createP2(double x, double y) {
320  Point2 p;
321 
322  p[0] = x;
323  p[1] = y;
324 
325  return p;
326 }
327 
329 Point1 createP1(double x) {
330  Point1 p;
331 
332  p[0] = x;
333 
334  return p;
335 }
336 
337 } // namespace Wonton
338 
339 #endif // WONTON_POINT_H_
WONTON_INLINE Point()
Default constructor - Point at origin in D-space.
Definition: Point.h:58
WONTON_INLINE Point< D > & operator+=(const Point< D > &p)
Add two Point vectors to get a third point vector.
Definition: Point.h:150
WONTON_INLINE Point< D > & operator+=(const Vector< D > &v)
Translate this Point along the Vector v.
Definition: Point.h:143
Point< 3 > Point3
Alias for creating a Point in 3d.
Definition: Point.h:213
WONTON_INLINE Vector< D > asV() const
Convert Point to Vector from the origin to the coordinates of the Point.
Definition: Point.h:204
WONTON_INLINE Point(const double &x, const double &y)
Specialized constructor for Points in 2d.
Definition: Point.h:91
WONTON_INLINE Point3 createP3(double x, double y, double z)
Definition: Point.h:308
Factorize a number N into D equal (or nearly equal) factors.
Definition: adaptive_refinement_mesh.h:31
WONTON_INLINE Point< D > operator*(const Point< D > &p, double s)
Definition: Point.h:247
WONTON_INLINE Vector< D > asV()
Convert Point to Vector from the origin to the coordinates of the Point.
Definition: Point.h:195
WONTON_INLINE Point(const double &x, const double &y, const double &z)
Specialized constructor for Points in 3d.
Definition: Point.h:79
Point< 2 > Point2
Alias for creating a Point in 2d.
Definition: Point.h:215
Represents a point in an N-dimensional space.
Definition: Point.h:50
WONTON_INLINE Point(const Point< D > &rhs)
Copy constructor.
Definition: Point.h:116
const int Y
Definition: Point.h:40
Point< 1 > Point1
Alias for creating a Point in 1d.
Definition: Point.h:217
WONTON_INLINE Point< D > operator/(const Point< D > &p, double s)
Definition: Point.h:259
WONTON_INLINE bool approxEq(const Point< D > &p1, const Point< D > &p2, double tol=1.0e-8)
Definition: Point.h:274
WONTON_INLINE const double & operator[](const int &i) const
Return component i of the Point.
Definition: Point.h:123
WONTON_INLINE Point< D > & operator+=(std::vector< double > const &v)
Definition: Point.h:156
WONTON_INLINE Point(const double &x)
Specialized constructor for Points in 1d.
Definition: Point.h:102
WONTON_INLINE Point< D > & operator/=(double s)
Scale this Point (/)
Definition: Point.h:172
WONTON_INLINE Point< D > & operator*=(double s)
Scale this Point (*)
Definition: Point.h:165
WONTON_INLINE Point2 ToCylindrical(const Point3 &p)
Definition: Point.h:299
WONTON_INLINE Point< D > operator-() const
Negative of this Point.
Definition: Point.h:135
const int X
Definition: Point.h:39
WONTON_INLINE Point2 createP2(double x, double y)
Definition: Point.h:319
const int Z
Definition: Point.h:41
WONTON_INLINE Point(const std::vector< double > &v)
Specialized constructor from a std::vector of arbitary size.
Definition: Point.h:68
#define WONTON_INLINE
Definition: wonton.h:235
WONTON_INLINE Point1 createP1(double x)
Definition: Point.h:329
WONTON_INLINE double & operator[](const int &i)
Return component i of the Point.
Definition: Point.h:129
WONTON_INLINE Point< D > operator+(const Point< D > &p, const Vector< D > &v)
Definition: Point.h:227
WONTON_INLINE Point(const Vector< D > &v)
Convert a Vector to a Point.
Definition: Point.h:109
WONTON_INLINE bool operator==(const Point< D > &p1, const Point< D > &p2)
Definition: Point.h:265
std::ostream & writeToStream(std::ostream &os) const
Pretty printing of the coordinates of a Point to an output stream.
Definition: Point.h:185
std::istream & readFromStream(std::istream &is)
Read in the coordinates a Point from an input stream.
Definition: Point.h:178
Represents a vector in N-dimensional space.
Definition: Vector.h:40