| 1 |
"""Contains the BackgroundRenderer class.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import numpy as np |
| 6 |
|
| 7 |
import pyvista as pv |
| 8 |
from pyvista._deprecate_positional_args import _deprecate_positional_args |
| 9 |
|
| 10 |
from .renderer import Renderer |
| 11 |
|
| 12 |
|
| 13 |
class BackgroundRenderer(Renderer): |
| 14 |
"""BackgroundRenderer for visualizing a background image. |
| 15 |
|
| 16 |
Parameters |
| 17 |
---------- |
| 18 |
parent : Renderer |
| 19 |
The parent renderer for the background renderer. |
| 20 |
image_path : str |
| 21 |
Path to the image to use as a background. |
| 22 |
scale : float, default: 1 |
| 23 |
Scaling factor for the background image. |
| 24 |
view_port : tuple[float], optional |
| 25 |
Viewport for the background renderer. |
| 26 |
|
| 27 |
""" |
| 28 |
|
| 29 |
@_deprecate_positional_args(allowed=['parent', 'image_path']) |
| 30 |
def __init__( # noqa: PLR0917 |
| 31 |
self, parent, image_path, scale=1, view_port=None |
| 32 |
): |
| 33 |
"""Initialize BackgroundRenderer with an image.""" |
| 34 |
# avoiding circular import |
| 35 |
from pyvista import _vtk # noqa: PLC0415 |
| 36 |
|
| 37 |
# read the image first as we don't need to create a render if |
| 38 |
# the image path is invalid |
| 39 |
image_data = pv.read(image_path, cls=pv.ImageData) |
| 40 |
|
| 41 |
super().__init__(parent, border=False) |
| 42 |
self.SetLayer(0) |
| 43 |
self.InteractiveOff() |
| 44 |
self.SetBackground(self.parent.renderer.GetBackground()) |
| 45 |
self._scale = scale |
| 46 |
self._modified_observer = None |
| 47 |
self._prior_window_size = None |
| 48 |
if view_port is not None: |
| 49 |
self.viewport = view_port |
| 50 |
|
| 51 |
# create image actor |
| 52 |
image_actor = _vtk.vtkImageActor() |
| 53 |
image_actor.SetInputData(image_data) |
| 54 |
self.add_actor(image_actor, name='background') |
| 55 |
self.camera.enable_parallel_projection() |
| 56 |
self.reset_camera() # necessary to get first render |
| 57 |
self.resize() |
| 58 |
|
| 59 |
def resize(self, *args): # noqa: ARG002 |
| 60 |
"""Resize a background renderer. |
| 61 |
|
| 62 |
Parameters |
| 63 |
---------- |
| 64 |
*args : tuple |
| 65 |
Ignored arguments. |
| 66 |
|
| 67 |
""" |
| 68 |
if self.parent is None: # when deleted |
| 69 |
return |
| 70 |
if self.parent.render_window is None: # BasePlotter |
| 71 |
return |
| 72 |
|
| 73 |
if self._prior_window_size != self.parent.window_size: |
| 74 |
self._prior_window_size = self.parent.window_size |
| 75 |
|
| 76 |
actor = self._actors['background'] |
| 77 |
image_data = actor.GetInput() |
| 78 |
origin = image_data.GetOrigin() |
| 79 |
extent = image_data.GetExtent() |
| 80 |
spacing = image_data.GetSpacing() |
| 81 |
xc = origin[0] + 0.5 * (extent[0] + extent[1]) * spacing[0] |
| 82 |
yc = origin[1] + 0.5 * (extent[2] + extent[3]) * spacing[1] |
| 83 |
yd = (extent[3] - extent[2] + 1) * spacing[1] |
| 84 |
dist = self.camera.distance |
| 85 |
|
| 86 |
# make the longest dimensions match the plotting window |
| 87 |
img_dim = np.array(image_data.dimensions[:2]) |
| 88 |
self.camera._focus = np.array([xc, yc, 0.0]) |
| 89 |
self.camera.position = np.array([xc, yc, dist]) |
| 90 |
|
| 91 |
ratio = img_dim / np.array(self.parent.window_size) |
| 92 |
scale_value = 1 |
| 93 |
if ratio.max() > 1: |
| 94 |
# images are not scaled if larger than the window |
| 95 |
scale_value = ratio.max() |
| 96 |
|
| 97 |
if self._scale is not None: |
| 98 |
scale_value /= self._scale |
| 99 |
|
| 100 |
self.camera.parallel_scale = 0.5 * yd / self._scale |