Debian Codegraph @

examples/02-plot/silhouette.py

raw
1 """
2 .. _silhouette_example:
3
4 Silhouette Highlight
5 ~~~~~~~~~~~~~~~~~~~~
6
7 Extract a subset of the edges of a polygonal mesh to generate an outline
8 (silhouette) of a mesh.
9 The silhouette may be created using the `silhouette` keyword with
10 :meth:`~pyvista.Plotter.add_mesh`, or by using
11 `~pyvista.Plotter.add_silhouette` directly.
12
13 """
14
15 import pyvista as pv
16 from pyvista import examples
17
18 # %%
19 # Prepare a triangulated ``PolyData``
20 bunny = examples.download_bunny()
21
22 # %%
23 # Now we can display the silhouette of the mesh and compare the result:
24 pl = pv.Plotter(shape=(1, 2))
25 pl.subplot(0, 0)
26 pl.add_mesh(bunny, color='lightblue', silhouette=True)
27 pl.add_text('Silhouette')
28 pl.view_xy()
29 pl.subplot(0, 1)
30 pl.add_mesh(bunny, color='lightblue')
31 pl.add_text('No silhouette')
32 pl.view_xy()
33 pl.show()
34
35
36 # %%
37 # Maybe the default parameters are not enough to really notice the silhouette.
38 # But by using a ``dict``, it is possible to modify the properties of the
39 # outline. For example, color and width could be specified like so:
40 pl = pv.Plotter()
41 silhouette = dict(
42 color='red',
43 line_width=8.0,
44 )
45 pl.add_mesh(bunny, silhouette=silhouette)
46 pl.view_xy()
47 pl.show()
48
49
50 # %%
51 # By default, PyVista uses a pretty aggressive decimation level but we might
52 # want to disable it. It is also possible to display sharp edges:
53 cylinder = pv.Cylinder(
54 center=(0, 0.04, 0),
55 direction=(0, 1, 0),
56 radius=0.15,
57 height=0.03,
58 ).triangulate()
59
60 pl = pv.Plotter(shape=(1, 3))
61 pl.subplot(0, 0)
62 pl.add_mesh(
63 cylinder,
64 color='lightblue',
65 smooth_shading=True,
66 silhouette=dict(color='red', line_width=8.0, decimate=None, feature_angle=True),
67 )
68 pl.add_text('Silhouette with sharp edges')
69 pl.view_isometric()
70 pl.subplot(0, 1)
71 pl.add_mesh(
72 cylinder,
73 color='lightblue',
74 smooth_shading=True,
75 silhouette=dict(color='red', line_width=8.0, decimate=None),
76 )
77 pl.add_text('Silhouette without sharp edges')
78 pl.view_isometric()
79 pl.subplot(0, 2)
80 pl.add_mesh(cylinder, color='lightblue', smooth_shading=True)
81 pl.add_text('No silhouette')
82 pl.view_isometric()
83 pl.show()
84
85
86 # %%
87 # Here is another example:
88 dragon = examples.download_dragon()
89 pl = pv.Plotter()
90 pl.set_background('black', top='blue')
91 pl.add_mesh(
92 dragon,
93 color='green',
94 specular=1,
95 smooth_shading=True,
96 silhouette=dict(line_width=8, color='white'),
97 )
98
99 pl.add_mesh(
100 cylinder,
101 color='lightblue',
102 smooth_shading=True,
103 silhouette=dict(decimate=None, feature_angle=True, line_width=8, color='white'),
104 )
105 pl.camera_position = pv.CameraPosition(
106 position=(-0.2936731887752889, 0.2389060430625446, 0.35138839367034236),
107 focal_point=(-0.005878899246454239, 0.12495124898850918, -0.004603400826454163),
108 viewup=(0.34348225747312017, 0.8567703221182346, -0.38466160965007384),
109 )
110 pl.show()
111 # %%
112 # .. tags:: plot