def same_side(p1, p2, a, b): """ Definition to figure which side a point is on with respect to a line and a point. See http://www.blackpawn.com/texts/pointinpoly/ for more. If p1 and p2 are on the sameside, this definition returns True. Parameters ---------- p1 : list Point(s) to check. p2 : list This is the point check against. a : list First point that forms the line. b : list Second point that forms the line. """ ba = np.subtract(b, a) p1a = np.subtract(p1, a) p2a = np.subtract(p2, a) cp1 = np.cross(ba, p1a) cp2 = np.cross(ba, p2a) test = np.dot(cp1, cp2) if len(p1.shape) > 1: return test >= 0 if test >= 0: return True return False
def find_nearest_points(ray0, ray1): """ Find the nearest points on given rays with respect to the other ray. Parameters ---------- ray0 : ndarray A ray. ray1 : ndarray A ray. Returns ---------- c0 : ndarray Closest point on ray0. c1 : ndarray Closest point on ray1. """ p0 = ray0[0].reshape(3, ) d0 = ray0[1].reshape(3, ) p1 = ray1[0].reshape(3, ) d1 = ray1[1].reshape(3, ) n = np.cross(d0, d1) if np.all(n) == 0: point, distances = calculate_intersection_of_two_rays(ray0, ray1) c0 = c1 = point else: n0 = np.cross(d0, n) n1 = np.cross(d1, n) c0 = p0 + (np.dot((p1 - p0), n1) / np.dot(d0, n1)) * d0 c1 = p1 + (np.dot((p0 - p1), n0) / np.dot(d1, n0)) * d1 return c0, c1
def get_triangle_normal(triangle, triangle_center=None): """ Definition to calculate surface normal of a triangle. Parameters ---------- triangle : ndarray Set of points in X,Y and Z to define a planar surface (3,3). It can also be list of triangles (mx3x3). triangle_center : ndarray Center point of the given triangle. See odak.raytracing.center_of_triangle for more. In many scenarios you can accelerate things by precomputing triangle centers. Returns ---------- normal : ndarray Surface normal at the point of intersection. """ triangle = np.asarray(triangle) if len(triangle.shape) == 2: triangle = triangle.reshape((1, 3, 3)) normal = np.zeros((triangle.shape[0], 2, 3)) direction = np.cross(triangle[:, 0] - triangle[:, 1], triangle[:, 2] - triangle[:, 1]) if type(triangle_center) == type(None): normal[:, 0] = center_of_triangle(triangle) else: normal[:, 0] = triangle_center normal[:, 1] = direction / np.sum(direction, axis=1)[0] if normal.shape[0] == 1: normal = normal.reshape((2, 3)) return normal
def cross_product(vector1, vector2): """ Definition to cross product two vectors and return the resultant vector. Used method described under: http://en.wikipedia.org/wiki/Cross_product Parameters ---------- vector1 : ndarray A vector/ray. vector2 : ndarray A vector/ray. Returns ---------- ray : ndarray Array that contains starting points and cosines of a created ray. """ angle = np.cross(vector1[1].T, vector2[1].T) angle = np.asarray(angle) ray = np.array([vector1[0], angle], dtype=np.float) return ray
def point_to_ray_distance(point, ray_point_0, ray_point_1): """ Definition to find point's closest distance to a line represented with two points. Parameters ---------- point : ndarray Point to be tested. ray_point_0 : ndarray First point to represent a line. ray_point_1 : ndarray Second point to represent a line. Returns ---------- distance : float Calculated distance. """ distance = np.sum( np.cross((point - ray_point_0), (point - ray_point_1))**2) / np.sum( (ray_point_1 - ray_point_0)**2) return distance