|
10 | 10 | from scipy.spatial import cKDTree |
11 | 11 |
|
12 | 12 | from . import util |
| 13 | +from . import bounds |
| 14 | +from . import transformations |
| 15 | + |
13 | 16 | from .transformations import transform_points |
14 | 17 |
|
15 | 18 |
|
| 19 | +def key_points(mesh, count): |
| 20 | + """ |
| 21 | + Return a combination of mesh vertices and surface samples |
| 22 | + with vertices chosen by likelyhood to be important to registation |
| 23 | + """ |
| 24 | + stack = [] |
| 25 | + if len(mesh.vertices) < (count / 2): |
| 26 | + return np.vstack(( |
| 27 | + mesh.vertices, |
| 28 | + mesh.sample(count - len(mesh.vertices)))) |
| 29 | + else: |
| 30 | + return mesh.sample(count) |
| 31 | + |
| 32 | + |
| 33 | +def mesh_other(mesh, other, samples=500, icp_first=10, icp_final=50): |
| 34 | + """ |
| 35 | + Align a mesh with another mesh or a PointCloud using |
| 36 | + the principal axes of inertia as a starting point which |
| 37 | + is refined by iterative closest point. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ------------ |
| 41 | + mesh : trimesh.Trimesh object |
| 42 | + Mesh to align with other |
| 43 | + other : trimesh.Trimesh or (n, 3) float |
| 44 | + Mesh or points in space |
| 45 | + samples : int |
| 46 | + Number of samples from mesh surface to align |
| 47 | + icp_first : int |
| 48 | + How many ICP iterations for the 9 possible |
| 49 | + combinations of |
| 50 | + icp_final : int |
| 51 | + How many ICP itertations for the closest |
| 52 | + candidate from the wider search |
| 53 | +
|
| 54 | + Returns |
| 55 | + ----------- |
| 56 | + mesh_to_other : (4, 4) float |
| 57 | + Transform to align mesh to the other object |
| 58 | + cost : float |
| 59 | + Average squared distance per point |
| 60 | + """ |
| 61 | + |
| 62 | + if not util.is_instance_named(mesh, 'Trimesh'): |
| 63 | + raise ValueError('mesh must be Trimesh object!') |
| 64 | + |
| 65 | + inverse = True |
| 66 | + search = mesh |
| 67 | + # if both are meshes use the smaller one for searching |
| 68 | + if util.is_instance_named(other, 'Trimesh'): |
| 69 | + if len(mesh.vertices) > len(other.vertices): |
| 70 | + search = other |
| 71 | + inverse = False |
| 72 | + points = key_points(mesh=mesh, |
| 73 | + count=samples) |
| 74 | + points_mesh = mesh |
| 75 | + else: |
| 76 | + points_mesh = other |
| 77 | + points = key_points(mesh=other, |
| 78 | + count=samples) |
| 79 | + |
| 80 | + if points_mesh.is_volume: |
| 81 | + points_PIT = points_mesh.principal_inertia_transform |
| 82 | + else: |
| 83 | + points_PIT = points_mesh.bounding_box_oriented.principal_inertia_transform |
| 84 | + |
| 85 | + elif util.is_shape(other, (-1, 3)): |
| 86 | + # case where other is just points |
| 87 | + points = other |
| 88 | + points_PIT = bounds.oriented_bounds(points)[0] |
| 89 | + else: |
| 90 | + raise ValueError('other must be mesh or (n, 3) points!') |
| 91 | + |
| 92 | + if search.is_volume: |
| 93 | + search_PIT = search.principal_inertia_transform |
| 94 | + else: |
| 95 | + search_PIT = search.bounding_box_oriented.principal_inertia_transform |
| 96 | + |
| 97 | + # move from mesh a to mesh b |
| 98 | + search_to_points = np.dot(np.linalg.inv(points_PIT), search_PIT) |
| 99 | + |
| 100 | + # permutations of cube rotations |
| 101 | + # the principal inertia transform has arbitrary sign |
| 102 | + # along the 3 major axis so try all combinations of |
| 103 | + # 180 degree rotations with a quick first ICP pass |
| 104 | + cubes = np.array([np.eye(4) * np.append(diag, 1) |
| 105 | + for diag in [[1, 1, 1], |
| 106 | + [1, 1, -1], |
| 107 | + [1, -1, 1], |
| 108 | + [-1, 1, 1], |
| 109 | + [-1, -1, 1], |
| 110 | + [-1, 1, -1], |
| 111 | + [1, -1, -1], |
| 112 | + [-1, -1, -1]]]) |
| 113 | + |
| 114 | + #from IPython import embed |
| 115 | + # embed() |
| 116 | + |
| 117 | + # loop through permutations and run iterative closest point on each |
| 118 | + costs, transforms = [], [] |
| 119 | + centroid = search.centroid |
| 120 | + for flip in cubes: |
| 121 | + a_to_b = np.dot(transformations.transform_around(flip, centroid), |
| 122 | + np.linalg.inv(search_to_points)) |
| 123 | + |
| 124 | + # import trimesh |
| 125 | + # vpt = trimesh.PointCloud(points) |
| 126 | + # vpt.apply_transform(a_to_b) |
| 127 | + # trimesh.Scene([search, vpt]).show() |
| 128 | + |
| 129 | + # run first pass ICP |
| 130 | + matrix, junk, cost = icp(a=points, |
| 131 | + b=search, |
| 132 | + initial=a_to_b, |
| 133 | + max_iterations=int(icp_first), |
| 134 | + scale=False) |
| 135 | + transforms.append(matrix) |
| 136 | + costs.append(cost) |
| 137 | + |
| 138 | + # run a final ICP refinement step |
| 139 | + matrix, junk, cost = icp(a=points, |
| 140 | + b=search, |
| 141 | + initial=transforms[np.argmin(costs)], |
| 142 | + max_iterations=int(icp_final), |
| 143 | + scale=False) |
| 144 | + |
| 145 | + # convert square sum distance to squared average distance |
| 146 | + cost /= len(points) |
| 147 | + |
| 148 | + if inverse: |
| 149 | + mesh_to_other = np.linalg.inv(matrix) |
| 150 | + else: |
| 151 | + mesh_to_other = matrix |
| 152 | + |
| 153 | + return mesh_to_other, cost |
| 154 | + |
| 155 | + |
16 | 156 | def procrustes(a, |
17 | 157 | b, |
18 | 158 | reflection=True, |
|
0 commit comments