Skip to content

Commit

Permalink
Fix aspect ratio of logo image so it is not stretched or squashed (#376)
Browse files Browse the repository at this point in the history
Also add two new tests that make sure logo stays the right aspect
ratio whether the canvas is tall/narrow or short/wide.
  • Loading branch information
scottwittenburg authored and doutriaux1 committed Dec 19, 2018
1 parent c7a9768 commit dadf5d0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
14 changes: 14 additions & 0 deletions tests/test_vcs_draw_logo_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ def testDrawLogoOn(self):
self.x.plot(a, bg=self.bg)
fnm = "test_vcs_draw_logo_on.png"
self.checkImage(fnm)

self.x.clear()
self.x.drawlogoon()
self.x.geometry(800, 400)
self.x.plot(a, bg=self.bg)
fnm = "test_vcs_draw_logo_on_wide.png"
self.checkImage(fnm)

self.x.clear()
self.x.drawlogoon()
self.x.geometry(400, 800)
self.x.plot(a, bg=self.bg)
fnm = "test_vcs_draw_logo_on_tall.png"
self.checkImage(fnm)
42 changes: 36 additions & 6 deletions vcs/VTKPlots.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def __init__(self, canvas, renWin=None,
self.plotRenderers = set()
# Maps priorities to renderers
self.text_renderers = {}
self.logoContextArea = None
self.logoContextItem = None
self.logoContextItemPython = None
self.renderer = None
Expand Down Expand Up @@ -576,6 +577,14 @@ def clear(self, render=True):
self.contextView.GetScene().ClearItems()
r, g, b = [c / 255. for c in self.canvas.backgroundcolor]
self.contextView.GetRenderer().SetBackground(r, g, b)

if self.logoContextItem:
self.logoContextArea.ClearItems()
self.contextView.GetScene().RemoveItem(self.logoContextArea)
self.logoContextArea = None
self.logoContextItem = None
self.logoContextItemPython = None

self._animationActorTransforms = {}

self.showGUI(render=False)
Expand Down Expand Up @@ -1653,22 +1662,43 @@ def createLogo(self):
position = [0.895, 0.0]
position2 = [0.10, 0.05]

[renWinWidth, renWinHeight] = self.renWin.GetSize()
vpLowerLeftX = position[0] * renWinWidth
vpLowerLeftY = position[1] * renWinHeight
vpWidth = position2[0] * renWinWidth
vpHeight = position2[1] * renWinHeight

imgAspect = float(imgWidth) / imgHeight
vpAspect = vpWidth / vpHeight

if vpAspect > imgAspect:
# We'll use the full vp height and adjust it's width so that it's
# aspect ratio matches that of the image (so no stretching of the
# image occurs). The image should be centered, so we'll offset
# position x value by half the difference.
vpWidth = vpHeight * imgAspect
halfDiff = ((position2[0] * renWinWidth) - vpWidth) / 2.0
vpLowerLeftX += halfDiff
else:
# Similar to above, but in this case we choose to keep the vp width
# and adjust it's height.
vpHeight = vpWidth / imgAspect
halfDiff = ((position2[1] * renWinHeight) - vpHeight) / 2.0
vpLowerLeftY += halfDiff

view = self.contextView

area = vtk.vtkContextArea()
view.GetScene().AddItem(area)

[renWinWidth, renWinHeight] = self.renWin.GetSize()
dataBounds = vtk.vtkRectd(0.0, 0.0, imgWidth, imgHeight)
screenGeom = vtk.vtkRecti(
int(position[0] * renWinWidth),
int(position[1] * renWinHeight),
int(position2[0] * renWinWidth),
int(position2[1] * renWinHeight))
screenGeom = vtk.vtkRecti(int(vpLowerLeftX), int(vpLowerLeftY),
int(vpWidth), int(vpHeight))

vcs2vtk.configureContextArea(area, dataBounds, screenGeom)
area.GetDrawAreaItem().AddItem(item)

self.logoContextArea = area
self.logoContextItem = item
self.logoContextItemPython = pythonItem

Expand Down

0 comments on commit dadf5d0

Please sign in to comment.