From fcadbed56f8414b90b359e9658814c498dcae2a4 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Sat, 2 Apr 2016 02:50:32 +0200 Subject: [PATCH] Fix issue in function to compute triangle barycentric coordinates --- src/mathematics/mathematics_functions.cpp | 2 +- .../mathematics/TestMathematicsFunctions.h | 22 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/mathematics/mathematics_functions.cpp b/src/mathematics/mathematics_functions.cpp index 54aa0878..1e13a0f1 100644 --- a/src/mathematics/mathematics_functions.cpp +++ b/src/mathematics/mathematics_functions.cpp @@ -47,7 +47,7 @@ void reactphysics3d::computeBarycentricCoordinatesInTriangle(const Vector3& a, c decimal denom = d00 * d11 - d01 * d01; v = (d11 * d20 - d01 * d21) / denom; w = (d00 * d21 - d01 * d20) / denom; - u = decimal(1.0) - u - w; + u = decimal(1.0) - v - w; } // Clamp a vector such that it is no longer than a given maximum length diff --git a/test/tests/mathematics/TestMathematicsFunctions.h b/test/tests/mathematics/TestMathematicsFunctions.h index f7cb56e5..1939efc2 100644 --- a/test/tests/mathematics/TestMathematicsFunctions.h +++ b/test/tests/mathematics/TestMathematicsFunctions.h @@ -107,19 +107,23 @@ class TestMathematicsFunctions : public Test { Vector3 a(0, 0, 0); Vector3 b(5, 0, 0); Vector3 c(0, 0, 5); + Vector3 testPoint(4, 0, 1); decimal u,v,w; computeBarycentricCoordinatesInTriangle(a, b, c, a, u, v, w); - test(approxEqual(u, 1.0, 0.0001)); - test(approxEqual(v, 0.0, 0.0001)); - test(approxEqual(w, 0.0, 0.0001)); + test(approxEqual(u, 1.0, 0.000001)); + test(approxEqual(v, 0.0, 0.000001)); + test(approxEqual(w, 0.0, 0.000001)); computeBarycentricCoordinatesInTriangle(a, b, c, b, u, v, w); - test(approxEqual(u, 0.0, 0.0001)); - test(approxEqual(v, 1.0, 0.0001)); - test(approxEqual(w, 0.0, 0.0001)); + test(approxEqual(u, 0.0, 0.000001)); + test(approxEqual(v, 1.0, 0.000001)); + test(approxEqual(w, 0.0, 0.000001)); computeBarycentricCoordinatesInTriangle(a, b, c, c, u, v, w); - test(approxEqual(u, 0.0, 0.0001)); - test(approxEqual(v, 0.0, 0.0001)); - test(approxEqual(w, 1.0, 0.0001)); + test(approxEqual(u, 0.0, 0.000001)); + test(approxEqual(v, 0.0, 0.000001)); + test(approxEqual(w, 1.0, 0.000001)); + + computeBarycentricCoordinatesInTriangle(a, b, c, testPoint, u, v, w); + test(approxEqual(u + v + w, 1.0, 0.000001)); } };