From 8c2df2f70c177a46d852dd541e48b4595ff67d4f Mon Sep 17 00:00:00 2001 From: Irlan <-> Date: Wed, 28 Mar 2018 01:22:09 -0300 Subject: [PATCH] add a function to find barycentric coordinates wrt. triangle --- include/bounce/common/geometry.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/include/bounce/common/geometry.h b/include/bounce/common/geometry.h index 8cfc325..49770f3 100644 --- a/include/bounce/common/geometry.h +++ b/include/bounce/common/geometry.h @@ -99,6 +99,33 @@ inline void b3BarycentricCoordinates(float32 out[3], out[1] = -b3Dot(QA, AB); out[2] = divisor; } +// Convert a point Q from Cartesian coordinates to Barycentric coordinates (u, v, w) +// with respect to a triangle ABC. +// The last output value is the divisor. +inline void b3BarycentricCoordinates(float32 out[4], + const b3Vec3& A, const b3Vec3& B, const b3Vec3& C, + const b3Vec3& Q) +{ + b3Vec3 AB = B - A; + b3Vec3 AC = C - A; + + b3Vec3 QA = A - Q; + b3Vec3 QB = B - Q; + b3Vec3 QC = C - Q; + + b3Vec3 QB_x_QC = b3Cross(QB, QC); + b3Vec3 QC_x_QA = b3Cross(QC, QA); + b3Vec3 QA_x_QB = b3Cross(QA, QB); + + b3Vec3 AB_x_AC = b3Cross(AB, AC); + + //float32 divisor = b3Dot(AB_x_AC, AB_x_AC); + + out[0] = b3Dot(QB_x_QC, AB_x_AC); + out[1] = b3Dot(QC_x_QA, AB_x_AC); + out[2] = b3Dot(QA_x_QB, AB_x_AC); + out[3] = out[0] + out[1] + out[2]; +} // Project a point onto a segment AB. inline b3Vec3 b3ClosestPointOnSegment(const b3Vec3& P, const b3Vec3& A, const b3Vec3& B)