-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inline most point functions. #301
Merged
MFraters
merged 6 commits into
GeodynamicWorldBuilder:master
from
MFraters:inline_point_functions
Jul 3, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e21b625
inlineing some point functions.
MFraters f0721c8
inline more point functions.
MFraters 296ff04
simplify some point operator functions.
MFraters e5e44c0
simplify point opertator+-
MFraters 4cfbdf1
create specialized inline for norm_square and operator*
MFraters 8100c33
inline the most used pointer constructor functions.
MFraters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<double,dim>()), | ||
coordinate_system(coordinate_system_) | ||
{} | ||
|
||
/** | ||
* Constructor. Constructs a Point from a std::array<double,dim> and | ||
* a coordinate system. | ||
*/ | ||
Point(const std::array<double,dim> &location, CoordinateSystem coordinate_system); | ||
inline | ||
Point(const std::array<double,dim> &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<dim> &point, CoordinateSystem coordinate_system); | ||
inline | ||
Point(const Point<dim> &location, CoordinateSystem coordinate_system_) | ||
: | ||
point(location.get_array()), | ||
coordinate_system(coordinate_system_) | ||
{} | ||
|
||
/** | ||
* Constructor. Constructs a Point from an other Point. | ||
*/ | ||
Point(const Point<dim> &point); | ||
inline | ||
Point(const Point<dim> &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<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; | ||
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; | ||
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); | ||
for (unsigned int i = 0; i < dim; ++i) | ||
point_tmp[i] += point_right[i]; | ||
|
||
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); | ||
for (unsigned int i = 0; i < dim; ++i) | ||
point_tmp[i] -= point_right[i]; | ||
|
||
return point_tmp; | ||
} | ||
|
||
|
||
/** | ||
* Multiply the vector with a scalar | ||
*/ | ||
Point<dim> &operator*=(const double scalar); | ||
|
||
inline | ||
Point<dim> &operator*=(const double scalar) | ||
{ | ||
for (unsigned int i = 0; i < dim; ++i) | ||
point[i] *= scalar; | ||
return *this; | ||
} | ||
/** | ||
* Divide the vector through a scalar | ||
*/ | ||
Point<dim> &operator/=(const double scalar); | ||
inline | ||
Point<dim> &operator/=(const double scalar) | ||
{ | ||
for (unsigned int i = 0; i < dim; ++i) | ||
point[i] /= scalar; | ||
return *this; | ||
} | ||
|
||
/** | ||
* add two points | ||
*/ | ||
Point<dim> &operator+=(const Point<dim> &point); | ||
|
||
inline | ||
Point<dim> &operator+=(const Point<dim> &point_right) | ||
{ | ||
for (unsigned int i = 0; i < dim; ++i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
point[i] += point_right[i]; | ||
return *this; | ||
} | ||
|
||
/** | ||
* substract two points | ||
*/ | ||
Point<dim> &operator-=(const Point<dim> &point); | ||
inline | ||
Point<dim> &operator-=(const Point<dim> &point_left) | ||
{ | ||
for (unsigned int i = 0; i < dim; ++i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here and probably some others as well |
||
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<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()); | ||
} | ||
|
||
|
||
/** | ||
* 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<int dim> | ||
Point<dim> operator*(const double scalar, const Point<dim> &point); | ||
inline | ||
Point<dim> operator*(const double scalar, const Point<dim> &point) | ||
{ | ||
return point*scalar; | ||
} | ||
|
||
template<int dim> | ||
Point<dim> operator/(const double scalar, const Point<dim> &point); | ||
inline | ||
Point<dim> operator/(const double scalar, const Point<dim> &point) | ||
{ | ||
// initialize the array to zero. | ||
std::array<double,dim> array = Point<dim>(point.coordinate_system).get_array(); | ||
for (unsigned int i = 0; i < dim; ++i) | ||
array[i] = scalar / point[i]; | ||
return Point<dim>(array,point.coordinate_system); | ||
} | ||
|
||
} // namespace WorldBuilder | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true for the dot product operator as well, by the way, even though it doesn't have this assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moreover, the formula for the dot product is only correct for Cartesian coordinate systems :-)