home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch15 / NurbsSurface.java < prev    next >
Text File  |  1997-02-02  |  6KB  |  162 lines

  1. /*
  2.  * Copyright (c) 1997 ORC Incorporated.  All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please contact <info@ocnus.com> for
  8.  * further information regarding copyright and licensing.
  9.  * This code is provided without warranty, either expressed or implied.
  10.  */
  11. /* 
  12.  * Portions of this code appear in the book
  13.  * "Late Night VRML 2.0 with Java", Ziff-Davis Press, 1997
  14.  *
  15.  * NurbsSurface.java
  16.  *
  17.  * author   Timothy F. Rohaly
  18.  * version  1.0, 01/15/97
  19.  */
  20.  
  21. import vrml.*;
  22. import vrml.field.*;
  23. import vrml.node.*;
  24.  
  25. import nurbs.*;
  26.  
  27.  
  28. /**
  29.  * VRML 2.0 Script node implementing the NurbsSurface prototype.
  30.  * @author   Timothy F. Rohaly
  31.  * @version  1.0, 1/15/97
  32.  */
  33. public class NurbsSurface extends Script {
  34.  
  35.     private SFBool   debug;
  36.     private SFInt32  uSegments;
  37.     private SFInt32  vSegments;
  38.     private MFFloat  uKnotSequence;
  39.     private MFFloat  vKnotSequence;
  40.     private SFInt32  numUControlPoints;
  41.     private SFInt32  numVControlPoints;
  42.     private MFFloat  controlPoints;
  43.     
  44.     private MFInt32  coordIndex_changed;
  45.     private SFNode   coord_changed;
  46.  
  47.     public void initialize() {
  48.         debug              = (SFBool)  getField("debug");
  49.         uSegments          = (SFInt32) getField("uSegments");
  50.         vSegments          = (SFInt32) getField("vSegments");
  51.         uKnotSequence      = (MFFloat) getField("uKnotSequence");
  52.         vKnotSequence      = (MFFloat) getField("vKnotSequence");
  53.         numUControlPoints  = (SFInt32) getField("numUControlPoints");
  54.         numVControlPoints  = (SFInt32) getField("numVControlPoints");
  55.         controlPoints      = (MFFloat) getField("controlPoints");
  56.  
  57.         coordIndex_changed = (MFInt32) getEventOut("coordIndex_changed");
  58.         coord_changed      = (SFNode)  getEventOut("coord_changed");
  59.  
  60.         //
  61.         // First create the Knots
  62.         //
  63.         int numControlPoints = numUControlPoints.getValue();
  64.         int numKnots         = uKnotSequence.getSize();
  65.         int order            = numKnots - numControlPoints;
  66.  
  67.         float[] knot = new float[numKnots];
  68.         for (int i=0; i<numKnots; i++) {
  69.             knot[i] = uKnotSequence.get1Value(i);
  70.         }
  71.         if (debug.getValue()) {
  72.             System.err.print("U Knot Sequence = [ ");
  73.             for (int i=0; i<numKnots; i++) {
  74.                 System.err.print(uKnotSequence.get1Value(i) + ", ");
  75.             }
  76.             System.err.println("]");
  77.         }
  78.  
  79.         Knot u = new Knot(order, numControlPoints);
  80.         u.setKnots(knot);
  81.  
  82.         numControlPoints = numVControlPoints.getValue();
  83.         numKnots         = vKnotSequence.getSize();
  84.         order            = numKnots - numControlPoints;
  85.  
  86.         knot = new float[numKnots];
  87.         for (int i=0; i<numKnots; i++) {
  88.             knot[i] = vKnotSequence.get1Value(i);
  89.         }
  90.         if (debug.getValue()) {
  91.             System.err.print("V Knot Sequence = [ ");
  92.             for (int i=0; i<numKnots; i++) {
  93.                 System.err.print(vKnotSequence.get1Value(i) + ", ");
  94.             }
  95.             System.err.println("]");
  96.         }
  97.  
  98.         Knot v = new Knot(order, numControlPoints);
  99.         v.setKnots(knot);
  100.  
  101.  
  102.         //
  103.         // Second create the ControlNet
  104.         //
  105.         Point4[][] points = new Point4[u.getNumControlPoints()]
  106.                                       [v.getNumControlPoints()];
  107.         for (int i=0; i<u.getNumControlPoints(); i++) {
  108.             for (int j=0; j<v.getNumControlPoints(); j++) {
  109.                 // Creates the Point4 objects and initializes them
  110.                 int index = 4*(j + i*v.getNumControlPoints());
  111.                 points[i][j] = new Point4(controlPoints.get1Value(index+0),
  112.                                           controlPoints.get1Value(index+1),
  113.                                           controlPoints.get1Value(index+2),
  114.                                           controlPoints.get1Value(index+3));
  115.             }
  116.         }
  117.         ControlNet controlNet = new ControlNet(u.getNumControlPoints(),
  118.                                                v.getNumControlPoints(), points);
  119.  
  120.         if (debug.getValue()) {
  121.             System.err.print("Control Points = [ ");
  122.             for (int i=0; i<controlPoints.getSize(); i+=4) {
  123.                 System.err.print(controlPoints.get1Value(i+0) + " ");
  124.                 System.err.print(controlPoints.get1Value(i+1) + " ");
  125.                 System.err.print(controlPoints.get1Value(i+2) + " ");
  126.                 System.err.println(controlPoints.get1Value(i+3) + ", ");
  127.             }
  128.             System.err.println("]");
  129.         }
  130.  
  131.         //
  132.         // Finally we can construct the NURBS
  133.         //
  134.         Nurbs  shape    = new Nurbs(u, v, controlNet);
  135.  
  136.         //
  137.         // Now tessellate to desired smoothness
  138.         //
  139.         Nurbs  temp     = shape.tessellate(uSegments.getValue(),
  140.                                            vSegments.getValue() );
  141.         String newCOORD = temp.toVRMLCoordinateNode();
  142.         int[]  newINDEX = temp.toVRMLCoordIndex();
  143.         if (debug.getValue()) {
  144.             System.err.println(newCOORD);
  145.             System.err.print("coordIndex [ ");
  146.             for (int i=0; i<newINDEX.length; i++) {
  147.                 System.err.print(newINDEX[i] + ", ");
  148.             }
  149.             System.err.println("]");
  150.         }
  151.  
  152.         //
  153.         // Send the eventOuts to the Script node
  154.         //
  155.         try {
  156.             coord_changed.setValue(getBrowser().createVrmlFromString(newCOORD)[0]);
  157.             coordIndex_changed.setValue(newINDEX);
  158.         }
  159.         catch (InvalidVRMLSyntaxException e) {}
  160.     }
  161. }
  162.