{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Resampling {#resampling_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.sample`{.interpreted-text\nrole=\"func\"}. For `pyvista.DataSetFilters.interpolate`{.interpreted-text\nrole=\"func\"}, see `interpolate_example`{.interpreted-text role=\"ref\"}.\n\nResample one mesh\\'s point/cell arrays onto another mesh\\'s nodes.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example will resample a volumetric mesh\\'s scalar data onto the\nsurface of a sphere contained in that volume.\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 Resample\n\nQuery a grid\\'s points onto a sphere\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = pv.Sphere(center=(4.5, 4.5, 4.5), radius=4.5)\ndata_to_probe = examples.load_uniform()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the two datasets\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\np.add_mesh(mesh, color=True)\np.add_mesh(data_to_probe, opacity=0.5)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the algorithm and plot the result\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "result = mesh.sample(data_to_probe)\n\n# Plot result\nname = \"Spatial Point Data\"\nresult.plot(scalars=name, clim=data_to_probe.get_data_range(name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Complex Resample\n\nTake a volume of data and create a grid of lower resolution to resample\non\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_to_probe = examples.download_embryo()\nmesh = pv.create_grid(data_to_probe, dimensions=(75, 75, 75))\n\nresult = mesh.sample(data_to_probe)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "threshold = lambda m: m.threshold(75.0, scalars='SLCImage')\ncpos = [\n (468.9075585873713, -152.8280322856109, 152.13046602188035),\n (121.65121514580106, 140.29327609542105, 112.28137570357188),\n (-0.10881224951051659, 0.006229357618166009, 0.9940428006178236),\n]\ndargs = dict(clim=[0, 200], cmap='rainbow')\n\np = pv.Plotter(shape=(1, 2))\np.add_mesh(threshold(data_to_probe), **dargs)\np.subplot(0, 1)\np.add_mesh(threshold(result), **dargs)\np.link_views()\np.view_isometric()\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 }