Debian Codegraph @

examples/02-plot/anti_aliasing.py

raw
1 """
2 .. _anti_aliasing_example:
3
4 Anti-Aliasing
5 ~~~~~~~~~~~~~
6 Demonstrate anti-aliasing within PyVista.
7
8 PyVista supports three types of anti-aliasing:
9
10 * SSAA - Super-Sample Anti-Aliasing
11 * MSAA - Multi-Sample Anti-Aliasing
12 * FXAA - Fast Approximate Anti-Aliasing
13
14 By default, MSAA anti-aliasing is enabled using 8 samples. This is the default
15 for VTK.
16
17 .. code-block:: python
18
19 >>> import pyvista as pv
20 >>> pv.global_theme.multi_samples
21 8
22
23 You can enable additional line smoothing by enabling SSAA or FXAA
24
25 **Which anti-aliasing technique should you use?**
26
27 Normally, the default MSAA anti-aliasing should be sufficient as it strikes a
28 balance between efficiency and quality. If you desire additional smoothing, you
29 can either increase the number of ``multi_samples`` or use SSAA. Low-end PCs
30 should consider FXAA.
31
32
33 """
34
35 import pyvista as pv
36
37 mesh = pv.Icosphere()
38
39 # %%
40 # No Anti-Aliasing
41 # ~~~~~~~~~~~~~~~~
42 # First, let's show a plot without any anti-aliasing
43 # using :func:`~pyvista.Plotter.disable_anti_aliasing`.
44 pl = pv.Plotter()
45 pl.add_mesh(mesh, style='wireframe', color='k', line_width=2)
46 pl.disable_anti_aliasing()
47 pl.camera.zoom(1.5)
48 pl.show()
49
50
51 # %%
52 # Default: Multi-Sample Anti-Aliasing (MSAA)
53 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54 # Next, let's show the default anti-aliasing configuration. By default, PyVista
55 # uses 8 samples of MSAA.
56 #
57 # MSAA, or Multi-Sample Anti-Aliasing is an optimization of SSAA that reduces
58 # the amount of pixel shader evaluations that need to be computed by focusing
59 # on overlapping regions of the scene. The result is anti-aliasing along edges
60 # that is on par with SSAA and less anti-aliasing along surfaces as these make
61 # up the bulk of SSAA computations. MSAA is substantially less computationally
62 # expensive than SSAA and results in comparable image quality.
63
64 pl = pv.Plotter()
65 pl.add_mesh(mesh, style='wireframe', color='k', line_width=2)
66 pl.camera.zoom(1.5)
67 pl.show()
68
69
70 # %%
71 # You can increase the smoothing by increasing multi_samples
72 # using :func:`~pyvista.Plotter.enable_anti_aliasing`.
73
74 pl = pv.Plotter()
75 pl.add_mesh(mesh, style='wireframe', color='k', line_width=2)
76 pl.enable_anti_aliasing('msaa', multi_samples=16)
77 pl.camera.zoom(1.5)
78 pl.show()
79
80
81 # %%
82 # Fast Approximate Anti-Aliasing (FXAA)
83 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84 # FXAA is the most performant of all three anti-aliasing techniques. This is
85 # because, in terms of hardware or GPU, FXAA is not that demanding. It directly
86 # smooths the 2D image and this reduces the strain on the GPU, making it best
87 # for low-end PCs.
88 #
89 # Because FXAA only operates on the rendered image, FXAA may result in
90 # smoothing out parts of the visual overlay that are usually kept sharp for
91 # reasons of clarity as well as smoothing out textures. In general, FXAA is
92 # inferior to MSAA and SSAA.
93 #
94 # Note how the line width has been adjusted for consistency.
95
96 pl = pv.Plotter()
97 pl.add_mesh(mesh, style='wireframe', color='k', line_width=1.5)
98 pl.camera.zoom(1.5)
99 pl.enable_anti_aliasing('fxaa')
100 pl.show()
101
102
103 # %%
104 # Super-Sample Anti-Aliasing (SSAA)
105 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106 # SSAA, or Super-Sample Anti-Aliasing is a brute force method of
107 # anti-aliasing. It results in the best image quality but comes at a tremendous
108 # resource cost. SSAA works by rendering the scene at a higher resolution. The
109 # final image is produced by downsampling the massive source image using an
110 # averaging filter. This acts as a low pass filter which removes the high
111 # frequency components that would cause jaggedness.
112 #
113 # Note how the line width has been adjusted for consistency.
114
115 pl = pv.Plotter()
116 pl.add_mesh(mesh, style='wireframe', color='k', line_width=4)
117 pl.camera.zoom(1.5)
118 pl.enable_anti_aliasing('ssaa')
119 pl.show()
120
121
122 # %%
123 # Compare render time
124 # ~~~~~~~~~~~~~~~~~~~
125 # You can compare the time to render for each one of the anti-aliasing
126 # approaches with:
127 #
128 # .. code-block:: python
129 #
130 # n_render = 100
131 # for anti_aliasing in [False, 'fxaa', 'msaa', 'ssaa']:
132 #
133 # pl = pv.Plotter(off_screen=True)
134 # pl.add_mesh(mesh, style='wireframe', color='k', line_width=4)
135 # pl.camera.zoom(1.5)
136 # if anti_aliasing:
137 # pl.enable_anti_aliasing(anti_aliasing)
138 # else:
139 # pl.disable_anti_aliasing()
140 # pl.show(auto_close=False)
141 # tstart = time.time()
142 # # repeately trigger a render via saving a screenshot
143 # for __ in range(n_render):
144 # pl.screenshot('tmp.png')
145 # telap = (time.time() - tstart)/n_render
146 #
147 # print(f'Render time for {str(anti_aliasing):6}: {telap*1000:.3f} ms')
148 #
149 # Here are the timings from an NVIDIA Quadro P2000 and a Intel(R) Xeon(R)
150 # E-2288G CPU @ 3.70GHz:
151 #
152 # .. code-block:: text
153 #
154 # Render time for False : 37.045 ms
155 # Render time for fxaa : 40.458 ms
156 # Render time for msaa : 42.566 ms
157 # Render time for ssaa : 51.450 ms
158 #
159
160 # %%
161 # .. tags:: plot