Three.js: when to call geometry. computeBoundingBox() ?

Created on 23 Jan 2012  路  14Comments  路  Source: mrdoob/three.js

Is there a rule on when to call geometry. computeBoundingBox()and other compute function ?

Question

Most helpful comment

I know it's an old topic but the following comment by @alteredq really ought to be in the manual if it is still valid for the current version:

Bounding box is not used internally for anything, it's there in case you need it somewhere at the application level. It's not computed at all anywhere automatically, you need to compute it yourself.

Bounding sphere is used for frustum culling and with Ray / Projector. It's computed automatically just on creation of objects. You need to recompute it manually if you change vertices (change positions, add / remove vertices)

All 14 comments

the question im asking myself are:

  • Should i always assume it is invalid ?

    • and so recompute it everytime before using it ? maybe costy

    • so no need to recompute it after modifying the geometry vertices

  • Should i always assume it is valid ?

    • no recomputation before using it

    • but recomputation after modifying geometry vertices ?

If it is always valid in your use cases (out of the box it should be) then just recompute both bounding box and bounding sphere when you modify vertice locations.

not sure i understand the "if" here... i look for a rule at three.js level.

either it is assumed to be always correct (so recompute only when you modify) xor never correct (so recompute before every usage) ?

or there are no assumption, then i have to recompute them before and after, just in case somebody else assume the opposite as me. :)

ps: currently im doing the last version. just trying to see if i can optimize

The if was just incase you are using any outside code which may not be updating the values. To my knowledge all of three's code updates the values whenever they need to be.

cool thanks

For the record, bounbindBox may be null, if the number of vertices is 0 from the constructor.
https://github.com/mrdoob/three.js/blob/master/src/core/Geometry.js

this.boundingBox = null;
this.boundingSphere = null;

but in computeboundbox, https://github.com/mrdoob/three.js/blob/master/src/core/Geometry.js#L342 , it isn't reseted to null if vertices.length === 0

so if somebody removes all the vertices, this.boundingBox would be invalid as it would have the values of hte previous vertices.

should i fill another issue for that ?

Bounding box is not used internally for anything, it's there in case you need it somewhere at the application level. It's not computed at all anywhere automatically, you need to compute it yourself.

Bounding sphere is used for frustum culling and with Ray / Projector. It's computed automatically just on creation of objects. You need to recompute it manually if you change vertices (change positions, add / remove vertices).

thanks for the clarification @alteredq

What about the inconsistency in .computeBoundingBox() ? should we add the following code ? it would make it consistent with the constructor (it assume if no vertices, then bounbingBox === null)

    if ( this.vertices.length === 0 ) {
        this.boundingBox = null;
    }

ps: computeBoundingSphere() is inconsistent too. but in a slightly different way.

If there are no vertices than it doesn't matter what bounding box / sphere are.

Though if for some reason you want to invalidate previous bounding box / sphere, I guess setting values to zeroes would be preferable to nulling the objects, as then if geometry would be reused again no new objects would need to be created.

sorry im unclear

var g= new THREE.CubeGeometry(2,2,2);
g.vertices = [];
console.log(JSON.stringify(g.boundingBox)); 
// output null with vertices  == []
g.computeBoundingBox();
console.log(JSON.stringify(g.boundingBox));
// output {"min":{"x":-1,"y":-1,"z":-1},"max":{"x":1,"y":1,"z":1}}
g.vertices = [];
g.computeBoundingBox();
console.log(JSON.stringify(g.boundingBox)); 
// output {"min":{"x":-1,"y":-1,"z":-1},"max":{"x":1,"y":1,"z":1}} with vertices == []

// here g.boundingBox seems to be invalid

adding the following code at the end of Geometry.computeBoundingBox() may fix this

}else{
        // if this.vertices.length === 0
        this.boundingBox = null;
}

I meant this would be probably better:

if ( ! this.boundingBox ) {

    this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };

}

if ( this.vertices.length > 0 ) {

    var position, firstPosition = this.vertices[ 0 ].position;

    this.boundingBox.min.copy( firstPosition );
    this.boundingBox.max.copy( firstPosition );

    // ...

} else {

    this.boundingBox.min.set( 0, 0, 0 );
    this.boundingBox.max.set( 0, 0, 0 );

}

seems good to me.

how do we do that ? you do the modif ? i do and i pull-request ?

thanks :)

I know it's an old topic but the following comment by @alteredq really ought to be in the manual if it is still valid for the current version:

Bounding box is not used internally for anything, it's there in case you need it somewhere at the application level. It's not computed at all anywhere automatically, you need to compute it yourself.

Bounding sphere is used for frustum culling and with Ray / Projector. It's computed automatically just on creation of objects. You need to recompute it manually if you change vertices (change positions, add / remove vertices)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

fuzihaofzh picture fuzihaofzh  路  3Comments

boyravikumar picture boyravikumar  路  3Comments

zsitro picture zsitro  路  3Comments

jens-duttke picture jens-duttke  路  3Comments