三维像素化曲面网格(网格曲面的体元化)
创建封闭曲面网格和网格体的体元模型。
示例演示了如何从包围盒计算隐式距离。
体元化步骤
- 确定体元的大小。
- 生成vtkUnStructuredGrid
- 确定体元是否在封闭曲面内或体中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | def voxelize(mesh, density=None, check_surface=True): """Voxelize mesh to UnstructuredGrid. Parameters ---------- density : float The uniform size of the voxels. Defaults to 1/100th of the mesh length. check_surface : bool Specify whether to check the surface for closure. If on, then the algorithm first checks to see if the surface is closed and manifold. If the surface is not closed and manifold, a runtime error is raised. """ if not pyvista.is_pyvista_dataset(mesh): mesh = pyvista.wrap(mesh) if density is None: density = mesh.length / 100 x_min, x_max, y_min, y_max, z_min, z_max = mesh.bounds x = np.arange(x_min, x_max, density) y = np.arange(y_min, y_max, density) z = np.arange(z_min, z_max, density) x, y, z = np.meshgrid(x, y, z) # Create unstructured grid from the structured grid grid = pyvista.StructuredGrid(x, y, z) ugrid = pyvista.UnstructuredGrid(grid) # get part of the mesh within the mesh's bounding surface. selection = ugrid.select_enclosed_points(mesh.extract_surface(), tolerance=0.0, check_surface=check_surface) mask = selection.point_arrays['SelectedPoints'].view(np.bool) # extract cells from point indices return ugrid.extract_points(mask) |
体元赋值属性
- 简单 Cell 单元属性
- 节点属性,计算每一个体元到封闭曲面的距离(隐式距离)
- 根据隐式距离属性,抽取等值面
效果
封闭曲面:

体元化:

常值属性体元:

隐式距离属性生成等值面:

Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | """ Voxelize a Surface Mesh ~~~~~~~~~~~~~~~~~~~~~~~ Create a voxel model (like legos) of a closed surface or volumetric mesh. This example also demonstrates how to compute an implicit distance from a bounding :class:`pyvista.PolyData` surface. """ # sphinx_gallery_thumbnail_number = 2 import pyvista as pv import numpy as np # Load a surface to voxelize surface =pv.read("footbones.ply") surface ############################################################################### # cpos (list) – List of camera position, focal point, and view up. # 相机位置,焦点(朝向),顶方向 cpos = [(7.656346967151718, -9.802071079151158, -11.021236183314311), (0.2224512272564101, -0.4594554282112895, 0.5549738359311297), (-0.6279216753504941, -0.7513057097368635, 0.20311105371647392)] surface.plot(cpos=cpos, opacity=0.75) ############################################################################### # Create a voxel model of the bounding surface # voxels = pv.voxelize(surface, density=surface.length/200) p = pv.Plotter() p.add_mesh(voxels, color=True, show_edges=True, opacity=0.5) p.add_mesh(surface, color="lightblue", opacity=0.5) p.show(cpos=cpos) ############################################################################### # We could even add a scalar field to that new voxel model in case we # wanted to create grids for modelling. In this case, let's add a scalar field # for bone density noting: voxels["density"] = np.full(voxels.n_cells, 3.65) # g/cc voxels ############################################################################### voxels.plot(scalars="density", cpos=cpos) ############################################################################### # A constant scalar field is kind of boring, so let's get a little fancier by # added a scalar field that varies by the distance from the bounding surface. voxels.compute_implicit_distance(surface, inplace=True) voxels ############################################################################### contours = voxels.contour(6, scalars="implicit_distance") p = pv.Plotter() p.add_mesh(voxels, opacity=0.25, scalars="implicit_distance") p.add_mesh(contours, opacity=0.5, scalars="implicit_distance") p.show(cpos=cpos) |