Resize window to fit data

Hello,

I’m searching for a function which i think exist to resize window to fit correctly the image resolution. The function CenterVolume just move the origin but do not fit to the size the window to the data.

It’s automatically done when loading a dicom, but i couldn’t find it.

Thank you.

You’re looking for the FitSliceToAll function:

layoutManager = slicer.app.layoutManager()
redWidget = layoutManager.sliceWidget('Red')
redLogic = redWidget.sliceLogic()
redLogic.FitSliceToAll() 

There are also functions for FitSliceToVolume and FitSliceToBackground:
http://apidocs.slicer.org/master/classvtkMRMLSliceLogic.html

This is it indeed, thank you !

1 Like

In fact FitSliceToAll resize all windows, if i have differents nodes with data of different size they are all resized the same way wich could be problematic.

Is it possible to resize windows volume by volume ? I guess FitSliceToVolume could do the job.

For now i did a this->GetMRMLApplicationLogic()->FitSliceToAll();
But I cant access FitSliceToVolume (i’m in c++), and i cant find sliceLogic since slice is not a module.

Any suggestions ?
Thanks

You can get the layout manager in C++ with:

qSlicerApplication::application()->layoutManager()

From there, you should be able to use the code snippet above to get the Slice logic, and call FitSliceToAll() separately on each slice view.

Ex.

qSlicerApplication::application()->layoutManager()->sliceWidget("Red")->sliceLogic()->FitSliceToAll();

Thank you it’s clear.

I got it to work but it have a problematic behavior on my workflow. The FitSliceToAll function seems to have effect only if I am actually watching something in the software. For instance if there is no node in slicer, then i receive data, create a node, fill it with imagedata, do a FitSliceToAll, it’s not resized (unless if with my speed of light reflex i watch the create data on the app before the FitSliceToAll occur). I could FitSliceToAll everytime i receive images, but any manual zoom would be negated as it would resize constantly.

I though i could bypass the problem with FitSliceToVolume, but it did’nt workout either, unlike FitSliceToAll it occur even when not watching anything but proportion are not respected and clicking on the view cancel the resizing.

Do I miss something, maybe i need to register nodes on the views or something ? I tried some UpdateImageData, or UpdateSliceNodeFromLayout but that did not worked.

FitSliceToAll is a per-slice viewer operation that fits to whatever is currently displayed in the viewer layers (which is controlled by the SliceCompositeNode for the slice viewer). You can use the viewer’s SliceLogic to fit to arbitrary volume nodes (https://apidocs.slicer.org/v4.8/classvtkMRMLSliceLogic.html).

You mean doing something like this :

qSlicerApplication::application()->layoutManager()->sliceWidget(“Red”)->sliceLogic()->FitSliceToVolume(volumeNode,int(width),int(height));
qSlicerApplication::application()->layoutManager()->sliceWidget(“Yellow”)->sliceLogic()->FitSliceToVolume(volumeNode,int(width),int(height));
qSlicerApplication::application()->layoutManager()->sliceWidget(“Green”)->sliceLogic()->FitSliceToVolume(volumeNode,int(width),int(height));

It does work indeed but not as well as a proper FitSliceToAll, the windows resize to fit height and width, but since it does not have the same ration height/width that the actual window, the image is distorted. I think FitSliceToAll automatically zoom back to fix this problem. But i did manage to isolate the code that does it.