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