def find_intersect_point_of_lines(line_0, line_1, use_double_rounding=False, round_adjust=0, ignore_check=False): # returns an arbitrary point where the provided lines intersect. # if they do not intersect, None will be returned instead. v0, d0 = matrices.Point(line_0[0]), matrices.Ray(line_0[1]) v1, d1 = matrices.Point(line_1[0]), matrices.Ray(line_1[1]) assert len(v0) == len(v1) assert len(d0) == len(d1) if d0.is_zero or d1.is_zero: # one or both vectors are the zero-vector return None try: reduced, result = matrices.Matrix( [(a, -b) for a, b in zip(d0, d1)]).row_reduce( matrices.Matrix([v0[i] - v1[i] for i in range(len(v0))])) except matrices.CannotRowReduce: return None intersect_a = v0 - d0 * result[0][0] intersect_b = v1 - d1 * result[1][0] if ignore_check or matrices.are_vectors_equal( intersect_a, intersect_b, use_double_rounding, round_adjust): # point lies on both lines return intersect_a + (intersect_b - intersect_a) / 2 return None
def find_intersect_point_of_planes(plane_0, plane_1, plane_2, use_double_rounding=False, round_adjust=0, ignore_check=False): try: reduced, result = matrices.Matrix( (plane_0[:-1], plane_1[:-1], plane_2[:-1])).row_reduce( matrices.Matrix([plane_0[3], plane_1[3], plane_2[3]])) except matrices.CannotRowReduce: return None return matrices.Point((result[0][0], result[1][0], result[2][0]))
def point_distance_to_line(point, line, use_double_rounding=False, round_adjust=0): mantissa_len = (53 if use_double_rounding else 23) line_point = matrices.Point(line[0]) line_dir = matrices.Ray(line[1]).normalized dist = matrices.Ray.cross(line_dir, line_point - point).mag # return True if point is on forward side of plane, otherwise False # take into account rounding errors for 32bit floats delta_max = 2**( int(ceil(log(abs(dist) + float_info.epsilon, 2))) - mantissa_len) + abs(round_adjust) if abs(dist) < delta_max: # point lies on plane return 0.0 return dist