PDA

View Full Version : building bounding boxes....what is wrong?



pplanza2
01-30-2017, 01:16 PM
Hello,

I'm trying to calculate the bounding boxes of my objects. Four cubes.

If the cube is located at positive coordinates all is OK. If the object is located in a negative coordinate then the bounding box looks like the image below


2337

Topleft cube is ok.

In the other cubes the negatives coordinates of the bounding box are changed to zero...why?

Inside the everyframe render method, this is the code when I arrange the matrix to rotate-translate the bounding box


....
....
float bbMatrix[]=new float[16];
Matrix.setIdentityM(bbMatrix,0);
Matrix.translateM(bbMatrix, 0, mesh.x, mesh.y, mesh.z);

Matrix.rotateM(bbMatrix, 0, mesh.ax, 1, 0, 0);
Matrix.rotateM(bbMatrix, 0, mesh.ay, 0, 1, 0);
Matrix.rotateM(bbMatrix, 0, mesh.az, 0, 0, 1);

Matrix.translateM(bbMatrix, 0, 0, 0, 0);

mesh.getBoundingBox().transformMinsAndMax(bbMatrix );
....
....



I pass the matrix to this method. The points of bounding box are rotated and the minimum and maximum coordinate values are calculated:


public void transformMinsAndMax(float[] vMatrixArray) {

float pointTransformed[]=new float[4];

Log.d(TAG,"yMin="+ymin+", yMax="+ymax);

for(int i=0;i<p.length;i++) {
Matrix.multiplyMV(pointTransformed, 0, vMatrixArray, 0, p[i].asFloat(), 0);
pc[i]=new Point(pointTransformed[0],pointTransformed[1],pointTransformed[2],1,1,0);
}


xmin=Float.MAX_VALUE;ymin=Float.MAX_VALUE;zmin=Flo at.MAX_VALUE;
xmax=Float.MIN_VALUE;ymax=Float.MIN_VALUE;zmax=Flo at.MIN_VALUE;

for(int i=0;i<p.length;i++) {
//x
if(pc[i].x<xmin){
xmin=pc[i].x;
}

if(pc[i].x>xmax){
xmax=pc[i].x;
}
...
...


p array contains the original points of the bounding box. The points are calculated one time when the cube is loaded.
p[i].asFloat return an array with the point coordinates (x,y,z,1)
pc array contains the bounding box points.

What is wrong? Can you help me please?

Thanks.

Carmine
01-30-2017, 03:46 PM
Perhaps the way you initialize xmin and xmax is the problem. Try something like below.


xmin = pc[0].x;
xmax = pc[0].x;

for (i = 1; i < p.length; i++) {
if (pc[i].x < xmin) xmin = pc[i].x;
if (pc[i].x > xmax) xmax = pc[i].x;
}

Silence
01-31-2017, 05:49 AM
Perhaps the way you initialize xmin and xmax is the problem.

Should not, according to the java (https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html) doc.

Spoops
01-31-2017, 09:02 AM
Should not, according to the java (https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html) doc.

No, Float.MIN_VALUE is the smallest number > 0. It should be -Float.MAX_VALUE or Float.NEGATIVE_INFINITY (used with Float.POSITIVE_INFINITY).

However Carmine's solution looks prettier (but you have to make sure pc has at least one element).

pplanza2
01-31-2017, 02:39 PM
Perhaps the way you initialize xmin and xmax is the problem. Try something like below.


xmin = pc[0].x;
xmax = pc[0].x;

for (i = 1; i < p.length; i++) {
if (pc[i].x < xmin) xmin = pc[i].x;
if (pc[i].x > xmax) xmax = pc[i].x;
}


Thanks Carmine.

Nothing changed....same results