def closest_point_to(self, p): """ Find distance to point p by rotating and projecting then return that closest point unrotated """ p = np.array(p) # align with z-axis so all triangle have same z-coord tri_rot, rot = self.align_with([0, 0, 1]) tri_rot_z = tri_rot.a[-1] p_rot = np.dot(rot, p) p_2d = p_rot[:2] tri_2d = geometry2d.Triangle(tri_rot.a[:2], tri_rot.b[:2], tri_rot.c[:2]) if tri_2d.is_inside(p_2d): # projects onto triangle, so return difference in z return np.dot(np.linalg.inv(rot), np.array(list(p_2d) + [tri_rot_z])) else: closest_pt_2d = tri_2d.closest_point_to(p_2d) closest_pt_3d = np.array(list(closest_pt_2d) + [tri_rot_z]) return np.dot(np.linalg.inv(rot), closest_pt_3d)
def area(self): """ :return area of the triangle """ tri_rot, rot = self.align_with([0, 0, 1]) tri_2d = geometry2d.Triangle(tri_rot.a[:2], tri_rot.b[:2], tri_rot.c[:2]) return tri_2d.area