Debian Codegraph @

examples/02-plot/maximum_intensity_projection.py

raw
1 """
2 .. _maximum_intensity_projection_example:
3
4 Maximum Intensity Projection
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Maximum Intensity Projection (MIP) is a rendering technique for point
8 clouds that reorders vertex depth so points with higher scalar values
9 are always rendered in front, regardless of their actual distance from
10 the camera.
11
12 This is useful for dense point cloud visualization where high-value
13 data points would otherwise be hidden behind lower-value points that
14 happen to be closer to the viewer. The technique was proposed by
15 Cowan (2014) for visualizing grade data in mining applications, where
16 it is referred to as "X-ray plunge projection."
17
18 MIP works by replacing the z-coordinate in OpenGL clip space with the
19 negated, normalized scalar value via a custom vertex shader. This
20 means that depth ordering is driven entirely by scalar magnitude
21 rather than spatial position.
22 """
23
24 # sphinx_gallery_start_ignore
25 # shader customizations do not work in interactive examples
26 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
27 # sphinx_gallery_end_ignore
28
29
30 import numpy as np
31 import pyvista as pv
32 from pyvista import examples
33
34 # %%
35 # Normal vs. MIP Rendering
36 # ~~~~~~~~~~~~~~~~~~~~~~~~~
37 # Using a sample of the knee dataset, we compare normal rendering
38 # (left) where closer points occlude farther ones, with MIP rendering
39 # (right) where the highest scalar values punch through to the front.
40
41 vol = examples.download_knee_full()
42
43 sample_n = 50000
44 rng = np.random.default_rng(0)
45 indices = rng.integers(0, vol.n_points, sample_n)
46 pts = vol.extract_points(indices, adjacent_cells=False, include_cells=False)
47
48 display = dict(
49 cmap='jet', clim=[0, 100], style='points', point_size=5, show_scalar_bar=False
50 )
51
52 pl = pv.Plotter(shape=(1, 2))
53 pl.enable_parallel_projection()
54
55 pl.subplot(0, 0)
56 pl.add_mesh(pts, **display)
57 pl.add_text('Normal Projection', font_size=12)
58
59 pl.subplot(0, 1)
60 mip_actor = pl.add_mesh(pts, **display)
61 mip_actor.enable_maximum_intensity_projection()
62 pl.add_text('Maximum Intensity Projection', font_size=12)
63
64 pl.link_views()
65 pl.show()
66
67
68 # %%
69 # MIP with Circle Point Sprites
70 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71 # MIP modifies the vertex shader while point sprites modify the
72 # fragment shader, so both features compose cleanly on the same actor.
73 # Using circle sprites with MIP produces a cleaner visualization than
74 # the default square points.
75
76 combined_display = dict(
77 cmap='jet', clim=[0, 100], style='points', point_size=15, show_scalar_bar=False
78 )
79
80 pl = pv.Plotter(shape=(1, 2))
81 pl.enable_parallel_projection()
82
83 pl.subplot(0, 0)
84 actor_squares = pl.add_mesh(
85 pts,
86 render_points_as_spheres=False,
87 **combined_display,
88 )
89 actor_squares.enable_maximum_intensity_projection()
90 pl.add_text('MIP (squares)', font_size=12)
91
92 pl.subplot(0, 1)
93 actor_circles = pl.add_mesh(
94 pts,
95 point_shape='circle',
96 **combined_display,
97 )
98 actor_circles.enable_maximum_intensity_projection()
99 pl.add_text('MIP + circle sprites', font_size=12)
100
101 pl.link_views()
102 pl.show()
103
104
105 # %%
106 # .. note::
107 # MIP requires VTK >= 9.3.
108 #
109 # .. note::
110 # MIP does not work correctly with ``opacity < 1`` unless depth
111 # peeling is enabled. See :func:`pyvista.Plotter.enable_depth_peeling`.
112 #
113 # References
114 # ~~~~~~~~~~
115 # Cowan, E.J., 2014. 'X-ray Plunge Projection', Understanding
116 # Structural Geology from Grade Data. AusIMM Monograph 30, 207-220.