def convert_grid_to_fiximage(mesh, vox_spacing, final_dims): n_tetrahedrons = mesh.number_of_cells mesh['num_tet'] = np.arange( n_tetrahedrons) + 1 ## each tetrahedron num is added 1++++ # Dmat for lambda computation #Ds = compute_D(mesh) #Ds_lin = Ds.reshape([-1, 16]) #mesh['Ds'] = Ds_lin # Displacement estimated #U = extract_u_for_grid2(mesh, graph) #U_reshape = U.reshape([-1, 12]) #mesh['U'] = U_reshape new_grid = polyv.create_grid(mesh, dimensions=final_dims) #if adapt_origin: # origin = padding * vox_spacing #else: origin = np.array([0., 0., 0.]) new_grid.SetOrigin(origin) new_grid.SetSpacing(vox_spacing) image_resampled = new_grid.sample(mesh) return image_resampled, origin # , [start_at,ends_at]
def sample_to_volume(sample, resolution=32, center: tuple = (0., 0., 0.)): grid = pv.create_grid(pv.Cube(center=center), dimensions=(resolution, resolution, resolution)) grid["recon"] = sample.flatten(order='F') grid.set_active_scalars("recon") # grid.plot(volume=True, show_grid=True) return grid
# surface mesh via the :func:`pyvista.DataSet.Filters.clip_surface` filter. # This will triangulate/tessellate the mesh geometries along the clip. clipped = dataset.clip_surface(surface, invert=False) # Visualize the results p = pv.Plotter() p.add_mesh(surface, color='w', opacity=0.75, label='Surface') p.add_mesh(clipped, color='gold', show_edges=True, label="clipped", opacity=0.75) p.add_legend() p.enable_depth_peeling() p.show() ############################################################################### # Here is another example of clipping a mesh by a surface. This time, we'll # generate a :class:`pyvista.UniformGrid` around a topography surface and then # clip that grid using the surface to create a closed 3D model of the surface surface = examples.load_random_hills() # Create a grid around that surface grid = pv.create_grid(surface) # Clip the grid using the surface model = grid.clip_surface(surface) # Compute height and display it model.elevation().plot()
def open_mesh(self): """ add a mesh to the pyqt frame """ global int_surface, ext_surface, mesh_vol, mesh global x_range, y_range, z_range, Vol_centroid global open_mesh_run global mesh_name # track pre-processing starting time open_mesh_start = time.time() # open file file_info = QtWidgets.QFileDialog.getOpenFileName() file_path = file_info[0] # determine file type and if conversion needed _, file_name = os.path.split(file_path) mesh_name, mesh_type = os.path.splitext(file_name) # read mesh & transform according to principal axes print(file_path) pre = trimesh.load(file_path) orient = pre.principal_inertia_transform pre = pre.apply_transform(orient) post_file_path = 'data/'+ mesh_name + '_oriented.stl' pre.export(post_file_path) ext_surface = pv.read(post_file_path) # scale meshes accordingly if mesh_name == 'elephant': ext_surface.points *= 12 # Elephant elif mesh_name == 'Bracket S24D1': ext_surface.points /= 10 # Bracket elif mesh_name == 'knight': ext_surface.points /= 2 # Knight # create internal offset thickness = 0.1 # inches grid = pv.create_grid(ext_surface).triangulate() solid = grid.clip_surface(ext_surface) solid.compute_implicit_distance(ext_surface, inplace=True) imp_dis_max = max(solid['implicit_distance']) shell_threshold = imp_dis_max - thickness shell = solid.clip_scalar('implicit_distance', value = shell_threshold) int_surface = shell.extract_geometry() meshfix = mf.MeshFix(int_surface) meshfix.repair(verbose=True) mesh = solid.clip_surface(int_surface, invert=False) # print mesh info print("Mesh Name:", mesh_name) print("Mesh Type:", mesh_type[1:]) # find mesh centroid and translate the mesh so that's the origin Vol_centroid = np.array([0,0,0]) self.skewed_centroid_action.setCheckable(True) # reset plotter self.reset_plotter(Vol_centroid) # find the max and min of x,y,z axes of mesh ranges = mesh.bounds x_range = abs(ranges[0] - ranges[1]) y_range = abs(ranges[2] - ranges[3]) z_range = abs(ranges[4] - ranges[5]) print("x:", float(format(x_range, ".2f")), "in") print("y:", float(format(y_range, ".2f")), "in") print("z:", float(format(z_range, ".2f")), "in") # mesh volume mesh_vol = float(format(mesh.volume, ".2f")) print("Mesh Volume:", mesh_vol, "in^3") # track pre-processing ending time & duration open_mesh_end = time.time() open_mesh_run = open_mesh_end - open_mesh_start print("Pre-Processing run time: %g seconds" % (open_mesh_run)) print("Mesh Cells:", mesh.n_cells)
p.show() ############################################################################### # Run the algorithm and plot the result result = mesh.sample(data_to_probe) # Plot result name = "Spatial Point Data" result.plot(scalars=name, clim=data_to_probe.get_data_range(name)) ############################################################################### # Complex Resample # ++++++++++++++++ # Take a volume of data and create a grid of lower resolution to resample on data_to_probe = examples.download_embryo() mesh = pv.create_grid(data_to_probe, dimensions=(75, 75, 75)) result = mesh.sample(data_to_probe) ############################################################################### threshold = lambda m: m.threshold(15.0) cpos = [(468.9075585873713, -152.8280322856109, 152.13046602188035), (121.65121514580106, 140.29327609542105, 112.28137570357188), (-0.10881224951051659, 0.006229357618166009, 0.9940428006178236)] dargs = dict(clim=data_to_probe.get_data_range(), cmap='rainbow') p = pv.Plotter(shape=(1,2)) p.add_mesh(threshold(data_to_probe), **dargs) p.subplot(0,1) p.add_mesh(threshold(result), **dargs) p.link_views()
def resample_uniform_grid(infile, dimensions=(100,100,100), radius=2): data = pv.read(infile) uniform = pv.create_grid(data,dimensions).interpolate(data, radius) return uniform