From 20724d9994afe39723299b85da58da3e63277b78 Mon Sep 17 00:00:00 2001 From: Nicolas Toussaint Date: Wed, 18 Oct 2023 09:32:43 +0100 Subject: [PATCH] barycentric cooredinate calculation correction --- include/sdf/sdf.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include/sdf/sdf.hpp b/include/sdf/sdf.hpp index 0c386a1..0b5fdbd 100644 --- a/include/sdf/sdf.hpp +++ b/include/sdf/sdf.hpp @@ -92,16 +92,17 @@ Eigen::Matrix bary( const Eigen::Ref>& c, const Eigen::Ref>& normal, float area_abc) { - float area_pbc = normal.dot((b - p).cross(c - p)); - float area_pca = normal.dot((c - p).cross(a - p)); + Eigen::Matrix u = b - a; + Eigen::Matrix v = c - a; + Eigen::Matrix w = p - a; + Eigen::Matrix n = normal; Eigen::Matrix uvw; - uvw.x() = area_pbc / area_abc; - uvw.y() = area_pca / area_abc; - uvw.z() = T(1.0) - uvw.x() - uvw.y(); + uvw[2] = u.cross(w).dot(n) / (area_abc + T(1e-5)); + uvw[1] = w.cross(v).dot(n) / (area_abc + T(1e-5)); + uvw[0] = T(1.0) - uvw[1] - uvw[2]; - return uvw; -} + return uvw;} template // 3D point to triangle shortest distance SQUARED