Hi All,

Here is the problem: I have a PickTool object with a PickBound as its PickShape.
The pick mode is PickTool.GEOMETRY. The scene graph consists of 1 polygon.
If there is any point of the polygon located inside the PickShape, then
the PickTool object should intersect with the polygons, so it should return
some PickResults, right? but if fails sometimes.

I guess this is also not because of numerical error of calculation, because
the bound object can correctly detect if the point is inside it while the
PickTool fails to do that.

Thanks---white


/******** PickAllTest.java ********************/

import javax.vecmath.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import java.awt.*;

public class PickAllTest extends javax.swing.JFrame {

    /** Creates new form PickAllTest */
    public PickAllTest() {
        initComponents();
        initGraphics();
    }

    private void initComponents() {

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        pack();
        java.awt.Dimension screenSize = 
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new java.awt.Dimension(640, 480));
        setLocation((screenSize.width-640)/2,(screenSize.height-480)/2);
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }

    private void initGraphics(){
        GraphicsConfiguration config =
            SimpleUniverse.getPreferredConfiguration();

        canvas = new Canvas3D(config);
        u = new SimpleUniverse(canvas);

        imagePanel = new javax.swing.JPanel(new java.awt.BorderLayout());
        getContentPane().add(imagePanel, java.awt.BorderLayout.CENTER);

        imagePanel.add(canvas, java.awt.BorderLayout.CENTER);

        BranchGroup scene = createSceneGraph();

        ViewingPlatform viewingPlatform = u.getViewingPlatform();
        Transform3D t = new Transform3D();
        Point3d cp = new Point3d();
        bounds.getCenter(cp);
        //System.out.println("The bounding center is "+cp.x+", "+cp.y+", "+cp.z);

        t.set(new Vector3d(cp.x, cp.y, cp.z + 3.0*bounds.getRadius()));
        (viewingPlatform.getViewPlatformTransform()).setTransform(t);

        u.addBranchGraph(scene);

        imagePanel.updateUI();

        try {
            Thread.sleep(10000);
        }
        catch (InterruptedException e) {
        }

        pickTest(scene);
    }

    private BranchGroup createSceneGraph(){
        double vertices[] = {0.236547,0.76,0.699422, -0.52633,0.732855,0.521687,
                             -0.85,0.5263,0.4213, -0.9779,0,0.397007,
                             0.0525,0,0.726};

        Shape3D part1 = new Shape3D();
        part1.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);


        int []stripCounts1 = new int[1];

        stripCounts1[0] = 5;

        GeometryInfo gi1 = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
        gi1.setCoordinates(vertices);
        gi1.setStripCounts(stripCounts1);

        GeometryArray ga1 = gi1.getGeometryArray();
        part1.setGeometry(ga1);

        Appearance myApp1 = new Appearance();
        ColoringAttributes colorAttr1 = new ColoringAttributes(0.5f, 0.4f, 0.0f,
                                              ColoringAttributes.SHADE_GOURAUD);

        myApp1.setColoringAttributes(colorAttr1);
        part1.setAppearance(myApp1);
        PickTool.setCapabilities(part1, PickTool.INTERSECT_FULL);

        BranchGroup group = new BranchGroup();
        group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);

        group.addChild(part1);
        bounds = new BoundingSphere(group.getBounds());
        group.compile();

        return group;
    }

    /**
     * Test a fixed pickShape object with the scene graph
     */
    private void pickTest(BranchGroup group) {
    //Define an array of Vector4d for all the planes of the pickBounds
        Vector4d []planeFunc = new Vector4d[5];
        planeFunc[0] = new Vector4d(0.0, 0.0, -1.0, -10.0);
        planeFunc[1] = new Vector4d(-0.99958, 0.0, -0.0288, -0.290);
        planeFunc[2] = new Vector4d(-0.0, -0.998, -0.0595, 0.544);
        planeFunc[3] = new Vector4d(0.9285, 0.0, 0.371, -0.682);
        planeFunc[4] = new Vector4d(-0.0, 0.9626, 0.2706, -1.1146);

        if (myIntersectTest(planeFunc, group))
            System.out.println("This top most point is inside the PickShape!");

        //Define the BoundingPolytope bounds object for picking
        BoundingPolytope bounds = new BoundingPolytope(planeFunc);
        //PickBounds pickBounds = new PickBounds(bounds);

        PickTool pickTool = new PickTool(group);
        pickTool.setMode(PickTool.GEOMETRY);
        //pickTool.setShape(pickBounds, pyramVertex[4]);
        pickTool.setShapeBounds(bounds, new Point3d(0.236547, 0.76, 0.699422));

        PickResult []pr = pickTool.pickAll();


        if (pr == null)
            System.out.println("The number of geometries picked is 0 ");
        else
            System.out.println("The number of geometries picked is " + pr.length);

    }


    /**
     * My test of intersection. If the point is inside the Picking Shape, return
     * true, otherwise, return false
     *
     * @param  planes  The array of planes function of the PickShape
     */
    private boolean myIntersectTest(Vector4d []planes, BranchGroup group) {
        //This is one of the points of the polygon
        Point3d pt = new Point3d(0.236547, 0.76, 0.699422);

        Shape3D shape = (Shape3D)(group.getChild(0));

        Transform3D t = new Transform3D();
        shape.getLocalToVworld(t);

        t.transform(pt, pt);

        /*
        double res;
        for (int ii=0; ii<planes.length; ii++) {
            res = planes[ii].x*pt.x + planes[ii].y*pt.y + planes[ii].z*pt.z +
                            planes[ii].w;
            if (res > 0)
                return false;
        }
        return true;
        */

        BoundingPolytope bounds = new BoundingPolytope(planes);

        return bounds.intersect(pt);

    }

    public static void main(String args[]) {
        new PickAllTest().show();
    }


    // Variables declaration - do not modify
    // End of variables declaration

    private BoundingSphere bounds;
    private SimpleUniverse u;
    private Canvas3D canvas;
    private javax.swing.JPanel imagePanel;
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to