home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / geometry / Transform.bak < prev    next >
Text File  |  1997-01-05  |  9KB  |  352 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.         try {
  67.         node = b.createVrmlFromString("Transform {}")[0];
  68.         }
  69.         catch(InvalidVrmlException e)
  70.         {
  71.             System.out.println("The transform failed " + e);
  72.         }
  73.             
  74.         _addChildren = (EventInMFNode)node.getEventIn("addChildren");
  75.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  76.         _set_center = (EventInSFVec3f)node.getEventIn("center");
  77.         _set_children = (EventInMFNode)node.getEventIn("children");
  78.         _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
  79.         _set_scale = (EventInSFVec3f)node.getEventIn("scale");
  80.         _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
  81.         _set_translation = (EventInSFVec3f)node.getEventIn("translation");
  82.  
  83.         have_browser = true;
  84.  
  85.     }
  86.  
  87.     public Transform(float[] bboxCenter, float[] bboxSize)
  88.     {
  89.         _children = new Vector();
  90.  
  91.         Browser b = Browser.getBrowser();
  92.  
  93.         if(b == null)
  94.             return;
  95.  
  96.         String vrml_string = "Transform { ";
  97.  
  98.         if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
  99.         {
  100.             vrml_string += "bboxCenter " +
  101.                             bboxCenter[0] + " " +
  102.                             bboxCenter[1] + " " +
  103.                             bboxCenter[2] + " ";
  104.  
  105.             _bboxCenter = bboxCenter;
  106.         }
  107.  
  108.         if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
  109.         {
  110.             vrml_string += "bboxCenter " +
  111.                             bboxSize[0] + " " +
  112.                             bboxSize[1] + " " +
  113.                             bboxSize[2] + " ";
  114.  
  115.             _bboxSize = bboxSize;
  116.         }
  117.  
  118.         vrml_string += "}";
  119.  
  120.  
  121.         node = b.createVrmlFromString(vrml_string)[0];
  122.         _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  123.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  124.         _set_center = (EventInSFVec3f)node.getEventIn("center");
  125.         _set_children = (EventInMFNode)node.getEventIn("children");
  126.         _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
  127.         _set_scale = (EventInSFVec3f)node.getEventIn("scale");
  128.         _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
  129.         _set_translation = (EventInSFVec3f)node.getEventIn("translation");
  130.  
  131.         have_browser = true;
  132.     }
  133.  
  134.     public Transform(Browser b, float[] bboxCenter, float[] bboxSize)
  135.     {
  136.         _children = new Vector();
  137.  
  138.         if(b == null)
  139.             return;
  140.  
  141.         String vrml_string = "Transform { ";
  142.  
  143.         if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
  144.         {
  145.             vrml_string += "bboxCenter " +
  146.                             bboxCenter[0] + " " +
  147.                             bboxCenter[1] + " " +
  148.                             bboxCenter[2] + " ";
  149.  
  150.             _bboxCenter = bboxCenter;
  151.         }
  152.  
  153.         if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
  154.         {
  155.             vrml_string += "bboxCenter " +
  156.                             bboxSize[0] + " " +
  157.                             bboxSize[1] + " " +
  158.                             bboxSize[2] + " ";
  159.  
  160.             _bboxSize = bboxSize;
  161.         }
  162.  
  163.         vrml_string += "}";
  164.  
  165.         node = b.createVrmlFromString(vrml_string)[0];
  166.         _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  167.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  168.         _set_center = (EventInSFVec3f)node.getEventIn("center");
  169.         _set_children = (EventInMFNode)node.getEventIn("children");
  170.         _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
  171.         _set_scale = (EventInSFVec3f)node.getEventIn("scale");
  172.         _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
  173.         _set_translation = (EventInSFVec3f)node.getEventIn("translation");
  174.  
  175.         have_browser = true;
  176.     }
  177.  
  178.     public void finalize()
  179.     {
  180.         // force the clean up of internally allocated objects
  181.         _center = null;
  182.         _children = null;
  183.         _rotation = null;
  184.         _scale = null;
  185.         _scaleOrientation = null;
  186.         _translation = null;
  187.         _bboxCenter = null;
  188.         _bboxSize = null;
  189.     }
  190.  
  191.     public void writeToFile(PrintStream fp, int indent)
  192.     {
  193.         int    i;
  194.         StringBuffer buffer = new StringBuffer();
  195.  
  196.         for(i = 0; i < indent; i++)
  197.             buffer.append("  ");
  198.  
  199.         fp.print(buffer.toString());
  200.         if(name != null)
  201.             fp.print("DEF " + name + " ");
  202.  
  203.         fp.println("Transform {");
  204.  
  205.         // print the contents of the Transform node
  206.         if(_center != null)
  207.             fp.println(buffer.toString() + " center " +
  208.                         _center[0] + " " +
  209.                         _center[1] + " " +
  210.                         _center[2]);
  211.  
  212.  
  213.         if(num_children != 0)
  214.         {
  215.             Enumeration e = _children.elements();
  216.  
  217.             fp.print(buffer.toString() + "  children " );
  218.             if(num_children != 1)
  219.                 fp.println("[");
  220.  
  221.             for(;e.hasMoreElements();)
  222.                 ((VrmlObject)(e.nextElement())).writeToFile(fp, indent + 2);
  223.  
  224.             if(num_children != 1)
  225.                 fp.println(buffer.toString() + "  ]");
  226.         }
  227.  
  228.         if(_rotation != null)
  229.             fp.println(buffer.toString() + " rotation " +
  230.                         _rotation[0] + " " +
  231.                         _rotation[1] + " " +
  232.                         _rotation[2] + " " +
  233.                         _rotation[3]);
  234.  
  235.         if(_scale != null)
  236.             fp.println(buffer.toString() + " scale " +
  237.                         _scale[0] + " " +
  238.                         _scale[1] + " " +
  239.                         _scale[2]);
  240.  
  241.         if(_scaleOrientation != null)
  242.             fp.println(buffer.toString() + " scaleOrientation " +
  243.                         _scaleOrientation[0] + " " +
  244.                         _scaleOrientation[1] + " " +
  245.                         _scaleOrientation[2] + " " +
  246.                         _scaleOrientation[3]);
  247.  
  248.         if(_translation != null)
  249.             fp.println(buffer.toString() + " translation " +
  250.                         _translation[0] + " " +
  251.                         _translation[1] + " " +
  252.                         _translation[2]);
  253.  
  254.         if(_bboxCenter != null)
  255.             fp.println(buffer.toString() + " bboxCenter " +
  256.                         _bboxCenter[0] + " " +
  257.                         _bboxCenter[1] + " " +
  258.                         _bboxCenter[2]);
  259.  
  260.         if(_bboxSize != null)
  261.             fp.println(buffer.toString() + " bboxSize " +
  262.                         _bboxSize[0] + " " +
  263.                         _bboxSize[1] + " " +
  264.                         _bboxSize[2]);
  265.  
  266.         fp.println(buffer.toString() + "}");
  267.     }
  268.  
  269.     public void addChildren(VrmlObject child)
  270.     {
  271.         Node[] n = new Node[1];
  272.         n[0] = child.node;
  273.  
  274.         _children.addElement(child);
  275.         if(have_browser)
  276.             _addChildren.setValue(n);
  277.         num_children++;
  278.     }
  279.  
  280.     public void removeChildren(VrmlObject child)
  281.     {
  282.         Node[] n = new Node[1];
  283.         n[0] = child.node;
  284.  
  285.         _children.removeElement(child);
  286.         if(have_browser)
  287.             _addChildren.setValue(n);
  288.         num_children--;
  289.     }
  290.  
  291.     public void set_center(float[] center)
  292.     {
  293.         _center = center;
  294.         if(have_browser)
  295.             _set_center.setValue(center);
  296.     }
  297.  
  298.     public void set_children(VrmlObject[] child_list)
  299.     {
  300.         int    i;
  301.         int num = child_list.length;
  302.         Node[]     node_list;
  303.  
  304.         // replace the current _children list with the new list.
  305.         // force the garbage collection
  306.         _children = null;
  307.  
  308.         // assign the new arrays.
  309.         _children = new Vector(num);
  310.         node_list = new Node[num];
  311.  
  312.         // copy the values across.
  313.         for(i = 0; i < num; i++)
  314.         {
  315.             _children.addElement(child_list[i]);
  316.             node_list[i] = child_list[i].node;
  317.         }
  318.  
  319.         // set the values in the VRML scene
  320.         if(have_browser)
  321.             _set_children.setValue(node_list);
  322.     }
  323.  
  324.     public void set_rotation(float[] rotation)
  325.     {
  326.         _rotation = rotation;
  327.         if(have_browser)
  328.             _set_center.setValue(rotation);
  329.     }
  330.  
  331.     public void set_scale(float[] scale)
  332.     {
  333.         _scale = scale;
  334.         if(have_browser)
  335.             _set_scale.setValue(scale);
  336.     }
  337.  
  338.     public void set_scaleOrientation(float[] val)
  339.     {
  340.         _scaleOrientation = val;
  341.         if(have_browser)
  342.             _set_scaleOrientation.setValue(val);
  343.     }
  344.  
  345.     public void set_translation(float[] val)
  346.     {
  347.         _translation = val;
  348.         if(have_browser)
  349.             _set_translation.setValue(val);
  350.     }
  351. }
  352.