Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix aspect ratio of logo image so it is not stretched or squashed #376

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix aspect ratio of logo image so it is not stretched or squashed
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 committed Dec 19, 2018
commit 2ad0f736c948ff6def34bcfb8e68fa57e032240c
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