一:主要的知识点
1、说明
本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①将抽象的 3D 体积或拓扑数据 (vtkStructuredGrid) 转换为可被渲染器直接绘制的 2D 几何体 (vtkPolyData)
二:代码及注释
import vtkmodules.vtkRenderingOpenGL2 import vtkmodules.vtkInteractionStyle from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkCommonCore import vtkPoints from vtkmodules.vtkCommonDataModel import vtkStructuredGrid from vtkmodules.vtkFiltersGeometry import vtkStructuredGridGeometryFilter from vtkmodules.vtkRenderingCore import ( vtkActor, vtkDataSetMapper, vtkRenderWindow, vtkRenderWindowInteractor, vtkRenderer ) def main(): colors = vtkNamedColors() points = vtkPoints() grid_size = 8 counter = 0 pt_idx = 0 # Create a 5x5 grid of points for j in range(0, grid_size): for i in range(0, grid_size): if i == 3 and j == 3: # Make one point higher than the rest. points.InsertNextPoint(i, j, 2) print(f'The different point is number {counter}.') pt_idx = counter else: # Make most of the points the same height. points.InsertNextPoint(i, j, 0) counter += 1 """ vtkStructuredGrid 结构化网格中,每个单元默认连接相邻的点,例如: 一个 2D 网格(nx×ny×1)中的单元是四边形, 一个 3D 网格(nx×ny×nz)中的单元是六面体(hexahedron) 比如: Cell (i,j,k) 由 8 个点组成:这个Cell所在位置索引为(i, j, k) (i,j,k) (i+1,j,k) (i,j+1,k) (i+1,j+1,k) (i,j,k+1) (i+1,j,k+1) (i,j+1,k+1) (i+1,j+1,k+1) VTK 在内部自动根据维度推导出这些连接关系 """ structured_grid = vtkStructuredGrid() # Specify the dimensions of the grid, set the points and blank one point. structured_grid.SetDimensions(grid_size, grid_size, 1) structured_grid.SetPoints(points) # 把第 pt_idx 个点(也就是那个高起来的点)标记为无效(blanked) # 这个点或单元在可视化或计算中应当被忽略,不参与渲染或插值 structured_grid.BlankPoint(pt_idx) # 忽略掉某个cell # structured_grid.BlankCell() def is_visible(pt_num): # 判定某个点是否可见 if structured_grid.IsPointVisible(pt_num): return f'Point {pt_num:2d} is visible.' else: return f'Point {pt_num:2d} is not visible.' # Should not be visible. print(is_visible(pt_idx)) # Should be visible. print(is_visible(7)) # 将structured_grid转化为polyData """ 将抽象的 3D 体积或拓扑数据 (vtkStructuredGrid) 转换为可被渲染器直接绘制的 2D 几何体 (vtkPolyData) 这里完成两个任务 1:提取集合表面,因为结构化网格代表的是 3D 空间中的点和体素。渲染器无法直接绘制这些体素 2:执行空白化操作,当你调用 structured_grid.BlankPoint(pt_idx) 时,你只是在数据结构内部标记了一个点是不可见的 vtkStructuredGridGeometryFilter 在生成多边形时,会检查每个单元的 8 个角点。 如果一个单元(Cell)的任何一个角点被标记为“空白”(Blanked),那么这个过滤器会完全跳过这个单元,不会为它生成任何几何表面 """ geometry_filter = vtkStructuredGridGeometryFilter() geometry_filter.SetInputData(structured_grid) # Create a mapper and actor. grid_mapper = vtkDataSetMapper() grid_mapper.SetInputConnection(geometry_filter.GetOutputPort()) grid_actor = vtkActor() grid_actor.SetMapper(grid_mapper) grid_actor.GetProperty().EdgeVisibilityOn() grid_actor.GetProperty().SetEdgeColor(colors.GetColor3d('Blue')) # Visualize renderer = vtkRenderer() ren_win = vtkRenderWindow() ren_win.AddRenderer(renderer) iren = vtkRenderWindowInteractor() iren.SetRenderWindow(ren_win) renderer.AddActor(grid_actor) renderer.SetBackground(colors.GetColor3d('ForestGreen')) # ren_win.SetSize(640, 480) ren_win.SetWindowName('BlankPoint') ren_win.Render() iren.Start() if __name__ == '__main__': main()