Skip to Main Content

URGENT: Custom Behavior Problem

843799Nov 21 2001 — edited Nov 22 2001
I'm writing an application which must plot a 3D curve. It has 3 panels, each of which has a 2D projection of the curve (xy, xz, yz). Moving the points on the panels will change the properties of the underlying 3D curve.
When I start the application, The 3D curve is shown correctly, but when I move the points, the 3D curve disappears.
The problem is not the canvas3d not being redrawn, because resizing the window doesn't bring the curve back. Any idea why the curve isn't redrawn?

The code for the curve is as follows:
import javax.media.j3d.*;
import javax.vecmath.*;
import java.lang.Math.*;

public class CubicCurve3D extends Shape3D{



	public CubicCurve3D(Point3d p1, Point3d p2, Point3d p3, Point3d p4){

		this.setGeometry(createGeometry(p1,p2,p3,p4));

	}

	public Geometry createGeometry(Point3d p1, Point3d p2, Point3d p3, Point3d p4){

		// setup the LineArray to represent the curve
		LineArray curveLine = new LineArray(148, GeometryArray.COORDINATES);
		// Add vertices to the array
		setupLineArray(p1,p2,p3,p4,curveLine);

		return curveLine;
	}

	// This method adds the vertices into the array
	public void setupLineArray(Point3d p1, Point3d p2, Point3d p3, Point3d p4, LineArray curve){

		final Point3d p = new Point3d();;
		double nx = 0;
		double ny = 0;
		double nz = 0;
		double u = 0.0;
		int lIndex = 0;
		for(int i=0;i<75;i++){

			u = ((double)i/100)/0.75;

			// Equations to work out point
			nx = ((p1.x)*Math.pow((1-u),3.0)) + ((p2.x)*(3*u)*Math.pow((1-u),2.0)) + ((p3.x)*(3*u*u)*(1-u)) + ((p4.x)*Math.pow(u,3.0));
			ny = ((p1.y)*Math.pow((1-u),3.0)) + ((p2.y)*(3*u)*Math.pow((1-u),2.0)) + ((p3.y)*(3*u*u)*(1-u)) + ((p4.y)*Math.pow(u,3.0));
			nz = ((p1.z)*Math.pow((1-u),3.0)) + ((p2.z)*(3*u)*Math.pow((1-u),2.0)) + ((p3.z)*(3*u*u)*(1-u)) + ((p4.z)*Math.pow(u,3.0));

			p.set(nx,ny,nz);

			// Put point into LineArray
			curve.setCoordinate(lIndex,p);
			if((i > 0) && (i < 74)){
				lIndex++;
				curve.setCoordinate(lIndex,p);
			}
			lIndex++;


		}

	}

	public void updateGeometry(Point3d p1, Point3d p2, Point3d p3, Point3d p4){

		this.setGeometry(createGeometry(p1,p2,p3,p4));

	}
}
The behavior, called UpdateBehavior, is:
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;

public class UpdateBehavior extends Behavior
{

	// The set of points to be updated
	private Point3d[] points;
	private CubicCurve3D cc;
	public UpdateBehavior(CubicCurve3D c)
	{
		cc = c;
		postId(1);
	}

	public void initialize()
	{
		this.wakeupOn(new WakeupOnBehaviorPost(this, 1));
	}

	public void processStimulus(Enumeration e)
	{
		cc.updateGeometry(points[0],points[1],points[2],points[3]);
		this.wakeupOn(new WakeupOnBehaviorPost(this, 1));
	}

	public void setNewPoints(Point3d[] p)
	{
		points = p;
		this.postId(1);
	}
}
And the createSceneGraph() method from the Graph3D class is:
	public BranchGroup createSceneGraph() {
		// Create the root of the branch group
		BranchGroup objRoot = new BranchGroup();

		// rotate object has composite transformation matrix
		Transform3D rotate = new Transform3D();
		Transform3D tempRotate = new Transform3D();

		rotate.rotX(Math.PI/20.0d);
		tempRotate.rotY(Math.PI/6.0d);

		rotate.mul(tempRotate);
		TransformGroup objRotate = new TransformGroup(rotate);

		cc = new CubicCurve3D(points[0],points[1],points[2],points[3]);

		cc.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
		cc.setCapability(Shape3D.ALLOW_GEOMETRY_READ);

		Line3D ax1 = new Line3D(new Point3d(0,0,0), new Point3d(0.75,0,0));
		Line3D ax2 = new Line3D(new Point3d(0,0,0), new Point3d(0,0.75,0));
		Line3D ax3 = new Line3D(new Point3d(0,0,0), new Point3d(0,0,0.75));

		objRotate.addChild(ax1);
		objRotate.addChild(ax2);
		objRotate.addChild(ax3);
		objRotate.addChild(cc);

		uBehavior = new UpdateBehavior(cc);
		uBehavior.setSchedulingBounds(new BoundingSphere());

		objRoot.addChild(objRotate);
		objRoot.addChild(uBehavior);


		return objRoot;
	}
Graph3D also has a method updatePoints():
	public void updatePoints(Point3d[] p)
	{
		points = p;
		uBehavior.setNewPoints(points);
		System.out.println(cc.getP1() + "," + cc.getP2() + "," + cc.getP3() + "," + cc.getP4());
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Dec 20 2001
Added on Nov 21 2001
1 comment
40 views