home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Late Night VRML 2.0 with Java CD-ROM
/
code.zip
/
Ch15
/
NurbsCurve.java
< prev
next >
Wrap
Text File
|
1997-02-02
|
5KB
|
146 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
*
* NurbsCurve.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 NurbsCurve prototype.
* @author Timothy F. Rohaly
* @version 1.0, 1/15/97
*/
public class NurbsCurve extends Script {
private SFBool debug;
private SFInt32 segments;
private MFFloat knotSequence;
private MFFloat controlPoints;
private MFInt32 coordIndex_changed;
private SFNode coord_changed;
private Nurbs curve;
public void initialize() {
debug = (SFBool) getField("debug");
segments = (SFInt32) getField("segments");
knotSequence = (MFFloat) getField("knotSequence");
controlPoints = (MFFloat) getField("controlPoints");
coordIndex_changed = (MFInt32) getEventOut("coordIndex_changed");
coord_changed = (SFNode) getEventOut("coord_changed");
//
// First create the Knots
//
int numControlPoints = controlPoints.getSize()/4;
int numKnots = knotSequence.getSize();
int order = numKnots - numControlPoints;
float[] knot = new float[numKnots];
for (int i=0; i<numKnots; i++) {
knot[i] = knotSequence.get1Value(i);
}
if (debug.getValue()) {
System.err.print("Knot Sequence = [ ");
for (int i=0; i<numKnots; i++) {
System.err.print(knotSequence.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(controlPoints.get1Value(index+0),
controlPoints.get1Value(index+1),
controlPoints.get1Value(index+2),
controlPoints.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<controlPoints.getSize(); i+=4) {
System.err.print(controlPoints.get1Value(i+0) + " ");
System.err.print(controlPoints.get1Value(i+1) + " ");
System.err.print(controlPoints.get1Value(i+2) + " ");
System.err.println(controlPoints.get1Value(i+3) + ", ");
}
System.err.println("]");
}
//
// Finally we can construct the NURBS
//
Nurbs temp = new Nurbs(u, v, controlNet);
//
// Now tessellate to desired smoothness
//
curve = temp.tessellate(segments.getValue(),0);
String newCOORD = curve.toVRMLCoordinateNode();
int[] newINDEX = curve.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) {}
}
}