一:主要的知识点
1、说明
本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①vtkExplicitStructuredGridToUnstructuredGrid结构化网格转换为非结构化网格
二:代码及注释
import vtkmodules.vtkRenderingOpenGL2 import vtkmodules.vtkInteractionStyle from vtkmodules.vtkCommonCore import vtkPoints from vtkmodules.vtkCommonDataModel import vtkCellArray, vtkExplicitStructuredGrid import numpy as np from vtkmodules.vtkFiltersCore import ( vtkExplicitStructuredGridToUnstructuredGrid, vtkUnstructuredGridToExplicitStructuredGrid ) from vtkmodules.vtkRenderingCore import ( vtkActor, vtkDataSetMapper, vtkRenderWindow, vtkRenderWindowInteractor, vtkRenderer ) from vtkmodules.vtkCommonColor import vtkNamedColors def convert_to_unstructured_grid(grid): """ vtkExplicitStructuredGridToUnstructuredGrid 将一个显式结构化网格(vtkExplicitStructuredGrid)转换为一个标准的非结构化网格(vtkUnstructuredGrid) 这种转换是因为兼容性,可以利用VTK最为广泛的现有过滤器库,也可以进行数据简化 """ converter = vtkExplicitStructuredGridToUnstructuredGrid() converter.SetInputData(grid) converter.Update() return converter.GetOutput() def convert_to_explicit_structured_grid(grid): """ vtkUnstructuredGridToExplicitStructuredGrid 将一个非结构化网格(vtkUnstructuredGrid, USGrid)转换回一个显式结构化网格(vtkExplicitStructuredGrid, ESGrid) """ converter = vtkUnstructuredGridToExplicitStructuredGrid() converter.SetInputData(grid) """ 该过滤器依赖于输入 USGrid 的**单元数据(Cell Data)**中存在三个特殊的数组。这些数组定义了 USGrid 中每个单元在目标 ESGrid 中的 I,J,K 索引 """ converter.SetInputArrayToProcess(0, 0, 0, 1, 'BLOCK_I') converter.SetInputArrayToProcess(1, 0, 0, 1, 'BLOCK_J') converter.SetInputArrayToProcess(2, 0, 0, 1, 'BLOCK_K') converter.Update() return converter.GetOutput() def main(): # 创建一个结构模型 dimensions = (5, 6, 7) spacing = (20, 10, 1) ni, nj, nk = dimensions si, sj, sk = spacing points = vtkPoints() for z in range(0, nk * sk, sk): for y in range(0, nj * sj, sj): for x in range(0, ni * si, si): points.InsertNextPoint((x, y, z)) cells = vtkCellArray() for k in range(0, nk - 1): for j in range(0, nj - 1): for i in range(0, ni - 1): multi_index = ([i, i + 1, i + 1, i, i, i + 1, i + 1, i], [j, j, j + 1, j + 1, j, j, j + 1, j + 1], [k, k, k, k, k + 1, k + 1, k + 1, k + 1]) pts = np.ravel_multi_index(multi_index, dimensions, order='F') cells.InsertNextCell(8, pts) """ vtkExplicitStructuredGrid 是 VTK 中一种用于表示 **3D 结构化数据集(Structured Data Set)**的高级数据结构 它在传统的结构化网格(如 vtkStructuredGrid)和非结构化网格(vtkUnstructuredGrid)之间提供了一个非常灵活的桥梁 1. 传统的结构化网格(如 vtkStructuredGrid) 隐式拓扑(Implicit Topology):你只需要定义网格的点坐标和维度(I×J×K)。 连接关系:单元(Cell)的连接关系是自动且固定的。例如,点 (i,j,k) 总是连接到 (i+1,j,k) 和 (i,j+1,k) 等等。 限制:由于连接是隐式的,这种网格必须是拓扑上的矩形体,不能包含内部空腔或断裂。 2. 显式结构化网格 (vtkExplicitStructuredGrid) 显式拓扑(Explicit Topology):它保留了 I×J×K 的维度信息(结构化特性)。 连接关系:它显式地存储了每个单元(Cell)由哪些点组成,就像非结构化网格一样。 简单来说:ESGrid 仍然具有 I,J,K 索引的便利性(结构化),但它通过显式定义连接关系,打破了传统结构化网格必须是拓扑矩形的限制 """ grid = vtkExplicitStructuredGrid() """ 构建ESGrid的要素,必须提供三个核心组件 维度: 必须定义网格的I*J*K的尺寸 点坐标 单元连接:一个 vtkCellArray,它显式地列出构成每个单元(通常是六面体)的 8 个点的全局索引 """ grid.SetDimensions(ni, nj, nk) grid.SetPoints(points) grid.SetCells(cells) # 转换为非结构化网格 grid = convert_to_unstructured_grid(grid) # 由非结构化网格,转化为显示结构化网格 grid = convert_to_explicit_structured_grid(grid) mapper = vtkDataSetMapper() mapper.SetInputData(grid) colors = vtkNamedColors() actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().EdgeVisibilityOn() actor.GetProperty().LightingOff() actor.GetProperty().SetColor(colors.GetColor3d('Seashell')) renderer = vtkRenderer() renderer.AddActor(actor) renderer.SetBackground(colors.GetColor3d('DarkSlateGray')) window = vtkRenderWindow() window.AddRenderer(renderer) window.SetWindowName('CreateESGrid') window.SetSize(1024, 768) window.Render() camera = renderer.GetActiveCamera() camera.SetPosition(8.383354, -72.468670, 94.262605) camera.SetFocalPoint(42.295234, 21.111537, -0.863606) camera.SetViewUp(0.152863, 0.676710, 0.720206) camera.SetDistance(137.681759) camera.SetClippingRange(78.173985, 211.583658) interactor = vtkRenderWindowInteractor() interactor.SetRenderWindow(window) interactor.SetInteractorStyle(vtkInteractorStyleRubberBandPick()) window.Render() interactor.Start() if __name__ == '__main__': main()