1

Is it possible to implement an auto range function for vispy like we have for pyqtgraph? I have search but can't find any or an alternative way to implement it.

Here is the sample code

from itertools import cycle

import numpy as np

from vispy import app, scene, io
from vispy.color import get_colormaps, BaseColormap

# Read volume
vol2 = np.load(io.load_data_file('brain/mri.npz'))['data']
vol2 = np.flipud(np.rollaxis(vol2, 1))

# Prepare canvas
canvas = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)
canvas.measure_fps()

# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()

# Set whether we are emulating a 3D texture
emulate_texture = False

# Create the volume visuals, only one is visible
volume1 = scene.visuals.Volume(vol2, parent=view.scene, threshold=0.225,
                               emulate_texture=emulate_texture)
# volume1.transform = scene.STTransform(translate=(64, 64, 0))
volume1.transform = scene.STTransform(scale=(3,3,3))
volume2 = scene.visuals.Volume(vol2, parent=view.scene, threshold=0.2,
                               emulate_texture=emulate_texture)
volume2.visible = False

print(volume1.bounds(0), volume1.bounds(1), volume1.bounds(2))

# Create two cameras (1 for firstperson, 3 for 3d person)
fov = 60.
cam1 = scene.cameras.FlyCamera(parent=view.scene, fov=fov)
cam2 = scene.cameras.TurntableCamera(parent=view.scene, fov=fov)
cam3 = scene.cameras.ArcballCamera(parent=view.scene, fov=fov)
view.camera = cam2  # Select turntable at first

if __name__ == '__main__':
    print(__doc__)
    app.run()
AcK
  • 2,063
  • 2
  • 20
  • 27
SorinT
  • 53
  • 1
  • 6
  • Can you please clarify what pieces of vispy you are currently using and what you mean by auto-range? – djhoese Apr 26 '21 at 12:03
  • @djhoese I'm using scene.visual to visualize a volume for code i'm working on. Plotting the initial volume fits well on screen but if I apply scene.STTransform to scale the volume, it becomes bigger and go off screen. In this case, I have t zoom back out to view the whole thing. pyqtgraph viewbox has this function called autoRange which detects all the children on the plot and zooms in or out accordingly to show all widgets. – SorinT Apr 26 '21 at 15:21
  • Could you provide the code you are using? There are various methods on the cameras for setting ranges (although I don't think we have an auto range) which might be the closest option. Are you only setting the STTransform once? Are you doing it after the initial draw? This is what I'm hoping to figure out from your code. – djhoese Apr 26 '21 at 21:01
  • @djhoese I have attached a sample file from the vispy examples. Line 26 shows the scale added to the volume. If the scale values are too big, the volume goes of screen. STTransform scale is set when needed but deactivated when not needed and I'm doing this with a check box. Yes, it is done after the initial draw of the volume. – SorinT Apr 27 '21 at 04:14
  • 3
    I think by default if you call `set_range` on a camera instance with no arguments, it will automatically get the bounds for all Visuals in the Scene (by accessing `.bounds` of the visuals) and update the camera accordingly. So you could try calling your camera `.set_range()` and see if that accomplishes the same thing. It should then be theoretically possible to have this method called on certain actions (like changing the Volume data or something else) and no additional STTransform should be needed. – djhoese Apr 27 '21 at 15:12
  • 1
    @djhoese Thank you for the subjection. The `set_range()` command did what I wanted. Using it with no arguments didn't change the view but since I'm scaling the volume, I had to multiply the camera x,y,z bounds by the scaler to get it to work properly. – SorinT Apr 28 '21 at 04:13

0 Answers0