RayCastIsosurface
VTKExamples/Cxx/VolumeRendering/RayCastIsosurface
Description¶
This examples show how volume rendering can produce isosurface-like images. Using vtkOpenGLGPUVolumeRayCastMapper with an isosurface blend mode, two isosurfaces are created using appropriate transfer functions. The effect is similar to what is shown in MedicalDemo2. The user can specify the .mhd file and the two isosurface values. The example uses 500 and 1150 for the two isosurface values. The volume rendering "surfaces" are fuzzier than the hard surfaces created by vtkMarchingCubes in MedicalDemo3.
Usage
RayCastIsosurface FullHead.mhd 500 1150
Info
The example uses FullHead.mhd which references FullHead.raw.gz.
Code¶
RayCastIsosurface.cxx
#include "vtkSmartPointer.h" #include "vtkOpenGLGPUVolumeRayCastMapper.h" #include "vtkPiecewiseFunction.h" #include "vtkColorTransferFunction.h" #include "vtkContourValues.h" #include "vtkInteractorStyleTrackballCamera.h" #include "vtkMetaImageReader.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkRenderer.h" #include "vtkCamera.h" #include "vtkVolume.h" #include "vtkVolumeProperty.h" #include "vtkNamedColors.h" int main (int argc, char *argv[]) { double iso1 = 500.0; double iso2 = 1150.0; if (argc < 2) { std::cout << "Usage: " << argv[0] << " file.mnd [iso1=500] [iso2=1150]" << std::endl; return EXIT_FAILURE; } vtkSmartPointer<vtkMetaImageReader> reader = vtkSmartPointer<vtkMetaImageReader>::New(); reader->SetFileName(argv[1]); auto colors = vtkSmartPointer<vtkNamedColors>::New(); auto mapper = vtkSmartPointer<vtkOpenGLGPUVolumeRayCastMapper>::New(); mapper->SetInputConnection(reader->GetOutputPort()); mapper->AutoAdjustSampleDistancesOff(); mapper->SetSampleDistance(0.5); mapper->SetBlendModeToIsoSurface(); if (argc > 3) { iso1 = atof(argv[2]); iso2 = atof(argv[3]); } auto colorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New(); colorTransferFunction->RemoveAllPoints(); colorTransferFunction->AddRGBPoint( iso2, colors->GetColor3d("ivory").GetData()[0], colors->GetColor3d("ivory").GetData()[1], colors->GetColor3d("ivory").GetData()[2]); colorTransferFunction->AddRGBPoint( iso1, colors->GetColor3d("flesh").GetData()[0], colors->GetColor3d("flesh").GetData()[1], colors->GetColor3d("flesh").GetData()[2]); auto scalarOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New(); scalarOpacity->AddPoint(iso1, .3); scalarOpacity->AddPoint(iso2, 0.6); auto volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New(); volumeProperty->ShadeOn(); volumeProperty->SetInterpolationTypeToLinear(); volumeProperty->SetColor(colorTransferFunction); volumeProperty->SetScalarOpacity(scalarOpacity); auto volume = vtkSmartPointer<vtkVolume>::New(); volume->SetMapper(mapper); volume->SetProperty(volumeProperty); auto renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddVolume(volume); renderer->SetBackground(colors->GetColor3d("cornflower").GetData()); renderer->ResetCamera(); auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->SetSize(800, 600); renderWindow->AddRenderer(renderer); auto style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); interactor->SetRenderWindow(renderWindow); interactor->SetInteractorStyle(style); // Add some contour values to draw iso surfaces volumeProperty->GetIsoSurfaceValues()->SetValue(0, iso1); volumeProperty->GetIsoSurfaceValues()->SetValue(1, iso2); // Generate a good view vtkSmartPointer<vtkCamera> aCamera = vtkSmartPointer<vtkCamera>::New(); aCamera->SetViewUp (0, 0, -1); aCamera->SetPosition (0, -1, 0); aCamera->SetFocalPoint (0, 0, 0); renderer->SetActiveCamera(aCamera); renderer->ResetCamera(); aCamera->Azimuth(30.0); aCamera->Elevation(30.0); aCamera->Dolly(1.5); renderer->ResetCameraClippingRange (); renderWindow->Render(); interactor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(RayCastIsosurface) find_package(VTK COMPONENTS vtkCommonColor vtkCommonCore vtkCommonDataModel vtkCommonMisc vtkIOImage vtkInteractionStyle vtkRenderingContextOpenGL2 vtkRenderingCore vtkRenderingFreeType vtkRenderingGL2PSOpenGL2 vtkRenderingOpenGL2 vtkRenderingVolumeOpenGL2 QUIET) if (NOT VTK_FOUND) message("Skipping RayCastIsosurface: ${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(RayCastIsosurface MACOSX_BUNDLE RayCastIsosurface.cxx ) target_link_libraries(RayCastIsosurface PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(RayCastIsosurface MACOSX_BUNDLE RayCastIsosurface.cxx ) target_link_libraries(RayCastIsosurface PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS RayCastIsosurface MODULES ${VTK_LIBRARIES} ) endif ()
Download and Build RayCastIsosurface¶
Click here to download RayCastIsosurface and its CMakeLists.txt file. Once the tarball RayCastIsosurface.tar has been downloaded and extracted,
cd RayCastIsosurface/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:
./RayCastIsosurface
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.