home *** CD-ROM | disk | FTP | other *** search
- // VRML Generator
- // Copyright Justin Couch 1996
- //
- // Chapter 13: Late Night VRML 2.0 and Java
- //
- // Shape class
-
- package geometry;
-
- import java.io.*;
- import java.awt.*;
- import java.util.Vector;
- import java.util.Enumeration;
- import vrml.external.*;
- import vrml.external.field.*;
- import vrml.external.exception.*;
- import geometry.*;
-
- public class Transform extends GroupingNode
- {
- private int num_children = 0;
-
- private EventInSFVec3f _set_center;
- private EventInSFRotation _set_rotation;
- private EventInSFVec3f _set_scale;
- private EventInSFRotation _set_scaleOrientation;
- private EventInSFVec3f _set_translation;
-
- private float[] _center = null;
- private float[] _rotation = null;
- private float[] _scale = null;
- private float[] _scaleOrientation = null;
- private float[] _translation = null;
-
- // constructor builds a copy of itself and is added to the scene
- // graph.
- public Transform()
- {
- _children = new Vector();
-
- Browser b = Browser.getBrowser();
-
- if(b == null)
- return;
-
- node = b.createVrmlFromString("Transform {}")[0];
- _addChildren = (EventInMFNode)node.getEventIn("addChildren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_center = (EventInSFVec3f)node.getEventIn("center");
- _set_children = (EventInMFNode)node.getEventIn("children");
- _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
- _set_scale = (EventInSFVec3f)node.getEventIn("scale");
- _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
- _set_translation = (EventInSFVec3f)node.getEventIn("translation");
-
- have_browser = true;
- }
-
- public Transform(Browser b)
- {
- _children = new Vector();
-
- if(b == null)
- return;
-
- try {
- node = b.createVrmlFromString("Transform {}")[0];
- }
- catch(InvalidVrmlException e)
- {
- System.out.println("The transform failed " + e);
- }
-
- _addChildren = (EventInMFNode)node.getEventIn("addChildren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_center = (EventInSFVec3f)node.getEventIn("center");
- _set_children = (EventInMFNode)node.getEventIn("children");
- _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
- _set_scale = (EventInSFVec3f)node.getEventIn("scale");
- _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
- _set_translation = (EventInSFVec3f)node.getEventIn("translation");
-
- have_browser = true;
-
- }
-
- public Transform(float[] bboxCenter, float[] bboxSize)
- {
- _children = new Vector();
-
- Browser b = Browser.getBrowser();
-
- if(b == null)
- return;
-
- String vrml_string = "Transform { ";
-
- if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
- {
- vrml_string += "bboxCenter " +
- bboxCenter[0] + " " +
- bboxCenter[1] + " " +
- bboxCenter[2] + " ";
-
- _bboxCenter = bboxCenter;
- }
-
- if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
- {
- vrml_string += "bboxCenter " +
- bboxSize[0] + " " +
- bboxSize[1] + " " +
- bboxSize[2] + " ";
-
- _bboxSize = bboxSize;
- }
-
- vrml_string += "}";
-
-
- node = b.createVrmlFromString(vrml_string)[0];
- _addChildren = (EventInMFNode)node.getEventIn("addChilren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_center = (EventInSFVec3f)node.getEventIn("center");
- _set_children = (EventInMFNode)node.getEventIn("children");
- _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
- _set_scale = (EventInSFVec3f)node.getEventIn("scale");
- _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
- _set_translation = (EventInSFVec3f)node.getEventIn("translation");
-
- have_browser = true;
- }
-
- public Transform(Browser b, float[] bboxCenter, float[] bboxSize)
- {
- _children = new Vector();
-
- if(b == null)
- return;
-
- String vrml_string = "Transform { ";
-
- if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
- {
- vrml_string += "bboxCenter " +
- bboxCenter[0] + " " +
- bboxCenter[1] + " " +
- bboxCenter[2] + " ";
-
- _bboxCenter = bboxCenter;
- }
-
- if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
- {
- vrml_string += "bboxCenter " +
- bboxSize[0] + " " +
- bboxSize[1] + " " +
- bboxSize[2] + " ";
-
- _bboxSize = bboxSize;
- }
-
- vrml_string += "}";
-
- node = b.createVrmlFromString(vrml_string)[0];
- _addChildren = (EventInMFNode)node.getEventIn("addChilren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_center = (EventInSFVec3f)node.getEventIn("center");
- _set_children = (EventInMFNode)node.getEventIn("children");
- _set_rotation = (EventInSFRotation)node.getEventIn("rotation");
- _set_scale = (EventInSFVec3f)node.getEventIn("scale");
- _set_scaleOrientation = (EventInSFRotation)node.getEventIn("scaleOrientation");
- _set_translation = (EventInSFVec3f)node.getEventIn("translation");
-
- have_browser = true;
- }
-
- public void finalize()
- {
- // force the clean up of internally allocated objects
- _center = null;
- _children = null;
- _rotation = null;
- _scale = null;
- _scaleOrientation = null;
- _translation = null;
- _bboxCenter = null;
- _bboxSize = null;
- }
-
- public void writeToFile(PrintStream fp, int indent)
- {
- int i;
- StringBuffer buffer = new StringBuffer();
-
- for(i = 0; i < indent; i++)
- buffer.append(" ");
-
- fp.print(buffer.toString());
- if(name != null)
- fp.print("DEF " + name + " ");
-
- fp.println("Transform {");
-
- // print the contents of the Transform node
- if(_center != null)
- fp.println(buffer.toString() + " center " +
- _center[0] + " " +
- _center[1] + " " +
- _center[2]);
-
-
- if(num_children != 0)
- {
- Enumeration e = _children.elements();
-
- fp.print(buffer.toString() + " children " );
- if(num_children != 1)
- fp.println("[");
-
- for(;e.hasMoreElements();)
- ((VrmlObject)(e.nextElement())).writeToFile(fp, indent + 2);
-
- if(num_children != 1)
- fp.println(buffer.toString() + " ]");
- }
-
- if(_rotation != null)
- fp.println(buffer.toString() + " rotation " +
- _rotation[0] + " " +
- _rotation[1] + " " +
- _rotation[2] + " " +
- _rotation[3]);
-
- if(_scale != null)
- fp.println(buffer.toString() + " scale " +
- _scale[0] + " " +
- _scale[1] + " " +
- _scale[2]);
-
- if(_scaleOrientation != null)
- fp.println(buffer.toString() + " scaleOrientation " +
- _scaleOrientation[0] + " " +
- _scaleOrientation[1] + " " +
- _scaleOrientation[2] + " " +
- _scaleOrientation[3]);
-
- if(_translation != null)
- fp.println(buffer.toString() + " translation " +
- _translation[0] + " " +
- _translation[1] + " " +
- _translation[2]);
-
- if(_bboxCenter != null)
- fp.println(buffer.toString() + " bboxCenter " +
- _bboxCenter[0] + " " +
- _bboxCenter[1] + " " +
- _bboxCenter[2]);
-
- if(_bboxSize != null)
- fp.println(buffer.toString() + " bboxSize " +
- _bboxSize[0] + " " +
- _bboxSize[1] + " " +
- _bboxSize[2]);
-
- fp.println(buffer.toString() + "}");
- }
-
- public void addChildren(VrmlObject child)
- {
- Node[] n = new Node[1];
- n[0] = child.node;
-
- _children.addElement(child);
- if(have_browser)
- _addChildren.setValue(n);
- num_children++;
- }
-
- public void removeChildren(VrmlObject child)
- {
- Node[] n = new Node[1];
- n[0] = child.node;
-
- _children.removeElement(child);
- if(have_browser)
- _addChildren.setValue(n);
- num_children--;
- }
-
- public void set_center(float[] center)
- {
- _center = center;
- if(have_browser)
- _set_center.setValue(center);
- }
-
- public void set_children(VrmlObject[] child_list)
- {
- int i;
- int num = child_list.length;
- Node[] node_list;
-
- // replace the current _children list with the new list.
- // force the garbage collection
- _children = null;
-
- // assign the new arrays.
- _children = new Vector(num);
- node_list = new Node[num];
-
- // copy the values across.
- for(i = 0; i < num; i++)
- {
- _children.addElement(child_list[i]);
- node_list[i] = child_list[i].node;
- }
-
- // set the values in the VRML scene
- if(have_browser)
- _set_children.setValue(node_list);
- }
-
- public void set_rotation(float[] rotation)
- {
- _rotation = rotation;
- if(have_browser)
- _set_center.setValue(rotation);
- }
-
- public void set_scale(float[] scale)
- {
- _scale = scale;
- if(have_browser)
- _set_scale.setValue(scale);
- }
-
- public void set_scaleOrientation(float[] val)
- {
- _scaleOrientation = val;
- if(have_browser)
- _set_scaleOrientation.setValue(val);
- }
-
- public void set_translation(float[] val)
- {
- _translation = val;
- if(have_browser)
- _set_translation.setValue(val);
- }
- }
-