How to Use Visualization Toolkit to Generate Contours
The Visualization Toolkit (VTK) is an open-source software system for 3D computer graphics, image processing and visualization. Today, I’d like to share how to use VTK to generate contours.
Problem
Given a regular dataset with m*n points. Each point can be denoted as (x,y,z). The x and y represents x-coordinate and y-coordinate in 2D Space, and the z represents scalar value upon (x,y) space point. For example, z may be height, precipitation or density of some pollutant. How to generate contours with different z-values?
Prerequisites
- Download VTK-6.1.0 source package.
- Download and install latest CMake binary package.
- Prepare build files of VTK-6.1.0 by CMake GUI.
- Build VTK-6.1.0 (dll mode) by visual studio 2010. Please refer to VTK/Configure and Build.
Read the Regular Dataset
Read a dataset file SainteHelens.dem (DEM: Digital Elevation Model), and then downsize image grids:
//1. Read regular dataset (DEM).
const char* fname = "SainteHelens.dem";
vtkSmartPointer<vtkDEMReader> demReader = vtkSmartPointer<vtkDEMReader>::New();
demReader->SetFileName(fname);
demReader->Update();
//1.1 Downsize Image Grids.
vtkSmartPointer<vtkImageResample> resample = vtkSmartPointer<vtkImageResample>::New();
resample->SetInputConnection(demReader->GetOutputPort());
resample->SetDimensionality(2);
resample->SetAxisMagnificationFactor(0,0.25);
resample->SetAxisMagnificationFactor(1,0.25);
resample->Update();
The dataset after resampling is as follow:
Generate Contours
In VTK, there are some methods for generating contours, I recommend to use vtkMarchingSquare if the dataset is image data. The output contour parameters could be set by two approaches:
- Batch setting:
marchingSquares->GenerateValues(20, lo, hi);
- Setting one by one:
marchingSquares->SetNumberOfContours(4);
marchingSquares->SetValue(0, lo+(hi-lo)/5);
marchingSquares->SetValue(1, lo+(hi-lo)/5*2);
marchingSquares->SetValue(2, lo+(hi-lo)/5*3);
marchingSquares->SetValue(3, lo+(hi-lo)/5*4);
Note: if you want to generate contours for structured grid or rectilinear grid dataset, it’s recommended to use vtkContourFilter:
Display Results
Here are four viewports for displaying the dataset output results. The upper-left corner displays the source grid dataset; the upper-right corner displays the source dataset with Colormap; the lower-left corner displays the contour results with Colormap; the lower-right corner displays the overview result with contours in 3D mode.
How to Compile and Build the Sample
- Build the VTK source code to generate dependency libraries.
- Specify the directories of VTK head files and libraries:
- Add the following dependency libs:
vtkRenderingOpenGL-6.1.lib;
vtkRenderingCore-6.1.lib;
vtkInteractionStyle-6.1.lib;
vtkCommonSystem-6.1.lib;
vtkCommonCore-6.1.lib;
vtkCommonTransforms-6.1.lib;
vtkCommonMisc-6.1.lib;
vtkCommonExecutionModel-6.1.lib;
vtkCommonDataModel-6.1.lib;
vtkCommonMath-6.1.lib;
vtkFiltersCore-6.1.lib;
vtkFiltersSources-6.1.lib;
vtkFiltersGeneral-6.1.lib;
vtkFiltersGeometry-6.1.lib;
vtkFiltersExtraction-6.1.lib;
vtkImagingCore-6.1.lib;
vtkIOImage-6.1.lib;
vtksys-6.1.lib;
- Build the project, and run the demo.
Source Code
https://github.com/DynamsoftRD/VTK-Contours
git clone https://github.com/DynamsoftRD/VTK-Contours.git