Debian Codegraph @

pyvista/plotting/utilities/gl_checks.py

raw
1 """Plotting GL checks."""
2
3 from __future__ import annotations
4
5 from pyvista import _vtk
6
7
8 def check_depth_peeling(number_of_peels=100, occlusion_ratio=0.0):
9 """Check if depth peeling is available.
10
11 Attempts to use depth peeling to see if it is available for the
12 current environment. Returns ``True`` if depth peeling is
13 available and has been successfully leveraged, otherwise
14 ``False``.
15
16 Parameters
17 ----------
18 number_of_peels : int, default: 100
19 Maximum number of depth peels.
20
21 occlusion_ratio : float, default: 0.0
22 Occlusion ratio.
23
24 Returns
25 -------
26 bool
27 ``True`` when system supports depth peeling with the specified
28 settings.
29
30 """
31 # Try Depth Peeling with a basic scene
32 source = _vtk.vtkSphereSource()
33 mapper = _vtk.vtkPolyDataMapper()
34 mapper.SetInputConnection(source.GetOutputPort())
35 actor = _vtk.vtkActor()
36 actor.SetMapper(mapper)
37 # requires opacity < 1
38 actor.GetProperty().SetOpacity(0.5)
39 renderer = _vtk.vtkRenderer()
40 renderWindow = _vtk.vtkRenderWindow()
41 renderWindow.SetOffScreenRendering(True)
42 if hasattr(renderWindow, 'SetConnectContextToNSView'):
43 renderWindow.SetConnectContextToNSView(False)
44 renderWindow.AddRenderer(renderer)
45 renderWindow.SetAlphaBitPlanes(True)
46 renderWindow.SetMultiSamples(0)
47 renderer.AddActor(actor)
48 renderer.SetUseDepthPeeling(True)
49 renderer.SetMaximumNumberOfPeels(number_of_peels)
50 renderer.SetOcclusionRatio(occlusion_ratio)
51 renderWindow.Render()
52 return renderer.GetLastRenderingUsedDepthPeeling() == 1
53
54
55 def uses_egl() -> bool:
56 """Check if VTK has been compiled with EGL support via OSMesa.
57
58 Returns
59 -------
60 bool
61 ``True`` if VTK has been compiled with EGL support via OSMesa,
62 otherwise ``False``.
63
64 """
65 ren_win_str = str(type(_vtk.vtkRenderWindow()))
66 return 'EGL' in ren_win_str or 'OSOpenGL' in ren_win_str