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 <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.12 FATAL_ERROR)

project(GreedyTerrainDecimation)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersHybrid
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "GreedyTerrainDecimation: 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(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}
)

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.