CurvaturesDemo
vtk-examples/Cxx/PolyData/CurvaturesDemo
Description¶
How to get the Gaussian and Mean curvatures of a surface.
Two different surfaces are used in this demonstration with each surface coloured according to its Gaussian and Mean curvatures.
-
The first surface is a superquadric surface, this demonstrates the use of extra filters that are needed to get a nice smooth surface.
-
The second surface is a parametric surface, in this case the surface has already been triangulated so no extra processing is necessary.
In order to get a nice coloured image, a vtkColorTransferFunction has been used to generate a set of colours for the vtkLookupTable tables. We have used a diverging colour space. Because of the symmetry of the ranges selected for the lookup tables, the white colouration represents a midpoint value whilst the blue represents values less than the midopoint value and red represents colours greater than the midpoint value.
In the case of the Random Hills Gaussian curvature surface, this colouration shows the nature of the surface quite nicely. The blue areas are saddle points (negative Gaussian curvature) and the red areas have a positive Gaussian curvature. In the case of the mean curvature the blue colouration is representing negative curvature perpendicular to one of the principal axes.
This example also demonstrates the use of std::vector's and the linking of the elements of the vectors together to form a pipeline.
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CurvaturesDemo.cxx
/*
The purpose of this is to demonstrate how to get the Gaussian and Mean
curvatures of a surface.
Two different surfaces are used in this demonstration with each
surface coloured according to its Gaussian and Mean curvatures.
The first surface is a superquadric surface, this demonstrates the use
of extra filters that are needed to get a nice smooth surface.
The second surface is a parametric surface, in this case the surface
has already been triangulated so no extra processing is necessary.
In order to get a nice coloured image, a vtkColorTransferFunction has
been used to generate a set of colours for the vtkLookUp tables. We
have used a diverging colour space. Because of the symmetry of the
ranges selected for the lookup tables, the white colouration
represents a midpoint value whilst the blue represents values less
than the midopoint value and red represents colours greater than the
midpoint value.
In the case of the Random Hills Gaussian Curvature surface, this
colouration shows the nature of the surface quite nicely. The blue
areas are saddle points (negative Gaussian curvature) and the red
areas have a positive Gaussian curvature. In the case of the mean
curvature the blue colouration is representing negative curvature
perpendicular to one of the principal axes.
This example also demonstrates the use of std::vector and the linking
of the elements of the vector together to form a pipeline.
*/
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCleanPolyData.h>
#include <vtkColorTransferFunction.h>
#include <vtkCurvatures.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkParametricFunctionSource.h>
#include <vtkParametricRandomHills.h>
#include <vtkPolyDataAlgorithm.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSuperquadricSource.h>
#include <vtkTextMapper.h>
#include <vtkTextProperty.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <vtkTriangleFilter.h>
#include <array>
#include <vector>
int main(int, char* argv[])
{
vtkNew<vtkNamedColors> colors;
// We are going to handle two different sources.
// The first source is a superquadric source.
vtkNew<vtkSuperquadricSource> torus;
torus->SetCenter(0.0, 0.0, 0.0);
torus->SetScale(1.0, 1.0, 1.0);
torus->SetPhiResolution(64);
torus->SetThetaResolution(64);
torus->SetThetaRoundness(1);
torus->SetThickness(0.5);
torus->SetSize(0.5);
torus->SetToroidal(1);
// Rotate the torus towards the observer (around the x-axis)
vtkNew<vtkTransform> torusT;
torusT->RotateX(55);
vtkNew<vtkTransformFilter> torusTF;
torusTF->SetInputConnection(torus->GetOutputPort());
torusTF->SetTransform(torusT);
// The quadric is made of strips, so pass it through a triangle filter as
// the curvature filter only operates on polys
vtkNew<vtkTriangleFilter> tri;
tri->SetInputConnection(torusTF->GetOutputPort());
// The quadric has nasty discontinuities from the way the edges are generated
// so let's pass it though a CleanPolyDataFilter and merge any points which
// are coincident, or very close
vtkNew<vtkCleanPolyData> cleaner;
cleaner->SetInputConnection(tri->GetOutputPort());
cleaner->SetTolerance(0.005);
// The next source will be a parametric function
vtkNew<vtkParametricRandomHills> rh;
vtkNew<vtkParametricFunctionSource> rhFnSrc;
rhFnSrc->SetParametricFunction(rh);
// Now we have the sources, lets put them into a vector
std::vector<vtkSmartPointer<vtkPolyDataAlgorithm>> sources;
sources.push_back(cleaner);
sources.push_back(cleaner);
sources.push_back(rhFnSrc);
sources.push_back(rhFnSrc);
// Colour transfer function.
vtkNew<vtkColorTransferFunction> ctf;
ctf->SetColorSpaceToDiverging();
ctf->AddRGBPoint(0.0, colors->GetColor3d("MidnightBlue").GetRed(),
colors->GetColor3d("MidnightBlue").GetGreen(),
colors->GetColor3d("MidnightBlue").GetBlue());
ctf->AddRGBPoint(1.0, colors->GetColor3d("DarkOrange").GetRed(),
colors->GetColor3d("DarkOrange").GetGreen(),
colors->GetColor3d("DarkOrange").GetBlue());
// Lookup table.
std::vector<vtkSmartPointer<vtkLookupTable>> luts;
for (auto idx = 0; idx < sources.size(); ++idx)
{
vtkNew<vtkLookupTable> lut;
lut->SetNumberOfColors(256);
for (auto i = 0; i < lut->GetNumberOfColors(); ++i)
{
std::array<double, 4> color;
ctf->GetColor(double(i) / lut->GetNumberOfColors(), color.data());
color[3] = 1.0;
lut->SetTableValue(i, color.data());
}
if (idx == 0)
{
lut->SetRange(-10, 10);
}
else if (idx == 1)
{
lut->SetRange(0, 4);
}
else if (idx == 2)
{
lut->SetRange(-1, 1);
}
else
{
lut->SetRange(-1, 1);
}
lut->Build();
luts.push_back(lut);
}
std::vector<vtkSmartPointer<vtkCurvatures>> curvatures;
for (auto idx = 0; idx < sources.size(); ++idx)
{
curvatures.push_back(vtkSmartPointer<vtkCurvatures>::New());
if (idx % 2 == 0)
{
curvatures[idx]->SetCurvatureTypeToGaussian();
}
else
{
curvatures[idx]->SetCurvatureTypeToMean();
}
}
std::vector<vtkSmartPointer<vtkRenderer>> renderers;
std::vector<vtkSmartPointer<vtkActor>> actors;
std::vector<vtkSmartPointer<vtkPolyDataMapper>> mappers;
std::vector<vtkSmartPointer<vtkTextMapper>> textmappers;
std::vector<vtkSmartPointer<vtkActor2D>> textactors;
for (auto idx = 0; idx < sources.size(); ++idx)
{
mappers.push_back(vtkSmartPointer<vtkPolyDataMapper>::New());
actors.push_back(vtkSmartPointer<vtkActor>::New());
textmappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
textactors.push_back(vtkSmartPointer<vtkActor2D>::New());
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
}
// Create a common text property.
vtkNew<vtkTextProperty> textProperty;
textProperty->SetFontSize(24);
textProperty->SetJustificationToCentered();
std::vector<std::string> names;
names.push_back("Torus - Gaussian Curvature");
names.push_back("Torus - Mean Curvature");
names.push_back("Random Hills - Gaussian Curvature");
names.push_back("Random Hills - Mean Curvature");
// Link the pipeline together.
for (auto idx = 0; idx < sources.size(); ++idx)
{
curvatures[idx]->SetInputConnection(sources[idx]->GetOutputPort());
mappers[idx]->SetInputConnection(curvatures[idx]->GetOutputPort());
mappers[idx]->SetLookupTable(luts[idx]);
mappers[idx]->SetUseLookupTableScalarRange(1);
actors[idx]->SetMapper(mappers[idx]);
textmappers[idx]->SetInput(names[idx].c_str());
textmappers[idx]->SetTextProperty(textProperty);
textactors[idx]->SetMapper(textmappers[idx]);
textactors[idx]->SetPosition(250, 16);
}
// Create the RenderWindow
//
auto rendererSize = 512;
auto gridDimensions = 2;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(rendererSize * gridDimensions,
rendererSize * gridDimensions);
renderWindow->SetWindowName("CurvaturesDemo");
// Add and position the renders to the render window.
for (auto row = 0; row < gridDimensions; ++row)
{
for (auto col = 0; col < gridDimensions; ++col)
{
auto idx = row * gridDimensions + col;
renderers[idx]->SetViewport(
double(col) / gridDimensions,
double(gridDimensions - (row + 1)) / gridDimensions,
double(col + 1) / gridDimensions,
double(gridDimensions - row) / gridDimensions);
renderWindow->AddRenderer(renderers[idx]);
renderers[idx]->AddActor(actors[idx]);
renderers[idx]->AddActor(textactors[idx]);
renderers[idx]->SetBackground(
colors->GetColor3d("CornflowerBlue").GetData());
}
}
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(CurvaturesDemo)
find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonComputationalGeometry
vtkCommonCore
vtkCommonExecutionModel
vtkCommonTransforms
vtkFiltersCore
vtkFiltersGeneral
vtkFiltersSources
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2
QUIET
)
if (NOT VTK_FOUND)
message("Skipping CurvaturesDemo: ${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(CurvaturesDemo MACOSX_BUNDLE CurvaturesDemo.cxx )
target_link_libraries(CurvaturesDemo PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(CurvaturesDemo MACOSX_BUNDLE CurvaturesDemo.cxx )
target_link_libraries(CurvaturesDemo PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CurvaturesDemo
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build CurvaturesDemo¶
Click here to download CurvaturesDemo and its CMakeLists.txt file. Once the tarball CurvaturesDemo.tar has been downloaded and extracted,
cd CurvaturesDemo/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:
./CurvaturesDemo
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.