{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using VTK Algorithms\n\nIn this exercise, you will use a VTK Algorithm directly to filter a\nPyVista mesh.\n\nVTK algorithms (filters) follow a standard flow for most cases:\n\n1. Instantiate the algorithm\n2. Set the input data object or connection: `.SetInputDataObject(mesh)`\n3. Adjust algorithm parameters with the setter methods, e.g.,\n `SetParameterName(value)`\n4. Call `.Update()` to run the algorithm\n5. Retrieve the output of the algorithm: `output = alg.GetOutput()`\n\nLet\\'s see if we can try a few VTK algorithms with that standard\nworkflow.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pyvista as pv\nimport vtk\nfrom pyvista import examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a sample mesh\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh = examples.load_random_hills()\nmesh"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simple Filter\n\nLet\\'s start out with a simple VTK filter: `vtkOutlineFilter`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"help(vtk.vtkOutlineFilter)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try using this VTK filter yourself here:\n\nRemember that you will have to wrap the output of the algorithm with\n`pyvista.wrap`{.interpreted-text role=\"func\"}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"alg = vtk.vtkOutlineFilter()\n\n# (your code here, answer below)\n\noutline = pv.wrap(alg.GetOutput())\noutline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"alg.SetInputDataObject(mesh)\nalg.SetGenerateFaces(False) # noqa: FBT003\nalg.Update()\n\noutline = pv.wrap(alg.GetOutput())\noutline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"::: note\n::: title\nNote\n:::\n\nNote that the about filter can be replaced with a `.outline()` filter in\nPyVista\n:::\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pl = pv.Plotter()\npl.add_mesh(mesh)\npl.add_mesh(outline, color=\"k\")\npl.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Find your own filter\n\nTake a look at VTK\\'s examples and documentation to find a filter you\\'d\nlike to apply to your mesh. The instructors will be around to help you\nimplement.\n\nSee <https://kitware.github.io/vtk-examples/site/Python/>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{=html}\n<center>\n <a target=\"_blank\" href=\"https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/06_vtk/c_vtk_algorithms.ipynb\">\n <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/ width=\"150px\">\n </a>\n</center>\n```\n"
]
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}