[vtkusers] Java, SimpleRayTrace, AbortCheckEvent not functioning

Kustaa Nyholm Kustaa.Nyholm at planmeca.com
Fri Mar 23 09:52:49 EDT 2007


Hi,

I've finally managed to get my VTK going on WinXP and been trying to
play and modify 
a litle the  SimpleRayTrace.java example.

The example works, but the the interactivity is poor because the
rendering is not aborted even if
there are new events to process.

The code, as posted on  ij-plugins website (where the code originates
from) has the following line commented out:


//    renWin.GetRenderWindow().AddObserver("AbortCheckEvent", this,
"checkAbort");


If I uncomment this and modify the checkAbort routine as follows:

	public void checkAbort() {
		Toolkit.getDefaultToolkit().beep();
		if (renWin.GetRenderWindow().GetEventPending() != 0) {
			renWin.GetRenderWindow().SetAbortRender(1);
		}
	}

then I can hear that the checkAbort does get called. Some times tens or
houndreds times in a rapid succession but never
during rendering. No wonder it is not aborting!

So does this checkAbort thing work in Java for anyone?

Below is my complete test code in case someone likes read through.

br Kusti


import vtk.util.VtkPanelContainer;
import vtk.util.VtkPanelUtil;
import vtk.util.VtkUtil;
import vtk.*;

import javax.swing.*;
import javax.swing.event.*;

import com.sun.j3d.utils.behaviors.sensor.Mouse6DPointerBehavior;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.*;

public class SimpleRayCast extends JComponent implements
VtkPanelContainer {

	private vtkPanel renWin;

	private int lastX, lastY;
	


public SimpleRayCast() {
		// Setup VTK rendering panel
		renWin = new vtkPanel();

		vtkImageReader reader2 = new vtkImageReader();
		reader2.SetFileName("C:/VTK-java-koe/data/sample-data");
		reader2.SetFileDimensionality(3);
		int s=256-1;
		reader2.SetDataExtent(0, s, 0, s, 0, s);
		reader2.SetDataVOI(0, s, 0, s, 0, s);
		reader2.SetNumberOfScalarComponents(1);
		reader2.SetDataScalarTypeToUnsignedShort();
		reader2.SetHeaderSize(0);
		reader2.SetDataByteOrderToBigEndian();

		// Create transfer mapping scalar value to opacity
		final vtkPiecewiseFunction opacityTransferFunction = new
vtkPiecewiseFunction();
		opacityTransferFunction.AddPoint(0, 0.);
		opacityTransferFunction.AddPoint(1500, 0.0);
		opacityTransferFunction.AddPoint(1501, 1.0);

		// Create transfer mapping scalar value to color
		vtkColorTransferFunction colorTransferFunction = new
vtkColorTransferFunction();
		colorTransferFunction.AddRGBPoint(0.0, 1, 1, 1);
		colorTransferFunction.AddRGBPoint(64.0, 1.0, 0.0, 0.0);
		colorTransferFunction.AddRGBPoint(128.0, 0.0, 0.0,
1.0);
		colorTransferFunction.AddRGBPoint(192.0, 0.0, 1.0,
0.0);
		colorTransferFunction.AddRGBPoint(1000.0, 1, 1, 1);

		// The property describes how the data will look
		vtkVolumeProperty volumeProperty = new
vtkVolumeProperty();
		volumeProperty.SetColor(colorTransferFunction);
		volumeProperty.SetScalarOpacity(opacityTransferFunction);
		volumeProperty.ShadeOn();
		volumeProperty.SetInterpolationTypeToLinear();

		// The mapper / ray cast function know how to render the
data
		vtkVolumeRayCastCompositeFunction compositeFunction =
new vtkVolumeRayCastCompositeFunction();
		vtkVolumeRayCastMapper volumeMapper = new
vtkVolumeRayCastMapper();
		volumeMapper.SetVolumeRayCastFunction(compositeFunction);
		// vtkVolumeTextureMapper3D volumeMapper = new
		// vtkVolumeTextureMapper3D();
		volumeMapper.SetInput(reader2.GetOutput());

		// The volume holds the mapper and the property and
		// can be used to position/orient the volume
		vtkVolume volume = new vtkVolume();
		volume.SetMapper(volumeMapper);
		volume.SetProperty(volumeProperty);

		renWin.GetRenderer().AddVolume(volume);
		renWin.GetRenderer().SetBackground(1, 1, 1);
		VtkPanelUtil.setSize(renWin, 600, 600);


		final vtkCamera aCamera = new vtkCamera();
		aCamera.SetViewUp(0, 0, -1);
		aCamera.SetPosition(0, 1, 0);
		aCamera.SetFocalPoint(0, 0, 0);
		aCamera.ComputeViewPlaneNormal();
		
		final vtkLight lgt = new vtkLight();

		renWin.GetRenderer().SetActiveCamera(aCamera);
		renWin.GetRenderer().ResetCamera();
		
		
		
		aCamera.Dolly(1.5);

		renWin.GetRenderer().SetBackground(1, 1, 1);

		renWin.GetRenderer().ResetCameraClippingRange();

		// Setup panel
		setLayout(new BorderLayout());
		add(renWin, BorderLayout.CENTER);

		JPanel controls = new JPanel();
		final JSlider slider1 = new JSlider(JSlider.VERTICAL, 0,
5000, 1000);
		controls.add(slider1);

		slider1.addChangeListener(new ChangeListener() {
			public void
stateChanged(javax.swing.event.ChangeEvent e) {
				int v = slider1.getValue();
				opacityTransferFunction.RemoveAllPoints();
				opacityTransferFunction.AddPoint(0,
0.);
				opacityTransferFunction.AddPoint(v,
0.0);
				opacityTransferFunction.AddPoint(v + 1,
1.0);
				if (slider1.getValueIsAdjusting())
					renWin.GetRenderWindow().SetDesiredUpdateRate(2.0);
				else
					renWin.GetRenderWindow().SetDesiredUpdateRate(0.1);

				renWin.Render();
					
			};
		});

		renWin.removeMouseMotionListener(renWin);
		renWin.removeMouseListener(renWin);
		renWin.removeKeyListener(renWin);
		
		
		renWin.addMouseMotionListener(new MouseMotionAdapter()
{
			@Override
			public void mouseDragged(MouseEvent e) {
				int x=e.getX();
				int y=e.getY();
		        aCamera.Azimuth(lastX - x);
		        //aCamera.Elevation(y - lastY);
		        //aCamera.OrthogonalizeViewUp();
	            lgt.SetPosition(aCamera.GetPosition());
	            lgt.SetFocalPoint(aCamera.GetFocalPoint());
		        renWin.resetCameraClippingRange();
		        renWin.Render();
		        lastX=x;
		        lastY=y;
			}
		});
		add(controls, BorderLayout.EAST);

		renWin.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				renWin.GetRenderWindow().SetDesiredUpdateRate(5.0);

			};
			public void mouseReleased(MouseEvent e) {
				renWin.GetRenderWindow().SetDesiredUpdateRate(0.01);
				renWin.Render();
			};
		});
		
		renWin.GetRenderWindow().AddObserver("AbortCheckEvent",this,"checkAbort");
	}	

	public void checkAbort() {
		Toolkit.getDefaultToolkit().beep();
		if (renWin.GetRenderWindow().GetEventPending() != 0) {
			renWin.GetRenderWindow().SetAbortRender(1);
		}
	}

	public vtkPanel getRenWin() {
		return renWin;
	}

	public static void main(String s[]) {
		SimpleRayCast panel = new SimpleRayCast();

		JFrame frame = new JFrame("SimpleRayCast");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add("Center", panel);
		frame.pack();
		frame.setVisible(true);
	}
}



Kustaa Nyholm
Research Manager, Software
Research and Technology Division
PLANMECA OY
Asentajankatu 6
00880 HELSINKI
FINLAND

Please note our new telephone and fax numbers!
Tel: +358 20 7795 572 (direct)
Fax: +358 20 7795 676
GSM: +358 40 580 5193
e-mail: kustaa.nyholm at planmeca.com 

This e-mail may contain confidential or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorized copying, disclosure or distribution of the material in this
e-mail is strictly forbidden. We will not be liable for direct,
indirect, special or consequential damages arising from alteration of
the contents of this message by a third party or as a result of any
virus being passed for viruses.





More information about the vtkusers mailing list