home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-02 | 5.3 KB | 154 lines |
- /*
- * Copyright (c) 1997 ORC Incorporated. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please contact <info@ocnus.com> for
- * further information regarding copyright and licensing.
- * This code is provided without warranty, either expressed or implied.
- */
- /*
- * Portions of this code appear in the book
- * "Late Night VRML 2.0 with Java", Ziff-Davis Press, 1997
- *
- * NurbsRevolve.java
- *
- * author Timothy F. Rohaly
- * version 1.0, 01/15/97
- */
-
- import vrml.*;
- import vrml.field.*;
- import vrml.node.*;
-
- import nurbs.*;
-
-
- /**
- * VRML 2.0 Script node implementing the NurbsRevolve prototype.
- * @author Timothy F. Rohaly
- * @version 1.0, 1/15/97
- */
- public class NurbsRevolve extends Script {
-
- private SFBool debug;
- private SFInt32 segments;
- private SFFloat minAngle;
- private SFFloat maxAngle;
- private SFInt32 profileSegments;
- private MFFloat profileKnotSequence;
- private MFFloat profileControlPoints;
-
-
- private MFInt32 coordIndex_changed;
- private SFNode coord_changed;
-
- public void initialize() {
- debug = (SFBool) getField("debug");
- segments = (SFInt32) getField("segments");
- minAngle = (SFFloat) getField("minAngle");
- maxAngle = (SFFloat) getField("maxAngle");
- profileSegments = (SFInt32) getField("profileSegments");
- profileKnotSequence = (MFFloat) getField("profileKnotSequence");
- profileControlPoints = (MFFloat) getField("profileControlPoints");
-
- coordIndex_changed = (MFInt32) getEventOut("coordIndex_changed");
- coord_changed = (SFNode) getEventOut("coord_changed");
-
-
- //
- // First create the Knots
- //
- int numControlPoints = profileControlPoints.getSize()/4;
- int numKnots = profileKnotSequence.getSize();
- int order = numKnots - numControlPoints;
-
- float[] knot = new float[numKnots];
- for (int i=0; i<numKnots; i++) {
- knot[i] = profileKnotSequence.get1Value(i);
- }
- if (debug.getValue()) {
- System.err.print("Profile Knot Sequence = [ ");
- for (int i=0; i<numKnots; i++) {
- System.err.print(profileKnotSequence.get1Value(i) + ", ");
- }
- System.err.println("]");
- }
-
- Knot u = new Knot(order, numControlPoints);
- u.setKnots(knot);
-
- numControlPoints = 1;
- numKnots = 1;
- order = 0; // constant
- knot = new float[numKnots];
- knot[0] = 0.00f;
-
- Knot v = new Knot(order, numControlPoints);
- v.setKnots(knot);
-
-
- //
- // Second create the ControlNet
- //
- Point4[][] points = new Point4[u.getNumControlPoints()]
- [v.getNumControlPoints()];
- for (int i=0; i<u.getNumControlPoints(); i++) {
- for (int j=0; j<v.getNumControlPoints(); j++) {
- // Creates the Point4 objects and initializes them
- int index = 4*(j + i*v.getNumControlPoints());
- points[i][j] =
- new Point4(profileControlPoints.get1Value(index+0),
- profileControlPoints.get1Value(index+1),
- profileControlPoints.get1Value(index+2),
- profileControlPoints.get1Value(index+3));
- }
- }
- ControlNet controlNet = new ControlNet(u.getNumControlPoints(),
- v.getNumControlPoints(), points);
-
- if (debug.getValue()) {
- System.err.print("Control Points = [ ");
- for (int i=0; i<profileControlPoints.getSize(); i+=4) {
- System.err.print(profileControlPoints.get1Value(i+0) + " ");
- System.err.print(profileControlPoints.get1Value(i+1) + " ");
- System.err.print(profileControlPoints.get1Value(i+2) + " ");
- System.err.println(profileControlPoints.get1Value(i+3) + ", ");
- }
- System.err.println("]");
- }
-
- //
- // Finally we can construct the NURBS
- //
- Nurbs shape = new Nurbs(u, v, controlNet);
- Nurbs temp1 = shape.revolve(minAngle.getValue(), maxAngle.getValue());
-
- //
- // Now tessellate to desired smoothness
- //
- Nurbs temp = temp1.tessellate(segments.getValue(),
- profileSegments.getValue());
- String newCOORD = temp.toVRMLCoordinateNode();
- int[] newINDEX = temp.toVRMLCoordIndex();
- if (debug.getValue()) {
- System.err.println(newCOORD);
- System.err.print("coordIndex [ ");
- for (int i=0; i<newINDEX.length; i++) {
- System.err.print(newINDEX[i] + ", ");
- }
- System.err.println("]");
- }
-
- //
- // Send the eventOuts to the Script node
- //
- try {
- coord_changed.setValue(getBrowser().createVrmlFromString(newCOORD)[0]);
- coordIndex_changed.setValue(newINDEX);
- }
- catch (InvalidVRMLSyntaxException e) {}
- }
- }
-