将 3D 数组(矢量)转换为 vtk 非结构化网格
Convert a 3D array (vector) to vtk unstructured mesh
我有一堆黑白图像,作为包含 0 和 1 的 3D 矢量存储在我的 C++ 代码中。我想将这个 3D 矢量转换为 vtk 非结构化网格,每个体素都是一个正交元素。有没有图书馆可以做到这一点
可以使用 vtk
将体素作为基本单元来创建非结构化网格。检查此 document 以提供有关其他可用单元格类型的详细信息。要创建这样的网格,您应该解析向量的内容并创建每个体素,然后将其添加到网格中,如以下 python 代码所示(现在没有可用于测试的 C++,但语法很容易移植) :
import vtk
voxelPoints = vtk.vtkPoints()
voxelPoints.SetNumberOfPoints(8)
voxelPoints.InsertPoint(0, 0, 0, 0)
voxelPoints.InsertPoint(1, 1, 0, 0)
voxelPoints.InsertPoint(2, 0, 1, 0)
voxelPoints.InsertPoint(3, 1, 1, 0)
voxelPoints.InsertPoint(4, 0, 0, 1)
voxelPoints.InsertPoint(5, 1, 0, 1)
voxelPoints.InsertPoint(6, 0, 1, 1)
voxelPoints.InsertPoint(7, 1, 1, 1)
aVoxel = vtk.vtkVoxel()
aVoxel.GetPointIds().SetId(0, 0)
aVoxel.GetPointIds().SetId(1, 1)
aVoxel.GetPointIds().SetId(2, 2)
aVoxel.GetPointIds().SetId(3, 3)
aVoxel.GetPointIds().SetId(4, 4)
aVoxel.GetPointIds().SetId(5, 5)
aVoxel.GetPointIds().SetId(6, 6)
aVoxel.GetPointIds().SetId(7, 7)
aVoxelGrid = vtk.vtkUnstructuredGrid()
aVoxelGrid.InsertNextCell(aVoxel.GetCellType(), aVoxel.GetPointIds())
aVoxelGrid.SetPoints(voxelPoints)
aVoxelMapper = vtk.vtkDataSetMapper()
aVoxelMapper.SetInputData(aVoxelGrid)
aVoxelActor = vtk.vtkActor()
aVoxelActor.SetMapper(aVoxelMapper)
aVoxelActor.GetProperty().SetDiffuseColor(1, 0, 0)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(aVoxelActor)
# Render the scene and start interaction.
iren.Initialize()
renWin.Render()
iren.Start()
我有一堆黑白图像,作为包含 0 和 1 的 3D 矢量存储在我的 C++ 代码中。我想将这个 3D 矢量转换为 vtk 非结构化网格,每个体素都是一个正交元素。有没有图书馆可以做到这一点
可以使用 vtk
将体素作为基本单元来创建非结构化网格。检查此 document 以提供有关其他可用单元格类型的详细信息。要创建这样的网格,您应该解析向量的内容并创建每个体素,然后将其添加到网格中,如以下 python 代码所示(现在没有可用于测试的 C++,但语法很容易移植) :
import vtk
voxelPoints = vtk.vtkPoints()
voxelPoints.SetNumberOfPoints(8)
voxelPoints.InsertPoint(0, 0, 0, 0)
voxelPoints.InsertPoint(1, 1, 0, 0)
voxelPoints.InsertPoint(2, 0, 1, 0)
voxelPoints.InsertPoint(3, 1, 1, 0)
voxelPoints.InsertPoint(4, 0, 0, 1)
voxelPoints.InsertPoint(5, 1, 0, 1)
voxelPoints.InsertPoint(6, 0, 1, 1)
voxelPoints.InsertPoint(7, 1, 1, 1)
aVoxel = vtk.vtkVoxel()
aVoxel.GetPointIds().SetId(0, 0)
aVoxel.GetPointIds().SetId(1, 1)
aVoxel.GetPointIds().SetId(2, 2)
aVoxel.GetPointIds().SetId(3, 3)
aVoxel.GetPointIds().SetId(4, 4)
aVoxel.GetPointIds().SetId(5, 5)
aVoxel.GetPointIds().SetId(6, 6)
aVoxel.GetPointIds().SetId(7, 7)
aVoxelGrid = vtk.vtkUnstructuredGrid()
aVoxelGrid.InsertNextCell(aVoxel.GetCellType(), aVoxel.GetPointIds())
aVoxelGrid.SetPoints(voxelPoints)
aVoxelMapper = vtk.vtkDataSetMapper()
aVoxelMapper.SetInputData(aVoxelGrid)
aVoxelActor = vtk.vtkActor()
aVoxelActor.SetMapper(aVoxelMapper)
aVoxelActor.GetProperty().SetDiffuseColor(1, 0, 0)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(aVoxelActor)
# Render the scene and start interaction.
iren.Initialize()
renWin.Render()
iren.Start()