{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interpolating {#interpolate_example}\n\nThere are two main methods of interpolating or sampling data from a\ntarget mesh in PyVista.\n`pyvista.DataSetFilters.interpolate`{.interpreted-text role=\"func\"} uses\na distance weighting kernel to interpolate point data from nearby points\nof the target mesh onto the desired points.\n`pyvista.DataSetFilters.sample`{.interpreted-text role=\"func\"}\ninterpolates data using the interpolation scheme of the enclosing cell\nfrom the target mesh.\n\nIf the target mesh is a point cloud, i.e. there is no connectivity in\nthe cell structure, then\n`pyvista.DataSetFilters.interpolate`{.interpreted-text role=\"func\"} is\ntypically preferred. If interpolation is desired within the cells of the\ntarget mesh, then `pyvista.DataSetFilters.sample`{.interpreted-text\nrole=\"func\"} is typically desired.\n\nThis example uses `pyvista.DataSetFilters.interpolate`{.interpreted-text\nrole=\"func\"}. For `pyvista.DataSetFilters.sample`{.interpreted-text\nrole=\"func\"}, see `resampling_example`{.interpreted-text role=\"ref\"}.\n\nInterpolate one mesh\\'s point/cell arrays onto another mesh\\'s nodes\nusing a Gaussian Kernel.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Surface Interpolation\n\nResample the points\\' arrays onto a surface\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Download sample data\nsurface = examples.download_saddle_surface()\npoints = examples.download_sparse_points()\n\np = pv.Plotter()\np.add_mesh(points, scalars=\"val\", point_size=30.0, render_points_as_spheres=True)\np.add_mesh(surface)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the interpolation\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "interpolated = surface.interpolate(points, radius=12.0)\n\n\np = pv.Plotter()\np.add_mesh(points, scalars=\"val\", point_size=30.0, render_points_as_spheres=True)\np.add_mesh(interpolated, scalars=\"val\")\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Complex Interpolation\n\nIn this example, we will in interpolate sparse points in 3D space into a\nvolume. These data are from temperature probes in the subsurface and the\ngoal is to create an approximate 3D model of the temperature field in\nthe subsurface.\n\nThis approach is a great for back-of-the-hand estimations but pales in\ncomparison to kriging\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Download the sparse data\nprobes = examples.download_thermal_probes()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the interpolation grid around the sparse data\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "grid = pv.ImageData()\ngrid.origin = (329700, 4252600, -2700)\ngrid.spacing = (250, 250, 50)\ngrid.dimensions = (60, 75, 100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dargs = dict(cmap=\"coolwarm\", clim=[0, 300], scalars=\"temperature (C)\")\ncpos = [\n (364280.5723737897, 4285326.164400684, 14093.431895014139),\n (337748.7217949739, 4261154.45054595, -637.1092549935128),\n (-0.29629216102673206, -0.23840196609932093, 0.9248651025279784),\n]\n\np = pv.Plotter()\np.add_mesh(grid.outline(), color='k')\np.add_mesh(probes, render_points_as_spheres=True, **dargs)\np.show(cpos=cpos)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run an interpolation\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "interp = grid.interpolate(probes, radius=15000, sharpness=10, strategy='mask_points')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Visualize the results\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "vol_opac = [0, 0, 0.2, 0.2, 0.5, 0.5]\n\np = pv.Plotter(shape=(1, 2), window_size=[1024 * 3, 768 * 2])\np.add_volume(interp, opacity=vol_opac, **dargs)\np.add_mesh(probes, render_points_as_spheres=True, point_size=10, **dargs)\np.subplot(0, 1)\np.add_mesh(interp.contour(5), opacity=0.5, **dargs)\np.add_mesh(probes, render_points_as_spheres=True, point_size=10, **dargs)\np.link_views()\np.show(cpos=cpos)" ] } ], "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 }