From c8ed01b393fa0c75785ab0c2e2071042b78bd324 Mon Sep 17 00:00:00 2001 From: Niklaus Johner <nij2003@med.cornell.edu> Date: Wed, 16 Jan 2013 13:43:17 -0500 Subject: [PATCH] Protected geom::Angle(Vec2,Vec2) and geom::SignedAngle(Vec2,Vec2) against floating point errors --- modules/geom/src/vecmat2_op.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/geom/src/vecmat2_op.cc b/modules/geom/src/vecmat2_op.cc index ea0a45213..221a6bbf2 100644 --- a/modules/geom/src/vecmat2_op.cc +++ b/modules/geom/src/vecmat2_op.cc @@ -50,7 +50,7 @@ Mat2 Invert(const Mat2& m) Real Angle(const Vec2& v1, const Vec2& v2) { - return std::acos(Dot(v1,v2)/Length(v1)/Length(v2)); + return std::acos(std::max(static_cast<Real>(-1.0),std::min(static_cast<Real>(1.0),Dot(v1,v2)/Length(v1)/Length(v2)))); } Real SignedAngle(const Vec2& v1, const Vec2& v2) @@ -63,7 +63,7 @@ Real SignedAngle(const Vec2& v1, const Vec2& v2) if(vc.z!=0.0){ sign=vc.z/std::fabs(vc.z); } - return acos(Dot(v1,v2)/Length(v1)/Length(v2))*sign; + return acos(std::max(static_cast<Real>(-1.0),std::min(static_cast<Real>(1.0),Dot(v1,v2)/Length(v1)/Length(v2))))*sign; } Vec2 Rotate(const Vec2& v,Real ang) -- GitLab