> Date:         Wed, 9 Jul 2003 15:05:49 +0200
> From: "Horst Walther (SiG)" <[EMAIL PROTECTED]>
>
> simple question: how can I add objects, e.g. simple spheres, to a life
> graph?
>
> Background is: I like to dynamically add 3d-datapoints read from a external
> data feeding device each minute to even the same transformation group.
> Depending on the users choice on the limit the oldest data points should
> disappear then, while he still is able to rotate, translate an zoom it
> manually.

I had to do almost exactly the same thing to test various Sensor
implementations: read positions from an array of Sensors, display the
current Sensor positions, and display a history of past positions while
allowing view manipulations as the positions are added.

I used a PointArray to store 100000 past positions and updated them using
by-reference semantics, reusing the individual points when the limit was
reached.  I tried using spheres but decided that I couldn't get enough
past history displayed with acceptable performance.

I've appended the Behavior I implemented.  If you really need spheres it
probably won't be too useful to you, but you might be able to combine
parts of it with the code that's already been posted on this list.  On
the other hand, I think points are a good alternative if performance is a
problem; you can use PointAttributes to make the points larger if you
need to.

-- Mark Hood

/*
 * Copyright (c) 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * 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.
 *
 * You acknowledge that Software is not designed,licensed or intended
 * for use in the design, construction, operation or maintenance of
 * any nuclear facility.
 */
import java.util.Enumeration ;
import javax.media.j3d.* ;
import javax.vecmath.* ;
import com.sun.j3d.utils.behaviors.sensor.* ;

/**
 * Read positions from an array of Sensors and displays them as points.
 * The current Sensor positions are displayed as gnomon echos.  The history of
 * past positions for each Sensor is displayed as trail of points.
 */
public class SensorDisplayBehavior
    extends Behavior implements GeometryUpdater {
    /**
     * The total number of points to display.  When this limit is reached they
     * are reused.  Note that all of them are always displayed, even in the
     * initial condition when they are all [0, 0, 0].
     */
    final static int pointCount = 100000 ;

    SensorEventAgent eventAgent = null ;
    WakeupCondition wakeupConditions = null ;
    Transform3D t3d = new Transform3D() ;

    // Points updated from sensors reads.
    Vector3f[] newPoints = null ;
    int curNewPoint = 0 ;

    // Leaves a static trail of points behind each sensor.
    float[] points = new float[pointCount*3] ;
    PointArray pointArray = null ;
    int curPoint = 0 ;

    /**
     * Create a new SensorDisplayBehavior.
     *
     * @param s an array of Sensors
     * @param tg an array of TransformGroups for displaying the current
     *  Sensor position; must map 1-1 with the Sensor array, and the
     *  caller is responsible for adding them to a scene graph
     * @param scene the BranchGroup used to display the trail of past
     *  positions
     */
    public SensorDisplayBehavior(Sensor[] s, TransformGroup[] tg,
                                 BranchGroup scene) {

        this.eventAgent = new SensorEventAgent(this) ;
        this.newPoints = new Vector3f[s.length] ;

        for (int i = 0 ; i < s.length ; i++) {
            newPoints[i] = new Vector3f() ;
            eventAgent.addSensorReadListener(s[i], new ReadListener(tg[i])) ;
        }

        pointArray = new PointArray(pointCount,
                                    GeometryArray.COORDINATES |
                                    GeometryArray.BY_REFERENCE) ;

        pointArray.setCoordRefFloat(points) ;
        pointArray.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE) ;

        Shape3D pointShape = new Shape3D(pointArray) ;
        scene.addChild(pointShape) ;
    }

    class ReadListener implements SensorReadListener {
        TransformGroup tg = null ;

        ReadListener(TransformGroup tg) {
            this.tg = tg ;
            tg.addChild(new SensorGnomonEcho(t3d, 0.02, 0.10, true)) ;
        }

        public void read(SensorEvent e) {
            e.getSensorRead(t3d) ;
            tg.setTransform(t3d) ;
            t3d.get(newPoints[curNewPoint++]) ;
        }
    }

    public void initialize() {
        wakeupConditions = new WakeupOnElapsedFrames(0) ;
        wakeupOn(wakeupConditions) ;
    }

    public void processStimulus(Enumeration e) {
        eventAgent.dispatchEvents() ;
        pointArray.updateData(this) ;
        wakeupOn(wakeupConditions) ;
    }

    public void updateData(Geometry g) {
        for (int i = 0 ; i < curNewPoint ; i++) {
            points[curPoint*3+0] = newPoints[i].x ;
            points[curPoint*3+1] = newPoints[i].y ;
            points[curPoint*3+2] = newPoints[i].z ;

            if (++curPoint == pointCount) curPoint = 0 ;
        }

        curNewPoint = 0 ;
    }
}

===========================================================================
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