home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch15 / NurbsCurve.java < prev    next >
Text File  |  1997-02-02  |  5KB  |  146 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.  * NurbsCurve.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 NurbsCurve prototype.
  30.  * @author   Timothy F. Rohaly
  31.  * @version  1.0, 1/15/97
  32.  */
  33. public class NurbsCurve extends Script {
  34.  
  35.     private SFBool   debug;
  36.     private SFInt32  segments;
  37.     private MFFloat  knotSequence;
  38.     private MFFloat  controlPoints;
  39.     
  40.     private MFInt32  coordIndex_changed;
  41.     private SFNode   coord_changed;
  42.  
  43.     private Nurbs    curve;
  44.  
  45.  
  46.     public void initialize() {
  47.         debug              = (SFBool)  getField("debug");
  48.         segments           = (SFInt32) getField("segments");
  49.         knotSequence       = (MFFloat) getField("knotSequence");
  50.         controlPoints      = (MFFloat) getField("controlPoints");
  51.  
  52.         coordIndex_changed = (MFInt32) getEventOut("coordIndex_changed");
  53.         coord_changed      = (SFNode)  getEventOut("coord_changed");
  54.  
  55.         //
  56.         // First create the Knots
  57.         //
  58.         int numControlPoints = controlPoints.getSize()/4;
  59.         int numKnots         = knotSequence.getSize();
  60.         int order            = numKnots - numControlPoints;
  61.  
  62.         float[] knot = new float[numKnots];
  63.         for (int i=0; i<numKnots; i++) {
  64.             knot[i] = knotSequence.get1Value(i);
  65.         }
  66.         if (debug.getValue()) {
  67.             System.err.print("Knot Sequence = [ ");
  68.             for (int i=0; i<numKnots; i++) {
  69.                 System.err.print(knotSequence.get1Value(i) + ", ");
  70.             }
  71.             System.err.println("]");
  72.         }
  73.  
  74.         Knot u = new Knot(order, numControlPoints);
  75.         u.setKnots(knot);
  76.  
  77.         numControlPoints =  1;
  78.         numKnots         =  1;
  79.         order            =  0;  // constant
  80.         knot = new float[numKnots];
  81.         knot[0]  = 0.00f;
  82.  
  83.         Knot v = new Knot(order, numControlPoints);
  84.         v.setKnots(knot);
  85.  
  86.  
  87.         //
  88.         // Second create the ControlNet
  89.         //
  90.         Point4[][] points = new Point4[u.getNumControlPoints()]
  91.                                       [v.getNumControlPoints()];
  92.         for (int i=0; i<u.getNumControlPoints(); i++) {
  93.             for (int j=0; j<v.getNumControlPoints(); j++) {
  94.                 // Creates the Point4 objects and initializes them
  95.                 int index = 4*(j + i*v.getNumControlPoints());
  96.                 points[i][j] = new Point4(controlPoints.get1Value(index+0),
  97.                                           controlPoints.get1Value(index+1),
  98.                                           controlPoints.get1Value(index+2),
  99.                                           controlPoints.get1Value(index+3));
  100.             }
  101.         }
  102.         ControlNet controlNet = new ControlNet(u.getNumControlPoints(),
  103.                                                v.getNumControlPoints(), points);
  104.  
  105.         if (debug.getValue()) {
  106.             System.err.print("Control Points = [ ");
  107.             for (int i=0; i<controlPoints.getSize(); i+=4) {
  108.                 System.err.print(controlPoints.get1Value(i+0) + " ");
  109.                 System.err.print(controlPoints.get1Value(i+1) + " ");
  110.                 System.err.print(controlPoints.get1Value(i+2) + " ");
  111.                 System.err.println(controlPoints.get1Value(i+3) + ", ");
  112.             }
  113.             System.err.println("]");
  114.         }
  115.  
  116.         //
  117.         // Finally we can construct the NURBS
  118.         //
  119.         Nurbs temp = new Nurbs(u, v, controlNet);
  120.  
  121.         //
  122.         // Now tessellate to desired smoothness
  123.         //
  124.         curve = temp.tessellate(segments.getValue(),0);
  125.         String newCOORD = curve.toVRMLCoordinateNode();
  126.         int[]  newINDEX = curve.toVRMLCoordIndex();
  127.         if (debug.getValue()) {
  128.             System.err.println(newCOORD);
  129.             System.err.print("coordIndex [ ");
  130.             for (int i=0; i<newINDEX.length; i++) {
  131.                 System.err.print(newINDEX[i] + ", ");
  132.             }
  133.             System.err.println("]");
  134.         }
  135.  
  136.         //
  137.         // Send the eventOuts to the Script node
  138.         //
  139.         try {
  140.             coord_changed.setValue(getBrowser().createVrmlFromString(newCOORD)[0]);
  141.             coordIndex_changed.setValue(newINDEX);
  142.         }
  143.         catch (InvalidVRMLSyntaxException e) {}
  144.     }
  145. }
  146.