diff --git a/src/PythonicAPI.md b/src/PythonicAPI.md index ba9fbbd4b138f934847b3cf83a2f7cf833f7f6a3..e2012514e41652d9d682980a40ee0ba3348f795c 100644 --- a/src/PythonicAPI.md +++ b/src/PythonicAPI.md @@ -124,6 +124,11 @@ This section includes examples of manipulating meshes. #### ?vtkExplicitStructuredGrid? +| Example Name | Description | Image | +| ------------ | ----------- | ----- | +[CreateESGrid](/PythonicAPI/ExplicitStructuredGrid/CreateESGrid) | Create an explicit structured grid and convert this to an unstructured grid or vice versa. +[LoadESGrid](/PythonicAPI/ExplicitStructuredGrid/LoadESGrid) | Load a VTU file and convert the dataset to an explicit structured grid. + #### ?vtkStructuredGrid? #### ?vtkStructuredPoints? diff --git a/src/PythonicAPI/ExplicitStructuredGrid/CreateESGrid.md b/src/PythonicAPI/ExplicitStructuredGrid/CreateESGrid.md new file mode 100644 index 0000000000000000000000000000000000000000..fff10923fc525ab7ee71fa482171e47def7e1769 --- /dev/null +++ b/src/PythonicAPI/ExplicitStructuredGrid/CreateESGrid.md @@ -0,0 +1,6 @@ +### Description + +This example demonstrates how to: + +1. Create an explicit structured grid. +2. Convert an explicit structured grid into an unstructured grid or vice versa. diff --git a/src/PythonicAPI/ExplicitStructuredGrid/CreateESGrid.py b/src/PythonicAPI/ExplicitStructuredGrid/CreateESGrid.py new file mode 100755 index 0000000000000000000000000000000000000000..6f50858e90881ab53d2911533c6f208ad4b3002a --- /dev/null +++ b/src/PythonicAPI/ExplicitStructuredGrid/CreateESGrid.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 + +import numpy as np + +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkCommonCore import vtkPoints +from vtkmodules.vtkCommonDataModel import ( + vtkCellArray, + vtkExplicitStructuredGrid +) +from vtkmodules.vtkFiltersCore import ( + vtkExplicitStructuredGridToUnstructuredGrid, + vtkUnstructuredGridToExplicitStructuredGrid +) +from vtkmodules.vtkInteractionStyle import vtkInteractorStyleRubberBandPick +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkDataSetMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) + + +def create_explicit_structured_grid(dimensions, spacing=(1, 1, 1)): + """Create an explicit structured grid. + + Parameters + ---------- + dimensions : tuple(int, int, int) + The number of points in the (I, J, K) directions. + spacing : tuple(int, int, int) + The spacing between points in the (I, J, K) directions. + + Returns + ------- + grid : vtkExplicitStructuredGrid + An explicit structured grid. + + """ + ni, nj, nk = dimensions + si, sj, sk = spacing + + points = vtkPoints() + for z in range(0, nk * sk, sk): + for y in range(0, nj * sj, sj): + for x in range(0, ni * si, si): + points.InsertNextPoint((x, y, z)) + + cells = vtkCellArray() + for k in range(0, nk - 1): + for j in range(0, nj - 1): + for i in range(0, ni - 1): + multi_index = ([i, i + 1, i + 1, i, i, i + 1, i + 1, i], + [j, j, j + 1, j + 1, j, j, j + 1, j + 1], + [k, k, k, k, k + 1, k + 1, k + 1, k + 1]) + pts = np.ravel_multi_index(multi_index, dimensions, order='F') + cells.InsertNextCell(8, pts) + + return vtkExplicitStructuredGrid(dimensions=(ni, nj, nk), points=points, cells=cells) + + +def convert_to_unstructured_grid(grid): + """Convert explicit structured grid to unstructured grid. + + Parameters + ---------- + grid : vtkExplicitStructuredGrid + An explicit structured grid. + + Returns + ------- + vtkUnstructuredGrid + An unstructured grid. + + """ + converter = vtkExplicitStructuredGridToUnstructuredGrid() + return (grid >> converter).update().output + + +def convert_to_explicit_structured_grid(grid): + """Convert unstructured grid to explicit structured grid. + + Parameters + ---------- + grid : UnstructuredGrid + An unstructured grid. + + Returns + ------- + vtkExplicitStructuredGrid + An explicit structured grid. The ``'BLOCK_I'``, ``'BLOCK_J'`` and + ``'BLOCK_K'`` cell arrays are required. + + """ + converter = vtkUnstructuredGridToExplicitStructuredGrid() + input_arrays_to_process = ((0, 0, 0, 1, 'BLOCK_I'), (1, 0, 0, 1, 'BLOCK_J'), (2, 0, 0, 1, 'BLOCK_K')) + for input_array in input_arrays_to_process: + converter.SetInputArrayToProcess(*input_array) + return grid >> converter + + +def main(): + grid = create_explicit_structured_grid((5, 6, 7), (20, 10, 1)) + grid = convert_to_unstructured_grid(grid) + grid = convert_to_explicit_structured_grid(grid) + + colors = vtkNamedColors() + + mapper = vtkDataSetMapper() + + actor = vtkActor(mapper=mapper) + actor.property.edge_visibility = True + actor.property.lighting = False + actor.property.color = colors.GetColor3d('Seashell') + + grid >> mapper + + renderer = vtkRenderer(background=colors.GetColor3d('DarkSlateGray')) + renderer.AddActor(actor) + + window = vtkRenderWindow(size=(1024, 768), window_name='CreateESGrid') + window.AddRenderer(renderer) + window.Render() + + camera = renderer.GetActiveCamera() + camera.SetPosition(8.383354, -72.468670, 94.262605) + camera.SetFocalPoint(42.295234, 21.111537, -0.863606) + camera.SetViewUp(0.152863, 0.676710, 0.720206) + camera.SetDistance(137.681759) + camera.SetClippingRange(78.173985, 211.583658) + + interactor = vtkRenderWindowInteractor() + interactor.SetRenderWindow(window) + interactor.SetInteractorStyle(vtkInteractorStyleRubberBandPick()) + window.Render() + interactor.Start() + + +if __name__ == '__main__': + main() diff --git a/src/PythonicAPI/ExplicitStructuredGrid/LoadESGrid.md b/src/PythonicAPI/ExplicitStructuredGrid/LoadESGrid.md new file mode 100644 index 0000000000000000000000000000000000000000..2915887699d5444e955f2d5dee0b70754e694e74 --- /dev/null +++ b/src/PythonicAPI/ExplicitStructuredGrid/LoadESGrid.md @@ -0,0 +1,3 @@ +### Description + +This example displays the [UNISIM-II-D reservoir model](https://www.unisim.cepetro.unicamp.br/benchmarks/en/unisim-ii/unisim-ii-d) using the vtkExplicitStructuredGrid class. diff --git a/src/PythonicAPI/ExplicitStructuredGrid/LoadESGrid.py b/src/PythonicAPI/ExplicitStructuredGrid/LoadESGrid.py new file mode 100755 index 0000000000000000000000000000000000000000..7dcd3aa1b1f810b7a6fd746d9739bf5a05bce331 --- /dev/null +++ b/src/PythonicAPI/ExplicitStructuredGrid/LoadESGrid.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +from dataclasses import dataclass + +# noinspection PyUnresolvedReferences +import vtkmodules.vtkRenderingOpenGL2 +from vtkmodules.vtkCommonColor import vtkNamedColors +from vtkmodules.vtkFiltersCore import vtkUnstructuredGridToExplicitStructuredGrid +from vtkmodules.vtkIOXML import vtkXMLUnstructuredGridReader +from vtkmodules.vtkInteractionStyle import vtkInteractorStyleRubberBandPick +from vtkmodules.vtkRenderingCore import ( + vtkActor, + vtkDataSetMapper, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkRenderer +) + + +def get_program_parameters(argv): + import argparse + description = 'Load an explicit structured grid from a file' + epilogue = ''' + ''' + parser = argparse.ArgumentParser(description=description, epilog=epilogue, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('fn', help='The explicit structured grid file name e.g. UNISIM-II-D.vtu.') + args = parser.parse_args() + return args.fn + + +def main(fn): + reader = vtkXMLUnstructuredGridReader(file_name=fn) + + # global_warning_display=False hides VTK errors. + converter = vtkUnstructuredGridToExplicitStructuredGrid(global_warning_display=False) + input_arrays_to_process = ((0, 0, 0, 1, 'BLOCK_I'), (1, 0, 0, 1, 'BLOCK_J'), (2, 0, 0, 1, 'BLOCK_K')) + for input_array in input_arrays_to_process: + converter.SetInputArrayToProcess(*input_array) + + grid = (reader >> converter).update().output + grid.ComputeFacesConnectivityFlagsArray() + grid.cell_data.SetActiveScalars('ConnectivityFlags') + + scalars = grid.cell_data.GetArray('ConnectivityFlags') + + mapper = vtkDataSetMapper(color_mode=MapperColorMode().VTK_COLOR_MODE_MAP_SCALARS, scalar_range=scalars.range) + + actor = vtkActor(mapper=mapper) + actor.property.EdgeVisibilityOn() + + grid >> mapper + + colors = vtkNamedColors() + + renderer = vtkRenderer() + renderer.AddActor(actor) + renderer.SetBackground(colors.GetColor3d('DimGray')) + + window = vtkRenderWindow() + window.AddRenderer(renderer) + window.SetWindowName('LoadESGrid') + window.SetSize(1024, 768) + window.Render() + + camera = renderer.GetActiveCamera() + camera.SetPosition(312452.407650, 7474760.406373, 3507.364723) + camera.SetFocalPoint(314388.388434, 7481520.509575, -2287.477388) + camera.SetViewUp(0.089920, 0.633216, 0.768734) + camera.SetDistance(9111.926908) + camera.SetClippingRange(595.217338, 19595.429475) + + interactor = vtkRenderWindowInteractor() + interactor.SetRenderWindow(window) + interactor.SetInteractorStyle(vtkInteractorStyleRubberBandPick()) + window.Render() + interactor.Start() + + +@dataclass(frozen=True) +class MapperColorMode: + VTK_COLOR_MODE_DEFAULT: int = 0 + VTK_COLOR_MODE_MAP_SCALARS: int = 1 + VTK_COLOR_MODE_DIRECT_SCALARS: int = 2 + + +if __name__ == '__main__': + import sys + + fn = get_program_parameters(sys.argv) + main(fn) diff --git a/src/Testing/Baseline/PythonicAPI/ExplicitStructuredGrid/TestCreateESGrid.png b/src/Testing/Baseline/PythonicAPI/ExplicitStructuredGrid/TestCreateESGrid.png new file mode 100644 index 0000000000000000000000000000000000000000..d14ecfce4c546ecf38c5b496f78703700c1fd400 --- /dev/null +++ b/src/Testing/Baseline/PythonicAPI/ExplicitStructuredGrid/TestCreateESGrid.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ccfa991483e948bd605252f654e6888c3cc7d1f7014e4cf9e3f543ec31aaf58 +size 106102 diff --git a/src/Testing/Baseline/PythonicAPI/ExplicitStructuredGrid/TestLoadESGrid.png b/src/Testing/Baseline/PythonicAPI/ExplicitStructuredGrid/TestLoadESGrid.png new file mode 100644 index 0000000000000000000000000000000000000000..24400b859d76f36faaa36179908afe200914fec7 --- /dev/null +++ b/src/Testing/Baseline/PythonicAPI/ExplicitStructuredGrid/TestLoadESGrid.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:06521154d65c6b8b30b48b1c6ddc3ee3b7d47db9629d7d2f04ca009f8b21449f +size 467507