TriangulateTerrainMap
Repository source: 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 <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.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.12 FATAL_ERROR)
project(TriangulateTerrainMap)
find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersCore
  FiltersGeneral
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)
if (NOT VTK_FOUND)
  message(FATAL_ERROR "TriangulateTerrainMap: 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(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}
)
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.
