[vtkusers] Picking problems in Java

Carlos Martinez Burgos cmarbur at iti.upv.es
Wed Jan 23 10:20:35 EST 2002


Hi Jeff.

Your solution works well!!! I think that the problem was that I was using
setPickMethod of the vtkActor to do the picking functionality and not
because of using one or another class of picker.

Now I get the actor from the picker and search it on a list of actors,
doing the things that I want when I find it.

Thank you very much.

See you.

On Wed, 23 Jan 2002, Jeff Lee wrote:

> Sorry.  Here is the deal.  The code you sent is fine, but if you want 
> more accurate picking you should use vtkPropPicker instead of vtkPicker. 
>  Your revised method should look something like this:
> 
>   public void picked() {
>     vtkPropPicker picker=new vtkPropPicker();
>     System.out.println("Selection point: ("+lastX+","+lastY+")");
>     
> picker.PickProp(lastX,panelVTK.getRenderWindow().GetSize()[1]-lastY,panelVTK.getRenderer());
>     vtkActor actor=picker.GetActor();
>     if (actor==coneActor) {
>       pickedCone();
>     }
>     else if (actor==sphereActor) {
>       pickedSphere();
>     }
>   }
> 
> -Jeff
> 
> Carlos Martinez Burgos wrote:
> 
> >Hi Jeff.
> >
> >Sorry but I didn't explain myself. The code below is my own code. In it I
> >have used vtkPanel adding picking functionality without interactor but the
> >picking doesn't works well.  Follow the instructions on the command line
> >and you could see it. If you get good results, perhaps is my VTK
> >compilation or a wrong picker.
> >
> >The code that uses the interactor is one that Ross Marges sent to me. I
> >didn't send it because I don't know if he want to publish it to the list.  
> >In it he used the vtkPanel but without GUI. He don't run the show() method
> >to rise the window of the GUI. It uses interactor.Start() method. If I run
> >the show() method two windows appear, one with the GUI (vtkPanel) and
> >another with the renderWindow and the interactor. I think that this could
> >be a problem of the Java wrapper that rise a new window on the start
> >method of the interactor.
> >
> >Sorry again. I send a copy of this mail to the list, to make it clear.
> >
> >Thanks.
> >
> >On Mon, 21 Jan 2002, J.A. Lee wrote:
> >
> >>Hi Carlos,
> >>You don't need the interactor if you are using vtkPanel.  I would try 
> >>subclassing  vtkPanel and adding the picking functionality to the 
> >>subclass.  I don't see any renderWindowInteractor in the code below - am 
> >>I missing something?  The code below looks right.  I didn't respond to 
> >>the users list on this one because I think it has been addressed before.
> >>-Jeff
> >>
> >>Carlos Martinez Burgos wrote:
> >>
> >>>Hi all.
> >>>
> >>>Some months ago I sent a mail to the list telling that I had problems with 
> >>>picking in Java. I send the code as attachment.
> >>>
> >>>Ross Manges answered me with some code. This code uses a 
> >>>vtkRenderWindowInteractor. I run it and everything is ok. The problem is 
> >>>that I have to add a GUI to the program and if I add it to the vtkPanel 
> >>>and show it, two windows appear. One of the GUI and other of the renderer. 
> >>>I would that the renderer panel was in the same window that the GUI.
> >>>
> >>>Is this resolved on VTK4.0? Why does the interactor open a window? Could 
> >>>you help me with some code in Java please?
> >>>
> >>>Thanks in advance.
> >>>
> >>>
> >>>------------------------------------------------------------------------
> >>>
> >>>import java.awt.*;
> >>>import java.awt.event.*;
> >>>import javax.swing.*;
> >>>import vtk.*;
> >>>
> >>>public class PickTest extends JFrame implements KeyListener,MouseListener,MouseMotionListener {
> >>>   MyVTKPanel panelVTK;
> >>>
> >>>   vtkConeSource cone;
> >>>   vtkOutlineFilter coneOutline;
> >>>   vtkPolyDataMapper coneMapper,coneOutlineMapper;
> >>>   vtkActor coneActor,coneOutlineActor;
> >>>
> >>>   vtkSphereSource sphere;
> >>>   vtkOutlineFilter sphereOutline;
> >>>   vtkPolyDataMapper sphereMapper,sphereOutlineMapper;
> >>>   vtkActor sphereActor,sphereOutlineActor;
> >>>
> >>>   int lastX,lastY;
> >>>   boolean helpShowed=false;
> >>>
> >>>   public PickTest() {
> >>>	super("Picking test");
> >>>
> >>>	addWindowListener(new WindowAdapter() {
> >>>		public void windowClosing(WindowEvent e) {
> >>>		    System.exit(0);
> >>>		}
> >>>	    });
> >>>
> >>>	panelVTK=new MyVTKPanel();
> >>>	panelVTK.setSize(700,700);
> >>>	panelVTK.LightFollowCameraOn();
> >>>	panelVTK.getRenderer().SetBackground(1,1,1);
> >>>	panelVTK.addKeyListener(this);
> >>>	panelVTK.addMouseListener(this);
> >>>	panelVTK.addMouseMotionListener(this);
> >>>	getContentPane().add(panelVTK,BorderLayout.CENTER);
> >>>
> >>>	cone=new vtkConeSource();
> >>>
> >>>	vtkPolyDataMapper coneMapper=new vtkPolyDataMapper();
> >>>	coneMapper.SetInput(cone.GetOutput());
> >>>
> >>>	coneActor=new vtkActor();
> >>>	coneActor.SetMapper(coneMapper);
> >>>
> >>>	coneOutline=new vtkOutlineFilter();
> >>>	coneOutline.SetInput(cone.GetOutput());
> >>>
> >>>	coneOutlineMapper=new vtkPolyDataMapper();
> >>>	coneOutlineMapper.SetInput(coneOutline.GetOutput());
> >>>
> >>>	coneOutlineActor=new vtkActor();
> >>>	coneOutlineActor.SetMapper(coneOutlineMapper);
> >>>	coneOutlineActor.GetProperty().SetColor(0,0,0);
> >>>	coneOutlineActor.PickableOff();
> >>>	coneOutlineActor.VisibilityOff();
> >>>
> >>>	panelVTK.getRenderer().AddActor(coneActor);
> >>>	panelVTK.getRenderer().AddActor(coneOutlineActor);
> >>>
> >>>	sphere=new vtkSphereSource();
> >>>
> >>>	sphereMapper=new vtkPolyDataMapper();
> >>>	sphereMapper.SetInput(sphere.GetOutput());
> >>>
> >>>	sphereActor=new vtkActor();
> >>>	sphereActor.SetMapper(sphereMapper);
> >>>	sphereActor.SetPosition(1,0,0);
> >>>
> >>>	sphereOutline=new vtkOutlineFilter();
> >>>	sphereOutline.SetInput(sphere.GetOutput());
> >>>
> >>>	sphereOutlineMapper=new vtkPolyDataMapper();
> >>>	sphereOutlineMapper.SetInput(sphereOutline.GetOutput());
> >>>
> >>>	sphereOutlineActor=new vtkActor();
> >>>	sphereOutlineActor.SetMapper(sphereOutlineMapper);
> >>>	sphereOutlineActor.GetProperty().SetColor(0,0,0);
> >>>	sphereOutlineActor.SetPosition(1,0,0);
> >>>	sphereOutlineActor.PickableOff();
> >>>	sphereOutlineActor.VisibilityOff();
> >>>
> >>>	panelVTK.getRenderer().AddActor(sphereActor);
> >>>	panelVTK.getRenderer().AddActor(sphereOutlineActor);
> >>>   }
> >>>
> >>>   public void picked() {
> >>>	vtkPicker picker=new vtkPicker();
> >>>	System.out.println("Selection point: ("+lastX+","+lastY+")");
> >>>	picker.Pick(lastX,getSize().getHeight()-lastY,0,panelVTK.getRenderer());
> >>>	vtkActor actor=picker.GetActor();
> >>>	if (actor==coneActor) {
> >>>	    pickedCone();
> >>>	}
> >>>	else if (actor==sphereActor) {
> >>>	    pickedSphere();
> >>>	}
> >>>   }
> >>>
> >>>   public void pickedCone() {
> >>>	if (coneOutlineActor.GetVisibility()==0) 
> >>>	    coneOutlineActor.VisibilityOn();
> >>>	else
> >>>	    coneOutlineActor.VisibilityOff();
> >>>   }
> >>>
> >>>   public void pickedSphere() {
> >>>	if (sphereOutlineActor.GetVisibility()==0) 
> >>>	    sphereOutlineActor.VisibilityOn();
> >>>	else
> >>>	    sphereOutlineActor.VisibilityOff();
> >>>   }
> >>>
> >>>   public void keyPressed(KeyEvent e) {
> >>>	if (e.getKeyChar()=='u') {
> >>>	    picked();
> >>>	}
> >>>	else if (e.getKeyChar()=='r') {
> >>>	    panelVTK.getCamera().Azimuth(130);
> >>>	    panelVTK.getCamera().OrthogonalizeViewUp();
> >>>	    panelVTK.getLight().SetPosition(panelVTK.getCamera().GetPosition());
> >>>	    panelVTK.getLight().SetFocalPoint(panelVTK.getCamera().GetFocalPoint());
> >>>	    System.out.println("Now point to the cone at left of position (465,350) and pick with key 'u'");
> >>>	}
> >>>	else if (e.getKeyChar()=='h') {
> >>>	    System.out.println("1: press the 'r' key to reset the camera.");
> >>>	    System.out.println("2: point to the cone at left of position (465,350)");
> >>>	    System.out.println("3: press the 'u' key to pick the object");
> >>>	    System.out.println("In my computer I get the sphere selected instead of the cone. Do you?");
> >>>	    helpShowed=true;
> >>>	}
> >>>	panelVTK.Render();
> >>>   }
> >>>   public void keyReleased(KeyEvent e) {}
> >>>   public void keyTyped(KeyEvent e) {}
> >>>
> >>>   public void mouseEntered(MouseEvent e) {
> >>>	lastX=e.getX();
> >>>	lastY=e.getY();
> >>>	if (helpShowed)
> >>>	    System.out.println("Mouse point: ("+lastX+","+lastY+")");
> >>>   }
> >>>   public void mouseExited(MouseEvent e) {}
> >>>   public void mousePressed(MouseEvent e) {}
> >>>   public void mouseReleased(MouseEvent e) {
> >>>	lastX=e.getX();
> >>>	lastY=e.getY();
> >>>	if (helpShowed)
> >>>	    System.out.println("Mouse point: ("+lastX+","+lastY+")");
> >>>   }
> >>>   public void mouseClicked(MouseEvent e) {}
> >>>   public void mouseDragged(MouseEvent e) {}
> >>>   public void mouseMoved(MouseEvent e) {
> >>>	lastX=e.getX();
> >>>	lastY=e.getY();
> >>>	if (helpShowed)
> >>>	    System.out.println("Mouse point: ("+lastX+","+lastY+")");
> >>>   }
> >>>
> >>>   public static void main(String args[]) {
> >>>	PickTest frame=new PickTest();
> >>>	frame.pack();
> >>>	frame.setVisible(true);
> >>>	System.out.println("Press the 'h' key to view some instructions.");
> >>>   }
> >>>
> >>>   class MyVTKPanel extends vtkPanel {
> >>>	public MyVTKPanel() {
> >>>	    super();
> >>>	}
> >>>
> >>>	public vtkCamera getCamera() {
> >>>	    return cam;
> >>>	}
> >>>
> >>>	public vtkLight getLight() {
> >>>	    return lgt;
> >>>	}
> >>>   }
> >>>}
> >>>
> >>
> >
> 
> 
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
> 

-- 
----------------------------------------------------------------------
Carlos Martínez Burgos      |     Instituto Tecnológico de Informática
Ingeniero Informático       |      Universidad Politécnica de Valencia
Tlf: +34 963877237          |                        Camí de Vera, S/N
cmarbur at iti.upv.es          |                   46071 Valencia - Spain
www.iti.upv.es/~cmarbur     |                           www.iti.upv.es
----------------------------------------------------------------------




More information about the vtkusers mailing list