Optimize dtClosestHeightPointTriangle (#364)

This commit is contained in:
云风 2018-11-09 21:52:00 +08:00 committed by Jakob Botsch Nielsen
parent 3087e805b0
commit 7ccb72b383

View File

@ -204,32 +204,31 @@ void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const floa
bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h) bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h)
{ {
float v0[3], v1[3], v2[3]; float v0[3], v1[3], v2[3];
dtVsub(v0, c,a); dtVsub(v0, c,a);
dtVsub(v1, b,a); dtVsub(v1, b,a);
dtVsub(v2, p,a); dtVsub(v2, p,a);
const float dot00 = dtVdot2D(v0, v0); // Compute scaled barycentric coordinates
const float dot01 = dtVdot2D(v0, v1); float denom = v0[0] * v1[2] - v0[2] * v1[0];
const float dot02 = dtVdot2D(v0, v2); float u = v1[2] * v2[0] - v1[0] * v2[2];
const float dot11 = dtVdot2D(v1, v1); float v = v0[0] * v2[2] - v0[2] * v2[0];
const float dot12 = dtVdot2D(v1, v2);
if (denom < 0) {
// Compute barycentric coordinates denom = -denom;
const float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01); u = -u;
const float u = (dot11 * dot02 - dot01 * dot12) * invDenom; v = -v;
const float v = (dot00 * dot12 - dot01 * dot02) * invDenom; }
// The (sloppy) epsilon is needed to allow to get height of points which // The (sloppy) epsilon is needed to allow to get height of points which
// are interpolated along the edges of the triangles. // are interpolated along the edges of the triangles.
static const float EPS = 1e-4f; float epsilon = - 1e-4f * denom;
// If point lies inside the triangle, return interpolated ycoord. // If point lies inside the triangle, return interpolated ycoord.
if (u >= -EPS && v >= -EPS && (u+v) <= 1+EPS) if (u >= epsilon && v >= epsilon && (u+v) <= denom - epsilon) {
{ h = a[1] + (v0[1]*u + v1[1]*v) / denom;
h = a[1] + v0[1]*u + v1[1]*v;
return true; return true;
} }
return false; return false;
} }