From 41a64ab4708c338293722d298536953d1a1ab05a Mon Sep 17 00:00:00 2001 From: Michael Krayer Date: Thu, 12 Aug 2021 10:34:56 +0200 Subject: [PATCH] switched to another formulation of the algorithm which computes surface normal of triangle: this is necessary for inside/outside check --- field.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/field.py b/field.py index 6806d15..07a0d84 100644 --- a/field.py +++ b/field.py @@ -934,23 +934,20 @@ class Features3d: # @staticmethod # def ray_triangle_intersect(self): - def ray_triangle_intersect(r0,dr,v0,v1,v2): - v0v1 = v1-v0 - v0v2 = v2-v0 - pvec = np.cross(dr,v0v2) - det = np.dot(v0v1,pvec) - if np.abs(det)<1e-6: - return float('-inf') - invDet = 1.0 / det - tvec = r0-v0 - u = np.dot(tvec,pvec)*invDet - if u<0 or u>1: - return float('-inf') - qvec = np.cross(tvec,v0v1) - v = np.dot(dr,qvec)*invDet - if v<0 or u+v>1: - return float('-inf') - return np.dot(v0v2,qvec)*invDet + def ray_triangle_intersect(R,dR,A,B,C): + E1 = B-A + E2 = C-A + N = np.cross(E1,E2) + det = -np.dot(dR,N) + invdet = 1.0/det + AO = R-A + DAO = np.cross(AO,dR) + # u,v,1-u-v are the barycentric coordinates of intersection + u = np.dot(E2,DAO)*invdet + v = -np.dot(E1,DAO)*invdet + # Intersection point is R+t*dR + t = np.dot(AO,N)*invdet + return (t,t*np.dot(N,dR)) if (np.abs(det) >= 1e-6 and u >= 0.0 and v >= 0.0 and (u+v) <= 1.0) else None def clean_points(self,report=False): nfaces_ = self._faces.shape[0]