首页 > 解决方案 > 我们应该如何在 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 为相同的数据创建了一个简单的点图,并且效果也很好。我不确定这段代码有什么问题以及我应该如何纠正它。任何帮助将非常感激。

标签: vtk

解决方案


我认为混合不透明度和透明度,在任何情况下将不透明度设置为 0 都会导致不可见。这可以解决它(我会让您根据您期望的外观检查不透明度所需的值):

  opacityTransferFunction->AddPoint(0,0.1);
  opacityTransferFunction->AddPoint(50,0.1);

推荐阅读