Skip to content

Commit

Permalink
Base: Vector3::GetAngle() uses a smaller epsilon
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Apr 6, 2021
1 parent 373e3a6 commit 69aa355
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/Base/Vector3D.cpp
Expand Up @@ -22,6 +22,7 @@


#include "PreCompiled.h"
#include <limits>
#include "Tools.h"
#include "Vector3D.h"

Expand Down Expand Up @@ -418,21 +419,21 @@ Vector3<_Precision> & Vector3<_Precision>::Normalize (void)
template <class _Precision>
_Precision Vector3<_Precision>::GetAngle (const Vector3 &rcVect) const
{
_Precision divid, fNum;
_Precision len1 = Length();
_Precision len2 = rcVect.Length();
if (len1 <= traits_type::epsilon() || len2 <= traits_type::epsilon())
return std::numeric_limits<_Precision>::quiet_NaN(); // division by zero

divid = Length() * ((Vector3<_Precision>&)rcVect).Length();
_Precision dot = Dot(rcVect);
dot /= len1;
dot /= len2;

if ((divid < -1e-10f) || (divid > 1e-10f)) {
fNum = (*this * rcVect) / divid;
if (fNum < -1)
return traits_type::pi();
else if (fNum > 1)
return 0.0F;
else
return _Precision(acos(fNum));
}
else
return traits_type::maximum(); // division by zero
if (dot <= -1.0)
return traits_type::pi();
else if (dot >= 1.0)
return 0.0;

return _Precision(acos(dot));
}

template <class _Precision>
Expand Down
12 changes: 12 additions & 0 deletions src/Mod/Test/BaseTests.py
Expand Up @@ -156,6 +156,18 @@ def testString(self):
self.TestPar.RemString("44")
self.failUnless(self.TestPar.GetString("44","hallo") == "hallo","Deletion error at String")

def testAngle(self):
v1 = FreeCAD.Vector(0,0,0.000001)
v2 = FreeCAD.Vector(0,0.000001,0)
self.assertAlmostEqual(v1.getAngle(v2), math.pi/2)
self.assertAlmostEqual(v2.getAngle(v1), math.pi/2)

def testAngleWithNullVector(self):
v1 = FreeCAD.Vector(0,0,0)
v2 = FreeCAD.Vector(0,1,0)
self.assertTrue(math.isnan(v1.getAngle(v2)))
self.assertTrue(math.isnan(v2.getAngle(v1)))

def testMatrix(self):
m=FreeCAD.Matrix(4,2,1,0,1,1,1,0,0,0,1,0,0,0,0,1)
u=m.multiply(m.inverse())
Expand Down

0 comments on commit 69aa355

Please sign in to comment.