Skip to content


# MinIntensityRendering

VTKEx/Cxx/VolumeRendering/MinIntensityRendering

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

MinIntensityRendering.cxx

    #include <vtkSmartPointer.h>
    #include <vtkCamera.h>
    #include <vtkFiniteDifferenceGradientEstimator.h>
    #include <vtkImageClip.h>
    #include <vtkPiecewiseFunction.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkStructuredPoints.h>
    #include <vtkStructuredPointsReader.h>
    #include <vtkVolume.h>
    #include <vtkVolumeProperty.h>
    #include <vtkFixedPointVolumeRayCastMapper.h>
    #include <vtkColorTransferFunction.h>

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

      std::string filename = argv[1]; //  "/Data/ironProt.vtk";

      // Create the renderers, render window, and interactor
      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      iren->SetRenderWindow(renWin);
      vtkSmartPointer<vtkRenderer> ren =
        vtkSmartPointer<vtkRenderer>::New();
      renWin->AddRenderer(ren);

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

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

      vtkSmartPointer<vtkColorTransferFunction> cTFun =
        vtkSmartPointer<vtkColorTransferFunction>::New();
      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
      vtkSmartPointer<vtkImageClip> clip =
        vtkSmartPointer<vtkImageClip>::New();
      clip->SetInputConnection( reader->GetOutputPort() );
      clip->SetOutputWholeExtent(0,66,0,66,30,37);
      clip->ClipDataOn();

      vtkSmartPointer<vtkVolumeProperty> property =
        vtkSmartPointer<vtkVolumeProperty>::New();
      property->SetScalarOpacity(oTFun);
      property->SetColor(cTFun);
      property->SetInterpolationTypeToLinear();

      vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> mapper =
        vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
      mapper->SetBlendModeToMinimumIntensity();
      mapper->SetInputConnection( clip->GetOutputPort() );

      vtkSmartPointer<vtkVolume> volume =
        vtkSmartPointer<vtkVolume>::New();
      volume->SetMapper(mapper);
      volume->SetProperty(property);

      ren->AddViewProp(volume);
      renWin->Render();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(MinIntensityRendering)

    find_package(VTK COMPONENTS
      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.