Debian Codegraph @

examples/01-filter/interpolate_sample.py

raw
1 """
2 .. _interpolate_sample_example:
3
4 Compare interpolation/sampling methods
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 There are two main methods of interpolating or sampling data from a target mesh
8 in PyVista. :func:`pyvista.DataSetFilters.interpolate` uses a distance weighting
9 kernel to interpolate point data from nearby points of the target mesh onto
10 the desired points.
11 :func:`pyvista.DataObjectFilters.sample` interpolates data using the
12 interpolation scheme of the enclosing cell from the target mesh.
13
14 If the target mesh is a point cloud, i.e. there is no connectivity in the cell
15 structure, then :func:`pyvista.DataSetFilters.interpolate` is typically
16 preferred. If interpolation is desired within the cells of the target mesh, then
17 :func:`pyvista.DataObjectFilters.sample` is typically desired.
18
19 Here the two methods are compared and contrasted using a simple example of
20 sampling data from a mesh in a rectangular domain. This example demonstrates the
21 main differences above. For more complex uses, see :ref:`interpolate_example`
22 and :ref:`resampling_example`.
23
24 """
25
26 # sphinx_gallery_thumbnail_number = 7
27 import numpy as np
28 import pyvista as pv
29
30 # %%
31 # Interpolating from point cloud
32 # ++++++++++++++++++++++++++++++
33 # A point cloud is a collection of points that have no connectivity in
34 # the mesh, i.e. the mesh contains no cells or the cells are 0D
35 # (vertex or polyvertex). The filter :func:`pyvista.DataSetFilters.interpolate`
36 # uses a distance-based weighting methodology to interpolate between the
37 # unconnected points.
38 #
39 # First, generate a point cloud mesh in a rectangular domain from
40 # ``(0, 0)`` to ``(3, 1)``. The data to be sampled is the square of the y position.
41
42 rng = np.random.default_rng(seed=0)
43 points = rng.uniform(low=[0, 0], high=[3, 1], size=(250, 2))
44 # Make points be z=0
45 points = np.hstack((points, np.zeros((250, 1))))
46 point_mesh = pv.PolyData(points)
47 point_mesh['ysquared'] = points[:, 1] ** 2
48
49 # %%
50 # The point cloud data looks like this.
51
52 pl = pv.Plotter()
53 pl.add_mesh(point_mesh, render_points_as_spheres=True, point_size=10)
54 pl.view_xy()
55 pl.show()
56
57 # %%
58 # Now estimate data on a regular grid from the point data. Note
59 # that the distance parameter ``radius`` determines how far away to
60 # look for point cloud data.
61
62 grid = pv.ImageData(dimensions=(11, 11, 1), spacing=[3 / 10, 1 / 10, 1])
63 output = grid.interpolate(point_mesh, radius=0.1, null_value=-1)
64 output
65
66 # %%
67 # When using ``radius=0.1``, the expected extents of the data are
68 # captured reasonably well over the domain, but there are holes in the
69 # data (represented by the darkest blue colors) caused by no points within
70 # the ``radius`` to interpolate from.
71
72 pl = pv.Plotter()
73 pl.add_mesh(output, clim=[0, 1])
74 pl.add_mesh(points, render_points_as_spheres=True, point_size=10, color='red')
75 pl.view_xy()
76 pl.show()
77
78
79 # %%
80 # Now repeat with ``radius=0.25``.
81 # There are no holes but the extents of the data is much narrower
82 # than ``[0, 1]``. This is caused by more interior points involved
83 # in the weighting near the lower and upper edges of the domain.
84 # Other parameters such as ``sharpness`` could be tuned to try to
85 # lessen the issue.
86
87 grid = pv.ImageData(dimensions=(11, 11, 1), spacing=[3 / 10, 1 / 10, 1])
88 output = grid.interpolate(point_mesh, radius=0.25, null_value=-1)
89
90 pl = pv.Plotter()
91 pl.add_mesh(output, clim=[0, 1])
92 pl.add_mesh(points, render_points_as_spheres=True, point_size=10, color='red')
93 pl.view_xy()
94 pl.show()
95
96 # %%
97 # While this filter is very useful for point clouds, it is possible to use
98 # it to interpolate from the points on other mesh types. With
99 # unstuitable choice of ``radius`` the interpolation doesn't look very good.
100 # It is recommended consider using :func:`pyvista.DataObjectFilters.sample` in a
101 # case like this (see next section below). However, there may be cases with
102 # non-point cloud meshes where :func:`pyvista.DataSetFilters.interpolate` is
103 # still preferred.
104
105 sphere = pv.SolidSphere(center=(0.5, 0.5, 0), outer_radius=1.0)
106 sphere['ysquared'] = sphere.points[:, 1] ** 2
107 output = grid.interpolate(sphere, radius=0.1)
108
109 pl = pv.Plotter()
110 pl.add_mesh(output, clim=[0, 1])
111 pl.add_mesh(sphere, style='wireframe', color='white')
112 pl.view_xy()
113 pl.show()
114
115 # %%
116 # Sampling from a mesh with connectivity
117 # ++++++++++++++++++++++++++++++++++++++
118 # This example is in many ways the opposite of the prior one.
119 # A mesh with cell connectivity that spans 2 dimensions is
120 # sampled at discrete points using :func:`pyvista.DataObjectFilters.sample`.
121 # Importantly, the cell connectivity enables direct interpolation
122 # inside the domain without needing distance or weighting parametization.
123 #
124 # First, show that sample does not work with point clouds with data.
125 # Either :func:`pyvista.DataSetFilters.interpolate` or the
126 # ``snap_to_closest_point`` parameter must be used.
127
128 grid = pv.ImageData(dimensions=(11, 11, 1), spacing=[3 / 10, 1 / 10, 1])
129 output = grid.sample(point_mesh)
130 # value of (0, 0) shows that no data was sampled
131 print(f'(min, max): {output["ysquared"].min()}, {output["ysquared"].min()}')
132
133 # %%
134 # Create the non-point cloud mesh that will be sampled from and plot it.
135
136 grid = pv.ImageData(dimensions=(11, 11, 1), spacing=[3 / 10, 1 / 10, 1])
137 grid['ysquared'] = grid.points[:, 1] ** 2
138
139 pl = pv.Plotter()
140 pl.add_mesh(grid, clim=[0, 1])
141 pl.view_xy()
142 pl.show()
143
144 # %%
145 # Now sample it at the discrete points used in the first example.
146 point_mesh = pv.PolyData(points)
147 output = point_mesh.sample(grid)
148 output
149
150 # %%
151 # This looks identical to the first plot of the first example as the
152 # data is not noisy, and there is little interpolation error.
153
154 pl = pv.Plotter()
155 pl.add_mesh(output, render_points_as_spheres=True, point_size=10)
156 pl.view_xy()
157 pl.show()
158
159 # %%
160 # Instead of sampling onto a point cloud, :func:`pyvista.DataObjectFilters.sample`
161 # can sample using other mesh types. For example, sampling onto a rotated subset
162 # of the grid.
163 #
164 # Make subset (0.7, 0.7, 0) units in dimension and then rotate by 45 degrees around
165 # its center.
166 subset = pv.ImageData(dimensions=(8, 8, 1), spacing=[0.1, 0.1, 0], origin=(0.15, 0.15, 0))
167 rotated_subset = subset.rotate_vector(vector=(0, 0, 1), angle=45, point=(0.5, 0.5, 0))
168 output = rotated_subset.sample(grid)
169 output
170
171 # %%
172 # The data in the sampled region looks identical to the original grid
173 # due to the well-behaved nature of the data and
174 # low interpolation error.
175
176 pl = pv.Plotter()
177 pl.add_mesh(grid, style='wireframe', clim=[0, 1])
178 pl.add_mesh(output, clim=[0, 1])
179 pl.view_xy()
180 pl.show()
181
182 # %%
183 # Repeat the sphere interpolation example, but using
184 # :func:`pyvista.DataObjectFilters.sample`. This method
185 # is directly able to sample from the mesh in this case without
186 # fiddling with distance weighting parameters.
187
188 sphere = pv.SolidSphere(center=(0.5, 0.5, 0), outer_radius=1.0)
189 sphere['ysquared'] = sphere.points[:, 1] ** 2
190 output = grid.sample(sphere)
191
192 pl = pv.Plotter()
193 pl.add_mesh(output, clim=[0, 1])
194 pl.add_mesh(sphere, style='wireframe', color='white')
195 pl.view_xy()
196 pl.show()
197
198
199 # %%
200 # .. tags:: filter