{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Create VTK Objects\n\nThis exercise walks through the creation of a few different types of VTK\ndatasets.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport pyvista as pv\nimport vtk\n\ntry:\n from vtkmodules.util.numpy_support import numpy_to_vtk\nexcept: # noqa: E722\n from vtk.util.numpy_support import numpy_to_vtk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create `vtkImageData`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "image = vtk.vtkImageData()\nimage.SetDimensions(10, 10, 2)\nimage.SetSpacing(1, 2, 5)\nimage.SetOrigin(-0.5, -3, 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add point data\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "values = np.arange(np.prod(image.GetDimensions()))\n# Convert numpy array to VTK array\narr = numpy_to_vtk(values)\narr.SetName(\"values\") # CRITICAL\nimage.GetPointData().SetScalars(arr)\nimage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot with PyVista for simplicity\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.plot(image, show_edges=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create `vtkStructuredGrid`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define structured points with NumPy\nx = np.arange(-10, 10, 0.25)\ny = np.arange(-10, 10, 0.25)\nx, y = np.meshgrid(x, y)\nr = np.sqrt(x**2 + y**2)\nz = np.sin(r)\n\n# Join the points\nvalues = np.c_[x.ravel(), y.ravel(), z.ravel()]\n\ncoords = numpy_to_vtk(values)\n\npoints = vtk.vtkPoints()\npoints.SetData(coords)\n\ngrid = vtk.vtkStructuredGrid()\ngrid.SetDimensions(*z.shape, 1)\ngrid.SetPoints(points)\ngrid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add point data\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "arr = numpy_to_vtk(z.ravel())\narr.SetName(\"z\") # CRITICAL\ngrid.GetPointData().SetScalars(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot with PyVista for simplicity\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.plot(grid, show_edges=True)" ] }, { "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/b_create_vtk.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.11" } }, "nbformat": 4, "nbformat_minor": 0 }