Skip to content

MinIntensityRendering

vtk-examples/Cxx/VolumeRendering/MinIntensityRendering

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

MinIntensityRendering.cxx

#include <vtkCamera.h>
#include <vtkColorTransferFunction.h>
#include <vtkFixedPointVolumeRayCastMapper.h>
#include <vtkImageClip.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPiecewiseFunction.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStructuredPointsReader.h>
#include <vtkVolume.h>
#include <vtkVolumeProperty.h>

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cerr << "Required arguments: vtkFile e.g. ironProt.vtk" << std::endl;
    return EXIT_FAILURE;
  }

  std::string filename = argv[1];

  vtkNew<vtkNamedColors> colors;

  // Create the renderers, render window, and interactor.
  vtkNew<vtkRenderWindow> renWin;
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);
  vtkNew<vtkRenderer> ren;
  renWin->AddRenderer(ren);
  renWin->SetWindowName("MinIntensityRendering");

  // Read the data from a vtk file
  vtkNew<vtkStructuredPointsReader> reader;
  reader->SetFileName(filename.c_str());
  reader->Update();

  // Create a transfer function mapping scalar value to opacity.
  vtkNew<vtkPiecewiseFunction> oTFun;
  oTFun->AddSegment(0, 1.0, 256, 0.1);

  vtkNew<vtkColorTransferFunction> cTFun;
  cTFun->AddRGBPoint(0, 1.0, 1.0, 1.0);
  cTFun->AddRGBPoint(255, 1.0, 1.0, 1.0);

  // Need to crop to actually see minimum intensity.
  vtkNew<vtkImageClip> clip;
  clip->SetInputConnection(reader->GetOutputPort());
  clip->SetOutputWholeExtent(0, 66, 0, 66, 30, 37);
  clip->ClipDataOn();

  vtkNew<vtkVolumeProperty> property;
  property->SetScalarOpacity(oTFun);
  property->SetColor(cTFun);
  property->SetInterpolationTypeToLinear();

  vtkNew<vtkFixedPointVolumeRayCastMapper> mapper;
  mapper->SetBlendModeToMinimumIntensity();
  mapper->SetInputConnection(clip->GetOutputPort());

  vtkNew<vtkVolume> volume;
  volume->SetMapper(mapper);
  volume->SetProperty(property);

  ren->AddViewProp(volume);
  ren->SetBackground(colors->GetColor3d("MidnightBlue").GetData());

  renWin->Render();

  ren->GetActiveCamera()->Zoom(1.3);

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(MinIntensityRendering)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  IOLegacy
  ImagingCore
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  RenderingVolume
  RenderingVolumeOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "MinIntensityRendering: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(MinIntensityRendering MACOSX_BUNDLE MinIntensityRendering.cxx )
  target_link_libraries(MinIntensityRendering PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS MinIntensityRendering
  MODULES ${VTK_LIBRARIES}
)

Download and Build MinIntensityRendering

Click here to download MinIntensityRendering and its CMakeLists.txt file. Once the tarball MinIntensityRendering.tar has been downloaded and extracted,

cd MinIntensityRendering/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./MinIntensityRendering

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.