ImageOrder
Repository source: ImageOrder
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImageOrder.cxx
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <array>
#define DISPLAY_FIRST_IMAGE
// Comment this out to display the first image.
#undef DISPLAY_FIRST_IMAGE
namespace {
void CreateColorImage(vtkImageData* image, std::string const& colorName);
}
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Image 1
vtkNew<vtkImageData> firstImage;
CreateColorImage(firstImage, "DarkOliveGreen");
vtkNew<vtkImageActor> firstImageActor;
firstImageActor->SetInputData(firstImage);
// Image 2
vtkNew<vtkImageData> secondImage;
CreateColorImage(secondImage, "DarkSalmon");
vtkNew<vtkImageActor> secondImageActor;
secondImageActor->SetInputData(secondImage);
// Visualize.
vtkNew<vtkRenderer> renderer;
#ifdef DISPLAY_FIRST_IMAGE
// The first image is displayed
renderer->AddActor(secondImageActor);
renderer->AddActor(firstImageActor);
#else
// The second image is displayed.
renderer->AddActor(firstImageActor);
renderer->AddActor(secondImageActor);
#endif
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderer->ResetCamera();
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("ImageOrder");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
namespace {
void CreateColorImage(vtkImageData* image, std::string const& colorName)
{
vtkNew<vtkNamedColors> colors;
std::array<unsigned char, 3> drawColor{0, 0, 0};
auto color = colors->GetColor3ub(colorName).GetData();
for (auto i = 0; i < 3; ++i)
{
drawColor[i] = color[i];
}
unsigned int dim = 10;
image->SetDimensions(dim, dim, 1);
image->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
for (unsigned int x = 0; x < dim; x++)
{
for (unsigned int y = 0; y < dim; y++)
{
auto pixel =
static_cast<unsigned char*>(image->GetScalarPointer(x, y, 0));
for (auto i = 0; i < 3; ++i)
{
pixel[i] = drawColor[i];
}
}
}
image->Modified();
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ImageOrder)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ImageOrder: 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(ImageOrder MACOSX_BUNDLE ImageOrder.cxx )
target_link_libraries(ImageOrder PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImageOrder
MODULES ${VTK_LIBRARIES}
)
Download and Build ImageOrder¶
Click here to download ImageOrder and its CMakeLists.txt file. Once the tarball ImageOrder.tar has been downloaded and extracted,
cd ImageOrder/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:
./ImageOrder
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.