> From: Zak <[EMAIL PROTECTED]>
>
> I am trying to set up a routine that will check for movement of an object. I
want to be able to tell the location(coordinates of an object) relative to the
screen.
>
> How can I make it check all the time for changes in the object?


Try using the attached utility class.  It should let you inquire the screen
position of a point on the object.  It does this by transforming the point to
VWorld coordinates and then calculating the screen space position of the VWorld
point.

Doug
/*
 *      @(#)WindowToLocal.java 1.3 99/09/15 13:44:27
 *
 * Copyright (c) 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

/**
 * Utility class for doing window->local or window->vworld transformations.
 *
 * Usage:
 *    // after the canvas and node are created
 *    WindowToLocal winToLocal = WindowToLocal(node, canvas);
 *    // node == null for window->vWorld
 *    ...
 *    // when we need to transform points (canvas location and node transforms
 *    // may have changed)
 *    windowToLocal.update(); // make sure transforms are up to date
 *
 *    Point[] windowPts = <some window coords to transform>
 *    Point3d[] localPts = <the transformed local points >
 *    for (int i = 0; i < windowPts.length; i++) {
 *       winToLocal.transformPt(windowPts[i] , localPts[i]);
 *    }
 */

// standard j3d packages
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.Point;
import java.awt.Dimension;

public class WindowToLocal {

    Canvas3D    canvas = null;
    Node        node = null;

    // inquired/derived data
    Transform3D imagePlateToVworld  = new Transform3D();
    Transform3D vWorldToLocal = new Transform3D();
    Transform3D imagePlateToLocal = new Transform3D();

    /**
     * Creates a WindowToLocal object with no associated node or canvas.
     * The canvas (and the node if vWorld to local transforms are wanted)
     * must be set before transforming points
     */
    public WindowToLocal() {};

    /**
     * Setup WindowToLocal to transform from window coordinates on the
     * specified Canvas3D to VWorld coordinates
     */
    public WindowToLocal(Canvas3D canvas) {
        this.canvas = canvas;
        this.node = null;
        update();
    }

    /**
     * Setup WindowToLocal to transform from window coordinates on the
     * specified Canvas3D to the local coordinates of the specified node.
     */
    public WindowToLocal(Node node, Canvas3D canvas) {
        this.canvas = canvas;
        this.node = node;
        update();
    }

    /**
     * Either create WindowToLocal() just before transforming points or call
     * this method to ensure that the transforms are up to date.  Note: if
     * you are transforming several points, you only need to call this method
     * once.
     */
    public void update() {
        if (this.canvas != null) {
            canvas.getImagePlateToVworld(imagePlateToVworld);
            //System.out.println("imagePlateToVworld = " + imagePlateToVworld);
            if (this.node != null) {
                node.getLocalToVworld(vWorldToLocal);
                vWorldToLocal.invert();
            } else {
                vWorldToLocal.set(1.0);
            }
            //System.out.println("vWorldToLocal = " + vWorldToLocal);

            // Make a composite transform:
            // imagePlatePt =  imagePlateToVworld * iplatePt
            // localPt = vWorldToLocal * imagePlatePt;
            // imagePlatePt = vWorldToLocal * imagePlateToVworld * iplatePt;
            imagePlateToLocal.mul(vWorldToLocal, imagePlateToVworld);
            //System.out.println("imagePlateToLocal = " + imagePlateToLocal);

        }
    }

    /**
     * Set the canvas and call update(). This will set up a Window->VWorld
     * transform.
     */
    public void update(Canvas3D canvas) {
        this.canvas = canvas;
        this.node = null;
        update();
    }

    /**
     * Set the node and canvas and call update().  This will set up a
     * Window->Local transform.
     */
    public void update(Node node, Canvas3D canvas) {
        this.canvas = canvas;
        this.node = node;
        update();
    }

    /**
     * Transform the point from Window to Local coords (or to VWorld coords
     * if node == null)
     */
    public void transformPt(Point windowPt, Point3d localPt) {
        // TODO: throw some kind of error if node and canvas haven't been
        // set

        //System.out.println("windowPt = " + windowPt);

        canvas.getPixelLocationInImagePlate(windowPt.x, windowPt.y,
                localPt);
        //System.out.println("imagePlatePt = " + localPt);

        imagePlateToLocal.transform(localPt);
        //System.out.println("local Pt = " + localPt);

    }
}
/*
 *      @(#)WindowToLocalTest.java 1.4 99/09/15 13:44:28
 *
 * Copyright (c) 1996-1999 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

/**
 * Test WindowToLocal utility. Use left (or main) mouse button to rotate,
 * right (or meta + main) mouse button to create new points in the current
 * z=0 plane in local coords
 */

// standard j3d packages
import javax.media.j3d.*;
import javax.vecmath.*;

// sun utility classes
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.applet.MainFrame;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;

public class WindowToLocalTest extends Applet {

    Canvas3D    canvas;
    WindowToLocal winToLocal = null;
    Group       primGroup;
    Appearance  primAppearance;
    Point       windowPt = new Point();
    Point3d[]   primPtArray = new Point3d[1];
    Point3d     primPt = new Point3d();
    WakeupOr    mouseWakeup;

    public BranchGroup createSceneGraph() {

        // create root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // create the TransformGroup
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

        // Create a group to add geometry to
        primGroup = new Group();
        primGroup.setCapability(Group.ALLOW_LOCAL_TO_VWORLD_READ);
        primGroup.setCapability(Group.ALLOW_CHILDREN_READ);
        primGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);
        primGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);

        // create box so that we can add geometry to it's area
        Point3d[] coords = new Point3d[5];

        coords[0] = new Point3d( 0.0, 0.0, 0.0);
        coords[1] = new Point3d( 0.5,-0.5, 0.0);
        coords[2] = new Point3d( 0.5, 0.5, 0.0);
        coords[3] = new Point3d(-0.5, 0.5, 0.0);
        coords[4] = new Point3d(-0.5,-0.5, 0.0);

        int [] lineLength = new int[1];
        lineLength[0] = 5;
        LineStripArray lineArray = new LineStripArray(5, LineArray.COORDINATES,
                lineLength);
        lineArray.setCoordinates(0, coords);

        primAppearance = new Appearance();
        primAppearance.setColoringAttributes(new ColoringAttributes());
        Shape3D shape = new Shape3D(lineArray, primAppearance);

        primGroup.addChild(shape);

        objTrans.addChild(primGroup);

        objRoot.addChild(new Callback());
        MouseRotate mr = new MouseRotate();
        mr.setTransformGroup(objTrans);
        mr.setSchedulingBounds(new BoundingSphere(new Point3d(),1000));
        objRoot.addChild(mr);

        return objRoot;
    }

    public WindowToLocalTest(){

        setLayout(new BorderLayout());
        canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        add("Center", canvas);

        // create a simple scene and attach it to virtual universe
        Transform3D t = new Transform3D();

        // set up a scene graph, this will also initialize "primGroup" and
        // "primAppearance"
        BranchGroup scene = createSceneGraph();

        SimpleUniverse u = new SimpleUniverse(canvas);


        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        u.addBranchGraph(scene);

        //canvas.getView().setProjectionPolicy(View.PARALLEL_PROJECTION);

    }


    // allows this to be run as application as well as applet
    public static void main(String[] argv){
        new MainFrame(new WindowToLocalTest(),256,256);
    }

    public class Callback extends Behavior {

        public void initialize(){
            setSchedulingBounds(new BoundingSphere(new Point3d(),1000));
            WakeupCriterion[] mouseEvents = new WakeupCriterion[2];
            mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
            mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
            mouseWakeup = new WakeupOr(mouseEvents);
            wakeupOn(mouseWakeup);
        }

        public void processStimulus(Enumeration criteria){


            WakeupOnAWTEvent awt=(WakeupOnAWTEvent) criteria.nextElement();
            MouseEvent e=(MouseEvent) awt.getAWTEvent()[0];
            if (e.isMetaDown()) {

                if (winToLocal == null) {
                    // intialize the window to local utility
                    winToLocal = new WindowToLocal(primGroup, canvas);
                } else {
                    winToLocal.update();
                }

                windowPt.x = e.getX();
                windowPt.y = e.getY();
                winToLocal.transformPt(windowPt, primPt);

                BranchGroup primBG = new BranchGroup();
                primPtArray[0] = primPt;
                PointArray pointArray = new PointArray(1,
                        PointArray.COORDINATES);
                pointArray.setCoordinates(0, primPtArray);

                Shape3D shape = new Shape3D(pointArray, primAppearance);
                primBG.addChild(shape);

                primGroup.addChild(primBG);


            }

            wakeupOn(mouseWakeup);
        }
    }
}

Reply via email to