DOLFIN
DOLFIN C++ interface
Point.h
1 // Copyright (C) 2006-2014 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Garth N. Wells 2006
19 // Modified by Andre Massing 2009
20 //
21 // First added: 2006-06-12
22 // Last changed: 2017-09-28
23 
24 #ifndef __POINT_H
25 #define __POINT_H
26 
27 #include <array>
28 #include <cmath>
29 #include <iostream>
30 #include <dolfin/log/log.h>
31 #include <dolfin/common/Array.h>
32 
33 namespace dolfin
34 {
39 
40  class Point
41  {
42  public:
43 
52  explicit Point(const double x=0.0, const double y=0.0, const double z=0.0)
53  : _x({{x, y, z}}) {}
54 
61  Point(std::size_t dim, const double* x) : _x({{0.0, 0.0, 0.0}})
62  {
63  for (std::size_t i = 0; i < dim; i++)
64  _x[i] = x[i];
65  }
66 
71  Point(const Array<double>& x) : _x({{0.0, 0.0, 0.0}})
72  {
73  for (std::size_t i = 0; i < x.size(); i++)
74  _x[i] = x[i];
75  }
76 
81  Point(const Point& p) : _x({{p._x[0], p._x[1], p._x[2]}}) {}
82 
84  ~Point() {}
85 
94  double& operator[] (std::size_t i)
95  { dolfin_assert(i < 3); return _x[i]; }
96 
104  double operator[] (std::size_t i) const
105  { dolfin_assert(i < 3); return _x[i]; }
106 
111  double x() const
112  { return _x[0]; }
113 
118  double y() const
119  { return _x[1]; }
120 
125  double z() const
126  { return _x[2]; }
127 
132  double* coordinates()
133  { return _x.data(); }
134 
139  const double* coordinates() const
140  { return _x.data(); }
141 
147  std::array<double, 3> array() const
148  {
149  return _x;
150  }
151 
155  Point operator+ (const Point& p) const
156  { Point q(_x[0] + p._x[0], _x[1] + p._x[1], _x[2] + p._x[2]); return q; }
157 
161  Point operator- (const Point& p) const
162  { Point q(_x[0] - p._x[0], _x[1] - p._x[1], _x[2] - p._x[2]); return q; }
163 
165  const Point& operator+= (const Point& p)
166  { _x[0] += p._x[0]; _x[1] += p._x[1]; _x[2] += p._x[2]; return *this; }
167 
169  const Point& operator-= (const Point& p)
170  { _x[0] -= p._x[0]; _x[1] -= p._x[1]; _x[2] -= p._x[2]; return *this; }
171 
174  { Point p(-_x[0], -_x[1], -_x[2]); return p; }
175 
177  Point operator* (double a) const
178  { Point p(a*_x[0], a*_x[1], a*_x[2]); return p; }
179 
181  const Point& operator*= (double a)
182  { _x[0] *= a; _x[1] *= a; _x[2] *= a; return *this; }
183 
185  Point operator/ (double a) const
186  { Point p(_x[0]/a, _x[1]/a, _x[2]/a); return p; }
187 
189  const Point& operator/= (double a)
190  { _x[0] /= a; _x[1] /= a; _x[2] /= a; return *this; }
191 
193  const Point& operator= (const Point& p)
194  { _x = {{p._x[0], p._x[1], p._x[2]}}; return *this; }
195 
197  bool operator== (const Point& p) const
198  { return _x == p._x; }
199 
201  bool operator!= (const Point& p) const
202  { return _x != p._x; }
203 
212  double squared_distance(const Point& p) const;
213 
229  inline double distance(const Point& p) const
230  { return sqrt(squared_distance(p)); }
231 
244  double norm() const
245  { return std::sqrt(_x[0]*_x[0] + _x[1]*_x[1] + _x[2]*_x[2]); }
246 
259  double squared_norm() const
260  { return _x[0]*_x[0] + _x[1]*_x[1] + _x[2]*_x[2]; }
261 
269  const Point cross(const Point& p) const;
270 
286  double dot(const Point& p) const;
287 
297  Point rotate(const Point& a, double theta) const;
298 
299  // Note: Not a subclass of Variable for efficiency!
300 
308  std::string str(bool verbose=false) const;
309 
310  private:
311 
312  std::array<double, 3> _x;
313 
314  };
315 
317  inline Point operator*(double a, const Point& p)
318  { return p*a; }
319 
321  inline std::ostream& operator<<(std::ostream& stream, const Point& point)
322  { stream << point.str(false); return stream; }
323 
324 }
325 
326 #endif
Point(const Point &p)
Definition: Point.h:81
double y() const
Definition: Point.h:118
double norm() const
Definition: Point.h:244
std::string str(bool verbose=false) const
Definition: Point.cpp:70
double distance(const Point &p) const
Definition: Point.h:229
const Point cross(const Point &p) const
Definition: Point.cpp:33
double z() const
Definition: Point.h:125
const Point & operator=(const Point &p)
Assignment operator.
Definition: Point.h:193
Definition: adapt.h:29
~Point()
Destructor.
Definition: Point.h:84
Definition: Point.h:40
Definition: Array.h:41
Point(const Array< double > &x)
Definition: Point.h:71
Point operator*(double a) const
Multiplication with scalar.
Definition: Point.h:177
const Point & operator-=(const Point &p)
Subtract given point.
Definition: Point.h:169
Point operator-()
Unary minus.
Definition: Point.h:173
double x() const
Definition: Point.h:111
Point operator/(double a) const
Division by scalar.
Definition: Point.h:185
bool operator==(const Point &p) const
Equal to operator.
Definition: Point.h:197
std::array< double, 3 > array() const
Definition: Point.h:147
Point rotate(const Point &a, double theta) const
Definition: Point.cpp:58
Point(std::size_t dim, const double *x)
Definition: Point.h:61
double * coordinates()
Definition: Point.h:132
bool operator!=(const Point &p) const
Not equal to operator.
Definition: Point.h:201
const double * coordinates() const
Definition: Point.h:139
double squared_distance(const Point &p) const
Definition: Point.cpp:44
double squared_norm() const
Definition: Point.h:259
Point operator+(const Point &p) const
Definition: Point.h:155
double & operator[](std::size_t i)
Definition: Point.h:94
std::ostream & operator<<(std::ostream &stream, const Point &point)
Output of Point to stream.
Definition: Point.h:321
double dot(const Point &p) const
Definition: Point.cpp:53
const Point & operator/=(double a)
Incremental division by scalar.
Definition: Point.h:189
const Point & operator+=(const Point &p)
Add given point.
Definition: Point.h:165
const Point & operator*=(double a)
Incremental multiplication with scalar.
Definition: Point.h:181
Point(const double x=0.0, const double y=0.0, const double z=0.0)
Definition: Point.h:52