Skip to content

Commit

Permalink
core: (merges !621) Add DistanceSquared and LengthSquared methods to …
Browse files Browse the repository at this point in the history
…Vectors

Complements existing methods to avoid unnecessary square roots in some cases
  • Loading branch information
scogginsnl authored and tomhenderson committed Aug 19, 2021
1 parent a02b47a commit 0f3fbff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/core/model/vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ Vector2D::GetLength () const
return std::sqrt (x * x + y * y);
}

double
Vector3D::GetLengthSquared () const
{
NS_LOG_FUNCTION (this);
return x * x + y * y + z * z;
}
double
Vector2D::GetLengthSquared () const
{
NS_LOG_FUNCTION (this);
return x * x + y * y;
}

double
CalculateDistance (const Vector3D &a, const Vector3D &b)
{
Expand All @@ -101,6 +114,19 @@ CalculateDistance (const Vector2D &a, const Vector2D &b)
return (b - a).GetLength ();
}

double
CalculateDistanceSquared (const Vector3D &a, const Vector3D &b)
{
NS_LOG_FUNCTION (a << b);
return (b - a).GetLengthSquared ();
}
double
CalculateDistanceSquared (const Vector2D &a, const Vector2D &b)
{
NS_LOG_FUNCTION (a << b);
return (b - a).GetLengthSquared ();
}

std::ostream &operator << (std::ostream &os, const Vector3D &vector)
{
os << vector.x << ":" << vector.y << ":" << vector.z;
Expand Down
27 changes: 27 additions & 0 deletions src/core/model/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class Vector3D
*/
double GetLength () const;

/**
* Compute the squared length of the vector.
* \returns the vector length squared.
*/
double GetLengthSquared () const;

/**
* \brief Calculate the Cartesian distance between two points.
* \param [in] a One point
Expand All @@ -74,6 +80,14 @@ class Vector3D
*/
friend double CalculateDistance (const Vector3D &a, const Vector3D &b);

/**
* \brief Calculate the squared Cartesian distance between two points.
* \param [in] a One point
* \param [in] b Another point
* \returns The distance between \pname{a} and \pname{b}.
*/
friend double CalculateDistanceSquared (const Vector3D &a, const Vector3D &b);

/**
* Output streamer.
* Vectors are written as "x:y:z".
Expand Down Expand Up @@ -184,6 +198,9 @@ class Vector2D
/** \copydoc Vector3D::GetLength() */
double GetLength () const;

/** \copydoc Vector3D::GetLengthSquared() */
double GetLengthSquared () const;

/**
* \brief Calculate the Cartesian distance between two points.
* \param [in] a One point
Expand All @@ -192,6 +209,14 @@ class Vector2D
*/
friend double CalculateDistance (const Vector2D &a, const Vector2D &b);

/**
* \brief Calculate the squared Cartesian distance between two points.
* \param [in] a One point
* \param [in] b Another point
* \returns The distance between \pname{a} and \pname{b}.
*/
friend double CalculateDistanceSquared (const Vector2D &a, const Vector2D &b);

/**
* Output streamer.
* Vectors are written as "x:y".
Expand Down Expand Up @@ -280,6 +305,8 @@ class Vector2D

double CalculateDistance (const Vector3D &a, const Vector3D &b);
double CalculateDistance (const Vector2D &a, const Vector2D &b);
double CalculateDistanceSquared (const Vector3D &a, const Vector3D &b);
double CalculateDistanceSquared (const Vector2D &a, const Vector2D &b);
std::ostream &operator << (std::ostream &os, const Vector3D &vector);
std::ostream &operator << (std::ostream &os, const Vector2D &vector);
std::istream &operator >> (std::istream &is, Vector3D &vector);
Expand Down

0 comments on commit 0f3fbff

Please sign in to comment.