我是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 :(得分:0)
我认为不透明度与透明度混合在一起,无论如何将不透明度设置为0都不会导致可见。可以解决此问题(我将让您根据所需的外观检查不透明度所需的值):
opacityTransferFunction->AddPoint(0,0.1);
opacityTransferFunction->AddPoint(50,0.1);