Debian Codegraph @

examples/01-filter/distance_between_surfaces.py

raw
1 """
2 .. _distance_between_surfaces_example:
3
4 Distance Between Two Surfaces
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Compute the average thickness between two surfaces.
8
9 For example, you might have two surfaces that represent the boundaries of
10 lithological layers in a subsurface geological model and you want to know the
11 average thickness of a unit between those boundaries.
12
13 A clarification on terminology in this example is important. A mesh point
14 exists on the vertex of each cell on the mesh. See :ref:`what_is_a_mesh`.
15 Each cell in this example encompasses a 2D region of space which contains an
16 infinite number of spatial points; these spatial points are not mesh points.
17 The distance between two surfaces can mean different things depending on context
18 and usage. Each example here explores different aspects of the distance from the
19 vertex points of the bottom mesh to the top mesh.
20
21 First, we will demo a method where we compute the normals on the vertex points
22 of the bottom surface, and then project a ray to the top surface to compute the
23 distance along the surface normals. This ray will usually intersect the top
24 surface at a spatial point inside a cell of the mesh.
25
26 Second, we will use a KDTree to compute the distance from every vertex point in
27 the bottom mesh to its closest vertex point in the top mesh.
28
29 Lastly, we will use a PyVista filter, :func:`pyvista.DataSet.find_closest_cell` to
30 calculate the distance from every vertex point in the bottom mesh to the closest spatial
31 point inside a cell of the top mesh. This will be the shortest distance from the vertex
32 point to the top surface, unlike the first two examples.
33
34 """
35
36 import numpy as np
37 import pyvista as pv
38
39
40 def hill(seed):
41 """Make a random surface."""
42 mesh = pv.ParametricRandomHills(
43 random_seed=seed, u_res=50, v_res=50, hill_amplitude=0.5
44 )
45 mesh.rotate_y(-10, inplace=True) # give the surfaces some tilt
46
47 return mesh
48
49
50 h0 = hill(1).elevation()
51 h1 = hill(10)
52 # Shift one surface
53 h1.points[:, -1] += 5
54 h1 = h1.elevation()
55
56 # %%
57
58 pl = pv.Plotter()
59 pl.add_mesh(h0, smooth_shading=True)
60 pl.add_mesh(h1, smooth_shading=True)
61 pl.show_grid()
62 pl.show()
63
64 # %%
65 # Ray Tracing Distance
66 # ++++++++++++++++++++
67 #
68 # Compute normals of lower surface at vertex points
69 h0n = h0.compute_normals(point_normals=True, cell_normals=False, auto_orient_normals=True)
70
71 # %%
72 # Travel along normals to the other surface and compute the thickness on each
73 # vector.
74
75 h0n['distances'] = np.empty(h0.n_points)
76 for i in range(h0n.n_points):
77 p = h0n.points[i]
78 vec = h0n['Normals'][i] * h0n.length
79 p0 = p - vec
80 p1 = p + vec
81 ip, ic = h1.ray_trace(p0, p1, first_point=True)
82 dist = np.sqrt(np.sum((ip - p) ** 2))
83 h0n['distances'][i] = dist
84
85 # Replace zeros with nans
86 mask = h0n['distances'] == 0
87 h0n['distances'][mask] = np.nan
88 np.nanmean(h0n['distances'])
89
90 # %%
91 pl = pv.Plotter()
92 pl.add_mesh(h0n, scalars='distances', smooth_shading=True)
93 pl.add_mesh(h1, color=True, opacity=0.75, smooth_shading=True)
94 pl.show()
95
96
97 # %%
98 # Nearest Neighbor Distance
99 # +++++++++++++++++++++++++
100 #
101 # You could also use a KDTree to compare the distance between each vertex point
102 # of the
103 # upper surface and the nearest neighbor vertex point of the lower surface.
104 # This will be
105 # noticeably faster than a ray trace, especially for large surfaces.
106 from scipy.spatial import KDTree
107
108 tree = KDTree(h1.points)
109 d_kdtree, idx = tree.query(h0.points)
110 h0['distances'] = d_kdtree
111 np.mean(d_kdtree)
112
113 # %%
114 pl = pv.Plotter()
115 pl.add_mesh(h0, scalars='distances', smooth_shading=True)
116 pl.add_mesh(h1, color=True, opacity=0.75, smooth_shading=True)
117 pl.show()
118
119
120 # %%
121 # Using PyVista Filter
122 # ++++++++++++++++++++
123 #
124 # The :func:`pyvista.DataSet.find_closest_cell` filter returns the spatial
125 # points inside the cells of the top surface that are closest to the vertex
126 # points of the bottom surface. ``closest_points`` is returned when using
127 # ``return_closest_point=True``.
128
129 closest_cells, closest_points = h1.find_closest_cell(h0.points, return_closest_point=True)
130 d_exact = np.linalg.norm(h0.points - closest_points, axis=1)
131 h0['distances'] = d_exact
132 np.mean(d_exact)
133
134
135 # %%
136 # As expected there is only a small difference between this method and the
137 # KDTree method.
138
139 pl = pv.Plotter()
140 pl.add_mesh(h0, scalars='distances', smooth_shading=True)
141 pl.add_mesh(h1, color=True, opacity=0.75, smooth_shading=True)
142 pl.show()
143 # %%
144 # .. tags:: filter