{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Extract Surface {#extract_surface_example}\n\nYou can extract the surface of nearly any object within `pyvista` using\nthe `extract_surface` filter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport pyvista as pv\nfrom pyvista import CellType" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create a quadratic cell and extract its surface\n\nHere we create a single quadratic hexahedral cell and then extract its\nsurface to demonstrate how to extract the surface of an\nUnstructuredGrid.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lin_pts = np.array(\n [\n [-1, -1, -1], # point 0\n [1, -1, -1], # point 1\n [1, 1, -1], # point 2\n [-1, 1, -1], # point 3\n [-1, -1, 1], # point 4\n [1, -1, 1], # point 5\n [1, 1, 1], # point 6\n [-1, 1, 1], # point 7\n ],\n np.double,\n)\n\n# these are the \"midside\" points of a quad cell. See the definition of a\n# vtkQuadraticHexahedron at:\n# https://vtk.org/doc/nightly/html/classvtkQuadraticHexahedron.html\nquad_pts = np.array(\n [\n (lin_pts[1] + lin_pts[0]) / 2, # between point 0 and 1\n (lin_pts[1] + lin_pts[2]) / 2, # between point 1 and 2\n (lin_pts[2] + lin_pts[3]) / 2, # and so on...\n (lin_pts[3] + lin_pts[0]) / 2,\n (lin_pts[4] + lin_pts[5]) / 2,\n (lin_pts[5] + lin_pts[6]) / 2,\n (lin_pts[6] + lin_pts[7]) / 2,\n (lin_pts[7] + lin_pts[4]) / 2,\n (lin_pts[0] + lin_pts[4]) / 2,\n (lin_pts[1] + lin_pts[5]) / 2,\n (lin_pts[2] + lin_pts[6]) / 2,\n (lin_pts[3] + lin_pts[7]) / 2,\n ],\n)\n\n# introduce a minor variation to the location of the mid-side points\nquad_pts += np.random.default_rng().random(quad_pts.shape) * 0.3\npts = np.vstack((lin_pts, quad_pts))\n\n# create the grid\ncells = np.hstack((20, np.arange(20))).astype(np.int64, copy=False)\ncelltypes = np.array([CellType.QUADRATIC_HEXAHEDRON])\ngrid = pv.UnstructuredGrid(cells, celltypes, pts)\n\n# finally, extract the surface and plot it\nsurf = grid.extract_surface()\nsurf.plot(show_scalar_bar=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Nonlinear Surface Subdivision\n\nShould your UnstructuredGrid contain quadratic cells, you can generate a\nsmooth surface based on the position of the \\\"mid-edge\\\" nodes. This\nallows the plotting of cells containing curvature. For additional\nreference, please see:\n<https://prod.sandia.gov/techlib-noauth/access-control.cgi/2004/041617.pdf>\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "surf_subdivided = grid.extract_surface(nonlinear_subdivision=5)\nsurf_subdivided.plot(show_scalar_bar=False)" ] } ], "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 }