From 4f13d19b7ebedd4b1a607c8fb5ccbb0e322b22fd Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 7 Dec 2011 10:35:58 +0900 Subject: [PATCH] Optimize edges_intersect(). I found wolfram's line-line intersection page and noticed their equation for the time of intersection was rather different to mine. After analyzing the differences, it turns out they produce exactly the same results (when the lines are coplanar), but their method allows me to eliminate one dot product (4->3). Not only that, but it turns out that their method works equally well for skew lines (ie, non-coplanar). mine: CxA.CxA ------- CxA.AxB theirs: CxA.AxB ------- AxB.AxB --- libs/models/trace.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libs/models/trace.c b/libs/models/trace.c index c87c2463f..78ed5c798 100644 --- a/libs/models/trace.c +++ b/libs/models/trace.c @@ -127,7 +127,7 @@ edges_intersect (const vec3_t p1, const vec3_t p2, { vec3_t p, r, b; vec3_t p_r, b_p, b_r; - vec_t tp, tpd, tr, trd; + vec_t tp, tr, den; VectorSubtract (p2, p1, p); VectorSubtract (r2, r1, r); @@ -137,11 +137,10 @@ edges_intersect (const vec3_t p1, const vec3_t p2, return false; CrossProduct (b, p, b_p); CrossProduct (b, r, b_r); - tr = DotProduct (b_p, b_p); - trd = DotProduct (b_p, p_r); - tp = DotProduct (b_r, b_r); - tpd = DotProduct (b_r, p_r); - if ((tr < 0 || tr > trd) || (tp < 0 || tp > tpd)) + tr = DotProduct (b_p, p_r); + tp = DotProduct (b_r, p_r); + den = DotProduct (p_r, p_r); + if ((tr < 0 || tr > den) || (tp < 0 || tp > den)) return false; return true; }