@@ -447,7 +447,6 @@ class VispyViewerCanvas(PCanvas):
447
447
448
448
def __init__ (self ) -> None :
449
449
self ._canvas = scene .SceneCanvas (size = (600 , 600 ))
450
- self ._current_shape : tuple [int , ...] = ()
451
450
self ._last_state : dict [Literal [2 , 3 ], Any ] = {}
452
451
453
452
central_wdg : scene .Widget = self ._canvas .central_widget
@@ -494,12 +493,10 @@ def add_image(
494
493
img = scene .visuals .Image (data , parent = self ._view .scene )
495
494
img .set_gl_state ("additive" , depth_test = False )
496
495
img .interactive = True
497
- if data is not None :
498
- self ._current_shape , prev_shape = data .shape , self ._current_shape
499
- if not prev_shape :
500
- self .set_range ()
501
496
handle = VispyImageHandle (img )
502
497
self ._elements [img ] = handle
498
+ if data is not None :
499
+ self .set_range ()
503
500
if cmap is not None :
504
501
handle .cmap = cmap
505
502
return handle
@@ -512,12 +509,10 @@ def add_volume(
512
509
)
513
510
vol .set_gl_state ("additive" , depth_test = False )
514
511
vol .interactive = True
515
- if data is not None :
516
- self ._current_shape , prev_shape = data .shape , self ._current_shape
517
- if len (prev_shape ) != 3 :
518
- self .set_range ()
519
512
handle = VispyImageHandle (vol )
520
513
self ._elements [vol ] = handle
514
+ if data is not None :
515
+ self .set_range ()
521
516
if cmap is not None :
522
517
handle .cmap = cmap
523
518
return handle
@@ -536,6 +531,7 @@ def add_roi(
536
531
self ._elements [h ] = VispyHandleHandle (h , handle )
537
532
if vertices :
538
533
handle .vertices = vertices
534
+ self .set_range ()
539
535
handle .color = color
540
536
handle .border_color = border_color
541
537
return handle
@@ -551,19 +547,37 @@ def set_range(
551
547
552
548
When called with no arguments, the range is set to the full extent of the data.
553
549
"""
554
- if len (self ._current_shape ) >= 2 :
555
- if x is None :
556
- x = (0 , self ._current_shape [- 1 ])
557
- if y is None :
558
- y = (0 , self ._current_shape [- 2 ])
559
- if z is None and len (self ._current_shape ) == 3 :
560
- z = (0 , self ._current_shape [- 3 ])
550
+ _x = [0.0 , 0.0 ]
551
+ _y = [0.0 , 0.0 ]
552
+ _z = [0.0 , 0.0 ]
553
+
554
+ for handle in self ._elements .values ():
555
+ if isinstance (handle , VispyImageHandle ):
556
+ shape = handle .data .shape
557
+ _x [1 ] = max (_x [1 ], shape [0 ])
558
+ _y [1 ] = max (_y [1 ], shape [1 ])
559
+ if len (shape ) > 2 :
560
+ _z [1 ] = max (_z [1 ], shape [2 ])
561
+ elif isinstance (handle , VispyRoiHandle ):
562
+ for v in handle .vertices :
563
+ _x [0 ] = min (_x [0 ], v [0 ])
564
+ _x [1 ] = max (_x [1 ], v [0 ])
565
+ _y [0 ] = min (_y [0 ], v [1 ])
566
+ _y [1 ] = max (_y [1 ], v [1 ])
567
+ if len (v ) > 2 :
568
+ _z [0 ] = min (_z [0 ], v [2 ])
569
+ _z [1 ] = max (_z [1 ], v [2 ])
570
+
571
+ x = cast (tuple [float , float ], _x ) if x is None else x
572
+ y = cast (tuple [float , float ], _y ) if y is None else y
573
+ z = cast (tuple [float , float ], _z ) if z is None else z
574
+
561
575
is_3d = isinstance (self ._camera , scene .ArcballCamera )
562
576
if is_3d :
563
577
self ._camera ._quaternion = DEFAULT_QUATERNION
564
578
self ._view .camera .set_range (x = x , y = y , z = z , margin = margin )
565
579
if is_3d :
566
- max_size = max (self . _current_shape )
580
+ max_size = max (x [ 1 ], y [ 1 ], z [ 1 ] )
567
581
self ._camera .scale_factor = max_size + 6
568
582
569
583
def canvas_to_world (
0 commit comments