SmartVolumeMapper
vtk-examples/Cxx/VolumeRendering/SmartVolumeMapper
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
SmartVolumeMapper.cxx
#include <vtkCamera.h>
#include <vtkColorTransferFunction.h>
#include <vtkDataArray.h>
#include <vtkImageData.h>
#include <vtkImageShiftScale.h>
#include <vtkNew.h>
#include <vtkPiecewiseFunction.h>
#include <vtkPointData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSampleFunction.h>
#include <vtkSmartVolumeMapper.h>
#include <vtkSphere.h>
#include <vtkVolumeProperty.h>
#include <vtkXMLImageDataReader.h>
namespace {
void CreateImageData(vtkImageData* im);
}
int main(int argc, char* argv[])
{
vtkNew<vtkImageData> imageData;
if (argc < 2)
{
CreateImageData(imageData);
}
else
{
vtkNew<vtkXMLImageDataReader> reader;
reader->SetFileName(argv[1]);
reader->Update();
imageData->ShallowCopy(reader->GetOutput());
}
vtkNew<vtkRenderWindow> renWin;
vtkNew<vtkRenderer> ren1;
ren1->SetBackground(0.1, 0.4, 0.2);
renWin->AddRenderer(ren1);
renWin->SetSize(301, 300); // intentional odd and NPOT width/height
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
renWin->Render(); // make sure we have an OpenGL context.
vtkNew<vtkSmartVolumeMapper> volumeMapper;
volumeMapper->SetBlendModeToComposite(); // composite first
volumeMapper->SetInputData(imageData);
vtkNew<vtkVolumeProperty> volumeProperty;
volumeProperty->ShadeOff();
volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
vtkNew<vtkPiecewiseFunction> compositeOpacity;
compositeOpacity->AddPoint(0.0, 0.0);
compositeOpacity->AddPoint(80.0, 1.0);
compositeOpacity->AddPoint(80.1, 0.0);
compositeOpacity->AddPoint(255.0, 0.0);
volumeProperty->SetScalarOpacity(compositeOpacity); // composite first.
vtkNew<vtkColorTransferFunction> color;
color->AddRGBPoint(0.0, 0.0, 0.0, 1.0);
color->AddRGBPoint(40.0, 1.0, 0.0, 0.0);
color->AddRGBPoint(255.0, 1.0, 1.0, 1.0);
volumeProperty->SetColor(color);
vtkNew<vtkVolume> volume;
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
ren1->AddViewProp(volume);
ren1->ResetCamera();
// Render composite. In default mode. For coverage.
renWin->Render();
// 3D texture mode. For coverage.
#if !defined(VTK_LEGACY_REMOVE) && !defined(VTK_OPENGL2)
volumeMapper->SetRequestedRenderModeToRayCastAndTexture();
#endif // VTK_LEGACY_REMOVE
renWin->Render();
// Software mode, for coverage. It also makes sure we will get the same
// regression image on all platforms.
volumeMapper->SetRequestedRenderModeToRayCast();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
namespace {
void CreateImageData(vtkImageData* imageData)
{
// Create a spherical implicit function.
vtkNew<vtkSphere> sphere;
sphere->SetRadius(0.1);
sphere->SetCenter(0.0, 0.0, 0.0);
vtkNew<vtkSampleFunction> sampleFunction;
sampleFunction->SetImplicitFunction(sphere);
sampleFunction->SetOutputScalarTypeToDouble();
sampleFunction->SetSampleDimensions(127, 127,
127); // intentional NPOT dimensions.
sampleFunction->SetModelBounds(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
sampleFunction->SetCapping(false);
sampleFunction->SetComputeNormals(false);
sampleFunction->SetScalarArrayName("values");
sampleFunction->Update();
vtkDataArray* a =
sampleFunction->GetOutput()->GetPointData()->GetScalars("values");
double range[2];
a->GetRange(range);
vtkNew<vtkImageShiftScale> t;
t->SetInputConnection(sampleFunction->GetOutputPort());
t->SetShift(-range[0]);
double magnitude = range[1] - range[0];
if (magnitude == 0.0)
{
magnitude = 1.0;
}
t->SetScale(255.0 / magnitude);
t->SetOutputScalarTypeToUnsignedChar();
t->Update();
imageData->ShallowCopy(t->GetOutput());
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(SmartVolumeMapper)
find_package(VTK COMPONENTS
CommonCore
CommonDataModel
IOXML
ImagingCore
ImagingHybrid
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
RenderingVolumeOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "SmartVolumeMapper: 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(SmartVolumeMapper MACOSX_BUNDLE SmartVolumeMapper.cxx )
target_link_libraries(SmartVolumeMapper PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS SmartVolumeMapper
MODULES ${VTK_LIBRARIES}
)
Download and Build SmartVolumeMapper¶
Click here to download SmartVolumeMapper and its CMakeLists.txt file. Once the tarball SmartVolumeMapper.tar has been downloaded and extracted,
cd SmartVolumeMapper/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:
./SmartVolumeMapper
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.