{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting with VTK Algorithms\n\nPass a `vtkAlgorithm` to the `Plotter` for dynamic visualizations.\n\n::: note\n::: title\nNote\n:::\n\nBy \\\"dynamic visualization\\\" we mean that as the input data/source\nchanges, so will the visualization in real time.\n:::\n\nA `vtkAlgorithm` is the superclass for all sources, filters, and sinks\nin VTK. It defines a generalized interface for executing data processing\nalgorithms. Pipeline connections are associated with input and output\nports that are independent of the type of data passing through the\nconnections.\n\nWe can connect the output port of a `vtkAlgorithm` to PyVista\\'s\nrendering pipeline when adding data to the scene through methods like\n`add_mesh() <pyvista.Plotter.add_mesh>`{.interpreted-text role=\"func\"}.\n\nThis example will walk through using a few `vtkAlgorithm` filters\ndirectly and passing them to PyVista for dynamic visualization.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import vtk\n\nimport pyvista as pv\nfrom pyvista import examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use `vtkConeSource` as a source algorithm. This source will dynamically\ncreate a cone object depending on the instances\\'s parameters. In this\nexample, we will connect a callback to set the cone source algorithm\\'s\nresolution via `vtkConeSource.SetResolution()`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo = pv.ConeSource()\n\n\ndef update_resolution(value):\n \"\"\"Callback to set the resolution of the cone generator.\"\"\"\n res = round(value)\n algo.resolution = res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pass the `vtkConeSource` (a `vtkAlgorithm` subclass) directly to the\nplotter and connect a slider widget to our callback that adjusts the\nresolution.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = pv.Plotter()\np.add_mesh(algo, color='red')\np.add_slider_widget(update_resolution, [5, 100], title='Resolution')\np.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is another example using `vtkRegularPolygonSource`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"poly_source = vtk.vtkRegularPolygonSource()\npoly_source.GeneratePolygonOff()\npoly_source.SetRadius(5.0)\npoly_source.SetCenter(0.0, 0.0, 0.0)\n\n\ndef update_n_sides(value):\n \"\"\"Callback to set the number of sides.\"\"\"\n res = round(value)\n poly_source.SetNumberOfSides(res)\n\n\np = pv.Plotter()\np.add_mesh_clip_box(poly_source, color='red')\np.add_slider_widget(update_n_sides, [3, 25], title='N Sides')\np.view_xy()\np.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Filter Pipeline\n\nWe can do this with any `vtkAlgorithm` subclass for dynamically\ngenerating or filtering data. Here is an example of executing a pipeline\nof VTK filters together.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Source mesh object (static)\nmesh = examples.download_bunny_coarse()\n\n# Initialize VTK algorithm to modify dynamically\nsplatter = vtk.vtkGaussianSplatter()\n\n# Pass PyVista object as input to VTK\nsplatter.SetInputData(mesh)\n\n# Set parameters of splatter filter\nn = 200\nsplatter.SetSampleDimensions(n, n, n)\nsplatter.SetRadius(0.02)\nsplatter.SetExponentFactor(-10)\nsplatter.SetEccentricity(2)\nsplatter.Update()\n\n# Pipe splatter filter into a contour filter\ncontour = vtk.vtkContourFilter()\ncontour.SetInputConnection(splatter.GetOutputPort())\ncontour.SetInputArrayToProcess(0, 0, 0, 0, 'SplatterValues')\ncontour.SetNumberOfContours(1)\ncontour.SetValue(0, 0.95 * splatter.GetRadius())\n\n# Use PyVista to plot output of contour filter\np = pv.Plotter(notebook=0)\np.add_mesh(mesh, style='wireframe')\np.add_mesh(contour, color=True)\np.add_slider_widget(splatter.SetRadius, [0.01, 0.05])\np.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}