CurvaturesDemo
Repository source: 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 colored image, a vtkColorTransferFunction has been used to generate a set of colors for the vtkLookupTable tables. We have used a diverging color space. Because of the symmetry of the ranges selected for the lookup tables, the white colouration represents a midpoint value whilst the bluish coloration represents values less than the midpoint value and yellow to red represents colours greater than the midpoint value.
In the case of the Parametric Hills surface, the Gaussian curvature colouration shows the nature of the surface quite nicely. The bluish areas are saddle points (negative Gaussian curvature) and the yellow-red areas have a positive Gaussian curvature.
In the case of the mean curvature the blue colouration represents negative curvature perpendicular to one of the principal axes.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CurvaturesDemo.py
#!/usr/bin/env python3
"""
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 orange 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 orange 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 lists and the linking of the elements of the
lists together to form a pipeline.
"""
import copy
from dataclasses import dataclass
from pathlib import Path
import numpy as np
from vtk.util import numpy_support
from vtkmodules.numpy_interface import dataset_adapter as dsa
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonComputationalGeometry import vtkParametricRandomHills
from vtkmodules.vtkCommonCore import (
VTK_DOUBLE,
vtkIdList,
vtkLookupTable
)
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkFiltersCore import (
vtkCleanPolyData,
vtkFeatureEdges,
vtkGenerateIds,
vtkTriangleFilter
)
from vtkmodules.vtkFiltersGeneral import (
vtkCurvatures,
vtkTransformFilter
)
from vtkmodules.vtkFiltersSources import (
vtkParametricFunctionSource,
vtkSuperquadricSource
)
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkInteractionWidgets import vtkScalarBarRepresentation, vtkScalarBarWidget, vtkTextRepresentation, \
vtkTextWidget
from vtkmodules.vtkRenderingAnnotation import vtkScalarBarActor
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkColorTransferFunction,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer,
vtkTextProperty, vtkTextActor
)
def main(argv):
colors = vtkNamedColors()
# We are going to handle two different sources.
# The first source is a superquadric source.
torus = vtkSuperquadricSource(center=(0.0, 0.0, 0.0), scale=(1.0, 1.0, 1.0),
phi_resolution=64,
theta_resolution=64, theta_roundness=1,
thickness=0.5, size=0.5, toroidal=True)
# Rotate the torus towards the observer (around the x-axis)
toroid_transform = vtkTransform()
toroid_transform.RotateX(55)
toroid_transform_filter = vtkTransformFilter(transform=toroid_transform)
# The quadric is made of strips, so pass it through a triangle filter as
# the curvature filter only operates on polys
tri = vtkTriangleFilter()
# 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
cleaner = vtkCleanPolyData(tolerance=0.005)
torus >> toroid_transform_filter >> tri >> cleaner
# The next source will be a parametric function.
rh_fn_src = vtkParametricFunctionSource(parametric_function=vtkParametricRandomHills())
sources = list()
for i in range(0, 4):
cc = vtkCurvatures()
if i < 2:
cc.input_connection = cleaner.output_port
else:
cc.input_connection = rh_fn_src.output_port
if i % 2 == 0:
cc.SetCurvatureTypeToGaussian()
curvature_name = 'Gauss_Curvature'
else:
cc.SetCurvatureTypeToMean()
curvature_name = 'Mean_Curvature'
cc.update()
adjust_edge_curvatures(cc.output, curvature_name)
sources.append(cc.output)
curvatures = {
0: 'Gauss_Curvature',
1: 'Mean_Curvature',
2: 'Gauss_Curvature',
3: 'Mean_Curvature',
}
lut = get_diverging_lut()
# lut = get_diverging_lut1()
# Create a common text property.
# Position the source name according to its length.
curvature_names = list(curvatures.values())
curvature_names[:] = [x.replace('_', '\n') for x in curvature_names]
text_positions = get_text_positions(curvature_names,
justification=TextProperty.Justification.VTK_TEXT_CENTERED,
vertical_justification=TextProperty.VerticalJustification.VTK_TEXT_BOTTOM,
width=0.5)
text_property = vtkTextProperty(color=colors.GetColor3d('AliceBlue'), bold=True, italic=True, shadow=True,
font_size=12, font_family_as_string='Courier',
justification=TextProperty.Justification.VTK_TEXT_CENTERED,
vertical_justification=TextProperty.VerticalJustification.VTK_TEXT_CENTERED)
title_text_property = vtkTextProperty(color=colors.GetColor3d('AliceBlue'), bold=True, italic=True, shadow=True,
font_size=16,
justification=TextProperty.Justification.VTK_TEXT_LEFT)
label_text_property = vtkTextProperty(color=colors.GetColor3d('AliceBlue'), bold=False, italic=False, shadow=True,
font_size=12,
justification=TextProperty.Justification.VTK_TEXT_LEFT)
# RenderWindow Dimensions.
renderer_size = 640
grid_dimensions = 2
# window_width = renderer_size * grid_dimensions
# window_height = renderer_size * grid_dimensions
# Create the RenderWindow and interactor.
ren_win = vtkRenderWindow(
size=(renderer_size * grid_dimensions, renderer_size * grid_dimensions),
window_name=f'{Path(argv[0]).stem:s}')
iren = vtkRenderWindowInteractor()
iren.render_window = ren_win
style = vtkInteractorStyleTrackballCamera()
iren.interactor_style = style
renderers = list()
actors = list()
sbws = list()
text_reps = list()
text_widgets = list()
# Link the pipeline together.
for idx, source in enumerate(sources):
curvature_name = curvatures[idx].replace('_', '\n')
source.point_data.active_scalars = curvatures[idx]
scalar_range = source.point_data.GetScalars(curvatures[idx]).range
mapper = vtkPolyDataMapper(input_data=source,
lookup_table=lut,
scalar_range=scalar_range,
scalar_mode=Mapper.ScalarMode.VTK_SCALAR_MODE_USE_POINT_FIELD_DATA
)
mapper.SelectColorArray(curvatures[idx])
actor = vtkActor(mapper=mapper)
actors.append(actor)
ren = vtkRenderer()
renderers.append(ren)
surface_name = curvature_name.replace('\n', ' ')
text_actor = vtkTextActor(input=surface_name.title(), text_scale_mode=vtkTextActor.TEXT_SCALE_MODE_NONE,
text_property=text_property)
# Create the text representation. Used for positioning the text actor.
text_representation = vtkTextRepresentation(enforce_normalized_viewport_bounds=True)
text_representation.position_coordinate.value = text_positions[curvature_name]['p']
text_representation.position2_coordinate.value = text_positions[curvature_name]['p2']
text_reps.append(text_representation)
text_widget = vtkTextWidget(representation=text_reps[idx], text_actor=text_actor, interactor=iren,
default_renderer=ren, current_renderer=ren,
selectable=False)
text_widgets.append(text_widget)
sbp = ScalarBarProperties()
sbp.title_text = curvature_name + '\n'
sbp.number_of_labels = 5
sbp.lut = lut
sbp.orientation = True
sbw = make_scalar_bar_widget(sbp, title_text_property,
label_text_property, ren, iren)
sbws.append(sbw)
for idx in range(len(sources)):
if idx < grid_dimensions * grid_dimensions:
renderers.append(vtkRenderer)
# Add and position the renders to the render window.
viewport = list()
for row in range(grid_dimensions):
for col in range(grid_dimensions):
idx = row * grid_dimensions + col
viewport[:] = []
viewport.append(float(col) / grid_dimensions)
viewport.append(float(grid_dimensions - (row + 1)) / grid_dimensions)
viewport.append(float(col + 1) / grid_dimensions)
viewport.append(float(grid_dimensions - row) / grid_dimensions)
if idx > (len(sources) - 1):
continue
renderers[idx].SetViewport(viewport)
ren_win.AddRenderer(renderers[idx])
renderers[idx].AddActor(actors[idx])
renderers[idx].SetBackground(colors.GetColor3d('ParaViewBlueGrayBkg'))
for sbw in sbws:
sbw.On()
for tw in text_widgets:
tw.On()
# Share the cameras.
renderers[1].active_camera = renderers[0].active_camera
renderers[0].ResetCamera()
renderers[3].active_camera = renderers[2].active_camera
renderers[2].ResetCamera()
ren_win.Render()
iren.Start()
def get_diverging_lut():
"""
See: [Diverging Color Maps for Scientific Visualization](https://www.kennethmoreland.com/color-maps/)
start point midPoint end point
cool to warm: 0.230, 0.299, 0.754 0.865, 0.865, 0.865 0.706, 0.016, 0.150
purple to orange: 0.436, 0.308, 0.631 0.865, 0.865, 0.865 0.759, 0.334, 0.046
green to purple: 0.085, 0.532, 0.201 0.865, 0.865, 0.865 0.436, 0.308, 0.631
blue to brown: 0.217, 0.525, 0.910 0.865, 0.865, 0.865 0.677, 0.492, 0.093
green to red: 0.085, 0.532, 0.201 0.865, 0.865, 0.865 0.758, 0.214, 0.233
:return:
"""
ctf = vtkColorTransferFunction(color_space=ColorTransferFunction.ColorSpace.VTK_CTF_DIVERGING)
# Cool to warm.
ctf.AddRGBPoint(0.0, 0.230, 0.299, 0.754)
ctf.AddRGBPoint(0.5, 0.865, 0.865, 0.865)
ctf.AddRGBPoint(1.0, 0.706, 0.016, 0.150)
table_size = 256
lut = vtkLookupTable()
lut.SetNumberOfTableValues(table_size)
lut.Build()
for i in range(0, table_size):
rgba = list(ctf.GetColor(float(i) / table_size))
rgba.append(1)
lut.SetTableValue(i, rgba)
return lut
def get_diverging_lut1():
colors = vtkNamedColors()
pts = list()
pts.append([0.0] + list(colors.GetColor3d('MidnightBlue')))
pts.append([0.5] + list(colors.GetColor3d('Gainsboro')))
pts.append([1.0] + list(colors.GetColor3d('DarkOrange')))
ctf = vtkColorTransferFunction(color_space=ColorTransferFunction.ColorSpace.VTK_CTF_DIVERGING)
for pt in pts:
ctf.AddRGBPoint(*pt)
table_size = 256
lut = vtkLookupTable(number_of_table_values=table_size)
lut.Build()
for i in range(0, table_size):
rgba = list(ctf.GetColor(float(i) / table_size)) + [1.0]
lut.SetTableValue(i, rgba)
return lut
def adjust_edge_curvatures(source, curvature_name, epsilon=1.0e-08):
"""
This function adjusts curvatures along the edges of the surface by replacing
the value with the average value of the curvatures of points in the neighborhood.
Remember to update the vtkCurvatures object before calling this.
:param source: A vtkPolyData object corresponding to the vtkCurvatures object.
:param curvature_name: The name of the curvature, 'Gauss_Curvature' or 'Mean_Curvature'.
:param epsilon: Absolute curvature values less than this will be set to zero.
:return: The vtkPolyData object with the adjusted edge curvatures.
"""
def point_neighbourhood(pt_id):
"""
Find the ids of the neighbors of pt_id.
:param pt_id: The point id.
:return: The neighbour ids.
"""
"""
Extract the topological neighbors for point pId. In two steps:
1) source.GetPointCells(pt_id, cell_ids)
2) source.GetCellPoints(cell_id, cell_point_ids) for all cell_id in cell_ids
"""
cell_ids = vtkIdList()
source.GetPointCells(pt_id, cell_ids)
neighbour = set()
for cell_idx in range(0, cell_ids.number_of_ids):
cell_id = cell_ids.GetId(cell_idx)
cell_point_ids = vtkIdList()
source.GetCellPoints(cell_id, cell_point_ids)
for cell_pt_idx in range(0, cell_point_ids.number_of_ids):
neighbour.add(cell_point_ids.GetId(cell_pt_idx))
return neighbour
def compute_distance(pt_id_a, pt_id_b):
"""
Compute the distance between two points given their ids.
:param pt_id_a:
:param pt_id_b:
:return:
"""
pt_a = np.array(source.GetPoint(pt_id_a))
pt_b = np.array(source.GetPoint(pt_id_b))
return np.linalg.norm(pt_a - pt_b)
# Get the active scalars
source.point_data.active_scalars = curvature_name
np_source = dsa.WrapDataObject(source)
curvatures = np_source.PointData[curvature_name]
# Get the boundary point IDs.
array_name = 'ids'
id_filter = vtkGenerateIds(input_data=source, point_ids=True, cell_ids=False,
point_ids_array_name=array_name, cell_ids_array_name=array_name)
edges = vtkFeatureEdges(boundary_edges=True, manifold_edges=False,
non_manifold_edges=False, feature_edges=False)
(source >> id_filter >> edges).update()
edge_array = edges.output.point_data.GetArray(array_name)
boundary_ids = []
for i in range(edges.output.number_of_points):
boundary_ids.append(edge_array.GetValue(i))
# Remove duplicate Ids.
p_ids_set = set(boundary_ids)
# Iterate over the edge points and compute the curvature as the weighted
# average of the neighbours.
count_invalid = 0
for p_id in boundary_ids:
p_ids_neighbors = point_neighbourhood(p_id)
# Keep only interior points.
p_ids_neighbors -= p_ids_set
# Compute distances and extract curvature values.
curvs = [curvatures[p_id_n] for p_id_n in p_ids_neighbors]
dists = [compute_distance(p_id_n, p_id) for p_id_n in p_ids_neighbors]
curvs = np.array(curvs)
dists = np.array(dists)
curvs = curvs[dists > 0]
dists = dists[dists > 0]
if len(curvs) > 0:
weights = 1 / np.array(dists)
weights /= weights.sum()
new_curv = np.dot(curvs, weights)
else:
# Corner case.
count_invalid += 1
# Assuming the curvature of the point is planar.
new_curv = 0.0
# Set the new curvature value.
curvatures[p_id] = new_curv
# Set small values to zero.
if epsilon != 0.0:
curvatures = np.where(abs(curvatures) < epsilon, 0, curvatures)
# Curvatures is now an ndarray
curv = numpy_support.numpy_to_vtk(num_array=curvatures.ravel(),
deep=True,
array_type=VTK_DOUBLE)
curv.name = curvature_name
source.point_data.RemoveArray(curvature_name)
source.point_data.AddArray(curv)
source.point_data.active_scalars = curvature_name
class ScalarBarProperties:
"""
The properties needed for scalar bars.
"""
named_colors = vtkNamedColors()
lut = None
# These are in pixels
maximum_dimensions = {'width': 100, 'height': 260}
title_text = '',
number_of_labels: int = 5
label_format = '{:0.2f}'
# Orientation vertical=True, horizontal=False.
orientation: bool = True
# Horizontal and vertical positioning.
# These are the default positions, don't change these.
default_v = {'p': (0.85, 0.1), 'p2': (0.1, 0.7)}
default_h = {'p': (0.1, 0.1), 'p2': (0.7, 0.1)}
# Modify these as needed.
position_v = copy.deepcopy(default_v)
position_h = copy.deepcopy(default_h)
def make_scalar_bar_widget(scalar_bar_properties, title_text_property, label_text_property, renderer,
interactor):
"""
Make a scalar bar widget.
:param scalar_bar_properties: The lookup table, title name, maximum dimensions in pixels and position.
:param title_text_property: The properties for the title.
:param label_text_property: The properties for the labels.
:param renderer: The default renderer.
:param interactor: The vtkInteractor.
:return: The scalar bar widget.
"""
sb_actor = vtkScalarBarActor(lookup_table=scalar_bar_properties.lut, title=scalar_bar_properties.title_text,
unconstrained_font_size=True,
number_of_labels=scalar_bar_properties.number_of_labels,
title_text_property=title_text_property, label_text_property=label_text_property,
label_format=scalar_bar_properties.label_format,
)
sb_rep = vtkScalarBarRepresentation(enforce_normalized_viewport_bounds=True,
orientation=scalar_bar_properties.orientation)
# Set the position.
sb_rep.position_coordinate.SetCoordinateSystemToNormalizedViewport()
sb_rep.position2_coordinate.SetCoordinateSystemToNormalizedViewport()
if scalar_bar_properties.orientation:
sb_rep.position_coordinate.value = scalar_bar_properties.position_v['p']
sb_rep.position2_coordinate.value = scalar_bar_properties.position_v['p2']
else:
sb_rep.position_coordinate.value = scalar_bar_properties.position_h['p']
sb_rep.position2_coordinate.value = scalar_bar_properties.position_h['p2']
widget = vtkScalarBarWidget(representation=sb_rep, scalar_bar_actor=sb_actor, default_renderer=renderer,
interactor=interactor, enabled=True)
return widget
def position_sbw_h(num_bands, max_bands):
"""
Position the vertical scalar bar widget.
:param: num_bands - the number of bands in the scalar bar.
:param: max_bands - the maximum number of bands.
:return: The scalar bar position.
"""
max_bands = abs(max_bands)
num_bands = abs(num_bands)
if num_bands > max_bands:
num_bands = max_bands
if num_bands == 0:
num_bands = 1
# Origin of the scalar bar.
xy0 = [0.125, 0.05]
# Width and height of the scalar bar.
dxy = [0.75, 0.1]
if num_bands >= max_bands:
return {'p': tuple(xy0), 'p2': tuple(dxy)}
dx = dxy[0] - xy0[0] * num_bands / max_bands
dxy[0] = dxy[0] * num_bands / max_bands
if num_bands == 1:
xy0[0] = 0.5 - dx * num_bands / (max_bands * 2)
else:
xy0[0] = 0.5 - dx * (num_bands + 1) / (max_bands * 2)
return {'p': tuple(xy0), 'p2': tuple(dxy)}
def position_sbw_v(num_bands, max_bands):
"""
Position the vertical scalar bar widget.
:param: num_bands - the number of bands in the scalar bar.
:param: max_bands - the maximum number of bands.
:return: The scalar bar position.
"""
max_bands = abs(max_bands)
num_bands = abs(num_bands)
if num_bands > max_bands:
num_bands = max_bands
if num_bands == 0:
num_bands = 1
# Origin of the scalar bar.
xy0 = [0.9, 0.25]
# Width and height of the scalar bar.
dxy = [0.08, 0.5]
if num_bands >= max_bands:
return {'p': tuple(xy0), 'p2': tuple(dxy)}
dy = dxy[1] - xy0[1] * num_bands / max_bands
dxy[1] = dxy[1] * num_bands / max_bands
if num_bands == 1:
xy0[1] = 0.5 - dy * num_bands / (max_bands * 2)
else:
xy0[1] = 0.5 - dy * (num_bands + 1) / (max_bands * 2)
return {'p': tuple(xy0), 'p2': tuple(dxy)}
def get_text_positions(names, justification=0, vertical_justification=0, width=0.96, height=0.1):
"""
Get viewport positioning information for a list of names.
:param names: The list of names.
:param justification: Horizontal justification of the text, default is left.
:param vertical_justification: Vertical justification of the text, default is bottom.
:param width: Width of the bounding_box of the text in screen coordinates.
:param height: Height of the bounding_box of the text in screen coordinates.
:return: A dictionary of positioning information.
"""
# The gap between the left or right edge of the screen and the text.
dx = 0.02
width = abs(width)
if width > 0.96:
width = 0.96
y0 = 0.01
height = abs(height)
if height > 0.9:
height = 0.9
dy = height
if vertical_justification == TextProperty.VerticalJustification.VTK_TEXT_TOP:
y0 = 1.0 - (dy + y0)
if vertical_justification == TextProperty.VerticalJustification.VTK_TEXT_CENTERED:
y0 = 0.5 - (dy / 2.0 + y0)
name_len_min = 0
name_len_max = 0
first = True
for k in names:
sz = len(k)
if first:
name_len_max = sz
first = False
else:
name_len_min = min(name_len_min, sz)
name_len_max = max(name_len_max, sz)
text_positions = dict()
for k in names:
sz = len(k)
delta_sz = width * sz / name_len_max
if delta_sz > width:
delta_sz = width
if justification == TextProperty.Justification.VTK_TEXT_CENTERED:
x0 = 0.5 - delta_sz / 2.0
elif justification == TextProperty.Justification.VTK_TEXT_RIGHT:
x0 = 1.0 - dx - delta_sz
else:
# Default is left justification.
x0 = dx
# For debugging!
# print(
# f'{k:16s}: (x0, y0) = ({x0:3.2f}, {y0:3.2f}), (x1, y1) = ({x0 + delta_sz:3.2f}, {y0 + dy:3.2f})'
# f', width={delta_sz:3.2f}, height={dy:3.2f}')
text_positions[k] = {'p': [x0, y0, 0], 'p2': [delta_sz, dy, 0]}
return text_positions
@dataclass(frozen=True)
class ColorTransferFunction:
@dataclass(frozen=True)
class ColorSpace:
VTK_CTF_RGB: int = 0
VTK_CTF_HSV: int = 1
VTK_CTF_LAB: int = 2
VTK_CTF_DIVERGING: int = 3
VTK_CTF_LAB_CIEDE2000: int = 4
VTK_CTF_STEP: int = 5
@dataclass(frozen=True)
class Scale:
VTK_CTF_LINEAR: int = 0
VTK_CTF_LOG10: int = 1
@dataclass(frozen=True)
class Mapper:
@dataclass(frozen=True)
class ColorMode:
VTK_COLOR_MODE_DEFAULT: int = 0
VTK_COLOR_MODE_MAP_SCALARS: int = 1
VTK_COLOR_MODE_DIRECT_SCALARS: int = 2
@dataclass(frozen=True)
class ResolveCoincidentTopology:
VTK_RESOLVE_OFF: int = 0
VTK_RESOLVE_POLYGON_OFFSET: int = 1
VTK_RESOLVE_SHIFT_ZBUFFER: int = 2
@dataclass(frozen=True)
class ScalarMode:
VTK_SCALAR_MODE_DEFAULT: int = 0
VTK_SCALAR_MODE_USE_POINT_DATA: int = 1
VTK_SCALAR_MODE_USE_CELL_DATA: int = 2
VTK_SCALAR_MODE_USE_POINT_FIELD_DATA: int = 3
VTK_SCALAR_MODE_USE_CELL_FIELD_DATA: int = 4
VTK_SCALAR_MODE_USE_FIELD_DATA: int = 5
@dataclass(frozen=True)
class TextProperty:
@dataclass(frozen=True)
class Justification:
VTK_TEXT_LEFT: int = 0
VTK_TEXT_CENTERED: int = 1
VTK_TEXT_RIGHT: int = 2
@dataclass(frozen=True)
class VerticalJustification:
VTK_TEXT_BOTTOM: int = 0
VTK_TEXT_CENTERED: int = 1
VTK_TEXT_TOP: int = 2
if __name__ == '__main__':
import sys
main(sys.argv)