home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Late Night VRML 2.0 with Java CD-ROM
/
code.zip
/
Ch15
/
NurbsSurface.java
< prev
next >
Wrap
Text File
|
1997-02-02
|
6KB
|
162 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
*
* NurbsSurface.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 NurbsSurface prototype.
* @author Timothy F. Rohaly
* @version 1.0, 1/15/97
*/
public class NurbsSurface extends Script {
private SFBool debug;
private SFInt32 uSegments;
private SFInt32 vSegments;
private MFFloat uKnotSequence;
private MFFloat vKnotSequence;
private SFInt32 numUControlPoints;
private SFInt32 numVControlPoints;
private MFFloat controlPoints;
private MFInt32 coordIndex_changed;
private SFNode coord_changed;
public void initialize() {
debug = (SFBool) getField("debug");
uSegments = (SFInt32) getField("uSegments");
vSegments = (SFInt32) getField("vSegments");
uKnotSequence = (MFFloat) getField("uKnotSequence");
vKnotSequence = (MFFloat) getField("vKnotSequence");
numUControlPoints = (SFInt32) getField("numUControlPoints");
numVControlPoints = (SFInt32) getField("numVControlPoints");
controlPoints = (MFFloat) getField("controlPoints");
coordIndex_changed = (MFInt32) getEventOut("coordIndex_changed");
coord_changed = (SFNode) getEventOut("coord_changed");
//
// First create the Knots
//
int numControlPoints = numUControlPoints.getValue();
int numKnots = uKnotSequence.getSize();
int order = numKnots - numControlPoints;
float[] knot = new float[numKnots];
for (int i=0; i<numKnots; i++) {
knot[i] = uKnotSequence.get1Value(i);
}
if (debug.getValue()) {
System.err.print("U Knot Sequence = [ ");
for (int i=0; i<numKnots; i++) {
System.err.print(uKnotSequence.get1Value(i) + ", ");
}
System.err.println("]");
}
Knot u = new Knot(order, numControlPoints);
u.setKnots(knot);
numControlPoints = numVControlPoints.getValue();
numKnots = vKnotSequence.getSize();
order = numKnots - numControlPoints;
knot = new float[numKnots];
for (int i=0; i<numKnots; i++) {
knot[i] = vKnotSequence.get1Value(i);
}
if (debug.getValue()) {
System.err.print("V Knot Sequence = [ ");
for (int i=0; i<numKnots; i++) {
System.err.print(vKnotSequence.get1Value(i) + ", ");
}
System.err.println("]");
}
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 shape = new Nurbs(u, v, controlNet);
//
// Now tessellate to desired smoothness
//
Nurbs temp = shape.tessellate(uSegments.getValue(),
vSegments.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) {}
}
}