将3D数组(矢量)转换为vtk非结构化网格

时间:2015-02-14 21:04:22

标签: c++ mesh vtk

我有一堆黑白图像,我将这些图像存储在我的c ++代码中,作为包含0和1的3D矢量。我想将这个3D矢量转换为vtk非结构化网格,每个体素是一个正交元素。有没有可以做到的图书馆

1 个答案:

答案 0 :(得分:0)

可以使用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()
相关问题