VTK - 来自直线网格的体绘制

时间:2015-11-25 09:40:25

标签: c++ visual-studio-2010 vtk paraview volume-rendering

我正在开发一个使用VTK进行可视化的C ++应用程序。我需要读取一个.vtr文件(RectilinearGrid)并使用VTK体积渲染功能渲染它。
我尝试用Paraview做到这一点非常简单,我只需要应用“IsoVolume”过滤,结果正是我所需要的:

vtkRectilinearGrid Volume Rendered with Paraview

问题是......我怎么用C ++做到这一点? IsoVolume过滤器属于extension of VTK made by the ParaView guys,因此它不在我的库中,我没有找到导入这些类的方法而没有链接错误。
有没有更简单的方法? VTK中的IsoVolume滤波器是否可以替代直线网格数据?

1 个答案:

答案 0 :(得分:4)

我自己想出来了:

#define vtkp vtkSmartPointer

// Read the file.
vtkp<vtkXMLRectilinearGridReader> vtrReader = vtkp<vtkXMLRectilinearGridReader>::New();
    vtrReader->SetFileName("../models/my_grid_model.vtr");
    vtrReader->Update();
vtkp<vtkRectilinearGrid> grid = vtrReader->GetOutput();
vtkp<vtkDataArray> scalars = grid->GetPointData()->GetArray("temperatures");

// Convert the vtkRectilinearGrid to vtkUnstructuredGrid.
vtkp<vtkUnstructuredGrid> ugrid = vtkp<vtkUnstructuredGrid>::New();
    vtkp<vtkPoints> points = vtkp<vtkPoints>::New();
        grid->GetPoints(points);
    ugrid->SetPoints(points);
    ugrid->GetPointData()->SetScalars(scalars);

for (unsigned int i = 0; i < grid->GetNumberOfCells(); i++){
    vtkCell* cell = grid->GetCell(i);
    ugrid->InsertNextCell(cell->GetCellType(), cell->GetPointIds());
}

// Make sure we have only tetrahedra. (may be slow for big data. tip: save the result to a file)
vtkp<vtkDataSetTriangleFilter> trifilter = vtkp<vtkDataSetTriangleFilter>::New();
    trifilter->SetInputData(ugrid);

// The mapper that renders the volume data.
vtkp<vtkOpenGLProjectedTetrahedraMapper> volumeMapper = vtkp<vtkOpenGLProjectedTetrahedraMapper>::New();
    volumeMapper->SetInputConnection(trifilter->GetOutputPort());

// Create transfer mapping scalar value to opacity.
vtkp<vtkPiecewiseFunction> opacityTransferFunction = vtkp<vtkPiecewiseFunction>::New();
    opacityTransferFunction->AddPoint(range[0], 0.05);
    opacityTransferFunction->AddPoint(range[1], 0.5);

// Create transfer mapping scalar value to color.
vtkp<vtkColorTransferFunction> colorTransferFunction = vtkp<vtkColorTransferFunction>::New();
    colorTransferFunction->AddRGBPoint(range[0], 0.0, 0.0, 1.0);
    colorTransferFunction->AddRGBPoint(range[1], 1.0, 0.0, 0.0);

// The property describes how the data will look.
vtkp<vtkVolumeProperty> volumeProperty = vtkp<vtkVolumeProperty>::New();
    volumeProperty->SetColor(colorTransferFunction);
    volumeProperty->SetScalarOpacity(opacityTransferFunction);
    volumeProperty->SetScalarOpacityUnitDistance(300);
    volumeProperty->ShadeOff();

// Creation of the volume.
vtkp<vtkVolume> volume = vtkp<vtkVolume>::New();
    volume->SetMapper(volumeMapper);
    volume->SetProperty(volumeProperty);

// Usual visualization.
vtkp<vtkRenderer> renderer = vtkp<vtkRenderer>::New();
    renderer->AddVolume(volume);
vtkp<vtkRenderWindow> window = vtkp<vtkRenderWindow>::New();
    window->Render();
vtkp<vtkInteractorStyleTrackballCamera> style = vtkp<vtkInteractorStyleTrackballCamera>::New();
vtkp<vtkRenderWindowInteractor> interactor = vtkRenderWindowInteractor::New();
    interactor->SetRenderWindow(window);
    interactor->SetInteractorStyle(style);
    interactor->Initialize();
    interactor->Start();
相关问题