[vtkusers] java pipeline browser

matthias specht specht at ifi.unizh.ch
Mon Oct 25 06:43:48 EDT 2004


hi all,
there seems to be some interest for my code. so here it is - sorry for the lengthy post. as I said, it's not very sophisticated...if somebody developes something more advanced, it would be great if you would pass it back to the comunity. 

regards

matthias

-----------------------------------

import vtk.*;
import java.lang.reflect.*;

// export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/apps/VTK/bin/
// javac -classpath ~/apps/VTK/bin/vtk.jar JVTKPipelineBrowser.java
// java -classpath ~/apps/VTK/bin/vtk.jar:. JVTKPipelineBrowser 


public class JVTKPipelineBrowser {
    public class JVTKPipelineReader implements Runnable {
	public JVTKPipelineReader(vtkRenderWindow renW) {
	    _renW = renW;
	}

	public void run() {
	    System.out.println("pipe reader started");

	    // renderers
	    vtkRendererCollection rendCol = _renW.GetRenderers();
	    System.out.println("nr. of Renderers: " + rendCol.GetNumberOfItems());

	    rendCol.InitTraversal();
	    vtkRenderer ren = rendCol.GetNextItem();
	    while(ren != null) {
		System.out.println("found renderer: " + ren.Print());
		printJavaClass(ren);

		// actors
		vtkActorCollection actCol = ren.GetActors();
		System.out.println("nr. of Actors: " + actCol.GetNumberOfItems());
		actCol.InitTraversal();
		vtkActor act = actCol.GetNextItem();
		while(act != null) {
		    System.out.println("found Actor: " + act.Print());
		    printJavaClass(act);

		    // mapper
		    vtkMapper map = act.GetMapper();
		    System.out.println("found Mapper: " + map.Print());

		    // dataset
		    vtkDataSet set = map.GetInputAsDataSet();
		    System.out.println("found DataSet: " + set.Print());
		    
		    // source
		    vtkSource src = set.GetSource();
		    System.out.println("found Source: " + src.Print());

		    act = actCol.GetNextItem();
		}

		ren = rendCol.GetNextItem();
	    }

	} 	

	// print java class information. lots of stuff.
	// and there is more....fields, interfaces, private shit....
	// todo: query some values. which ones?
	public void printJavaClass(Object o) {
	    Class c = o.getClass();
	    while(c != null) {
		Method[] m = c.getDeclaredMethods();
		for(int i=0; i<m.length; i++) {
		    String s = m[i].toString();
		    if(s.startsWith("public"))
			System.out.println(s);
		}
		c = c.getSuperclass();
	    }
	}

	private vtkRenderWindow _renW;
    }


  // in the static contructor we load in the native code
  // The libraries must be in your path to work
  static { 
    System.loadLibrary("vtkCommonJava"); 
    System.loadLibrary("vtkFilteringJava"); 
    System.loadLibrary("vtkIOJava"); 
    System.loadLibrary("vtkImagingJava"); 
    System.loadLibrary("vtkGraphicsJava"); 
    System.loadLibrary("vtkRenderingJava"); 
  }

    public void startReader(vtkRenderWindow renW) {
	JVTKPipelineReader r = new JVTKPipelineReader(renW);
	Thread t = new Thread(r);
	t.start();
    }

  // Define the callback
  public void myCallback()
  {
    System.out.println("my bloody callback");
  }

  // now the main program
  public static void main (String []args) {
    //
    // Now we create the pipeline as usual, see Cone.java in Step1 for details
    //
    vtkConeSource cone = new vtkConeSource();
    cone.SetHeight( 3.0 );
    cone.SetRadius( 1.0 );
    cone.SetResolution( 10 );
  
    vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
    coneMapper.SetInput( cone.GetOutput() );
    vtkActor coneActor = new vtkActor();
    coneActor.SetMapper( coneMapper );

    vtkTextSource text = new vtkTextSource();
    text.SetText("yo man, easy!");
    vtkPolyDataMapper textMapper = new vtkPolyDataMapper();
    textMapper.SetInput( text.GetOutput() );
    vtkActor textActor = new vtkActor();
    textActor.SetMapper( textMapper );

    vtkRenderer ren1 = new vtkRenderer();
    ren1.AddActor( coneActor );

    ren1.AddActor( textActor );

    ren1.SetBackground( 0.1, 0.2, 0.4 );

    // Add the observer here, the first argument is the event name
    // the second argument is the instance to invoke the method on
    // the third argument is which method to invoke
    JVTKPipelineBrowser me = new JVTKPipelineBrowser();
    ren1.AddObserver("StartEvent",me,"myCallback");

    // setup the window
    vtkRenderWindow renWin = new vtkRenderWindow();
    renWin.AddRenderer( ren1 );
    renWin.SetSize( 300, 300 );
    

     // 
    // The vtkRenderWindowInteractor class watches for events (e.g., keypress,
    // mouse) in the vtkRenderWindow. These events are translated into event
    // invocations that VTK understands (see VTK/Common/vtkCommand.h for all
    // events that VTK processes). Then observers of these VTK events can
    // process them as appropriate.
    vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
    iren.SetRenderWindow(renWin);

    //
    // By default the vtkRenderWindowInteractor instantiates an instance
    // of vtkInteractorStyle. vtkInteractorStyle translates a set of events
    // it observes into operations on the camera, actors, and/or properties
    // in the vtkRenderWindow associated with the vtkRenderWinodwInteractor. 
    // Here we specify a particular interactor style.
    vtkInteractorStyleTrackballCamera style = 
        new vtkInteractorStyleTrackballCamera();
    iren.SetInteractorStyle(style);

    //
    // Unlike the previous examples where we performed some operations and then
    // exited, here we leave an event loop running. The user can use the mouse
    // and keyboard to perform the operations on the scene according to the
    // current interaction style.
    //

    
    //
    // Initialize and start the event loop. Once the render window appears,
    // mouse in the window to move the camera. The Start() method executes
    // an event loop which listens to user mouse and keyboard events. Note
    // that keypress-e exits the event loop. (Look in vtkInteractorStyle.h
    // for a summary of events, or the appropriate Doxygen documentation.)
    //
    iren.Initialize();

    me.startReader(renWin);

    iren.Start();


    //

  }
}


-- 
matthias specht
| mailto:specht at ifi.unizh.ch  http://www.ifi.unizh.ch/staff/specht
office
| morphometrics lab, institute for anthropology, university of zurich
| winterthurerstr. 190, 8057 zurich, switzerland
| phone +41-44-635-54-28 fax +41-44-635-68-04




More information about the vtkusers mailing list