Debian Codegraph @

examples/01-filter/resampling.py

raw
1 """
2 .. _resampling_example:
3
4 Detailed Resampling
5 ~~~~~~~~~~~~~~~~~~~
6
7 This example uses :func:`pyvista.DataObjectFilters.sample`.
8 :func:`pyvista.DataSetFilters.interpolate` is similar, and the two
9 methods are compared in :ref:`interpolate_sample_example`.
10
11 Resample one mesh's point/cell arrays onto another mesh's nodes.
12 """
13
14 # %%
15 # This example will resample a volumetric mesh's scalar data onto the surface
16 # of a sphere contained in that volume.
17
18 # sphinx_gallery_thumbnail_number = 3
19 import pyvista as pv
20 from pyvista import examples
21
22 # %%
23 # Simple Resample
24 # +++++++++++++++
25 # Query a grid's points onto a sphere
26 mesh = pv.Sphere(center=(4.5, 4.5, 4.5), radius=4.5)
27 data_to_probe = examples.load_uniform()
28
29 # %%
30 # Plot the two datasets
31 pl = pv.Plotter()
32 pl.add_mesh(mesh, color=True)
33 pl.add_mesh(data_to_probe, opacity=0.5)
34 pl.show()
35
36 # %%
37 # Run the algorithm and plot the result
38 result = mesh.sample(data_to_probe)
39
40 # Plot result
41 name = 'Spatial Point Data'
42 result.plot(scalars=name, clim=data_to_probe.get_data_range(name))
43
44 # %%
45 # Complex Resample
46 # ++++++++++++++++
47 # Take a volume of data and create a grid of lower resolution to resample on
48 data_to_probe = examples.download_embryo()
49 mesh = pv.create_grid(data_to_probe, dimensions=(75, 75, 75))
50
51 result = mesh.sample(data_to_probe)
52
53 # %%
54 threshold = lambda m: m.threshold(75.0, scalars='SLCImage')
55 cpos = pv.CameraPosition(
56 position=(468.9075585873713, -152.8280322856109, 152.13046602188035),
57 focal_point=(121.65121514580106, 140.29327609542105, 112.28137570357188),
58 viewup=(-0.10881224951051659, 0.006229357618166009, 0.9940428006178236),
59 )
60 dargs = dict(clim=[0, 200], cmap='rainbow')
61
62 pl = pv.Plotter(shape=(1, 2))
63 pl.add_mesh(threshold(data_to_probe), **dargs)
64 pl.subplot(0, 1)
65 pl.add_mesh(threshold(result), **dargs)
66 pl.link_views()
67 pl.view_isometric()
68 pl.show(cpos=cpos)
69 # %%
70 # .. tags:: filter