From 3e5056144d38aeb05911cf74921eb0657a8e5f96 Mon Sep 17 00:00:00 2001 From: Andrew Heather <> Date: Thu, 17 Jun 2021 10:50:27 +0100 Subject: [PATCH] ENH: Vector2D - added isClose function --- src/OpenFOAM/primitives/Vector2D/Vector2D.H | 9 +++++++-- src/OpenFOAM/primitives/Vector2D/Vector2DI.H | 13 ++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/OpenFOAM/primitives/Vector2D/Vector2D.H b/src/OpenFOAM/primitives/Vector2D/Vector2D.H index 9b67b6187c3..e15434bc6a0 100644 --- a/src/OpenFOAM/primitives/Vector2D/Vector2D.H +++ b/src/OpenFOAM/primitives/Vector2D/Vector2D.H @@ -116,13 +116,18 @@ public: //- Access to the vector y component inline Cmpt& y(); - //- Normalise the vector by its magnitude inline Vector2D<Cmpt>& normalise(); - //- Perp dot product (dot product with perpendicular vector) inline scalar perp(const Vector2D<Cmpt>& b) const; + + //- Return true if vector is within tol + inline bool isClose + ( + const Vector2D<Cmpt>& b, + const scalar tol = 1e-10 + ) const; }; diff --git a/src/OpenFOAM/primitives/Vector2D/Vector2DI.H b/src/OpenFOAM/primitives/Vector2D/Vector2DI.H index b7387191cec..0eaa0b92e8f 100644 --- a/src/OpenFOAM/primitives/Vector2D/Vector2DI.H +++ b/src/OpenFOAM/primitives/Vector2D/Vector2DI.H @@ -126,7 +126,18 @@ operator&(const Vector2D<Cmpt>& v1, const Vector2D<Cmpt>& v2) template<class Cmpt> inline scalar Vector2D<Cmpt>::perp(const Vector2D<Cmpt>& b) const { - return x()*b.y()-y()*b.x(); + return x()*b.y() - y()*b.x(); +} + + +template<class Cmpt> +inline bool Vector2D<Cmpt>::isClose +( + const Vector2D<Cmpt>& b, + const scalar tol +) const +{ + return (mag(x() - b.x()) < tol && mag(y() - b.y()) < tol); } -- GitLab