Skip to content

TriangulateTerrainMap

vtk-examples/Cxx/Filtering/TriangulateTerrainMap


Description

This example generates heights (z-values) on a 10x10 grid (a terrain map) and triangulates the points.

Other languages

See (Python)

Question

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

Code

TriangulateTerrainMap.cxx

#include <vtkActor.h>
#include <vtkDelaunay2D.h>
#include <vtkMath.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVertexGlyphFilter.h>

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

  // Create points on an XY grid with random Z coordinate
  vtkNew<vtkPoints> points;

  unsigned int gridSize = 10;
  unsigned int seed = 0;
  vtkNew<vtkMinimalStandardRandomSequence> randomSequence;
  randomSequence->Initialize(seed);
  for (unsigned int x = 0; x < gridSize; x++)
  {
    for (unsigned int y = 0; y < gridSize; y++)
    {
      auto d = randomSequence->GetValue();
      randomSequence->Next();
      points->InsertNextPoint(x, y, d * 3.0);
    }
  }

  // Add the grid points to a polydata object
  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);

  vtkNew<vtkVertexGlyphFilter> glyphFilter;
  glyphFilter->SetInputData(polydata);
  glyphFilter->Update();

  // Create a mapper and actor
  vtkNew<vtkPolyDataMapper> pointsMapper;
  pointsMapper->SetInputConnection(glyphFilter->GetOutputPort());

  vtkNew<vtkActor> pointsActor;
  pointsActor->SetMapper(pointsMapper);
  pointsActor->GetProperty()->SetPointSize(3);
  pointsActor->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());
  // Triangulate the grid points
  vtkNew<vtkDelaunay2D> delaunay;
  delaunay->SetInputData(polydata);
  delaunay->Update();

  // Create a mapper and actor
  vtkNew<vtkPolyDataMapper> triangulatedMapper;
  triangulatedMapper->SetInputConnection(delaunay->GetOutputPort());

  vtkNew<vtkActor> triangulatedActor;
  triangulatedActor->SetMapper(triangulatedMapper);

  // Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actor to the scene
  renderer->AddActor(pointsActor);
  renderer->AddActor(triangulatedActor);
  renderer->SetBackground(colors->GetColor3d("Green").GetData());

  // Render and interact
  renderWindow->SetWindowName("TriangulateTerrainMap");
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TriangulateTerrainMap)

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

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

Download and Build TriangulateTerrainMap

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

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

./TriangulateTerrainMap

WINDOWS USERS

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