{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import subprocess\nimport sys\n\nif \"google.colab\" in sys.modules:\n subprocess.run(\"apt-get update\", shell=True, check=True)\n subprocess.run(\"apt-get install -qq xvfb libgl1-mesa-glx\", shell=True, check=True)\n subprocess.run(\"pip install pyvista[all] -qq\", shell=True, check=True)\n\n import pyvista as pv\n\n # Seems that only static plotting is supported by colab at the moment\n pv.global_theme.jupyter_backend = \"static\"\n pv.global_theme.notebook = True\n pv.start_xvfb()\nelse:\n %matplotlib inline\n from pyvista import set_plot_theme\n\n set_plot_theme(\"document\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Computing Surface Normals\n=========================\n\nCompute normals on a surface.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom pyvista import examples\n\nmesh = examples.download_topo_global()\nmesh.plot(cmap=\"gist_earth\", show_scalar_bar=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have a surface dataset of the globe loaded - unfortunately, the\ndataset shows the globe with a uniform radius which hides topographic\nrelief. Using `pyvista.PolyData.compute_normals`{.interpreted-text\nrole=\"func\"}, we can compute the normal vectors on the globe at all\npoints in the dataset, then use the values given in the dataset to warp\nthe surface in the normals direction to create some exaggerated\ntopographic relief.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Compute the normals in-place and use them to warp the globe\n..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now use those normals to warp the surface\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "warp = mesh.warp_by_scalar(factor=0.5e-5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And let\\'s see it!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "warp.plot(cmap=\"gist_earth\", show_scalar_bar=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also use face or cell normals to extract all the faces of a\nmesh facing a general direction. In the following snippet, we take a\nmesh, compute the normals along its cell faces, and extract the faces\nthat face upward.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = examples.download_nefertiti()\n# Compute normals\nmesh.compute_normals(...)\n\n# Get list of cell IDs that meet condition\nids = np.arange(mesh.n_cells)[mesh['Normals'][...] > ...]\n\n# Extract those cells\ntop = mesh.extract_cells(ids)\n\ncpos = [\n (-834.3184529757553, -918.4677714398535, 236.5468795300025),\n (11.03829376004883, -13.642289291587957, -35.91218884207208),\n (0.19212361465657216, 0.11401076390090074, 0.9747256344254143),\n]\n\ntop.plot(cpos=cpos, color=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{=html}\n