gpt4 book ai didi

c++ - VTK - 直线网格的体积渲染

转载 作者:搜寻专家 更新时间:2023-10-31 00:31:31 37 4
gpt4 key购买 nike

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

vtkRectilinearGrid Volume Rendered with Paraview

问题是……我如何在 C++ 中做到这一点? IsoVolume 过滤器属于 extension of VTK made by the ParaView guys ,所以它不在我的库中,而且我没有找到一种方法来导入这些类而不会出现链接错误。
有更容易的方法吗? VTK 中是否有可用于直线网格数据的 IsoVolume 过滤器的替代方案?

最佳答案

我自己想出来的:

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

关于c++ - VTK - 直线网格的体积渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33913044/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com