From 8e8ff31458eb35f7386d0d22103770f2070b3aee Mon Sep 17 00:00:00 2001
From: laurence <laurence>
Date: Tue, 7 Feb 2012 12:04:20 +0000
Subject: [PATCH] ENH: vectorTools: add cosPhi function

---
 .../cvMesh/vectorTools/vectorTools.H          | 23 +++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/applications/utilities/mesh/generation/cvMesh/vectorTools/vectorTools.H b/applications/utilities/mesh/generation/cvMesh/vectorTools/vectorTools.H
index 91ffae198d8..9fb39a159ba 100644
--- a/applications/utilities/mesh/generation/cvMesh/vectorTools/vectorTools.H
+++ b/applications/utilities/mesh/generation/cvMesh/vectorTools/vectorTools.H
@@ -102,6 +102,21 @@ namespace vectorTools
         return ((a & b) < 0) ? true : false;
     }
 
+    //- Calculate angle between a and b in radians
+    template <typename T>
+    T cosPhi
+    (
+        const Vector<T>& a,
+        const Vector<T>& b,
+        const T& tolerance = SMALL
+    )
+    {
+        scalar cosPhi = (a & b)/(mag(a)*mag(b) + tolerance);
+
+        // Enforce bounding between -1 and 1
+        return min(max(cosPhi, -1), 1);
+    }
+
     //- Calculate angle between a and b in radians
     template <typename T>
     T radAngleBetween
@@ -111,7 +126,10 @@ namespace vectorTools
         const T& tolerance = SMALL
     )
     {
-        return Foam::acos( (a & b)/(mag(a)*mag(b) + tolerance) );
+        scalar cosPhi = (a & b)/(mag(a)*mag(b) + tolerance);
+
+        // Enforce bounding between -1 and 1
+        return acos( min(max(cosPhi, -1), 1) );
     }
 
     //- Calculate angle between a and b in degrees
@@ -123,8 +141,9 @@ namespace vectorTools
         const T& tolerance = SMALL
     )
     {
-        return Foam::radToDeg(radAngleBetween(a, b, tolerance));
+        return radToDeg(radAngleBetween(a, b, tolerance));
     }
+
 } // End namespace vectorTools
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-- 
GitLab