From ac06ef72700662b52462f29ad0daa11f360d93ba Mon Sep 17 00:00:00 2001
From: Anders Nilsson <anders.nilsson@cs.lth.se>
Date: Mon, 27 Aug 2007 11:38:52 +0200
Subject: [PATCH] Added computation of AABB. Cleaned up bounding box
 implementation.

---
 README                 | 28 ++++++++++++++++++++--------
 examples/workpiece.x3d |  2 +-
 x3d/BoundingBox.jrag   | 33 ++++++++++++++++++++++++++++++---
 x3d/OBB.java           | 25 ++++++++++++++++++++++---
 4 files changed, 73 insertions(+), 15 deletions(-)

diff --git a/README b/README
index e0379c3..c11a70c 100644
--- a/README
+++ b/README
@@ -1,7 +1,4 @@
-
-
 ABOUT
-
 The main purpose of the Schemacompiler is to automatically generate
 compiler front-ends for different XML dialects. Given an XML schema,
 the schemacompiler will generate an abstract grammar and some aspect
@@ -11,7 +8,6 @@ possibly new abstract grammar rules, to form a complete compiler.
 
 
 USAGE
-
 $ java GenCompiler [-d dir] [-o outfile] schema
 
 options:
@@ -22,11 +18,27 @@ options:
         -o outfile      Name for generated grammar files.
                         Default is 'x3d'.
 
-'$ java GenCompiler examples/x3d-3.0.xsd' will generate a compiler for
-x3d in the x3d subdirectory. x3d/build.xml should be studied as an
-example on how to use the generated code.
 
+EXAMPLE
 
-AUTHOR
+$ ant
+$ java GenCompiler examples/x3d-3.0.xsd 
+$ cd x3d
+$ ant
 
+Will generate a compiler for x3d in the x3d subdirectory. This
+generated front-end together with some cupplied code is then built to
+form a compiler for x3d models.
+
+NOTE! The supplied code in OBB.java and BoundingBox.jrag depends on
+the jme package from http://www.jmonkeyengine.com/
+
+There are two main programs supplied:
+$ java PrettyPrint ../examples/workpiece.x3d 
+will just print out an x3d representation.
+$ java OBB ../examples/workpiece.x3d 
+will compute bounding boxes (both AABB and OBB) and print out the 
+characteristics.
+
+AUTHOR
 Anders Nilsson <anders.nilsson@cs.lth.se>
\ No newline at end of file
diff --git a/examples/workpiece.x3d b/examples/workpiece.x3d
index 096d725..3ef55e6 100644
--- a/examples/workpiece.x3d
+++ b/examples/workpiece.x3d
@@ -2,7 +2,7 @@
 <!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN"   "http://www.web3d.org/specifications/x3d-3.0.dtd">
 <X3D profile="Full">
   <head>
-    <meta name="filename" content="..\Sample Parts\Head&plate.x3d"/>
+    <meta name="filename" content="..\Sample Parts\Headplate.x3d"/>
     <meta name="description" content="*enter description here, short-sentence summaries preferred*"/>
     <meta name="author" content="*enter name of original author here*"/>
     <meta name="translator" content="*if manually translating VRML-to-X3D, enter name of person translating here*"/>
diff --git a/x3d/BoundingBox.jrag b/x3d/BoundingBox.jrag
index 43740c5..23777ab 100644
--- a/x3d/BoundingBox.jrag
+++ b/x3d/BoundingBox.jrag
@@ -39,11 +39,38 @@ aspect BoundingBox {
         }
         obb.computeFromPoints(BufferUtils.createFloatBuffer(fa));
         obb.computeCorners();
-        System.out.println("Center: "+obb.getCenter());
-        System.out.println("Extent: "+obb.getExtent());
-        System.out.println("Volume: "+obb.getVolume());
         return obb;
     }
+
+    syn BoundingBox ASTNode.aabb() = new BoundingBox();
+    eq Start.aabb() = getSpecification().aabb();
+    eq Specification.aabb() = getElement(0).aabb();
+    eq ComplexElement.aabb() = getNumElement()>0 ? getElement(0).aabb() : null;
+    eq X3D.aabb() {
+        for (int i=0; i<getNumElement(); i++) {
+            ComplexElement e = (ComplexElement) getElement(i);
+            if (e instanceof Scene) {
+                return e.aabb();
+            }
+        }
+        // Fallback, didn't suceed in finding a bounding box
+        return null;
+    }
+    eq Scene.aabb() {
+        BoundingBox aabb = new BoundingBox();
+        StringBuffer sb = getCoordinates();
+        String s = sb.toString().replace('\"',' ').replace(',',' ');
+//         String s = sb.toString().replace('\"',' ').replace(',',' ').replace("  "," ");
+        String[] sa = s.split(" ",0);
+        float[] fa = new float[sa.length];
+        for (int i=0,j=0; i<sa.length; i++) {
+            if (!sa[i].equals("")) {
+                fa[j++] = Float.parseFloat(sa[i]);
+            }
+        }
+        aabb.computeFromPoints(BufferUtils.createFloatBuffer(fa));
+        return aabb;
+    }
 }
 
 aspect Coordinates {
diff --git a/x3d/OBB.java b/x3d/OBB.java
index a497d01..66b6c0f 100644
--- a/x3d/OBB.java
+++ b/x3d/OBB.java
@@ -1,13 +1,32 @@
 import AST.Start;
 import com.jme.bounding.*;
+import com.jme.math.*;
 
 public class OBB extends Parser {
     public static void main(String args[]) {
         Start ast = parse(args);
+
+        Vector3f vec;
         
+        BoundingBox aabb = ast.aabb();
+        System.out.println("AxisAligned BoundingBox");
+        vec = aabb.getCenter();
+        System.out.println("Center: X="+vec.getX()+" Y="+vec.getY()+
+                           " Z="+vec.getZ());
+        vec = aabb.getExtent(vec);
+        System.out.println("Extent: X="+vec.getX()+" Y="+vec.getY()+
+                           " Z="+vec.getZ());
+        System.out.println("Volume: "+aabb.getVolume());
+
         OrientedBoundingBox obb = ast.obb();
-        System.out.println("X axis: "+obb.getXAxis());
-        System.out.println("Y axis: "+obb.getYAxis());
-        System.out.println("Z axis: "+obb.getZAxis());
+        System.out.println("\nOriented BoundingBox");
+        vec = obb.getCenter();
+        System.out.println("Center: X="+vec.getX()+" Y="+vec.getY()+
+                           " Z="+vec.getZ());
+        vec = obb.getExtent();
+        System.out.println("Extent: X="+vec.getX()+" Y="+vec.getY()+
+                           " Z="+vec.getZ());
+        System.out.println("Volume: "+obb.getVolume());
+
     }
 }
-- 
GitLab