From 8a7e1ca1416e61014c31315096343c83e1c8f91e Mon Sep 17 00:00:00 2001 From: Sebastien Jourdain Date: Wed, 24 Aug 2022 15:54:13 -0600 Subject: [PATCH] docs(examples): add validation examples --- .../core/23_pv_interactive_render_exit.py | 59 ++++++++++++++ .../core/23_vtk_interactive_render_exit.py | 81 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 examples/validation/core/23_pv_interactive_render_exit.py create mode 100644 examples/validation/core/23_vtk_interactive_render_exit.py diff --git a/examples/validation/core/23_pv_interactive_render_exit.py b/examples/validation/core/23_pv_interactive_render_exit.py new file mode 100644 index 00000000..e27601de --- /dev/null +++ b/examples/validation/core/23_pv_interactive_render_exit.py @@ -0,0 +1,59 @@ +# To run with Paraview +# pvpython ./examples/validation/core/23_pv_interactive_render_exit.py --venv .venv +import paraview.web.venv + +from trame.app import get_server +from trame.widgets import html, paraview +from trame.ui.html import DivLayout + +# ----------------------------------------------------------------------------- +# VTK +# ----------------------------------------------------------------------------- +from paraview import simple + +DEFAULT_RESOLUTION = 8 + +cone = simple.Cone() +sphere = simple.Sphere() + +simple.Show(cone) +simple.Show(sphere) + +view = simple.Render() + +# ----------------------------------------------------------------------------- +# Web App setup +# ----------------------------------------------------------------------------- + +server = get_server() +state, ctrl = server.state, server.controller + +with DivLayout(server) as layout: + container = layout.root + container.style = "width: 100vw; height: 100vh;" + html_view = paraview.VtkRemoteView(view, interactive_ratio=1, interactive_quality=0) + ctrl.view_update = html_view.update + html.Input( + type="range", + min=8, + max=1000, + step=1, + v_model=("resolution", 8), + style="position: absolute; top: 20px; left: 20px; z-index: 1; width: 25%; min-width: 300px;", + ) + + +@state.change("resolution") +def update_resolution(resolution, **kwargs): + cone.Resolution = int(resolution) + sphere.PhiResolution = int(resolution) + sphere.ThetaResolution = int(resolution) + ctrl.view_update() + + +# ----------------------------------------------------------------------------- +# start server +# ----------------------------------------------------------------------------- + +if __name__ == "__main__": + server.start() diff --git a/examples/validation/core/23_vtk_interactive_render_exit.py b/examples/validation/core/23_vtk_interactive_render_exit.py new file mode 100644 index 00000000..605c59c9 --- /dev/null +++ b/examples/validation/core/23_vtk_interactive_render_exit.py @@ -0,0 +1,81 @@ +from trame.app import get_server +from trame.widgets import html, vtk +from trame.ui.html import DivLayout + +# ----------------------------------------------------------------------------- +# VTK +# ----------------------------------------------------------------------------- + +from vtkmodules.vtkFiltersSources import vtkSphereSource, vtkConeSource +from vtkmodules.vtkRenderingCore import ( + vtkRenderer, + vtkRenderWindow, + vtkRenderWindowInteractor, + vtkPolyDataMapper, + vtkActor, +) +import vtkmodules.vtkRenderingOpenGL2 # noqa +from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch # noqa + +DEFAULT_RESOLUTION = 6 + +renderer = vtkRenderer() +renderWindow = vtkRenderWindow() +renderWindow.AddRenderer(renderer) + +renderWindowInteractor = vtkRenderWindowInteractor() +renderWindowInteractor.SetRenderWindow(renderWindow) +renderWindowInteractor.GetInteractorStyle().SetCurrentStyleToTrackballCamera() + + +def show(source): + mapper = vtkPolyDataMapper() + actor = vtkActor() + mapper.SetInputConnection(source.GetOutputPort()) + actor.SetMapper(mapper) + renderer.AddActor(actor) + return source + + +cone = show(vtkConeSource()) +sphere = show(vtkSphereSource()) + +renderer.ResetCamera() +renderWindow.Render() + +# ----------------------------------------------------------------------------- +# Web App setup +# ----------------------------------------------------------------------------- + +server = get_server() +state, ctrl = server.state, server.controller + +with DivLayout(server) as layout: + container = layout.root + container.style = "width: 100vw; height: 100vh;" + view = vtk.VtkRemoteView(renderWindow, interactive_ratio=1, interactive_quality=0) + ctrl.view_update = view.update + html.Input( + type="range", + min=8, + max=1000, + step=1, + v_model=("resolution", 8), + style="position: absolute; top: 20px; left: 20px; z-index: 1; width: 25%; min-width: 300px;", + ) + + +@state.change("resolution") +def update_resolution(resolution, **kwargs): + cone.SetResolution(int(resolution)) + sphere.SetPhiResolution(int(resolution)) + sphere.SetThetaResolution(int(resolution)) + ctrl.view_update() + + +# ----------------------------------------------------------------------------- +# start server +# ----------------------------------------------------------------------------- + +if __name__ == "__main__": + server.start()