from libs import * # https://iquilezles.org/articles/intersectors/ def intersect_tri(ro, rd, verts): e1 = verts[1] - verts[0] e2 = verts[2] - verts[0] to = ro - verts[0] n = np.cross(e1, e2) q = np.cross(to, rd) d = 1.0 / np.dot(rd, n) u = d * np.dot(-q, e2) v = d * np.dot(q, e1) t = d * np.dot(-n, to) if u > 0.0 and v > 0.0 and u + v < 1.0 and t > 1e-3: return t def intersect_box(ro, rd, c, s): m = 1 / rd n = m * (ro - c) k = np.abs(m) * s tn = np.max(-n - k) tf = np.min(-n + k) if tf > tn and tf > 0: return tn, tf else: return None, None