diff --git a/src/Python.md b/src/Python.md index 4cb867d5d2e1c3a4a771d226c882606905e20c1b..766c3f302e8a9688897e1c23fa0b2376f9d394b0 100644 --- a/src/Python.md +++ b/src/Python.md @@ -178,6 +178,7 @@ If you are new to VTK then these [tutorials](#tutorial) will help to get you sta | Example Name | Description | Image | | -------------- | ------------- | ------- | [BooleanOperationImplicitFunctions](/Python/ImplicitFunctions/BooleanOperationImplicitFunctions) | Demonstrate booleans of two different implicit functions +[ContourTriangulator](/Python/Modelling/ContourTriangulator) | Create a contour from a structured point set (image) and triangulate it. [CutWithScalars](/Python/VisualizationAlgorithms/CutWithScalars) | Cut a surface with scalars. [DiscreteMarchingCubes](/Cxx/Python/DiscreteMarchingCubes) | Generate surfaces from labeled data. [ExtractData](/Python/VisualizationAlgorithms/ExtractData) | Implicit functions used to select data: Two ellipsoids are combined using the union operation used to select voxels from a volume. Voxels are shrunk 50 percent. diff --git a/src/Python/Modelling/ContourTriangulator.py b/src/Python/Modelling/ContourTriangulator.py new file mode 100755 index 0000000000000000000000000000000000000000..23c90f7c3f3be8a3b73cbc1151f4f1ef9f787333 --- /dev/null +++ b/src/Python/Modelling/ContourTriangulator.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 + + +# noinspection PyUnresolvedReferences +import vtkmodules.vtkInteractionStyle +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkFiltersCore import vtkMarchingSquares +from vtkmodules.vtkFiltersGeneral import vtkContourTriangulator +from vtkmodules.vtkIOImage import vtkPNGReader +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkDataSetMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) + + +def get_program_parameters(): + import argparse + description = 'Create a contour from a structured point set (image) and triangulate it.' + epilogue = ''' + Try with different iso values e.g. -i1000. + ''' + parser = argparse.ArgumentParser(description=description, epilog=epilogue) + parser.add_argument('file_name', help='The path to the image file to use e.g fullhead15.png.') + parser.add_argument('-i', '--iso_value', help='The contour value for generating the isoline.', default=500, + type=int) + args = parser.parse_args() + return args.file_name, args.iso_value + + +def main(): + file_name, iso_value = get_program_parameters() + + colors = vtkNamedColors() + + reader = vtkPNGReader() + if not reader.CanReadFile(file_name): + print('Error: Could not read', file_name) + return + reader.SetFileName(file_name) + reader.Update() + + iso = vtkMarchingSquares() + iso.SetInputConnection(reader.GetOutputPort()) + iso.SetValue(0, iso_value) + + iso_mapper = vtkDataSetMapper() + iso_mapper.SetInputConnection(iso.GetOutputPort()) + iso_mapper.ScalarVisibilityOff() + + iso_actor = vtkActor() + iso_actor.SetMapper(iso_mapper) + iso_actor.GetProperty().SetColor( + colors.GetColor3d('MediumOrchid')) + + poly = vtkContourTriangulator() + poly.SetInputConnection(iso.GetOutputPort()) + + poly_mapper = vtkDataSetMapper() + poly_mapper.SetInputConnection(poly.GetOutputPort()) + poly_mapper.ScalarVisibilityOff() + + poly_actor = vtkActor() + poly_actor.SetMapper(poly_mapper) + poly_actor.GetProperty().SetColor(colors.GetColor3d('Gray')) + + # Standard rendering classes. + renderer = vtkRenderer() + ren_win = vtkRenderWindow() + ren_win.SetMultiSamples(0) + ren_win.AddRenderer(renderer) + ren_win.SetWindowName('ContourTriangulator') + + iren = vtkRenderWindowInteractor() + iren.SetRenderWindow(ren_win) + + renderer.AddActor(poly_actor) + renderer.AddActor(iso_actor) + renderer.SetBackground(colors.GetColor3d('DarkSlateGray')) + ren_win.SetSize(300, 300) + + camera = renderer.GetActiveCamera() + renderer.ResetCamera() + camera.Azimuth(180) + + ren_win.Render() + iren.Initialize() + iren.Start() + + +if __name__ == '__main__': + main() diff --git a/src/Testing/Baseline/Python/Modelling/TestContourTriangulator.png b/src/Testing/Baseline/Python/Modelling/TestContourTriangulator.png new file mode 100644 index 0000000000000000000000000000000000000000..4c5ec8c369afac9f3802947b8ddaf4b3b84b4f91 --- /dev/null +++ b/src/Testing/Baseline/Python/Modelling/TestContourTriangulator.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ddcaa40a72049a4fa9153e9c23aba5ac8931e224c8e8f0cad665ebdeae961369 +size 2947