From fe8e02c9972063267d1e8a8a66fcc5917bcad75f Mon Sep 17 00:00:00 2001 From: Nigel Barber Date: Mon, 16 Apr 2018 20:47:12 +0100 Subject: [PATCH] Replace calcAngle() with inCircle() to avoid expensive trigonometry --- Source/geom.c | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/Source/geom.c b/Source/geom.c index 97a5046..b2fce6b 100755 --- a/Source/geom.c +++ b/Source/geom.c @@ -260,30 +260,34 @@ void tesedgeIntersect( TESSvertex *o1, TESSvertex *d1, v->t = Interpolate( z1, o2->t, z2, d2->t ); } } - -/* - Calculate the angle between v1-v2 and v1-v0 - */ -TESSreal calcAngle( TESSvertex *v0, TESSvertex *v1, TESSvertex *v2 ) -{ - TESSreal num, den, ax, ay, bx, by; - ax = v2->s - v1->s; - ay = v2->t - v1->t; - bx = v0->s - v1->s; - by = v0->t - v1->t; - num = ax * bx + ay * by; - den = sqrt( ax * ax + ay * ay ) * sqrt( bx * bx + by * by ); - if ( den > 0.0 ) num /= den; - if ( num < -1.0 ) num = -1.0; - if ( num > 1.0 ) num = 1.0; - return acos( num ); -} + +TESSreal inCircle( TESSvertex *v, TESSvertex *v0, TESSvertex *v1, TESSvertex *v2 ) { + TESSreal adx, ady, bdx, bdy, cdx, cdy; + TESSreal abdet, bcdet, cadet; + TESSreal alift, blift, clift; + + adx = v0->s - v->s; + ady = v0->t - v->t; + bdx = v1->s - v->s; + bdy = v1->t - v->t; + cdx = v2->s - v->s; + cdy = v2->t - v->t; + + abdet = adx * bdy - bdx * ady; + bcdet = bdx * cdy - cdx * bdy; + cadet = cdx * ady - adx * cdy; + + alift = adx * adx + ady * ady; + blift = bdx * bdx + bdy * bdy; + clift = cdx * cdx + cdy * cdy; + + return alift * bcdet + blift * cadet + clift * abdet; +} /* Returns 1 is edge is locally delaunay */ int tesedgeIsLocallyDelaunay( TESShalfEdge *e ) -{ - return (calcAngle(e->Lnext->Org, e->Lnext->Lnext->Org, e->Org) + - calcAngle(e->Sym->Lnext->Org, e->Sym->Lnext->Lnext->Org, e->Sym->Org)) < (M_PI + 0.01); +{ + return inCircle(e->Sym->Lnext->Lnext->Org, e->Lnext->Org, e->Lnext->Lnext->Org, e->Org) < 0; }