Newer
Older
#!/usr/bin/env python
from __future__ import print_function
SURFACE_TYPE = {"PLANE", "SPHERE", "PARAMETRIC_SURFACE"}
def WritePNG(ren, fn, magnification=1):
Save the image as a PNG
:param: ren - the renderer.
:param: fn - the file name.
:param: magnification - the magnification, usually 1.
renLgeIm = vtk.vtkRenderLargeImage()
imgWriter = vtk.vtkPNGWriter()
renLgeIm.SetInput(ren)
renLgeIm.SetMagnification(magnification)
imgWriter.SetInputConnection(renLgeIm.GetOutputPort())
imgWriter.SetFileName(fn)
imgWriter.Write()
def MakeBands(dR, numberOfBands, nearestInteger):
Divide a range into bands
:param: dR - [min, max] the range that is to be covered by the bands.
:param: numberOfBands - the number of bands, a positive integer.
:param: nearestInteger - if True then [floor(min), ceil(max)] is used.
:return: A List consisting of [min, midpoint, max] for each band.
bands = list()
if (dR[1] < dR[0]) or (numberOfBands <= 0):
return bands
x = list(dR)
if nearestInteger:
x[0] = math.floor(x[0])
x[1] = math.ceil(x[1])
dx = (x[1] - x[0]) / float(numberOfBands)
b = [x[0], x[0] + dx / 2.0, x[0] + dx]
i = 0
while i < numberOfBands:
bands.append(b)
b = [b[0] + dx, b[1] + dx, b[2] + dx]
i += 1
return bands
Divide a range into integral bands
:param: dR - [min, max] the range that is to be covered by the bands.
:return: A List consisting of [min, midpoint, max] for each band.
return bands
x = list(dR)
x[0] = math.floor(x[0])
x[1] = math.ceil(x[1])
numberOfBands = int(abs(x[1]) + abs(x[0]))
return MakeBands(x, numberOfBands, False)
Generate elevations over the surface.
:param: src - the vtkPolyData source.
:return: - vtkPolyData source with elevations.
"""
bounds = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
src.GetBounds(bounds)
elevFilter = vtk.vtkElevationFilter()
elevFilter.SetInputData(src)
elevFilter.SetLowPoint(0, bounds[2], 0)
elevFilter.SetHighPoint(0, bounds[3], 0)
elevFilter.SetScalarRange(bounds[2], bounds[3])
elevFilter.Update()
return elevFilter.GetPolyDataOutput()
def MakePlane():
Make a plane as the source.
:return: vtkPolyData with normal and scalar data.
source = vtk.vtkPlaneSource()
source.SetOrigin(-10.0, -10.0, 0.0)
source.SetPoint2(-10.0, 10.0, 0.0)
source.SetPoint1(10.0, -10.0, 0.0)
source.SetXResolution(20)
source.SetYResolution(20)
source.Update()
return MakeElevations(source.GetOutput())
Make a sphere as the source.
:return: vtkPolyData with normal and scalar data.
source = vtk.vtkSphereSource()
source.SetCenter(0.0, 0.0, 0.0)
source.SetRadius(10.0)
source.SetThetaResolution(32)
source.SetPhiResolution(32)
source.Update()
return MakeElevations(source.GetOutput())
Make a parametric surface as the source.
:return: vtkPolyData with normal and scalar data.
fn = vtk.vtkParametricRandomHills()
fn.AllowRandomGenerationOn()
fn.SetRandomSeed(1)
fn.SetNumberOfHills(30)
# Make the normals face out of the surface.
# Not needed with VTK 8.0 or later.
# if fn.GetClassName() == 'vtkParametricRandomHills':
# fn.ClockwiseOrderingOff()
source = vtk.vtkParametricFunctionSource()
source.SetParametricFunction(fn)
source.SetUResolution(50)
source.SetVResolution(50)
source.SetScalarModeToZ()
source.Update()
# Name the arrays (not needed in VTK 6.2+ for vtkParametricFunctionSource)
source.GetOutput().GetPointData().GetNormals().SetName('Normals')
source.GetOutput().GetPointData().GetScalars().SetName('Scalars')
return source.GetOutput()
Make a lookup table using vtkColorSeries.
:return: An indexed lookup table.
# Make the lookup table.
colorSeries = vtk.vtkColorSeries()
# Select a color scheme.
# colorSeriesEnum = colorSeries.BREWER_DIVERGING_BROWN_BLUE_GREEN_9
# colorSeriesEnum = colorSeries.BREWER_DIVERGING_SPECTRAL_10
# colorSeriesEnum = colorSeries.BREWER_DIVERGING_SPECTRAL_3
# colorSeriesEnum = colorSeries.BREWER_DIVERGING_PURPLE_ORANGE_9
# colorSeriesEnum = colorSeries.BREWER_SEQUENTIAL_BLUE_PURPLE_9
# colorSeriesEnum = colorSeries.BREWER_SEQUENTIAL_BLUE_GREEN_9
colorSeriesEnum = colorSeries.BREWER_QUALITATIVE_SET3
colorSeries.SetColorScheme(colorSeriesEnum)
lut = vtk.vtkLookupTable()
colorSeries.BuildLookupTable(lut)
Create a lookup table with the colors reversed.
:param: lut - An indexed lookup table.
:return: The reversed indexed lookup table.
lutr = vtk.vtkLookupTable()
lutr.DeepCopy(lut)
t = lut.GetNumberOfTableValues() - 1
revList = reversed(list(range(t + 1)))
for i in revList:
for i in revList:
lutr.SetAnnotation(t - i, lut.GetAnnotation(i))
return lutr
Count the number of scalars in each band.
:param: bands - the bands.
:param: src - the vtkPolyData source.
:return: The frequencies of the scalars in each band.
freq = dict()
for i in range(len(bands)):
tuples = src.GetPointData().GetScalars().GetNumberOfTuples()
for i in range(tuples):
x = src.GetPointData().GetScalars().GetTuple1(i)
for j in range(len(bands)):
if x <= bands[j][2]:
freq[j] = freq[j] + 1
break
return freq
Glyph the normals on the surface.
You may need to adjust the parameters for maskPts, arrow and glyph for a
nice appearance.
:param: src - the surface to glyph.
:param: reverseNormals - if True the normals on the surface are reversed.
:return: The glyph object.
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Sometimes the contouring algorithm can create a volume whose gradient
# vector and ordering of polygon (using the right hand rule) are
# inconsistent. vtkReverseSense cures this problem.
reverse = vtk.vtkReverseSense()
# Choose a random subset of points.
maskPts = vtk.vtkMaskPoints()
maskPts.SetOnRatio(5)
maskPts.RandomModeOn()
if reverseNormals:
reverse.SetInputData(src)
reverse.ReverseCellsOn()
reverse.ReverseNormalsOn()
maskPts.SetInputConnection(reverse.GetOutputPort())
else:
maskPts.SetInputData(src)
# Source for the glyph filter
arrow = vtk.vtkArrowSource()
arrow.SetTipResolution(16)
arrow.SetTipLength(0.3)
arrow.SetTipRadius(0.1)
glyph = vtk.vtkGlyph3D()
glyph.SetSourceConnection(arrow.GetOutputPort())
glyph.SetInputConnection(maskPts.GetOutputPort())
glyph.SetVectorModeToUseNormal()
glyph.SetScaleFactor(1)
glyph.SetColorModeToColorByVector()
glyph.SetScaleModeToScaleByVector()
glyph.OrientOn()
glyph.Update()
return glyph
Make and display the surface.
:param: st - the surface to display.
:return The vtkRenderWindowInteractor.
print(st, "is not a surface.")
iren = vtk.vtkRenderWindowInteractor()
return iren
# ------------------------------------------------------------
# Create the surface, lookup tables, contour filter etc.
# ------------------------------------------------------------
src = vtk.vtkPolyData()
src = MakeParametricSource()
# The scalars are named "Scalars"by default
# in the parametric surfaces, so change the name.
src.GetPointData().GetScalars().SetName("Elevation")
scalarRange = src.GetScalarRange()
lut = MakeLUT()
lut.SetTableRange(scalarRange)
numberOfBands = lut.GetNumberOfTableValues()
# bands = MakeIntegralBands(scalarRange)
bands = MakeBands(scalarRange, numberOfBands, False)
# Let's do a frequency table.
# The number of scalars in each band.
# We will use the midpoint of the band as the label.
labels = []
for i in range(len(bands)):
labels.append('{:4.2f}'.format(bands[i][1]))
# Annotate
values = vtk.vtkVariantArray()
for i in range(len(labels)):
values.InsertNextValue(vtk.vtkVariant(labels[i]))
for i in range(values.GetNumberOfTuples()):
lut.SetAnnotation(i, values.GetValue(i).ToString())
# Create a lookup table with the colors reversed.
lutr = ReverseLUT(lut)
# Create the contour bands.
bcf = vtk.vtkBandedPolyDataContourFilter()
bcf.SetInputData(src)
# Use either the minimum or maximum value for each band.
for i in range(0, numberOfBands):
bcf.SetValue(i, bands[i][2])
# We will use an indexed lookup table.
bcf.SetScalarModeToIndex()
bcf.GenerateContourEdgesOn()
# Generate the glyphs on the original surface.
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# ------------------------------------------------------------
# Create the mappers and actors
# ------------------------------------------------------------
srcMapper = vtk.vtkPolyDataMapper()
srcMapper.SetInputConnection(bcf.GetOutputPort())
srcMapper.SetScalarRange(scalarRange)
srcMapper.SetLookupTable(lut)
srcMapper.SetScalarModeToUseCellData()
srcActor = vtk.vtkActor()
srcActor.SetMapper(srcMapper)
srcActor.RotateX(-45)
srcActor.RotateZ(45)
# Create contour edges
edgeMapper = vtk.vtkPolyDataMapper()
edgeMapper.SetInputData(bcf.GetContourEdgesOutput())
edgeMapper.SetResolveCoincidentTopologyToPolygonOffset()
edgeActor = vtk.vtkActor()
edgeActor.SetMapper(edgeMapper)
edgeActor.GetProperty().SetColor(0, 0, 0)
edgeActor.RotateX(-45)
edgeActor.RotateZ(45)
glyphMapper = vtk.vtkPolyDataMapper()
glyphMapper.SetInputConnection(glyph.GetOutputPort())
glyphMapper.SetScalarModeToUsePointFieldData()
glyphMapper.SetColorModeToMapScalars()
glyphMapper.ScalarVisibilityOn()
glyphMapper.SelectColorArray('Elevation')
# Colour by scalars.
glyphMapper.SetScalarRange(scalarRange)
glyphActor = vtk.vtkActor()
glyphActor.SetMapper(glyphMapper)
glyphActor.RotateX(-45)
glyphActor.RotateZ(45)
# Add a scalar bar.
scalarBar = vtk.vtkScalarBarActor()
# scalarBar.SetLookupTable(lut)
# Use this LUT if you want the highest value at the top.
scalarBar.SetLookupTable(lutr)
scalarBar.SetTitle('Elevation (m)')
# ------------------------------------------------------------
# Create the RenderWindow, Renderer and Interactor
# ------------------------------------------------------------
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
iren = vtk.vtkRenderWindowInteractor()
renWin.AddRenderer(ren)
iren.SetRenderWindow(renWin)
# add actors
ren.AddViewProp(srcActor)
ren.AddViewProp(edgeActor)
ren.AddViewProp(glyphActor)
ren.AddActor2D(scalarBar)
ren.SetBackground(0.7, 0.8, 1.0)
renWin.SetSize(800, 800)
renWin.Render()
ren.GetActiveCamera().Zoom(1.5)
return iren
# iren = DisplaySurface("PLANE")
# iren = DisplaySurface("SPHERE")
iren = DisplaySurface("PARAMETRIC_SURFACE")
iren.Render()
iren.Start()
# WritePNG(iren.GetRenderWindow().GetRenderers().GetFirstRenderer(),