Skip to content

GreedyTerrainDecimation

vtk-examples/Cxx/PolyData/GreedyTerrainDecimation


Other languages

See (CSharp)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

GreedyTerrainDecimation.cxx

#include <vtkActor.h>
#include <vtkGreedyTerrainDecimation.h>
#include <vtkImageData.h>
#include <vtkMath.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create an image
  vtkNew<vtkImageData> image;
  image->SetDimensions(3, 3, 1);
  image->AllocateScalars(VTK_UNSIGNED_CHAR, 1);

  vtkNew<vtkMinimalStandardRandomSequence> randomSequence;
  randomSequence->SetSeed(8775070);

  int dims[3];
  image->GetDimensions(dims);
  for (int i = 0; i < dims[0]; i++)
  {
    for (int j = 0; j < dims[1]; j++)
    {
      auto pixel =
          static_cast<unsigned char*>(image->GetScalarPointer(i, j, 0));
      // pixel[0] = vtkMath::Round(vtkMath::Random(0, 255));
      pixel[0] = vtkMath::Round(randomSequence->GetRangeValue(0.0, 5.0));
      randomSequence->Next();
    }
  }

  vtkNew<vtkGreedyTerrainDecimation> decimation;
  decimation->SetInputData(image);
  decimation->Update();

  // Visualize
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(decimation->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetInterpolationToFlat();
  actor->GetProperty()->EdgeVisibilityOn();
  actor->GetProperty()->SetEdgeColor(colors->GetColor3d("Red").GetData());

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetWindowName("GreedyTerrainDecimation");

  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(GreedyTerrainDecimation)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersHybrid
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping GreedyTerrainDecimation: ${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(GreedyTerrainDecimation MACOSX_BUNDLE GreedyTerrainDecimation.cxx )
  target_link_libraries(GreedyTerrainDecimation PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(GreedyTerrainDecimation MACOSX_BUNDLE GreedyTerrainDecimation.cxx )
  target_link_libraries(GreedyTerrainDecimation PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS GreedyTerrainDecimation
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build GreedyTerrainDecimation

Click here to download GreedyTerrainDecimation and its CMakeLists.txt file. Once the tarball GreedyTerrainDecimation.tar has been downloaded and extracted,

cd GreedyTerrainDecimation/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:

./GreedyTerrainDecimation

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.