DEMReader
Repository source: DEMReader
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
DEMReader.cxx
#include <vtkDEMReader.h>
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageMapToColors.h>
#include <vtkImageMapper3D.h>
#include <vtkInteractorStyleImage.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
// Verify arguments.
if (argc < 2)
{
std::cerr << "Required: filename.dem e.g. SainteHelens.dem" << std::endl;
return EXIT_FAILURE;
}
// Read the file
vtkNew<vtkDEMReader> reader;
reader->SetFileName(argv[1]);
reader->Update();
vtkNew<vtkLookupTable> lut;
lut->SetHueRange(0.6, 0);
lut->SetSaturationRange(1.0, 0);
lut->SetValueRange(0.5, 1.0);
lut->SetTableRange(reader->GetOutput()->GetScalarRange());
// Visualize
vtkNew<vtkImageMapToColors> mapColors;
mapColors->SetLookupTable(lut);
mapColors->SetInputConnection(reader->GetOutputPort());
// Create an actor.
vtkNew<vtkImageActor> actor;
actor->GetMapper()->SetInputConnection(mapColors->GetOutputPort());
// Setup renderer.
vtkNew<vtkRenderer> renderer;
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
// Setup render window.
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("DEMReader");
// Setup render window interactor.
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
vtkNew<vtkInteractorStyleImage> style;
renderWindowInteractor->SetInteractorStyle(style);
// Render and start interaction.
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(DEMReader)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
IOImage
ImagingCore
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "DEMReader: 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(DEMReader MACOSX_BUNDLE DEMReader.cxx )
target_link_libraries(DEMReader PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS DEMReader
MODULES ${VTK_LIBRARIES}
)
Download and Build DEMReader¶
Click here to download DEMReader and its CMakeLists.txt file. Once the tarball DEMReader.tar has been downloaded and extracted,
cd DEMReader/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:
./DEMReader
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.