How should we do volume rendering for this simple data file in VTK?

Hi. I am new to VTK and trying to write a simple code for volumetric rendering. I used examples from the VTK website to write this following code.

#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;
}

I am using the following data file.

# 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

I tried using this file in paraview and it worked perfectly. I was able to produce a nicely rendered 3D object. But, with the above VTK code, I only get a black screen with no volumetric plot. I created a simple points plot using PolyDataMapper in VTK for the same data and that worked fine as well. I am not sure what to do here. Any help would be really appreciated. Best regards.

As this line is setting the opacity of both points to 0.0, the volume becomes transparent.

  vtkNew<vtkPiecewiseFunction> opacityTransferFunction;
  opacityTransferFunction->AddPoint(0,0.0);
  opacityTransferFunction->AddPoint(50,0.0);

You should set opacity greater than 0.0. For example:

  opacityTransferFunction->AddPoint(0,0.0);
  opacityTransferFunction->AddPoint(50,1.0);
1 Like

Kenichiro, thank you for the help. This solved the problem for me.