简体   繁体   English

VTK-直线网格的体绘制

[英]VTK - Volume Rendering from a Rectilinear Grid

I'm developing a C++ application that uses VTK for some visualization. 我正在开发使用VTK进行可视化的C ++应用程序。 I need to read a .vtr file (RectilinearGrid) and render it using VTK volume rendering features. 我需要读取.vtr文件(RectilinearGrid)并使用VTK体积渲染功能对其进行渲染。
I tried to do that with Paraview and it was VERY easy, I just had to apply a "IsoVolume" Filter and the result is just what I needed: 我尝试使用Paraview做到这一点 ,这非常容易,我只需要应用“ IsoVolume”过滤器,结果就是我所需要的:

使用Paraview渲染的vtkRectilinearGrid体积

The problem is... how do I do that in C++? 问题是...如何在C ++中做到这一点? The IsoVolume Filter belongs to an extension of VTK made by the ParaView guys , so it is not in my libraries and I didn't find a way to import these classes without having linking errors. IsoVolume过滤器属于ParaView家伙制作的VTK扩展 ,因此它不在我的库中,并且我没有找到没有链接错误即可导入这些类的方法。
Is there an easier way? 有更容易的方法吗? Is there an alternative to the IsoVolume Filter inside VTK that works with rectilinear grid data? VTK中是否有IsoVolume过滤器的替代品,可用于直线网格数据?

I figured it out by myself: 我自己弄清楚了:

#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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM