# TriangulateTerrainMap

VTKExamples/Cxx/Filtering/TriangulateTerrainMap

### Description¶

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

Other Languages

See (Python)

Question

### Code¶

TriangulateTerrainMap.cxx

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

int main(int, char *[])
{
// Create points on an XY grid with random Z coordinate
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();

unsigned int gridSize = 10;
for(unsigned int x = 0; x < gridSize; x++)
{
for(unsigned int y = 0; y < gridSize; y++)
{
points->InsertNextPoint(x, y, vtkMath::Random(0.0, 3.0));
}
}

// Add the grid points to a polydata object
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);

vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputData(polydata);
glyphFilter->Update();

// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> pointsMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
pointsMapper->SetInputConnection(glyphFilter->GetOutputPort());

vtkSmartPointer<vtkActor> pointsActor =
vtkSmartPointer<vtkActor>::New();
pointsActor->SetMapper(pointsMapper);
pointsActor->GetProperty()->SetPointSize(3);
pointsActor->GetProperty()->SetColor(1,0,0);
// Triangulate the grid points
vtkSmartPointer<vtkDelaunay2D> delaunay =
vtkSmartPointer<vtkDelaunay2D>::New();
delaunay->SetInputData(polydata);
delaunay->Update();

// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> triangulatedMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
triangulatedMapper->SetInputConnection(delaunay->GetOutputPort());

vtkSmartPointer<vtkActor> triangulatedActor =
vtkSmartPointer<vtkActor>::New();
triangulatedActor->SetMapper(triangulatedMapper);

// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

// Add the actor to the scene
renderer->SetBackground(.3, .6, .3); // Background color green

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

return EXIT_SUCCESS;
}


### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TriangulateTerrainMap)

find_package(VTK COMPONENTS
vtkCommonCore
vtkCommonDataModel
vtkFiltersCore
vtkFiltersGeneral
vtkInteractionStyle
vtkRenderingCore
vtkRenderingFreeType
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
target_link_libraries(TriangulateTerrainMap PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS TriangulateTerrainMap MODULES${VTK_LIBRARIES}
)
endif ()


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.