Skip to content

Commit

Permalink
inline more point functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
MFraters committed Jul 3, 2021
1 parent e21b625 commit f0721c8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 96 deletions.
80 changes: 71 additions & 9 deletions include/world_builder/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,84 @@ namespace WorldBuilder
*/
~Point();

Point<dim> &operator=(const Point<dim> &point);
inline
Point<dim> &operator=(const Point<dim> &point_right)
{
point = point_right.point;
coordinate_system = point_right.coordinate_system;
return *this;
}

/**
* dot product
*/
double operator*(const Point<dim> &point) const;
inline
double operator*(const Point<dim> &point_right) const
{
const std::array<double,dim> array = point_right.get_array();
double dot_product = 0;
for (unsigned int i = 0; i < dim; ++i)
dot_product += point[i] * array[i];
return dot_product;
}


/**
* Multiply the vector with a scalar
*/
Point<dim> operator*(const double scalar) const;
inline
Point<dim> operator*(const double scalar) const
{
// initialize the array to zero.
std::array<double,dim> array = Point<dim>(coordinate_system).get_array();
for (unsigned int i = 0; i < dim; ++i)
array[i] += point[i] * scalar;
return Point<dim>(array,coordinate_system);
}

/**
* Divide the vector through a scalar
*/
Point<dim> operator/(const double scalar) const;
inline
Point<dim> operator/(const double scalar) const
{
// initialize the array to zero.
std::array<double,dim> array = Point<dim>(coordinate_system).get_array();
const double one_over_scalar = 1/scalar;
for (unsigned int i = 0; i < dim; ++i)
array[i] += point[i] * one_over_scalar;
return Point<dim>(array,coordinate_system);
}

/**
* add two points
*/
Point<dim> operator+(const Point<dim> &point) const;
inline
Point<dim> operator+(const Point<dim> &point_right) const
{
WBAssert(coordinate_system == point_right.get_coordinate_system(),
"Cannot add two points which represent different coordinate systems.");
Point<dim> point_tmp(point,coordinate_system);
point_tmp += point_right;

return point_tmp;
}


/**
* Substract two points
*/
Point<dim> operator-(const Point<dim> &point) const;
inline
Point<dim> operator-(const Point<dim> &point_right) const
{
WBAssert(coordinate_system == point_right.get_coordinate_system(),
"Cannot substract two points which represent different coordinate systems. Internal has type " << static_cast<int>(coordinate_system)
<< ", other point has type " << static_cast<int>(point_right.get_coordinate_system()));
Point<dim> point_tmp(point,coordinate_system);
point_tmp -= point_right;

return point_tmp;
}


/**
Expand Down Expand Up @@ -192,19 +242,31 @@ namespace WorldBuilder
/**
* return the internal array which stores the point data.
*/
const std::array<double,dim> &get_array() const;
inline
const std::array<double,dim> &get_array() const
{
return point;
}


/**
* returns the coordinate system associated with the data.
*/
CoordinateSystem get_coordinate_system() const;
inline
CoordinateSystem get_coordinate_system() const
{
return coordinate_system;
}


/**
* Computes the L2 norm: sqrt(x_i * x_i + y_i * y_i + z_i * z_i) in 3d.
*/
double norm() const;
inline
double norm() const
{
return std::sqrt(this->norm_square());
}


/**
Expand Down
87 changes: 0 additions & 87 deletions source/point.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,93 +98,6 @@ namespace WorldBuilder
Point<dim>::~Point()
= default;

template<int dim>
Point<dim> &Point<dim>::operator=(const Point<dim> &point_)
{
point = point_.point;
coordinate_system = point_.coordinate_system;
return *this;
}

template<int dim>
double Point<dim>::operator*(const Point<dim> &point_) const
{
const std::array<double,dim> array = point_.get_array();
double dot_product = 0;
for (unsigned int i = 0; i < dim; ++i)
dot_product += point[i] * array[i];
return dot_product;
}

template<int dim>
Point<dim> Point<dim>::operator*(const double scalar) const
{
// initialize the array to zero.
std::array<double,dim> array = Point<dim>(coordinate_system).get_array();
for (unsigned int i = 0; i < dim; ++i)
array[i] += point[i] * scalar;
return Point<dim>(array,coordinate_system);
}

template<int dim>
Point<dim> Point<dim>::operator/(const double scalar) const
{
// initialize the array to zero.
std::array<double,dim> array = Point<dim>(coordinate_system).get_array();
const double one_over_scalar = 1/scalar;
for (unsigned int i = 0; i < dim; ++i)
array[i] += point[i] * one_over_scalar;
return Point<dim>(array,coordinate_system);
}

template<int dim>
Point<dim>
Point<dim>::operator+(const Point<dim> &point_) const
{
WBAssert(coordinate_system == point_.get_coordinate_system(),
"Cannot add two points which represent different coordinate systems.");
Point<dim> point_tmp(point,coordinate_system);
point_tmp += point_;

return point_tmp;
}

template<int dim>
Point<dim>
Point<dim>::operator-(const Point<dim> &point_) const
{
WBAssert(coordinate_system == point_.get_coordinate_system(),
"Cannot substract two points which represent different coordinate systems. Internal has type " << static_cast<int>(coordinate_system)
<< ", other point has type " << static_cast<int>(point_.get_coordinate_system()));
Point<dim> point_tmp(point,coordinate_system);
point_tmp -= point_;

return point_tmp;
}


template<int dim>
const std::array<double,dim> &
Point<dim>::get_array() const
{
return point;
}


template<int dim>
CoordinateSystem
Point<dim>::get_coordinate_system() const
{
return coordinate_system;
}


template<int dim>
double
Point<dim>::norm() const
{
return std::sqrt(this->norm_square());
}


template<>
Expand Down

0 comments on commit f0721c8

Please sign in to comment.