Skip to content
Snippets Groups Projects
Commit 8e8ff314 authored by laurence's avatar laurence
Browse files

ENH: vectorTools: add cosPhi function

parent ec2b286b
No related merge requests found
...@@ -102,6 +102,21 @@ namespace vectorTools ...@@ -102,6 +102,21 @@ namespace vectorTools
return ((a & b) < 0) ? true : false; 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 //- Calculate angle between a and b in radians
template <typename T> template <typename T>
T radAngleBetween T radAngleBetween
...@@ -111,7 +126,10 @@ namespace vectorTools ...@@ -111,7 +126,10 @@ namespace vectorTools
const T& tolerance = SMALL 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 //- Calculate angle between a and b in degrees
...@@ -123,8 +141,9 @@ namespace vectorTools ...@@ -123,8 +141,9 @@ namespace vectorTools
const T& tolerance = SMALL const T& tolerance = SMALL
) )
{ {
return Foam::radToDeg(radAngleBetween(a, b, tolerance)); return radToDeg(radAngleBetween(a, b, tolerance));
} }
} // End namespace vectorTools } // End namespace vectorTools
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment