Debian Codegraph @

examples/01-filter/compute_normals.py

raw
1 """
2 .. _compute_normals_example:
3
4 Computing Surface Normals
5 ~~~~~~~~~~~~~~~~~~~~~~~~~
6 Compute normals on a surface.
7
8 """
9
10 # sphinx_gallery_start_ignore
11 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
12 # sphinx_gallery_end_ignore
13
14 import numpy as np
15
16 # sphinx_gallery_thumbnail_number = 2
17 from pyvista import examples
18
19 # %%
20 # Computing the normals of a surface is quite easy using
21 # :class:`pyvista.PolyData`'s :func:`pyvista.PolyDataFilters.compute_normals`
22 # method.
23
24 mesh = examples.download_topo_global()
25 mesh.plot(cmap='gist_earth', show_scalar_bar=False)
26
27 # %%
28 # Now we have a surface dataset of the globe loaded - unfortunately, the
29 # dataset shows the globe with a uniform radius which hides topographic relief.
30 # Using :func:`pyvista.PolyDataFilters.compute_normals`, we can compute the normal
31 # vectors on the globe at all points in the dataset, then use the values given
32 # in the dataset to warp the surface in the normals direction to create some
33 # exaggerated topographic relief.
34
35 # Compute the normals in-place and use them to warp the globe
36 mesh.compute_normals(inplace=True) # this activates the normals as well
37
38 # Now use those normals to warp the surface
39 warp = mesh.warp_by_scalar(factor=0.5e-5)
40
41 # And let's see it
42 warp.plot(cmap='gist_earth', show_scalar_bar=False)
43
44
45 # %%
46 # We could also use face/cell normals to extract all the faces of a mesh
47 # facing a general direction. In the following snippet, we take a mesh, compute
48 # the normals along its cell faces, and extract the faces that face upward.
49
50 mesh = examples.download_washington_bust()
51 # Compute normals
52 mesh.compute_normals(cell_normals=True, point_normals=False, inplace=True)
53
54 # Get list of cell IDs that meet condition
55 ids = np.arange(mesh.n_cells)[mesh['Normals'][:, 2] > 0.0]
56
57 # Extract those cells
58 top = mesh.extract_cells(ids)
59
60 top.plot(color=True)
61 # %%
62 # .. tags:: filter