Exercise 9: Picking a Shape

This Lab involves writing an application that displays 3d shapes. When one of these shapes is clicked on the program will determine which shape, and print it out.

Lab 9 ScreenShot


Without the utilities

Or

   private BranchGroup pickBG = new BranchGroup();
   private Board3D board = ???

   ....
   ....

   board.addMouseListener(new MouseHandler());

   ....
   ....

   private void mouseClick(int x, int y)
   {
       Point3d eye_pos = new Point3d();
       Point3d mouse_pos = new Point3d();

       board.getCenterEyeInImagePlate(eye_pos);
       board.getPixelLocationInImagePlate(x, y, mouse_pos);

       Transform3D motion = new Transform3D();
       board.getImagePlateToVworld(motion);
       motion.transform(eye_pos);
       motion.transform(mouse_pos);

       Vector3d direction = new Vector3d(mouse_pos);
       direction.sub(eye_pos);

       PickRay pickRay = new PickRay(eye_pos, direction);

       SceneGraphPath[] path = pickBG.pickAll(pickRay);

       ....
   }

   ....
   ....

   private class MouseHandler extends MouseAdapter
   {
       public void mousePressed(MouseEvent e)
       {
           int x = e.getX();
           int y = e.getY();

           mouseClick(x, y);
       }
   }


With the utilities

G.3.21 PickTest


Directory: demo/java3d/PickTest
The PickTest program displays several 3D objects and a control panel. The control panel allows the user to change the pick mode, the pick tolerance, and the view mode. You can pick and rotate objects with the mouse. The PickTest program demonstrates the use of the PickMouseBehavior utility classes.
The IntersectTest program demonstrates the ability to get geometric intersection information from a picked object. Use the mouse to pick a point on one of the objects in the window. The program illuminates the picked location and the vertices of the primitive with tiny spheres. Information about the picked primitive and the point of intersection is printed. The IntersectTest program uses a mouse-based behavior, IntersectInfoBehavior, to control the picking. The relevant source code fragments from IntersectInfoBehavior.java follow:

------------------------------------------------------------------------
PickCanvas pickCanvas;
PickResult[] pickResult;
public IntersectInfoBehavior(Canvas3D canvas3D,
BranchGroup branchGroup,
float size) {
pickCanvas = new PickCanvas(canvas3D, branchGroup);
pickCanvas.setTolerance(5.0f);
pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO);
...
------------------------------------------------------------------------
The IntersectInfoBehavior class constructor creates a new PickCanvas object, initializes the PickCanvas with the desired tolerance, and sets the mode to allow geometry intersection information to be retrieved.

http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_2_API/j3dguide/AppendixExamples.html


public void processStimulus(Enumeration criteria) {
...
<check for mouse event> if (eventId == MouseEvent.MOUSE_PRESSED) {
int x = ((MouseEvent)event[i]).getX();
int y = ((MouseEvent)event[i]).getY();
pickCanvas.setShapeLocation(x, y);
Point3d eyePos = pickCanvas.getStartPosition();
pickResult = pickCanvas.pickAllSorted();
if (pickResult != null) {
// Get node info
Node curNode = pickResult[0].getObject();
Geometry curGeom = ((Shape3D)curNode).getGeometry();
GeometryArray curGeomArray = (GeometryArray) curGeom;
// Get closest intersection results
PickIntersection pi =
pickResult[0].getClosestIntersection(eyePos);
<get specific info from PickIntersection>
}
}
...