home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / geometry / Transform.java < prev    next >
Text File  |  1997-01-05  |  9KB  |  345 lines

  1. // VRML Generator
  2. // Copyright Justin Couch 1996
  3. //
  4. // Chapter 13: Late Night VRML 2.0 and Java
  5. //
  6. // Shape class
  7.  
  8. package geometry;
  9.  
  10. import java.io.*;
  11. import java.awt.*;
  12. import java.util.Vector;
  13. import java.util.Enumeration;
  14. import vrml.external.*;
  15. import vrml.external.field.*;
  16. import vrml.external.exception.*;
  17. import geometry.*;
  18.  
  19. public class Transform extends GroupingNode
  20. {
  21.     private int num_children = 0;
  22.  
  23.     private EventInSFVec3f        _set_center;
  24.     private EventInSFRotation    _set_rotation;
  25.     private EventInSFVec3f        _set_scale;
  26.     private EventInSFRotation    _set_scaleOrientation;
  27.     private EventInSFVec3f        _set_translation;
  28.  
  29.     private float[]        _center = null;
  30.     private float[]        _rotation = null;
  31.     private float[]        _scale = null;
  32.     private float[]     _scaleOrientation = null;
  33.     private float[]        _translation = null;
  34.  
  35.     // constructor builds a copy of itself and is added to the scene
  36.     // graph.
  37.     public Transform()
  38.     {
  39.         _children = new Vector();
  40.  
  41.         Browser b = Browser.getBrowser();
  42.  
  43.         if(b == null)
  44.             return;
  45.  
  46.         node = b.createVrmlFromString("Transform {}")[0];
  47.         _addChildren = (EventInMFNode)node.getEventIn("addChildren");
  48.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  49.         _set_center = (EventInSFVec3f)node.getEventIn("center");
  50.         _set_children = (EventInMFNode)node.getEventIn("children");
  51.         _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
  52.         _set_scale = (EventInSFVec3f)node.getEventIn("scale");
  53.         _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
  54.         _set_translation = (EventInSFVec3f)node.getEventIn("translation");
  55.  
  56.         have_browser = true;
  57.     }
  58.  
  59.     public Transform(Browser b)
  60.     {
  61.         _children = new Vector();
  62.  
  63.         if(b == null)
  64.             return;
  65.  
  66.         node = b.createVrmlFromString("Transform {}")[0];
  67.         _addChildren = (EventInMFNode)node.getEventIn("addChildren");
  68.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  69.         _set_center = (EventInSFVec3f)node.getEventIn("center");
  70.         _set_children = (EventInMFNode)node.getEventIn("children");
  71.         _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
  72.         _set_scale = (EventInSFVec3f)node.getEventIn("scale");
  73.         _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
  74.         _set_translation = (EventInSFVec3f)node.getEventIn("translation");
  75.  
  76.         have_browser = true;
  77.  
  78.     }
  79.  
  80.     public Transform(float[] bboxCenter, float[] bboxSize)
  81.     {
  82.         _children = new Vector();
  83.  
  84.         Browser b = Browser.getBrowser();
  85.  
  86.         if(b == null)
  87.             return;
  88.  
  89.         String vrml_string = "Transform { ";
  90.  
  91.         if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
  92.         {
  93.             vrml_string += "bboxCenter " +
  94.                             bboxCenter[0] + " " +
  95.                             bboxCenter[1] + " " +
  96.                             bboxCenter[2] + " ";
  97.  
  98.             _bboxCenter = bboxCenter;
  99.         }
  100.  
  101.         if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
  102.         {
  103.             vrml_string += "bboxCenter " +
  104.                             bboxSize[0] + " " +
  105.                             bboxSize[1] + " " +
  106.                             bboxSize[2] + " ";
  107.  
  108.             _bboxSize = bboxSize;
  109.         }
  110.  
  111.         vrml_string += "}";
  112.  
  113.  
  114.         node = b.createVrmlFromString(vrml_string)[0];
  115.         _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  116.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  117.         _set_center = (EventInSFVec3f)node.getEventIn("center");
  118.         _set_children = (EventInMFNode)node.getEventIn("children");
  119.         _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
  120.         _set_scale = (EventInSFVec3f)node.getEventIn("scale");
  121.         _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
  122.         _set_translation = (EventInSFVec3f)node.getEventIn("translation");
  123.  
  124.         have_browser = true;
  125.     }
  126.  
  127.     public Transform(Browser b, float[] bboxCenter, float[] bboxSize)
  128.     {
  129.         _children = new Vector();
  130.  
  131.         if(b == null)
  132.             return;
  133.  
  134.         String vrml_string = "Transform { ";
  135.  
  136.         if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
  137.         {
  138.             vrml_string += "bboxCenter " +
  139.                             bboxCenter[0] + " " +
  140.                             bboxCenter[1] + " " +
  141.                             bboxCenter[2] + " ";
  142.  
  143.             _bboxCenter = bboxCenter;
  144.         }
  145.  
  146.         if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
  147.         {
  148.             vrml_string += "bboxCenter " +
  149.                             bboxSize[0] + " " +
  150.                             bboxSize[1] + " " +
  151.                             bboxSize[2] + " ";
  152.  
  153.             _bboxSize = bboxSize;
  154.         }
  155.  
  156.         vrml_string += "}";
  157.  
  158.         node = b.createVrmlFromString(vrml_string)[0];
  159.         _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  160.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  161.         _set_center = (EventInSFVec3f)node.getEventIn("center");
  162.         _set_children = (EventInMFNode)node.getEventIn("children");
  163.         _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
  164.         _set_scale = (EventInSFVec3f)node.getEventIn("scale");
  165.         _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
  166.         _set_translation = (EventInSFVec3f)node.getEventIn("translation");
  167.  
  168.         have_browser = true;
  169.     }
  170.  
  171.     public void finalize()
  172.     {
  173.         // force the clean up of internally allocated objects
  174.         _center = null;
  175.         _children = null;
  176.         _rotation = null;
  177.         _scale = null;
  178.         _scaleOrientation = null;
  179.         _translation = null;
  180.         _bboxCenter = null;
  181.         _bboxSize = null;
  182.     }
  183.  
  184.     public void writeToFile(PrintStream fp, int indent)
  185.     {
  186.         int    i;
  187.         StringBuffer buffer = new StringBuffer();
  188.  
  189.         for(i = 0; i < indent; i++)
  190.             buffer.append("  ");
  191.  
  192.         fp.print(buffer.toString());
  193.         if(name != null)
  194.             fp.print("DEF " + name + " ");
  195.  
  196.         fp.println("Transform {");
  197.  
  198.         // print the contents of the Transform node
  199.         if(_center != null)
  200.             fp.println(buffer.toString() + " center " +
  201.                         _center[0] + " " +
  202.                         _center[1] + " " +
  203.                         _center[2]);
  204.  
  205.  
  206.         if(num_children != 0)
  207.         {
  208.             Enumeration e = _children.elements();
  209.  
  210.             fp.print(buffer.toString() + "  children " );
  211.             if(num_children != 1)
  212.                 fp.println("[");
  213.  
  214.             for(;e.hasMoreElements();)
  215.                 ((VrmlObject)(e.nextElement())).writeToFile(fp, indent + 2);
  216.  
  217.             if(num_children != 1)
  218.                 fp.println(buffer.toString() + "  ]");
  219.         }
  220.  
  221.         if(_rotation != null)
  222.             fp.println(buffer.toString() + " rotation " +
  223.                         _rotation[0] + " " +
  224.                         _rotation[1] + " " +
  225.                         _rotation[2] + " " +
  226.                         _rotation[3]);
  227.  
  228.         if(_scale != null)
  229.             fp.println(buffer.toString() + " scale " +
  230.                         _scale[0] + " " +
  231.                         _scale[1] + " " +
  232.                         _scale[2]);
  233.  
  234.         if(_scaleOrientation != null)
  235.             fp.println(buffer.toString() + " scaleOrientation " +
  236.                         _scaleOrientation[0] + " " +
  237.                         _scaleOrientation[1] + " " +
  238.                         _scaleOrientation[2] + " " +
  239.                         _scaleOrientation[3]);
  240.  
  241.         if(_translation != null)
  242.             fp.println(buffer.toString() + " translation " +
  243.                         _translation[0] + " " +
  244.                         _translation[1] + " " +
  245.                         _translation[2]);
  246.  
  247.         if(_bboxCenter != null)
  248.             fp.println(buffer.toString() + " bboxCenter " +
  249.                         _bboxCenter[0] + " " +
  250.                         _bboxCenter[1] + " " +
  251.                         _bboxCenter[2]);
  252.  
  253.         if(_bboxSize != null)
  254.             fp.println(buffer.toString() + " bboxSize " +
  255.                         _bboxSize[0] + " " +
  256.                         _bboxSize[1] + " " +
  257.                         _bboxSize[2]);
  258.  
  259.         fp.println(buffer.toString() + "}");
  260.     }
  261.  
  262.     public void addChildren(VrmlObject child)
  263.     {
  264.         Node[] n = new Node[1];
  265.         n[0] = child.node;
  266.  
  267.         _children.addElement(child);
  268.         if(have_browser)
  269.             _addChildren.setValue(n);
  270.         num_children++;
  271.     }
  272.  
  273.     public void removeChildren(VrmlObject child)
  274.     {
  275.         Node[] n = new Node[1];
  276.         n[0] = child.node;
  277.  
  278.         _children.removeElement(child);
  279.         if(have_browser)
  280.             _addChildren.setValue(n);
  281.         num_children--;
  282.     }
  283.  
  284.     public void set_center(float[] center)
  285.     {
  286.         _center = center;
  287.         if(have_browser)
  288.             _set_center.setValue(center);
  289.     }
  290.  
  291.     public void set_children(VrmlObject[] child_list)
  292.     {
  293.         int    i;
  294.         int num = child_list.length;
  295.         Node[]     node_list;
  296.  
  297.         // replace the current _children list with the new list.
  298.         // force the garbage collection
  299.         _children = null;
  300.  
  301.         // assign the new arrays.
  302.         _children = new Vector(num);
  303.         node_list = new Node[num];
  304.  
  305.         // copy the values across.
  306.         for(i = 0; i < num; i++)
  307.         {
  308.             _children.addElement(child_list[i]);
  309.             node_list[i] = child_list[i].node;
  310.         }
  311.  
  312.         // set the values in the VRML scene
  313.         if(have_browser)
  314.             _set_children.setValue(node_list);
  315.     }
  316.  
  317.     public void set_rotation(float[] rotation)
  318.     {
  319.         _rotation = rotation;
  320.         if(have_browser)
  321.             _set_center.setValue(rotation);
  322.     }
  323.  
  324.     public void set_scale(float[] scale)
  325.     {
  326.         _scale = scale;
  327.         if(have_browser)
  328.             _set_scale.setValue(scale);
  329.     }
  330.  
  331.     public void set_scaleOrientation(float[] val)
  332.     {
  333.         _scaleOrientation = val;
  334.         if(have_browser)
  335.             _set_scaleOrientation.setValue(val);
  336.     }
  337.  
  338.     public void set_translation(float[] val)
  339.     {
  340.         _translation = val;
  341.         if(have_browser)
  342.             _set_translation.setValue(val);
  343.     }
  344. }
  345.