DrawViewportBorder
Description¶
Draw a border around a renderer's viewport.
To use the snippet, click the Copy to clipboard at the upper right of the code blocks.
Implementation¶
# from dataclasses import dataclass
#
# from vtkmodules.vtkCommonColor import vtkNamedColors
# from vtkmodules.vtkCommonCore import vtkPoints
# from vtkmodules.vtkCommonDataModel import vtkCellArray, vtkPolyData, vtkPolyLine
# from vtkmodules.vtkRenderingCore import vtkActor2D, vtkPolyDataMapper2D, vtkRenderer
# from vtkmodules.vtkRenderingCore import vtkCoordinate
def draw_viewport_border(renderer, sides, border_color, border_width):
"""
Set a border around a viewport.
:param renderer: The renderer corresponding to the viewport.
:param sides: An array of boolean corresponding to [top, left, bottom, right]
:param border_color: The color of the border.
:param border_width: The width of the border.
:return:
"""
colors = vtkNamedColors()
# Points start at upper right and proceed anti-clockwise.
points = vtkPoints()
points.SetNumberOfPoints(4)
points.InsertPoint(0, 1, 1, 0)
points.InsertPoint(1, 0, 1, 0)
points.InsertPoint(2, 0, 0, 0)
points.InsertPoint(3, 1, 0, 0)
cells = vtkCellArray()
cells.Initialize()
if sides[0]:
# Top
top = vtkPolyLine()
top.GetPointIds().SetNumberOfIds(2)
top.GetPointIds().SetId(0, 0)
top.GetPointIds().SetId(1, 1)
cells.InsertNextCell(top)
if sides[1]:
# Left
left = vtkPolyLine()
left.GetPointIds().SetNumberOfIds(2)
left.GetPointIds().SetId(0, 1)
left.GetPointIds().SetId(1, 2)
cells.InsertNextCell(left)
if sides[2]:
# Bottom
bottom = vtkPolyLine()
bottom.GetPointIds().SetNumberOfIds(2)
bottom.GetPointIds().SetId(0, 2)
bottom.GetPointIds().SetId(1, 3)
cells.InsertNextCell(bottom)
if sides[3]:
# Right
right = vtkPolyLine()
right.GetPointIds().SetNumberOfIds(2)
right.GetPointIds().SetId(0, 3)
right.GetPointIds().SetId(1, 0)
cells.InsertNextCell(right)
# Now make the polydata and display it.
poly = vtkPolyData()
poly.Initialize()
poly.SetPoints(points)
poly.SetLines(cells)
# Use normalized viewport coordinates since
# they are independent of window size.
coordinate = vtkCoordinate()
coordinate.SetCoordinateSystemToNormalizedViewport()
mapper = vtkPolyDataMapper2D()
mapper.SetInputData(poly)
mapper.SetTransformCoordinate(coordinate)
actor = vtkActor2D()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(colors.GetColor3d(border_color))
# Line width should be at least 2 to be visible at extremes.
actor.GetProperty().SetLineWidth(border_width)
renderer.AddViewProp(actor)
@dataclass(frozen=True)
class ViewPortBorders:
# Define borders for the viewports = [top, left, bottom, right].
t = [True, False, False, False]
l = [False, True, False, False]
b = [False, False, True, False]
r = [False, False, False, True]
lb = [False, True, True, False]
lbr = [False, True, True, True]
tlb = [True, True, True, False]
tlbr = [True, True, True, True]
rtl = [True, True, False, True]
tl = [True, True, False, False]
Usage¶
draw_viewport_border(renderer, sides=border, border_color='Yellow', border_width=4)