Skip to content

Instantly share code, notes, and snippets.

@waldyrious

waldyrious/CMakeLists.txt

Last active Oct 19, 2020
Embed
What would you like to do?
VTK Hello World: display a 2D image

This is a basic example of how the VTK pipeline works in practice.

It is set up with the minimum of components. You'll need (besides VTK and a C++ compiler, of course):

  • CMake to run CMakeLists.txt and configure the build
  • a "lenna.jpg" file, placed in the same folder as the source files
cmake_minimum_required (VERSION 3.0)
project(VtkJpegViewer)
set(CMAKE_PREFIX_PATH "C:/Path/To/vtk-6.3.0")
find_package(VTK 6 REQUIRED)
include(${VTK_USE_FILE})
add_executable(${PROJECT_NAME} WIN32 main.cpp)
target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/lenna.jpg ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>
)
#include <vtkJPEGReader.h>
#include <vtkImageData.h>
#include <vtkImageMapper.h> // Note: this is a 2D mapper (cf. vtkImageActor which is 3D)
#include <vtkActor2D.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
int main(int argc, char **argv)
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lenna.jpg");
reader->Update(); // why is this necessary? shouldn't the VTK pipeline take care of this automatically?
vtkSmartPointer<vtkImageMapper> mapper =
vtkSmartPointer<vtkImageMapper>::New();
mapper->SetInputData(reader->GetOutput());
mapper->SetColorWindow(255); // width of the color range to map to
mapper->SetColorLevel(127.5); // center of the color range to map to
vtkSmartPointer<vtkActor2D> image =
vtkSmartPointer<vtkActor2D>::New();
image->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(image);
vtkSmartPointer<vtkRenderWindow> window =
vtkSmartPointer<vtkRenderWindow>::New();
window->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(window);
// Set window to the image size. Note: why is this so cumbersome?
int imageSize[3];
reader->GetOutput()->GetDimensions(imageSize);
window->SetSize(imageSize[0], imageSize[1]);
// Here we'd normally call window->Render() to run the pipeline assembled above,
// But instead we'll start an event loop using an interactor,
// which prevents the program from returning as soon as it opens the image
interactor->Start();
return EXIT_SUCCESS;
}
// This version allows manipulating the image (pan, zoom...) using an Interactor Style.
// It requires some changes, including using a 3D actor to display a 2D image, which kinda irks me.
// Nevertheless, the code ends up being simpler than the 2D implementation.
#include <vtkJPEGReader.h>
#include <vtkImageMapper3D.h>
#include <vtkImageActor.h> // Note: this is a 3D actor (c.f. vtkImageMapper which is 2D)
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkSmartPointer.h>
int main(int argc, char **argv)
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lenna.jpg");
vtkSmartPointer<vtkImageActor> image =
vtkSmartPointer<vtkImageActor>::New();
image->GetMapper()->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(image);
vtkSmartPointer<vtkRenderWindow> window =
vtkSmartPointer<vtkRenderWindow>::New();
window->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(window);
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
interactor->SetInteractorStyle(style);
interactor->Start();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment