|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from __future__ import print_function, division |
| 4 | +import vtk |
| 5 | + |
| 6 | +def main(filename, curvature=0, scalarRange=None, scheme=None): |
| 7 | + print("Loading", filename) |
| 8 | + reader = vtk.vtkXMLPolyDataReader() |
| 9 | + reader.SetFileName(filename) |
| 10 | + |
| 11 | + curvaturesFilter = vtk.vtkCurvatures() |
| 12 | + curvaturesFilter.SetInputConnection(reader.GetOutputPort()) |
| 13 | + if curvature == 0: |
| 14 | + curvaturesFilter.SetCurvatureTypeToMinimum() |
| 15 | + elif curvature == 1: |
| 16 | + curvaturesFilter.SetCurvatureTypeToMaximum() |
| 17 | + elif curvature == 2: |
| 18 | + curvaturesFilter.SetCurvatureTypeToGaussian() |
| 19 | + else: |
| 20 | + curvaturesFilter.SetCurvatureTypeToMean() |
| 21 | + curvaturesFilter.Update() |
| 22 | + |
| 23 | + # Get scalar range from command line if present, otherwise use |
| 24 | + # range of computed curvature |
| 25 | + if scalarRange is None: |
| 26 | + scalarRange = curvaturesFilter.GetOutput().GetScalarRange() |
| 27 | + |
| 28 | + # Build a lookup table |
| 29 | + if scheme is None: |
| 30 | + scheme = 16 |
| 31 | + colorSeries = vtk.vtkColorSeries() |
| 32 | + colorSeries.SetColorScheme(scheme) |
| 33 | + print("Using color scheme #:", colorSeries.GetColorScheme(), \ |
| 34 | + "is", colorSeries.GetColorSchemeName()) |
| 35 | + |
| 36 | + lut = vtk.vtkColorTransferFunction() |
| 37 | + lut.SetColorSpaceToHSV() |
| 38 | + |
| 39 | + # Use a color series to create a transfer function |
| 40 | + numColors = colorSeries.GetNumberOfColors() |
| 41 | + for i in range(numColors): |
| 42 | + color = colorSeries.GetColor(i) |
| 43 | + dColor = [color[0]/255.0, color[1]/255.0, color[2]/255.0] |
| 44 | + t = scalarRange[0] + (scalarRange[1] - scalarRange[0]) / (numColors - 1) * i |
| 45 | + lut.AddRGBPoint(t, dColor[0], dColor[1], dColor[2]) |
| 46 | + |
| 47 | + # Create a mapper and actor |
| 48 | + mapper = vtk.vtkPolyDataMapper() |
| 49 | + mapper.SetInputConnection(curvaturesFilter.GetOutputPort()) |
| 50 | + mapper.SetLookupTable(lut) |
| 51 | + mapper.SetScalarRange(scalarRange) |
| 52 | + |
| 53 | + actor = vtk.vtkActor() |
| 54 | + actor.SetMapper(mapper) |
| 55 | + |
| 56 | + # Create a scalar bar |
| 57 | + print("Displaying", curvaturesFilter.GetOutput().GetPointData().GetScalars().GetName()) |
| 58 | + scalarBarActor = vtk.vtkScalarBarActor() |
| 59 | + scalarBarActor.SetLookupTable(mapper.GetLookupTable()) |
| 60 | + scalarBarActor.SetTitle( |
| 61 | + curvaturesFilter.GetOutput().GetPointData().GetScalars().GetName()) |
| 62 | + |
| 63 | + scalarBarActor.SetNumberOfLabels(5) |
| 64 | + |
| 65 | + # Create a renderer, render window, and interactor |
| 66 | + renderer = vtk.vtkRenderer() |
| 67 | + renderWindow = vtk.vtkRenderWindow() |
| 68 | + renderWindow.AddRenderer(renderer) |
| 69 | + renderWindowInteractor = vtk.vtkRenderWindowInteractor() |
| 70 | + renderWindowInteractor.SetRenderWindow(renderWindow) |
| 71 | + |
| 72 | + # Add the actors to the scene |
| 73 | + renderer.AddActor(actor) |
| 74 | + renderer.AddActor2D(scalarBarActor) |
| 75 | + |
| 76 | + renderer.SetBackground(.1, .2, .3) # Background color blue |
| 77 | + |
| 78 | + # Render and interact |
| 79 | + renderWindow.Render() |
| 80 | + renderWindowInteractor.Start() |
| 81 | + |
| 82 | + |
| 83 | +def get_program_parameters(): |
| 84 | + import argparse |
| 85 | + description = 'Computes the curvature of a polydata surface.' |
| 86 | + epilogue = ''' |
| 87 | + filename=./src/Testing/Data/cowHead.vtp |
| 88 | + curvature: 0=Min, 1=Max, 2=Gauss, 3=Mean |
| 89 | + ''' |
| 90 | + parser = argparse.ArgumentParser(description=description, epilog=epilogue, |
| 91 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 92 | + parser.add_argument('filename', help='Filename.vtp') |
| 93 | + parser.add_argument('curvature', type=int, help='Int value') |
| 94 | + parser.add_argument('scalarRangeLow', nargs='?', type=float, help='Float value') |
| 95 | + parser.add_argument('scalarRangeHigh', nargs='?', type=float, help='Float value') |
| 96 | + parser.add_argument('colorScheme', nargs='?', type=int, help='Int value') |
| 97 | + args = parser.parse_args() |
| 98 | + scalarRange = None |
| 99 | + if args.scalarRangeLow and args.scalarRangeHigh: |
| 100 | + scalarRange = (args.scalarRangeLow, args.scalarRangeHigh) |
| 101 | + return args.filename, args.curvature, scalarRange, args.colorScheme |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main(*get_program_parameters()) |
0 commit comments