diff --git a/include/world_builder/point.h b/include/world_builder/point.h index b2faebb89..02ad6a2ca 100644 --- a/include/world_builder/point.h +++ b/include/world_builder/point.h @@ -42,24 +42,44 @@ namespace WorldBuilder * Constructor. Constructs a Point at (0,0) in 2d or (0,0,0) in 3d * with a Cartesian coordinate system. */ - Point(CoordinateSystem coordinate_system); + inline + Point(CoordinateSystem coordinate_system_) + : + point(std::array()), + coordinate_system(coordinate_system_) + {} /** * Constructor. Constructs a Point from a std::array and * a coordinate system. */ - Point(const std::array &location, CoordinateSystem coordinate_system); + inline + Point(const std::array &location, CoordinateSystem coordinate_system_) + : + point(location), + coordinate_system(coordinate_system_) + {} /** * Constructor. Constructs a Point from an other Point and * a coordinate system. */ - Point(const Point &point, CoordinateSystem coordinate_system); + inline + Point(const Point &location, CoordinateSystem coordinate_system_) + : + point(location.get_array()), + coordinate_system(coordinate_system_) + {} /** * Constructor. Constructs a Point from an other Point. */ - Point(const Point &point); + inline + Point(const Point &location) + : + point(location.get_array()), + coordinate_system(location.get_coordinate_system()) + {} /** * Constructor. Constructs a 2d Point from two doubles and @@ -76,59 +96,133 @@ namespace WorldBuilder /** * Destructor */ - ~Point(); + inline + ~Point() = default; - Point &operator=(const Point &point); + inline + Point &operator=(const Point &point_right) + { + point = point_right.point; + coordinate_system = point_right.coordinate_system; + return *this; + } /** * dot product */ - double operator*(const Point &point) const; + inline + double operator*(const Point &point_right) const + { + const std::array &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 operator*(const double scalar) const; + inline + Point operator*(const double scalar) const + { + // initialize the array to zero. + std::array array; + for (unsigned int i = 0; i < dim; ++i) + array[i] = point[i] * scalar; + return Point(array,coordinate_system); + } /** * Divide the vector through a scalar */ - Point operator/(const double scalar) const; + inline + Point operator/(const double scalar) const + { + // initialize the array to zero. + std::array 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(array,coordinate_system); + } /** * add two points */ - Point operator+(const Point &point) const; + inline + Point operator+(const Point &point_right) const + { + WBAssert(coordinate_system == point_right.get_coordinate_system(), + "Cannot add two points which represent different coordinate systems."); + Point point_tmp(point,coordinate_system); + for (unsigned int i = 0; i < dim; ++i) + point_tmp[i] += point_right[i]; + + return point_tmp; + } /** * Substract two points */ - Point operator-(const Point &point) const; - + inline + Point operator-(const Point &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(coordinate_system) + << ", other point has type " << static_cast(point_right.get_coordinate_system())); + Point point_tmp(point,coordinate_system); + for (unsigned int i = 0; i < dim; ++i) + point_tmp[i] -= point_right[i]; + + return point_tmp; + } /** * Multiply the vector with a scalar */ - Point &operator*=(const double scalar); - + inline + Point &operator*=(const double scalar) + { + for (unsigned int i = 0; i < dim; ++i) + point[i] *= scalar; + return *this; + } /** * Divide the vector through a scalar */ - Point &operator/=(const double scalar); + inline + Point &operator/=(const double scalar) + { + for (unsigned int i = 0; i < dim; ++i) + point[i] /= scalar; + return *this; + } /** * add two points */ - Point &operator+=(const Point &point); - + inline + Point &operator+=(const Point &point_right) + { + for (unsigned int i = 0; i < dim; ++i) + point[i] += point_right[i]; + return *this; + } /** * substract two points */ - Point &operator-=(const Point &point); + inline + Point &operator-=(const Point &point_left) + { + for (unsigned int i = 0; i < dim; ++i) + point[i] -= point_left[i]; + return *this; + } /** * access index (const) @@ -171,26 +265,43 @@ namespace WorldBuilder /** * return the internal array which stores the point data. */ - const std::array &get_array() const; + inline + const std::array &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()); + } /** * Computes the square of the norm, which is the sum of the absolute squares * x_i * x_i + y_i * y_i + z_i * z_i in 3d. */ - double norm_square() const; + inline + double norm_square() const + { + WBAssertThrow(false,"This function is only available in 2d or 3d."); + return 0; + } /** * Outputs the values of the point to std cout separated by spaces. This does not @@ -214,6 +325,20 @@ namespace WorldBuilder }; + template <> + inline + double Point<2>::norm_square() const + { + return (point[0] * point[0]) + (point[1] * point[1]); + } + + template <> + inline + double Point<3>::norm_square() const + { + return (point[0] * point[0]) + (point[1] * point[1]) + (point[2] * point[2]); + } + /** * This namespace contains some faster but less accurate version of the * trigonomic functions and a faster version of the fmod function. @@ -281,9 +406,22 @@ namespace WorldBuilder template - Point operator*(const double scalar, const Point &point); + inline + Point operator*(const double scalar, const Point &point) + { + return point*scalar; + } template - Point operator/(const double scalar, const Point &point); + inline + Point operator/(const double scalar, const Point &point) + { + // initialize the array to zero. + std::array array = Point(point.coordinate_system).get_array(); + for (unsigned int i = 0; i < dim; ++i) + array[i] = scalar / point[i]; + return Point(array,point.coordinate_system); + } + } // namespace WorldBuilder #endif diff --git a/source/point.cc b/source/point.cc index 90326b9af..bd8692904 100644 --- a/source/point.cc +++ b/source/point.cc @@ -23,41 +23,6 @@ namespace WorldBuilder { - template<> - Point<3>::Point(const CoordinateSystem coordinate_system_) - : - point({{0,0,0}}), - coordinate_system(coordinate_system_) - {} - - template<> - Point<2>::Point(const CoordinateSystem coordinate_system_) - : - point({{0,0}}), - coordinate_system(coordinate_system_) - {} - - template - Point::Point(const std::array &location, const CoordinateSystem coordinate_system_) - : - point(location), - coordinate_system(coordinate_system_) - {} - - template - Point::Point(const Point &point_, const CoordinateSystem coordinate_system_) - : - point(point_.get_array()), - coordinate_system(coordinate_system_) - {} - - template - Point::Point(const Point &point_) - : - point(point_.get_array()), - coordinate_system(point_.get_coordinate_system()) - {} - template<> Point<2>::Point(const double x, const double y, const CoordinateSystem coordinate_system_) @@ -94,152 +59,9 @@ namespace WorldBuilder {} - template - Point::~Point() - = default; - - template - Point &Point::operator=(const Point &point_) - { - point = point_.point; - coordinate_system = point_.coordinate_system; - return *this; - } - - template - double Point::operator*(const Point &point_) const - { - const std::array 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 - Point Point::operator*(const double scalar) const - { - // initialize the array to zero. - std::array array = Point(coordinate_system).get_array(); - for (unsigned int i = 0; i < dim; ++i) - array[i] += point[i] * scalar; - return Point(array,coordinate_system); - } - template - Point Point::operator/(const double scalar) const - { - // initialize the array to zero. - std::array array = Point(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(array,coordinate_system); - } - template - Point - Point::operator+(const Point &point_) const - { - WBAssert(coordinate_system == point_.get_coordinate_system(), - "Cannot add two points which represent different coordinate systems."); - Point point_tmp(point,coordinate_system); - point_tmp += point_; - return point_tmp; - } - - template - Point - Point::operator-(const Point &point_) const - { - WBAssert(coordinate_system == point_.get_coordinate_system(), - "Cannot substract two points which represent different coordinate systems. Internal has type " << static_cast(coordinate_system) - << ", other point has type " << static_cast(point_.get_coordinate_system())); - Point point_tmp(point,coordinate_system); - point_tmp -= point_; - - return point_tmp; - } - - - template - Point & - Point::operator*=(const double scalar) - { - for (unsigned int i = 0; i < dim; ++i) - point[i] *= scalar; - return *this; - } - - - template - Point & - Point::operator/=(const double scalar) - { - for (unsigned int i = 0; i < dim; ++i) - point[i] /= scalar; - return *this; - } - - - template - Point & - Point::operator+=(const Point &point_) - { - for (unsigned int i = 0; i < dim; ++i) - point[i] += point_[i]; - return *this; - } - - - template - Point & - Point::operator-=(const Point &point_) - { - for (unsigned int i = 0; i < dim; ++i) - point[i] -= point_[i]; - return *this; - } - - - template - const std::array & - Point::get_array() const - { - return point; - } - - - template - CoordinateSystem - Point::get_coordinate_system() const - { - return coordinate_system; - } - - - template - double - Point::norm() const - { - return std::sqrt(this->norm_square()); - } - - - template<> - double - Point<2>::norm_square() const - { - return (point[0] * point[0]) + (point[1] * point[1]); - } - - template<> - double - Point<3>::norm_square() const - { - return (point[0] * point[0]) + (point[1] * point[1]) + (point[2] * point[2]); - } template @@ -263,8 +85,6 @@ namespace WorldBuilder } - - template double Point::cheap_relative_distance(const Point &two) const @@ -286,30 +106,6 @@ namespace WorldBuilder } - /** - * Multiplies a point with a scalar. - */ - template - Point - operator*(const double scalar, const Point &point) - { - return point*scalar; - } - - /** - * Divides a scalar by a point: output_vector[i] = scalar / point[i]. - */ - template - Point - operator/(const double scalar, const Point &point) - { - // initialize the array to zero. - std::array array = Point(point.coordinate_system).get_array(); - for (unsigned int i = 0; i < dim; ++i) - array[i] = scalar / point[i]; - return Point(array,point.coordinate_system); - } - /** * The 2d version of the point class. */