def calculateCentroid(point_on_plane, plane_normal, cuboid_dimensions): ''' Takes a description of a plane as a point on the plane and a normal of the plane with a cuboids dimensions, with one corner defined by [0, 0, 0] and the opposite by cuboid_dimensions, and calculates the centroid formed by the given plane intersecting with the cuboid. ''' tol = 1e-08 dim = cuboid_dimensions # print(point_on_plane, plane_normal) axes = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] coordinate_set = [[0, 0, 0], [dim[0], 0, 0], [0, dim[1], 0], [dim[0], dim[1], 0], [0, 0, dim[2]], [dim[0], 0, dim[2]], [0, dim[1], dim[2]], [dim[0], dim[1], dim[2]]] ipts = [] for axis in axes: den = dot(axis, plane_normal) if abs(den) < tol: continue for corner in coordinate_set: num = dot(sub(point_on_plane, corner), plane_normal) d = num / den ipt = add(mult(axis, d), corner) # check if all intersections are valid, taking care to be aware of minus signs. ipt_0 = checkRange(ipt[0], 0.0, dim[0]) ipt_1 = checkRange(ipt[1], 0.0, dim[1]) ipt_2 = checkRange(ipt[2], 0.0, dim[2]) if ipt_0 and ipt_1 and ipt_2: ipts.append(ipt) unique_ipts = [] for p in ipts: insert = True for u in unique_ipts: vdiff = sub(p, u) if sqrt(dot(vdiff, vdiff)) < tol: insert = False if insert: unique_ipts.append(p) ca = CentroidAlgorithm(unique_ipts) # wa = WeiszfeldsAlgorithm(unique_ipts) plane_centre = ca.compute() return plane_centre
def boundCoordinatesToCuboid(pt1, pt2, cuboid_dimensions): ''' Takes two points and a cuboids dimensions, with one corner defined by [0, 0, 0] and the opposite by cuboid_dimensions, and returns a point that is inside the cuboid. pt2 *must* be inside the cuboid. ''' bounded_pt = pt1[:] axes = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] coordinate_set = [[0, 0, 0], [cuboid_dimensions[0], cuboid_dimensions[1], cuboid_dimensions[2]]] outside = [False, False, False] for i, axis in enumerate(axes): v1 = sub(pt1, coordinate_set[0]) v2 = sub(pt1, coordinate_set[1]) dir1 = dot(v1, axis) dir2 = dot(v2, axis) if copysign(1, dir1) == copysign(1, dir2): if copysign(1, dir1) < 0: outside[i] = 1 else: outside[i] = 2 if any(outside): indices = [i for i, x in enumerate(outside) if x] ipt = None for index in indices: if outside[index] == 1: ipt = calculateLinePlaneIntersection(pt1, pt2, coordinate_set[0], axes[index]) else: ipt = calculateLinePlaneIntersection(pt1, pt2, coordinate_set[1], axes[index]) ipt_0 = checkRange(ipt[0], coordinate_set[0][0], coordinate_set[1][0]) ipt_1 = checkRange(ipt[1], coordinate_set[0][1], coordinate_set[1][1]) ipt_2 = checkRange(ipt[2], coordinate_set[0][2], coordinate_set[1][2]) if ipt_0 and ipt_1 and ipt_2: bounded_pt = ipt return bounded_pt
def boundCoordinatesToCuboid(pt1, pt2, cuboid_dimensions): ''' Takes two points and a cuboids dimensions, with one corner defined by [0, 0, 0] and the opposite by cuboid_dimensions, and returns a point that is inside the cuboid. pt2 *must* be inside the cuboid. ''' bounded_pt = pt1[:] axes = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] coordinate_set = [[0, 0, 0], [cuboid_dimensions[0], cuboid_dimensions[1], cuboid_dimensions[2]]] outside = [False, False, False] for i, axis in enumerate(axes): v1 = sub(pt1, coordinate_set[0]) v2 = sub(pt1, coordinate_set[1]) dir1 = dot(v1, axis) dir2 = dot(v2, axis) if copysign(1, dir1) == copysign(1, dir2): if copysign(1, dir1) < 0: outside[i] = 1 else: outside[i] = 2 if any(outside): indices = [i for i, x in enumerate(outside) if x] for index in indices: if outside[index] == 1: ipt = calculateLinePlaneIntersection(pt1, pt2, coordinate_set[0], axes[index]) else: ipt = calculateLinePlaneIntersection(pt1, pt2, coordinate_set[1], axes[index]) ipt_0 = checkRange(ipt[0], coordinate_set[0][0], coordinate_set[1][0]) ipt_1 = checkRange(ipt[1], coordinate_set[0][1], coordinate_set[1][1]) ipt_2 = checkRange(ipt[2], coordinate_set[0][2], coordinate_set[1][2]) if ipt_0 and ipt_1 and ipt_2: bounded_pt = ipt return bounded_pt