Loading...
Searching...
No Matches
Point.h
// Copyright (C) 2006-2014 Anders Logg
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Garth N. Wells 2006
// Modified by Andre Massing 2009
//
// First added: 2006-06-12
// Last changed: 2017-09-28
#ifndef __POINT_H
#define __POINT_H
#include <array>
#include <cmath>
#include <iostream>
#include <dolfin/log/log.h>
#include <dolfin/common/Array.h>
namespace dolfin
{
class Point
{
public:
explicit Point(const double x=0.0, const double y=0.0, const double z=0.0)
: _x({{x, y, z}}) {}
Point(std::size_t dim, const double* x) : _x({{0.0, 0.0, 0.0}})
{
for (std::size_t i = 0; i < dim; i++)
_x[i] = x[i];
}
Point(const Array<double>& x) : _x({{0.0, 0.0, 0.0}})
{
for (std::size_t i = 0; i < x.size(); i++)
_x[i] = x[i];
}
Point(const Point& p) : _x({{p._x[0], p._x[1], p._x[2]}}) {}
~Point() {}
double& operator[] (std::size_t i)
{ dolfin_assert(i < 3); return _x[i]; }
double operator[] (std::size_t i) const
{ dolfin_assert(i < 3); return _x[i]; }
double x() const
{ return _x[0]; }
double y() const
{ return _x[1]; }
double z() const
{ return _x[2]; }
double* coordinates()
{ return _x.data(); }
const double* coordinates() const
{ return _x.data(); }
std::array<double, 3> array() const
{
return _x;
}
Point operator+ (const Point& p) const
{ Point q(_x[0] + p._x[0], _x[1] + p._x[1], _x[2] + p._x[2]); return q; }
Point operator- (const Point& p) const
{ Point q(_x[0] - p._x[0], _x[1] - p._x[1], _x[2] - p._x[2]); return q; }
const Point& operator+= (const Point& p)
{ _x[0] += p._x[0]; _x[1] += p._x[1]; _x[2] += p._x[2]; return *this; }
const Point& operator-= (const Point& p)
{ _x[0] -= p._x[0]; _x[1] -= p._x[1]; _x[2] -= p._x[2]; return *this; }
Point operator- ()
{ Point p(-_x[0], -_x[1], -_x[2]); return p; }
Point operator* (double a) const
{ Point p(a*_x[0], a*_x[1], a*_x[2]); return p; }
const Point& operator*= (double a)
{ _x[0] *= a; _x[1] *= a; _x[2] *= a; return *this; }
Point operator/ (double a) const
{ Point p(_x[0]/a, _x[1]/a, _x[2]/a); return p; }
const Point& operator/= (double a)
{ _x[0] /= a; _x[1] /= a; _x[2] /= a; return *this; }
const Point& operator= (const Point& p)
{ _x = {{p._x[0], p._x[1], p._x[2]}}; return *this; }
bool operator== (const Point& p) const
{ return _x == p._x; }
bool operator!= (const Point& p) const
{ return _x != p._x; }
double squared_distance(const Point& p) const;
inline double distance(const Point& p) const
{ return sqrt(squared_distance(p)); }
double norm() const
{ return std::sqrt(_x[0]*_x[0] + _x[1]*_x[1] + _x[2]*_x[2]); }
double squared_norm() const
{ return _x[0]*_x[0] + _x[1]*_x[1] + _x[2]*_x[2]; }
const Point cross(const Point& p) const;
double dot(const Point& p) const;
Point rotate(const Point& a, double theta) const;
// Note: Not a subclass of Variable for efficiency!
std::string str(bool verbose=false) const;
private:
std::array<double, 3> _x;
};
inline Point operator*(double a, const Point& p)
{ return p*a; }
inline std::ostream& operator<<(std::ostream& stream, const Point& point)
{ stream << point.str(false); return stream; }
}
#endif
dolfin::Array
Definition Array.h:42
dolfin::Point
Definition Point.h:41
dolfin::Point::squared_norm
double squared_norm() const
Definition Point.h:259
dolfin::Point::operator*=
const Point & operator*=(double a)
Incremental multiplication with scalar.
Definition Point.h:181
dolfin::Point::operator=
const Point & operator=(const Point &p)
Assignment operator.
Definition Point.h:193
dolfin::Point::rotate
Point rotate(const Point &a, double theta) const
Definition Point.cpp:58
dolfin::Point::operator-
Point operator-()
Unary minus.
Definition Point.h:173
dolfin::Point::coordinates
double * coordinates()
Definition Point.h:132
dolfin::Point::operator+=
const Point & operator+=(const Point &p)
Add given point.
Definition Point.h:165
dolfin::Point::cross
const Point cross(const Point &p) const
Definition Point.cpp:33
dolfin::Point::Point
Point(std::size_t dim, const double *x)
Definition Point.h:61
dolfin::Point::operator/=
const Point & operator/=(double a)
Incremental division by scalar.
Definition Point.h:189
dolfin::Point::array
std::array< double, 3 > array() const
Definition Point.h:147
dolfin::Point::operator==
bool operator==(const Point &p) const
Equal to operator.
Definition Point.h:197
dolfin::Point::norm
double norm() const
Definition Point.h:244
dolfin::Point::x
double x() const
Definition Point.h:111
dolfin::Point::Point
Point(const Array< double > &x)
Definition Point.h:71
dolfin::Point::coordinates
const double * coordinates() const
Definition Point.h:139
dolfin::Point::y
double y() const
Definition Point.h:118
dolfin::Point::squared_distance
double squared_distance(const Point &p) const
Definition Point.cpp:44
dolfin::Point::Point
Point(const double x=0.0, const double y=0.0, const double z=0.0)
Definition Point.h:52
dolfin::Point::operator+
Point operator+(const Point &p) const
Definition Point.h:155
dolfin::Point::operator/
Point operator/(double a) const
Division by scalar.
Definition Point.h:185
dolfin::Point::dot
double dot(const Point &p) const
Definition Point.cpp:53
dolfin::Point::str
std::string str(bool verbose=false) const
Definition Point.cpp:70
dolfin::Point::operator[]
double & operator[](std::size_t i)
Definition Point.h:94
dolfin::Point::~Point
~Point()
Destructor.
Definition Point.h:84
dolfin::Point::Point
Point(const Point &p)
Definition Point.h:81
dolfin::Point::distance
double distance(const Point &p) const
Definition Point.h:229
dolfin::Point::z
double z() const
Definition Point.h:125
dolfin::Point::operator*
Point operator*(double a) const
Multiplication with scalar.
Definition Point.h:177
dolfin::Point::operator!=
bool operator!=(const Point &p) const
Not equal to operator.
Definition Point.h:201
dolfin::Point::operator-=
const Point & operator-=(const Point &p)
Subtract given point.
Definition Point.h:169
dolfin
Definition adapt.h:30
dolfin::operator*
Point operator*(double a, const Point &p)
Multiplication with scalar.
Definition Point.h:317
dolfin::operator<<
std::ostream & operator<<(std::ostream &stream, const Point &point)
Output of Point to stream.
Definition Point.h:321