Page 1 of 1

BoundingBox

Posted: Sun 13. Mar 2011, 03:59
by gpo
The BoundingBox computations are off sometimes; e.g. I have a thin rectangle 600 x 300 x (z=depth<100) and it gives me a depth in the thousands, as in the following

Rectangle3D rc=BoundingBoxUtility.calculateBoundingBox(sgc);
double[] c=rc.getCenter();
double[] rd=rc.getExtent();
// System.out.println(rc.toString());
Rectangle3D rcL=BoundingBoxUtility.calculateBoundingBox(sgcL);
double[] cL=rcL.getCenter();
double[] rdL=rcL.getExtent();
// System.out.println("rdL "+rcL.toString());
rc=BoundingBoxUtility.calculateBoundingBox(sgc);
c=rc.getCenter();
rd=rc.getExtent();

This also compounds with the failure to move an object to another e.g,

MatrixBuilder.euclidean().scale(30, 30, 11).translate(cL[0]-c[0], cL[1]-c[1], cL[2]-c[2]).assignTo(sgcL);


This fails to bring sgcL to sgc, at least as visually I can inspect.


gpo

Re: BoundingBox

Posted: Tue 15. Mar 2011, 21:08
by gunn
There's not enough information in your post to really debug anything. If you wish to have someone look into this problem, please post a Java class with a main() method which, when it runs, demonstrates clearly the problem you are having.

Re: BoundingBox

Posted: Thu 17. Mar 2011, 12:26
by gpo
I'm probably getting confused working over the same thing again and again
but I would appreciate any light shed into this calculation. See the program below:

- It needs also the Mirror-2.3ds file - can't seem to get through the attachment so please use another object ..

Look at "transform 1" and "transform 2" in the while(true) loop. "transform 1" works fine it moves the object (sphere) away. Shouldn't "transform 2" move the sphere toward the center of the mirror, after 5 or so steps it should reach there and then pierce through and move away

Why not?? I don't see it.


gpo

import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.JRootPane;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import de.jreality.geometry.*;
import de.jreality.plugin.*;
import de.jreality.scene.*;
import de.jreality.ui.viewerapp.*;
import static de.jreality.shader.CommonAttributes.VERTEX_DRAW;
import static de.jreality.shader.CommonAttributes.DIFFUSE_COLOR;
import static de.jreality.shader.CommonAttributes.LINE_SHADER;
import static de.jreality.shader.CommonAttributes.POINT_SHADER;
import static de.jreality.shader.CommonAttributes.POLYGON_SHADER;
import de.jreality.scene.data.*;
import de.jreality.shader.*;
import de.jreality.util.*;
import de.jreality.math.*;
import de.jreality.plugin.JRViewer.*;
import de.jreality.plugin.basic.View;
import de.jreality.plugin.content.*;
import de.jreality.ui.widgets.TextSlider;
import de.jtem.jrworkspace.plugin.Plugin;
import de.jtem.jrworkspace.plugin.PluginInfo;
import de.jtem.jrworkspace.plugin.sidecontainer.SideContainerPerspective;
import de.jtem.jrworkspace.plugin.sidecontainer.template.ShrinkPanelPlugin;
import de.jtem.jrworkspace.plugin.simplecontroller.SimpleController;
import de.jreality.shader.CommonAttributes;
import de.jreality.tools.*;


import de.jreality.math.MatrixBuilder;
import de.jreality.scene.IndexedFaceSet;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.shader.Texture2D;
import de.jreality.shader.TextureUtility;
import de.jreality.util.SceneGraphUtility;
import de.jreality.geometry.*;
import de.jreality.plugin.*;
import de.jreality.scene.*;
import de.jreality.util.*;
import de.jreality.scene.data.*;
import de.jreality.shader.CommonAttributes;
import de.jreality.geometry.ParametricSurfaceFactory;
import de.jreality.geometry.ParametricSurfaceFactory.Immersion;
import de.jreality.reader.*;
import de.jreality.shader.*;


public class Read3DS2 {

long time=System.currentTimeMillis(), time2;

int rX, rY, rZ, tX, tY, tZ;
Color goffc;

Viewer v;
SceneGraphComponent sgc = new SceneGraphComponent(""), sgc1=new SceneGraphComponent("");

int i=0;

public Read3DS2() {

}


public void mirror() {
Reader3DS r=new Reader3DS();
SceneGraphComponent sgc2=null;
try {
String f="Mirror-2.3DS";
sgc=r.read(new File(f));
// sgc2=new Reader3DS().read(new File(f));
System.out.println(f);
}
catch(IOException e) { e.printStackTrace(); }
Appearance app= new Appearance();
Color cln=getColor();
app.setAttribute(CommonAttributes.DIFFUSE_COLOR, cln);
app.setAttribute(CommonAttributes.SPECULAR_COLOR, Color.white);
// app.setAttribute(CommonAttributes.ADDITIVE_BLENDING_ENABLED, true);
app.setAttribute(CommonAttributes.EDGE_DRAW, false);
app.setAttribute(CommonAttributes.VERTEX_DRAW, false);
sgc.setAppearance(app);
Geometry geom=sgc.getGeometry();
Geometry geomf=SceneGraphUtility.getFirstGeometry(sgc);
java.util.List l=(java.util.List)sgc.getChildComponents();
for(int i=0; i<l.size(); i++) {
SceneGraphComponent ch=sgc.getChildComponent(i);
Appearance chapp= new Appearance();
chapp.setAttribute(CommonAttributes.DIFFUSE_COLOR, getColor());
chapp.setAttribute(CommonAttributes.SPECULAR_COLOR, Color.white);
chapp.setAttribute(CommonAttributes.EDGE_DRAW, false);
chapp.setAttribute(CommonAttributes.VERTEX_DRAW, false);
if(i==0) {
Appearance appch=ch.getAppearance();
System.out.println("d "+appch.getAttribute("polygonShader.diffuseColor").toString());
System.out.println("s "+appch.getAttribute("polygonShader.specularColor").toString());
System.out.println("a "+appch.getAttribute("polygonShader.ambientColor").toString());
Iterator itst=ch.getAppearance().getStoredAttributes().iterator();
while(itst.hasNext()) {
String attr=itst.next().toString();
System.out.println(" "+attr);
}
}
if(i>0) ch.setAppearance(chapp);
else {
chapp.setAttribute(CommonAttributes.DIFFUSE_COLOR, Color.white);
chapp.setAttribute(CommonAttributes.RMAN_RAY_TRACING_REFLECTIONS, true);
chapp.setAttribute(CommonAttributes.RMAN_LIGHT_SHADER+"."+CommonAttributes.RMAN_RAY_TRACING_REFLECTIONS, true);
chapp.setAttribute(CommonAttributes.RMAN_SURFACE_SHADER+"."+CommonAttributes.RMAN_RAY_TRACING_REFLECTIONS, true);
// chapp.setAttribute(CommonAttributes.RMAN_SURFACE_SHADER+"."+CommonAttributes.SPECULAR_EXPONENT, 11);
chapp.setAttribute(CommonAttributes.POLYGON_SHADER+"."+CommonAttributes.SPECULAR_EXPONENT, 22.1);
chapp.setAttribute(CommonAttributes.POLYGON_SHADER+"."+CommonAttributes.SPECULAR_COEFFICIENT, 1.0);
chapp.setAttribute(CommonAttributes.POLYGON_SHADER+"."+CommonAttributes.DIFFUSE_COEFFICIENT, 0.0);
ch.setAppearance(chapp);
}
Geometry gch=SceneGraphUtility.getFirstGeometry(ch);
// System.out.print(i+" "+ch.getAppearance().toString()+" ");
}
Appearance app2=new Appearance();
cln=getColor();
app2.setAttribute(CommonAttributes.DIFFUSE_COLOR, Color.red);
app2.setAttribute(CommonAttributes.SPECULAR_COLOR, Color.white);
app2.setAttribute(CommonAttributes.EDGE_DRAW, false);
app2.setAttribute(CommonAttributes.VERTEX_DRAW, false);
// sgc2.setAppearance(app2);
// sgc1.addChild(sgc2);
SceneGraphComponent sgcL=Primitives.sphere(4, 100, 0, 10);
sgcL.getAppearance().setAttribute(CommonAttributes.DIFFUSE_COLOR, Color.cyan);
Light lt=sgc1.getLight();
// Light lt2=viewer.getLight();
// lt=new DirectionalLight();
lt=new PointLight();
lt.setGlobal(true);
lt.setColor(Color.magenta);
lt.setIntensity(10);
sgcL.setLight(lt);
// MatrixBuilder.euclidean().rotateY(Math.toRadians(10)).assignTo(sgcL);
IndexedFaceSet box= Primitives.box(1, 2, 1, true);
SceneGraphComponent sgc3=new SceneGraphComponent(); //=Primitives.sphere(4, 400, 0, 2);
sgc3.setGeometry(box);
Appearance app3=new Appearance();
// app3.setAttribute(CommonAttributes.DIFFUSE_COLOR, Color.yellow);
// sgc3.setAppearance(app3);
sgc1.addChild(sgc);
sgc1.addChild(sgcL);
// sgc1.addChild(sgc3);
if(v==null) { v=JRViewer.display(sgc1); }
// createSkybox(v);
// SceneGraphUtility.removeLights(v);
// sgcL.getAppearance().setAttribute(CommonAttributes.LIGHTING_ENABLED, false);
// sgc1.setLight(lt);
// sgc1.addChild(createBackground(sgc));
Transformation tf=new Transformation();
sgc.setTransformation(tf);
de.jreality.math.Matrix m=new de.jreality.math.Matrix();
double a=5;
time=System.currentTimeMillis();
// toFront();
MatrixBuilder.euclidean().translate(500, 0, -1).rotateX(Math.toRadians(0+rX)).rotateY(Math.toRadians(200+rY)).rotateZ(Math.toRadians(80+rZ)).getMatrix().assignTo(sgc);
Rectangle3D rc=BoundingBoxUtility.calculateBoundingBox(sgc);
double[] c=rc.getCenter();
double[] rd=rc.getExtent();
System.out.println(rc.toString());
Rectangle3D rcL=BoundingBoxUtility.calculateBoundingBox(sgcL);
double[] cL=rcL.getCenter();
double[] rdL=rcL.getExtent();
System.out.println("rdL "+rcL.toString());
Camera cm=CameraUtility.getCamera(v);
double cf=cm.getFieldOfView();
double dx=(cL[0]-c[0])/5, dy=(cL[1]-c[1])/5, dz=(cL[2]-c[2])/5;
System.out.println("d "+dx+" "+dy+" "+dz);
v.render();
try { Thread.sleep(1000); } catch(InterruptedException ie) {}
while(true) {
MatrixBuilder.euclidean().translate(500, 0, -1).rotateX(Math.toRadians(0+rX)).rotateY(Math.toRadians(200+rY)).rotateZ(Math.toRadians(80+rZ)).getMatrix().assignTo(m);
tf.setMatrix(m.getArray());
rc=BoundingBoxUtility.calculateBoundingBox(sgc);
c=rc.getCenter();
rd=rc.getExtent();
MatrixBuilder.euclidean().scale(60, 60, 60).translate(a/5, a/5, -a).assignTo(sgcL); // transform 1
System.out.println("d "+(-dx*a/5)+" "+(-dy*a/5)+" "+(-dz*a/5));
// MatrixBuilder.euclidean().scale(60, 60, 60).translate(-dx*a/5, -dy*a/5, -dz*a/5).assignTo(sgcL); // // transform 2
// System.out.println("rdL "+rcL.toString()+" diffs are "+(cL[0]-c[0])+" "+(cL[1]-c[1])+" "+(cL[2]-c[2]));
try { Thread.sleep(1000); } catch(InterruptedException ie) {}
a+=5;
cf+=1.5;
// cm.setFieldOfView(cf);
if(System.currentTimeMillis()-time>30000) break;
}
}

public Color getColor() {
return new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255));
}

public void test() {
mirror();
try { Thread.sleep(5000); } catch(InterruptedException ie) {}
}


public static void main(String args[]) {
Read3DS2 r=new Read3DS2();
r.test();
System.exit(0);
}

}

Re: BoundingBox

Posted: Thu 17. Mar 2011, 13:56
by gpo
Ok I figured it out. I have to specify a very small step of movement to see it otherwise it would fly off the screen before you can see it

double dx=(cL[0]-c[0])/1000, dy=(cL[1]-c[1])/1000, dz=(cL[2]-c[2])/1000;

I still can't much explain the numbers given as min/max etc on the bounding boxes but by math formulation (above step) it works. Nobody knows if the bounding box will create further trouble later on.


gpo

Re: BoundingBox

Posted: Thu 17. Mar 2011, 14:14
by gunn
Glad you got it to work.

Perhaps the bounding box calculations appear wrong because you are expecting something different. The calculateBoundingBox() methods return a bounding box in the coordinate system of the root of the scene graph which you pass into the method. So, if you apply a translation below this root node, which moves the object away from the root of the scene graph, then the bounding box will move away too. Also rotating a box shape (like your mirror) around will increase its bounding box entries since one needs a bigger box to encompass a box which is not aligned on the xyz coordinate axes.