Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THREE.Box3.setFromObject( object ) & THREE.BoundingBoxHelper( object ); #3471

Closed
WestLangley opened this issue May 13, 2013 · 8 comments
Closed

Comments

@WestLangley
Copy link
Collaborator

This is a proposal for a new Box3 method Box3.setFromObject( object ) that computes the world-axis-aligned bounding box of an object (including its children), accounting for both the object's, and childrens', world transforms.

This is different from Geometry.computeBoundingBox() which is not hierarchical, and does not take transforms into consideration.

THREE.Box3.prototype.setFromObject = ( function() {
    var v1 = new THREE.Vector3();
    return function( object ) {
        var scope = this;
        object.updateMatrixWorld( true );
        this.makeEmpty();
        object.traverse( function ( node ) {
            if ( node.geometry !== undefined && node.geometry.vertices !== undefined ) {
                var vertices = node.geometry.vertices;
                for ( var i = 0, il = vertices.length; i < il; i++ ) {
                    v1.copy( vertices[ i ] );
                    v1.applyMatrix4( node.matrixWorld );
                    scope.expandByPoint( v1 );
                }
            }
        } );
        return this;
    };
}());

In addition, I propose a BoundingBoxHelper ( different from the existing BoxHelper as it is axis-aligned ) to show the world-axis-aligned bounding box for an object. This helper calls Box3.setFromObject( object ).

bbHelper = new THREE.BoundingBoxHelper( object, 0xffffff );
scene.add( bbHelper );

The advantages of these features should be pretty obvious. The disadvantage is that computation of the AABB of an object and it's children requires processing each vertex individually -- there are no transforms that can be applied to just the geometry.boundingBox to get the correct result.

Feedback as to usefulness, naming, etc. is welcome. Maybe Object3D.computeBoundingBox() is better.

Fiddle: http://jsfiddle.net/MREL4/

@mrdoob
Copy link
Owner

mrdoob commented May 13, 2013

Yes yes yes!

Uhm, somehow Object3D.computeBoundingBox() sounds better to me. Maybe Object3D could also have both boundingSphere and boundingBox initialised as null.

@gaitat
Copy link

gaitat commented May 13, 2013

Also prefer Object3D.computeBoundingBox()

Fixing Issue #3370 or something must be added to the collada loader in regards to the bounding box ?

After having looked at the fiddle, shouldn't the update() be happening automatically? It is part of the scene so an additional call shouldn't be required.

@WestLangley
Copy link
Collaborator Author

In the PR, I did create Object3D.computeBoundingBox(), and added a boundingBox property to the Object3D class.

I'm thinking that perhaps my original idea was better -- not having an Object3D.boundingBox property, and instead providing Box3.setFromObject( object ).

The reason is that an object's bounding box becomes invalid after every transformation, and we can't afford to recompute it each time -- like we do for geometries in Geometry.applyMatrix().

Hmmm....

@mrdoob
Copy link
Owner

mrdoob commented May 27, 2013

Yup, I see what the issue... Your original idea sounds best to me now.

@WestLangley
Copy link
Collaborator Author

Yup, I see what the issue... Your original idea sounds best to me now.

OK. Backed out and reimplemented.

@potomek
Copy link

potomek commented Aug 1, 2013

Can the bounding box functions compute also actual appearance of geometries, I mean morph targets interpolations which offset vertices ? :-)

@WestLangley
Copy link
Collaborator Author

@potomek No, the bounding box functions do not take into account morph targets.

@potomek
Copy link

potomek commented Aug 1, 2013

So I will add that but not yet, because I'm still learning of three.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants