Skip to content
Snippets Groups Projects
Commit a084ba5b authored by Anders Nilsson's avatar Anders Nilsson
Browse files

Can compute OBB for simple x3d example. No idea if it's correct though.

parent 197b3473
No related branches found
No related tags found
No related merge requests found
/* -*-Java-*- */
/*
* Copyright (C) 2007 Anders Nilsson <anders.nilsson@cs.lth.se>
*
* This file is part of XmlSchemaCompiler.
*/
import com.jme.bounding.*;
import com.jme.util.geom.BufferUtils;
aspect BoundingBox {
syn OrientedBoundingBox ASTNode.obb() = new OrientedBoundingBox();
eq Start.obb() = getSpecification().obb();
eq Specification.obb() = getElement(0).obb();
eq ComplexElement.obb() = getNumElement()>0 ? getElement(0).obb() : null;
eq X3D.obb() {
for (int i=0; i<getNumElement(); i++) {
ComplexElement e = (ComplexElement) getElement(i);
if (e instanceof Scene) {
return e.obb();
}
}
// Fallback, didn't suceed in finding a bounding box
return null;
}
eq Scene.obb() {
OrientedBoundingBox obb = new OrientedBoundingBox();
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]);
}
}
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;
}
}
aspect Coordinates {
syn StringBuffer Element.getCoordinates() = new StringBuffer();
eq ComplexElement.getCoordinates() {
StringBuffer sb = new StringBuffer();
for (int i=0; i<getNumElement(); i++) {
sb.append(getElement(i).getCoordinates());
}
return sb;
}
eq Coordinate.getCoordinates() {
StringBuffer sb = new StringBuffer();
for (int i=0; i<getNumAttribute(); i++) {
sb.append(getAttribute(i).getCoordinates());
}
return sb;
}
syn String Attribute.getCoordinates() = null;
eq point.getCoordinates() = getAttrValue().getSTRING_LITERAL();
}
\ No newline at end of file
import AST.Start;
import com.jme.bounding.*;
public class OBB extends Parser {
public static void main(String args[]) {
Start ast = parse(args);
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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment