def visualize_sampling3D(self, data_dict, only_pc=False): import vtkplotter from matplotlib import cm cmap = cm.get_cmap('jet') samples = data_dict[f'samples_geo'] labels = data_dict[f'labels_geo'] colors = cmap(labels / labels.max())[:, :3] # [-1, 1] points = samples # create plot vp = vtkplotter.Plotter(title="", size=(1500, 1500)) vis_list = [] if not only_pc: # create a mesh mesh = data_dict['mesh'] faces = mesh.faces verts = mesh.verts mesh = trimesh.Trimesh(verts, faces) mesh.visual.face_colors = [200, 200, 250, 255] vis_list.append(mesh) # create a pointcloud pc = vtkplotter.Points(points, r=12, c=np.float32(colors)) vis_list.append(pc) vp.show(*vis_list, bg="white", axes=1, interactive=True)
def visualize_sampling3D(self, data_dict): import vtkplotter samples = data_dict[f'samples_geo'] labels = data_dict[f'labels_geo'] colors = np.stack([labels, labels, labels], axis=1) mesh = data_dict['mesh'] faces = mesh.faces # [-1, 1] points = samples verts = mesh.verts # create plot vp = vtkplotter.Plotter(title="", size=(1500, 1500)) vis_list = [] # create a mesh mesh = trimesh.Trimesh(verts, faces) mesh.visual.face_colors = [200, 200, 250, 255] vis_list.append(mesh) # create a pointcloud pc = vtkplotter.Points(points, r=12, c=np.float32(colors)) vis_list.append(pc) vp.show(*vis_list, bg="white", axes=1, interactive=True)
def plot_mask3D(mask=None, title="", point_coords=None, figsize=1500, point_marker_size=8, interactive=True): ''' Simple plotting tool to show intermediate mask predictions and points where PointRend is applied. Args: mask (Tensor): mask prediction of shape DxHxW title (str): title for the plot point_coords ((Tensor, Tensor, Tensor)): x and y and z point coordinates figsize (int): size of the figure to plot point_marker_size (int): marker size for points ''' import trimesh import vtkplotter from skimage import measure vp = vtkplotter.Plotter(title=title, size=(figsize, figsize)) vis_list = [] if mask is not None: mask = mask.detach().to("cpu").numpy() mask = mask.transpose(2, 1, 0) # marching cube to find surface verts, faces, normals, values = measure.marching_cubes_lewiner( mask, 0.5, gradient_direction='ascent') # create a mesh mesh = trimesh.Trimesh(verts, faces) mesh.visual.face_colors = [200, 200, 250, 100] vis_list.append(mesh) if point_coords is not None: point_coords = torch.stack(point_coords, 1).to("cpu").numpy() # import numpy as np # select_x = np.logical_and(point_coords[:, 0] >= 16, point_coords[:, 0] <= 112) # select_y = np.logical_and(point_coords[:, 1] >= 48, point_coords[:, 1] <= 272) # select_z = np.logical_and(point_coords[:, 2] >= 16, point_coords[:, 2] <= 112) # select = np.logical_and(np.logical_and(select_x, select_y), select_z) # point_coords = point_coords[select, :] pc = vtkplotter.Points(point_coords, r=point_marker_size, c='red') vis_list.append(pc) vp.show(*vis_list, bg="white", axes=1, interactive=interactive, azimuth=30, elevation=30)
def save_pts_to_vtk(pts, save_path="./test.vtk"): """ 将牙龈点pts格式转为vtk格式 @pts: 点集 [[x, y, z], [x, y, z], ...] @save_path: 保存路径 return: None """ import vtkplotter as vtkp vtk_point = vtkp.Points(pts.reshape(-1, 3)) vtkp.write(vtk_point, save_path, binary=False)
def pts_to_vtk(self): """ 将牙龈点pts格式转为vtk格式 @pts: 点集 [[x, y, z], [x, y, z], ...] @save_path: 保存路径 return: None """ import vtkplotter as vtkp save_path = os.path.join(self.show_path, os.path.basename(self.obj_path)[:-4]) if not os.path.isdir(save_path): os.makedirs(save_path) save_path = os.path.join(save_path, "test.vtk") vtk_point = vtkp.Points(self.gum_line_pts.reshape(-1, 3)) vtkp.write(vtk_point, save_path, binary=False)
a = vtk.load('tayl.stl') sc = [c[1] for c in a.getPoints()] a.pointColors(sc) b = a.ybounds() i = a.isolines(200, b[0], b[1]) i2 = a.isolines(50, b[1]*0.7, b[1]).clean() ii = i.clean() ii2 = i2.clean() pnts1 = np.array([tuple(p) for p in ii.getPoints()], dtype=[ ('x', float), ('y', float), ('z', float)]) pnts2 = np.array([tuple(p) for p in ii2.getPoints()], dtype=[ ('x', float), ('y', float), ('z', float)]) g1 = group_by(pnts1, by='y', crit=1e-3) g2 = group_by(pnts2, by='y', crit=1e-3) p.show(a, i, i2, at=0, N=2) p.show(vtk.Points(ii.getPoints()), vtk.Points(i2.getPoints()), vtk.Points(g1[10].tolist(), c='green'), vtk.Points(g1[0].tolist(), c='red'), at=1, interactive=True) N = 10 z = [] r = [] for g in g1+g2: if len(g) < 10: continue def residuals(p): rez = [] for point in g: rez.append((point['x']-p[0])**2+(point['z']-p[1])**2-p[2]**2) return rez
""" Script for visualizing subject's cortical surface and the implanted electrodes. """ import vtkplotter as vp from util.surface import Surface from util.contacts import Contacts subject = "id001" surf = Surface.from_npz_file(f"data/Geometry/{subject}/surface.npz") contacts = Contacts(f"data/Geometry/{subject}/seeg.txt") mesh = vp.Mesh([surf.vertices, surf.triangles], alpha=0.1) points = vp.Points(contacts.xyz, r=20, c=(1, 1, 0)) vplotter = vp.Plotter(axes=0) vplotter.show([mesh, points])