|
| 1 | +import os |
| 2 | +import numpy as np |
| 3 | +import open3d as o3d |
| 4 | +import torch |
| 5 | +import sys |
| 6 | + |
| 7 | +from tqdm import tqdm |
| 8 | + |
| 9 | +from ..data_types.branch import BranchSkeleton |
| 10 | +from ..util.mesh.geometries import o3d_cloud, o3d_path, o3d_sphere, o3d_tube_mesh |
| 11 | +from ..util.misc import flatten_list |
| 12 | +from .graph import nn |
| 13 | +from ..util.visualizer.view import o3d_viewer |
| 14 | + |
| 15 | + |
| 16 | +def trace_route(preds, idx, end_points): |
| 17 | + # cpu_preds = preds.cpu().numpy() |
| 18 | + path = [] |
| 19 | + |
| 20 | + while idx >= 0 and idx not in end_points: |
| 21 | + path.append(idx) |
| 22 | + idx = preds[idx] |
| 23 | + |
| 24 | + return preds.new_tensor(path, dtype=torch.long).flip(0) |
| 25 | + |
| 26 | + |
| 27 | +def select_path_points( |
| 28 | + points: torch.tensor, path_verts: torch.tensor, radii: torch.tensor |
| 29 | +): |
| 30 | + """ |
| 31 | + Finds points nearest to a path (specified by points with radii). |
| 32 | + points: (N, 3) 3d points of point cloud |
| 33 | + path_verts: (M, 3) 3d points of path |
| 34 | + radii: (M, 1) radii of path |
| 35 | + returns: (X, 2) index tuples of (path, point) for X points falling within the path, ordered by path index |
| 36 | + """ |
| 37 | + |
| 38 | + point_path, dists, _ = nn( |
| 39 | + points, |
| 40 | + path_verts, |
| 41 | + r=radii.max().item(), |
| 42 | + ) # nearest path idx for each point |
| 43 | + valid = dists[point_path >= 0] < radii[point_path[point_path >= 0]].squeeze( |
| 44 | + 1 |
| 45 | + ) # where the path idx is less than the distance to the point |
| 46 | + |
| 47 | + on_path = point_path.new_zeros(point_path.shape, dtype=torch.bool) |
| 48 | + on_path[point_path >= 0] = valid # points that are on the path. |
| 49 | + |
| 50 | + idx_point = on_path.nonzero().squeeze(1) |
| 51 | + idx_path = point_path[idx_point] |
| 52 | + |
| 53 | + order = torch.argsort(idx_path) |
| 54 | + return idx_point[order], idx_path[order] |
| 55 | + |
| 56 | + |
| 57 | +def find_branch_parent(idx_pt, idx_lookup): |
| 58 | + """Finds which branch pt is from ... |
| 59 | + so we can work out the branch parent...""" |
| 60 | + for _id, _idxs in idx_lookup.items(): |
| 61 | + if idx_pt in _idxs: |
| 62 | + return int(_id) |
| 63 | + return -1 |
| 64 | + |
| 65 | + |
| 66 | +def sample_tree( |
| 67 | + medial_pts, |
| 68 | + medial_radii, |
| 69 | + preds, |
| 70 | + distances, |
| 71 | + all_points, |
| 72 | + root_idx=0, |
| 73 | + visualize=False, |
| 74 | + pbar=None, |
| 75 | +): |
| 76 | + """ |
| 77 | + Medial Points: NN estimated medial points |
| 78 | + Medial Radii: NN estimated radii of points |
| 79 | + Preds: Predecessor of each medial point (on path to root node) |
| 80 | + Distance: Distance from root node to medial points |
| 81 | + Surface Points: The point the medial pts got projected from.. |
| 82 | + """ |
| 83 | + |
| 84 | + selection_mask = preds > 0 |
| 85 | + distances[~selection_mask] = -1 |
| 86 | + |
| 87 | + end_points = torch.tensor([], device=torch.device("cuda")) |
| 88 | + |
| 89 | + branch_id = 0 |
| 90 | + |
| 91 | + idx_lookup = {} |
| 92 | + branches = {} |
| 93 | + |
| 94 | + while True: |
| 95 | + farthest = distances.argmax().item() |
| 96 | + |
| 97 | + if distances[farthest] <= 0: |
| 98 | + break |
| 99 | + |
| 100 | + if pbar: |
| 101 | + pts_sampled = f"{100 * (1.0 - ((distances > 0).sum().item() / medial_pts.shape[0])):.2f}" |
| 102 | + pbar.set_postfix_str(f"Sampling Graph: {pts_sampled} %") |
| 103 | + |
| 104 | + path_vertices_idx = trace_route( |
| 105 | + preds, |
| 106 | + farthest, |
| 107 | + end_points, |
| 108 | + ) |
| 109 | + |
| 110 | + idx_points, idx_path = select_path_points( |
| 111 | + medial_pts, |
| 112 | + medial_pts[path_vertices_idx], |
| 113 | + medial_radii[path_vertices_idx], |
| 114 | + ) |
| 115 | + |
| 116 | + distances[idx_points] = -1 |
| 117 | + distances[idx_path] = -1 |
| 118 | + |
| 119 | + end_points = torch.unique(torch.cat((end_points, idx_points, idx_path))) |
| 120 | + |
| 121 | + if len(path_vertices_idx) > 1: |
| 122 | + branches[branch_id] = BranchSkeleton( |
| 123 | + branch_id, |
| 124 | + xyz=medial_pts[path_vertices_idx].cpu().numpy(), |
| 125 | + radii=medial_radii[path_vertices_idx].cpu().numpy(), |
| 126 | + parent_id=find_branch_parent(int(path_vertices_idx[0]), idx_lookup), |
| 127 | + child_id=-1, |
| 128 | + ) |
| 129 | + |
| 130 | + idx_lookup[branch_id] = ( |
| 131 | + path_vertices_idx.cpu().tolist() + idx_points.cpu().tolist() |
| 132 | + ) |
| 133 | + branch_id += 1 |
| 134 | + |
| 135 | + return branches |
0 commit comments