-
Notifications
You must be signed in to change notification settings - Fork 159
Ported GradientFilter from Cxx to Python #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FlorianFritz
wants to merge
4
commits into
lorensen:master
Choose a base branch
from
FlorianFritz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from __future__ import print_function, division | ||
import vtk | ||
|
||
def main(filename, curvature=0, scalarRange=None, scheme=None): | ||
print("Loading", filename) | ||
reader = vtk.vtkXMLPolyDataReader() | ||
reader.SetFileName(filename) | ||
|
||
curvaturesFilter = vtk.vtkCurvatures() | ||
curvaturesFilter.SetInputConnection(reader.GetOutputPort()) | ||
if curvature == 0: | ||
curvaturesFilter.SetCurvatureTypeToMinimum() | ||
elif curvature == 1: | ||
curvaturesFilter.SetCurvatureTypeToMaximum() | ||
elif curvature == 2: | ||
curvaturesFilter.SetCurvatureTypeToGaussian() | ||
else: | ||
curvaturesFilter.SetCurvatureTypeToMean() | ||
curvaturesFilter.Update() | ||
8000 |
|
|
# Get scalar range from command line if present, otherwise use | ||
# range of computed curvature | ||
if scalarRange is None: | ||
scalarRange = curvaturesFilter.GetOutput().GetScalarRange() | ||
|
||
# Build a lookup table | ||
if scheme is None: | ||
scheme = 16 | ||
colorSeries = vtk.vtkColorSeries() | ||
colorSeries.SetColorScheme(scheme) | ||
print("Using color scheme #:", colorSeries.GetColorScheme(), \ | ||
"is", colorSeries.GetColorSchemeName()) | ||
|
||
lut = vtk.vtkColorTransferFunction() | ||
lut.SetColorSpaceToHSV() | ||
|
||
# Use a color series to create a transfer function | ||
numColors = colorSeries.GetNumberOfColors() | ||
for i in range(numColors): | ||
color = colorSeries.GetColor(i) | ||
dColor = [color[0]/255.0, color[1]/255.0, color[2]/255.0] | ||
t = scalarRange[0] + (scalarRange[1] - scalarRange[0]) / (numColors - 1) * i | ||
lut.AddRGBPoint(t, dColor[0], dColor[1], dColor[2]) | ||
|
||
# Create a mapper and actor | ||
mapper = vtk.vtkPolyDataMapper() | ||
mapper.SetInputConnection(curvaturesFilter.GetOutputPort()) | ||
mapper.SetLookupTable(lut) | ||
mapper.SetScalarRange(scalarRange) | ||
|
||
actor = vtk.vtkActor() | ||
actor.SetMapper(mapper) | ||
|
||
# Create a scalar bar | ||
print("Displaying", curvaturesFilter.GetOutput().GetPointData().GetScalars().GetName()) | ||
scalarBarActor = vtk.vtkScalarBarActor() | ||
scalarBarActor.SetLookupTable(mapper.GetLookupTable()) | ||
scalarBarActor.SetTitle( | ||
curvaturesFilter.GetOutput().GetPointData().GetScalars().GetName()) | ||
|
||
scalarBarActor.SetNumberOfLabels(5) | ||
|
||
# Create a renderer, render window, and interactor | ||
renderer = vtk.vtkRenderer() | ||
renderWindow = vtk.vtkRenderWindow() | ||
renderWindow.AddRenderer(renderer) | ||
renderWindowInteractor = vtk.vtkRenderWindowInteractor() | ||
renderWindowInteractor.SetRenderWindow(renderWindow) | ||
|
||
# Add the actors to the scene | ||
renderer.AddActor(actor) | ||
renderer.AddActor2D(scalarBarActor) | ||
|
||
renderer.SetBackground(.1, .2, .3) # Background color blue | ||
|
||
# Render and interact | ||
renderWindow.Render() | ||
renderWindowInteractor.Start() | ||
|
||
|
||
def get_program_parameters(): | ||
import argparse | ||
description = 'Computes the curvature of a polydata surface.' | ||
epilogue = ''' | ||
filename=./src/Testing/Data/cowHead.vtp | ||
curvature: 0=Min, 1=Max, 2=Gauss, 3=Mean | ||
''' | ||
parser = argparse.ArgumentParser(description=description, epilog=epilogue, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument('filename', help='Filename.vtp') | ||
parser.add_argument('curvature', type=int, help='Int value') | ||
parser.add_argument('scalarRangeLow', nargs='?', type=float, help='Float value') | ||
parser.add_argument('scalarRangeHigh', nargs='?', type=float, help='Float value') | ||
parser.add_argument('colorScheme', nargs='?', type=int, help='Int value') | ||
args = parser.parse_args() | ||
scalarRange = None | ||
if args.scalarRangeLow and args.scalarRangeHigh: | ||
scalarRange = (args.scalarRangeLow, args.scalarRangeHigh) | ||
return args.filename, args.curvature, scalarRange, args.colorScheme | ||
|
||
if __name__ == "__main__": | ||
main(*get_program_parameters()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import vtk | ||
|
||
def main(): | ||
# Create a sphere | ||
sphereSource = vtk.vtkSphereSource() | ||
sphereSource.Update() | ||
|
||
dijkstra = vtk.vtkDijkstraGraphGeodesicPath() | ||
dijkstra.SetInputConnection(sphereSource.GetOutputPort()) | ||
dijkstra.SetStartVertex(0) | ||
dijkstra.SetEndVertex(10) | ||
dijkstra.Update() | ||
|
||
# Create a mapper and actor | ||
pathMapper = vtk.vtkPolyDataMapper() | ||
pathMapper.SetInputConnection(dijkstra.GetOutputPort()) | ||
|
||
pathActor = vtk.vtkActor() | ||
pathActor.SetMapper(pathMapper) | ||
pathActor.GetProperty().SetColor(1,0,0) # Red | ||
pathActor.GetProperty().SetLineWidth(4) | ||
|
||
# Create a mapper and actor | ||
mapper = vtk.vtkPolyDataMapper() | ||
mapper.SetInputConnection(sphereSource.GetOutputPort()) | ||
|
||
actor = vtk.vtkActor() | ||
actor.SetMapper(mapper) | ||
|
||
# Create a renderer, render window, and interactor | ||
renderer = vtk.vtkRenderer() | ||
renderWindow = vtk.vtkRenderWindow() | ||
renderWindow.AddRenderer(renderer) | ||
renderWindowInteractor = vtk.vtkRenderWindowInteractor() | ||
renderWindowInteractor.SetRenderWindow(renderWindow) | ||
|
||
# Add the actor to the scene | ||
renderer.AddActor(actor) | ||
renderer.AddActor(pathActor) | ||
renderer.SetBackground(.3, .6, .3) # Background color green | ||
|
||
# Render and interact | ||
renderWindow.Render() | ||
renderWindowInteractor.Start() | ||
|
||
return 1 | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import vtk | ||
|
||
def main(filename): | ||
print("Loading", filename) | ||
reader = vtk.vtkUnstructuredGridReader() | ||
reader.SetFileName(filename) | ||
|
||
edges = vtk.vtkExtractEdges() | ||
edges.SetInputConnection(reader.GetOutputPort()) | ||
|
||
tubes = vtk.vtkTubeFilter() | ||
tubes.SetInputConnection(edges.GetOutputPort()) | ||
tubes.SetRadius(0.0625) | ||
tubes.SetVaryRadiusToVaryRadiusOff() | ||
tubes.SetNumberOfSides(32) | ||
|
||
tubesMapper = vtk.vtkPolyDataMapper() | ||
tubesMapper.SetInputConnection(tubes.GetOutputPort()) | ||
tubesMapper.SetScalarRange(0.0, 26.0) | ||
|
||
tubesActor = vtk.vtkActor() | ||
tubesActor.SetMapper(tubesMapper) | ||
|
||
gradients = vtk.vtkGradientFilter() | ||
gradients.SetInputConnection(reader.GetOutputPort()) | ||
|
||
vectors = vtk.vtkAssignAttribute() | ||
vectors.SetInputConnection(gradients.GetOutputPort()) | ||
vectors.Assign("Gradients", vtk.vtkDataSetAttributes.VECTORS, \ | ||
vtk.vtkAssignAttribute.POINT_DATA) | ||
|
||
arrow = vtk.vtkArrowSource() | ||
|
||
glyphs = vtk.vtkGlyph3D() | ||
glyphs.SetInputConnection(0, vectors.GetOutputPort()) | ||
glyphs.SetInputConnection(1, arrow.GetOutputPort()) | ||
glyphs.ScalingOn() | ||
glyphs.SetScaleModeToScaleByVector() | ||
glyphs.SetScaleFactor(0.25) | ||
glyphs.OrientOn() | ||
glyphs.ClampingOff() | ||
glyphs.SetVectorModeToUseVector() | ||
glyphs.SetIndexModeToOff() | ||
|
||
glyphMapper = vtk.vtkPolyDataMapper() | ||
glyphMapper.SetInputConnection(glyphs.GetOutputPort()) | ||
glyphMapper.ScalarVisibilityOff() | ||
|
||
glyphActor = vtk.vtkActor() | ||
glyphActor.SetMapper(glyphMapper) | ||
|
||
renderer = vtk.vtkRenderer() | ||
renderer.AddActor(tubesActor) | ||
renderer.AddActor(glyphActor) | ||
renderer.SetBackground(0.328125, 0.347656, 0.425781) | ||
|
||
renwin = vtk.vtkRenderWindow() | ||
renwin.AddRenderer(renderer) | ||
renwin.SetSize(350, 500) | ||
|
||
renderer.ResetCamera() | ||
camera = renderer.GetActiveCamera() | ||
camera.Elevation(-80.0) | ||
camera.OrthogonalizeViewUp() | ||
camera.Azimuth(135.0) | ||
|
||
iren = vtk.vtkRenderWindowInteractor() | ||
iren.SetRenderWindow(renwin) | ||
iren.Initialize() | ||
iren.Start() | ||
return 1 | ||
|
||
def get_program_parameters(): | ||
import argparse | ||
description = 'Computes the gradient of a scalar field defined on the points of a data set.' | ||
epilogue = ''' | ||
filename=./src/Testing/Data/uGridEx.vtk | ||
''' | ||
parser = argparse.ArgumentParser(description=description, epilog=epilogue, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument('filename', help='Filename.vtk') | ||
args = parser.parse_args() | ||
return args.filename | ||
|
||
if __name__ == "__main__": | ||
main(get_program_parameters()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import vtk | ||
|
||
def main(): | ||
# Generate some random points | ||
numberOfPoints = 8 | ||
pointSource = vtk.vtkPointSource() | ||
pointSource.SetNumberOfPoints(numberOfPoints) | ||
pointSource.Update() | ||
|
||
points = pointSource.GetOutput().GetPoints() | ||
|
||
spline = vtk.vtkParametricSpline() | ||
spline.SetPoints(points) | ||
|
||
functionSource = vtk.vtkParametricFunctionSource() | ||
functionSource.SetParametricFunction(spline) | ||
functionSource.SetUResolution(10 * numberOfPoints) | ||
functionSource.SetVResolution(10 * numberOfPoints) | ||
functionSource.SetWResolution(10 * numberOfPoints) | ||
|
||
# Create the frame | ||
frame = vtk.vtkFrenetSerretFrame() | ||
frame.SetInputConnection(functionSource.GetOutputPort()) | ||
frame.ConsistentNormalsOn() | ||
frame.Update() | ||
|
||
# Setup renderer | ||
renderer = vtk.vtkRenderer() | ||
renderer.SetBackground(.4, .5, .7) | ||
|
||
# for each vector, create a Glyph3D and DeepCopy the output | ||
arrow_radius = .05 | ||
|
||
for vector, color in zip(["FSNormals", "FSTangents","FSBinormals"], [(0.8900, 0.8100, 0.3400), (1.0000, 0.3882, 0.2784), (0.1804,0.5451,0.3412)]): | ||
polyData = MakeGlyphs(frame, arrow_radius, vector) | ||
|
||
# Setup actors and mappers | ||
glyphMapper = vtk.vtkPolyDataMapper() | ||
glyphMapper.SetInputData(normalsPolyData) | ||
|
||
glyphActor = vtk.vtkActor() | ||
glyphActor.SetMapper(mapper) | ||
glyphActor.GetProperty().SetColor(color) | ||
|
||
renderer.AddActor(glyphActor) | ||
|
||
# Display spline | ||
mapper = vtk.vtkPolyDataMapper() | ||
mapper.SetInputConnection(functionSource.GetOutputPort()) | ||
actor = vtk.vtkActor() | ||
actor.SetMapper(mapper) | ||
renderer.AddActor(actor) | ||
|
||
# Setup render window, and interactor | ||
renderWindow = vtk.vtkRenderWindow() | ||
renderWindow.AddRenderer(renderer) | ||
renderWindowInteractor = vtk.vtkRenderWindowInteractor() | ||
renderWindowInteractor.SetRenderWindow(renderWindow) | ||
|
||
# Pick a good view | ||
renderer.ResetCamera() | ||
renderer.GetActiveCamera().Azimuth(120) | ||
renderer.GetActiveCamera().Elevation(30) | ||
renderer.GetActiveCamera().Dolly(1.8) | ||
renderer.ResetCameraClippingRange() | ||
|
||
renderWindow.SetSize(640, 480) | ||
renderWindow.Render() | ||
renderWindowInteractor.Start() | ||
|
||
def MakeGlyphs(frame, arrow_size, vector_name) | ||
frame.GetOutput().GetPointData().SetActiveVectors(vector_name) | ||
srcPolyData = frame.GetOutput() | ||
|
||
arrow = vtk.vtkArrowSource() | ||
arrow.SetTipResolution(16) | ||
arrow.SetTipLength(.3) | ||
arrow.SetTipRadius(.1) | ||
|
||
glyph = vtk.vtkGlyph3D() | ||
glyph.SetSourceConnection(arrow.GetOutputPort()) | ||
glyph.SetInputData(srcPolyData) | ||
glyph.SetVectorModeToUseVector() | ||
glyph.SetScaleModeToScaleByVector() | ||
glyph.SetScaleFactor(size) | ||
glyph.OrientOn() | ||
glyph.Update() | ||
|
||
normalsPolyData = vtk.vtkPolyData() | ||
normalsPolyData.DeepCopy(glyph.GetOutput()) | ||
|
||
return normalsPolyData |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This return statement is not needed.