Tuesday, February 21, 2006

VTK: Copy Image Data and run stuff in the background

I spent quite some time to figure out how to copy image data. I am setting up a pipeline in a Java method but want a copy of the image data in one of the fields, hence:


public void runSegmentation() {
//set up progress bar and hourglass cursor
progressBar = new ProgressBar("Segementation");
progressBar.OpenProgressBar();
currentSegmentation.setProgressBar(progressBar);
renWin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

if (!currentSegmentation.is3D()) {
//set up pipeline

vtkImageReslice reslice2 = new vtkImageReslice();
reslice2.SetOutputExtent(actor.GetDisplayExtent());
reslice2.SetInput(canvas.GetOutput());
reslice2.Update();

currentSegmentation.setModel(reslice2.GetOutput()); //use the canvas as model;-)

undo = true;
slice = actor.GetSliceNumber();
//copy the image
undoImage = new vtkImageData();
undoImage.DeepCopy(canvas.GetOutput());

}

//set up segmentation in the background
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
if (!currentSegmentation.is3D()) {
currentSegmentation.Update();
canvas.CopyImage(currentSegmentation.GetOutput());
}
return null;
}

@Override
public void finished() {
currentSegmentation.SetInput(null);
currentSegmentation.setModel(null);
pane.refresh(); //refresh view
progressBar.CloseProgressBar();
renWin.setCursor(transparentCursor);
}

};

//run segmentation in the background
worker.start();

}
}


As you see I have thrown in some multithreading (segmenting data takes awfully long) but the code to copy the image data is essentially:

undoImage = new vtkImageData();
undoImage.DeepCopy(canvas.GetOutput());

I think DeepCopy is the right command but it took me a while. It might also be that the ImageCanvas is a weird construct though.

Wednesday, February 08, 2006

VTK and JMenu

Whenever you tried to have Menus and VTK in your Java program the menus pop up but disappear behind the VTK widget. The same is true for JTabbedPane, JComboBox, and the like. The root of the problem is the incompatibilty of Lightweight and Heavyweight components. To solve it in my case at least -- write before you instantiate any component:

JPopupMenu.setDefaultLightWeightPopupEnabled( false );

This will make the menus behave like heaviweight components and everything will be good.