vtk - 我们应该如何在 VTK 中对这个简单的数据文件进行体绘制?
问题描述
我是 VTK 的新手,正在尝试为体积渲染编写一个简单的代码。我使用 VTK 网站上的示例来编写以下代码。
#include <vtkNamedColors.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkNew.h>
#include <vtkStructuredPointsReader.h>
#include <vtkPiecewiseFunction.h>
#include <vtkSmartVolumeMapper.h>
#include <vtkColorTransferFunction.h>
#include <vtkVolume.h>
#include <vtkVolumeProperty.h>
int main( int argc, char* argv[] )
{
// Add named color library
vtkNew<vtkNamedColors> colors ;
// Create renderer
vtkNew<vtkRenderer> renderer ;
// Create a new render window
vtkNew<vtkRenderWindow> renWin ;
renWin->AddRenderer(renderer);
// Make the render window interacting
vtkNew<vtkRenderWindowInteractor> iren ;
iren->SetRenderWindow(renWin);
//Create a Structure Points or Image data reader and read the data
vtkNew<vtkStructuredPointsReader> reader;
reader->SetFileName (argv[1]);
reader->Update();
// For volume rendering, we need to first define a map from scalar values to
// colors and opacity (transparency) values. Then add these maps to volume
// property
// Add a piece-wise function for color transfer functions. Piece-wise means
// adding control (interpolation) points.
vtkNew<vtkPiecewiseFunction> opacityTransferFunction;
opacityTransferFunction->AddPoint(0,0.0);
opacityTransferFunction->AddPoint(50,0.0);
// Piece-wise function cannot be used for colors because colors are vectors
vtkNew<vtkColorTransferFunction> colorTransferFunction;
colorTransferFunction->AddRGBPoint(0,0.0,0.0,1.0);
colorTransferFunction->AddRGBPoint(25,1.0,0.0,0.0);
colorTransferFunction->AddRGBPoint(50,1.0,1.0,1.0);
// Set volume rendering properties
vtkNew<vtkVolumeProperty> volumeProperty;
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->ShadeOn();
volumeProperty->SetInterpolationTypeToLinear();
// Add a mapper to create graphic primitives from the data
vtkNew<vtkSmartVolumeMapper> mapper;
mapper->SetBlendModeToComposite();
mapper->SetInputConnection(reader->GetOutputPort());
// Create a new actor(the actual graphics object) and add the mapped data to
// it
vtkNew<vtkVolume> volume;
volume->SetMapper(mapper);
volume->SetProperty(volumeProperty);
// Add the volume actor to the renderer
renderer->AddVolume(volume);
// Set the background color
renderer->SetBackground(colors->GetColor3d("Black").GetData());
// Set the size of the render window
renWin->SetSize(512,512);
// Render the data
renWin->Render();
// Start the interactor
iren->Start();
return EXIT_SUCCESS;
}
我正在使用以下数据文件。
# vtk DataFile Version 3.0
First time trying vtk import \n
ASCII
DATASET STRUCTURED_POINTS
DIMENSIONS 3 4 6
ORIGIN 0 0 0
SPACING 1 1 1
POINT_DATA 72
SCALARS volume_scalars unsigned_char 1
LOOKUP_TABLE default
0 0 0 0 0 0 0 0 0 0 0 0
0 5 10 15 20 25 25 20 15 10 5 0
0 10 20 30 40 50 50 40 30 20 10 0
0 10 20 30 40 50 50 40 30 20 10 0
0 5 10 15 20 25 25 20 15 10 5 0
0 0 0 0 0 0 0 0 0 0 0 0
我尝试在 paraview 中使用这个文件,它工作得很好。我能够制作出精美渲染的 3D 对象。但是,使用上面的 VTK 代码,我只得到一个没有体积图的黑屏。我使用 VTK 中的 PolyDataMapper 为相同的数据创建了一个简单的点图,并且效果也很好。我不确定这段代码有什么问题以及我应该如何纠正它。任何帮助将非常感激。
解决方案
我认为混合不透明度和透明度,在任何情况下将不透明度设置为 0 都会导致不可见。这可以解决它(我会让您根据您期望的外观检查不透明度所需的值):
opacityTransferFunction->AddPoint(0,0.1);
opacityTransferFunction->AddPoint(50,0.1);
推荐阅读
- java - 闪屏漏内存怎么解决?
- c++ - Qt Creator 无法自动完成标准 C++
- python - 查看 Python 脚本发出的 POST 请求的标头和正文
- wordpress - 如何解决 Wordpress 中的 Elementor 插入错误?
- php - 如何使用 php 和 ajax 使用函数插入文件和其他输入类型
- django - django-filter 搜索多个字段和模型
- javascript - BootstrapVue 的组件无法在某些 .vue 文件中读取
- c# - 从接收到的字节中轮询数据时无休止的while循环
- karate - 空手道匹配每个响应断言未能识别响应中缺少的键
- javascript - Mongodb聚合以对数组中最常见的项目进行排序?