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 <vtkFiniteDifferenceGradientEstimator.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 <vtkStructuredPoints.h>
#include <vtkStructuredPointsReader.h>
#include <vtkVolume.h>
#include <vtkVolumeProperty.h>

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.3 FATAL_ERROR)

project(MinIntensityRendering)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkIOLegacy
  vtkImagingCore
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  vtkRenderingVolume
  vtkRenderingVolumeOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping MinIntensityRendering: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(MinIntensityRendering MACOSX_BUNDLE MinIntensityRendering.cxx )
  target_link_libraries(MinIntensityRendering PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  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}
    )
endif ()

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.