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

Object bounding box relative to given reference #11066

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 45 additions & 19 deletions src/math/Box3.js
@@ -1,5 +1,6 @@
import { Vector3 } from './Vector3';
import { Sphere } from './Sphere';
import { Matrix4 } from './Matrix4';

/**
* @author bhouston / http://clara.io
Expand Down Expand Up @@ -123,12 +124,12 @@ Object.assign( Box3.prototype, {

}(),

setFromObject: function ( object ) {

setFromObject: function (object, reference, excludeInvisibleObjects) {
this.makeEmpty();

return this.expandByObject( object );

return this.expandByObject( object, reference, excludeInvisibleObjects );
},

clone: function () {
Expand Down Expand Up @@ -204,36 +205,62 @@ Object.assign( Box3.prototype, {

},

expandByObject: function () {

// Computes the world-axis-aligned bounding box of an object (including its children),
// accounting for both the object's, and children's, world transforms

expandByObject: function (object, ref, excludeInvisibleObjects) {
// Computes the bounding box of an object (including its children) in the given reference object's coordinate system, or world coordinates if no reference is specified,
// accounting for both the object's, and children's, transforms.
// excludeInvisibleObjects specifies whether to use traverseVisible instead of traverse.
var trans;

if (ref !== undefined) {

trans = new Matrix4().getInverse(ref.matrixWorld);

}

this.expandByTransformedObject(object, trans, excludeInvisibleObjects);
},
expandByTransformedObject: function () {

// Computes the bounding box of an object (including its children)in the coordinate system given by trans,
// or world coordinates if no trans is specified,
// accounting for both the object's, and children's, transforms.
// excludeInvisibleObjects specifies whether to use traverseVisible instead of traverse.

var v1 = new Vector3();
var m1 = new Matrix4();

return function expandByObject( object ) {

return function expandByObject( object, trans, excludeInvisibleObjects ) {
var scope = this;

object.updateMatrixWorld( true );

object.traverse( function ( node ) {
object["traverse"+(excludeInvisibleObjects ? "Visible" : "")]( function ( node ) {

var i, l;

if (trans !== undefined) {

m1.multiplyMatrices(trans, node.matrixWorld);

} else {

m1.copy(node.matrixWorld);

}

var geometry = node.geometry;

if ( geometry !== undefined ) {

if ( geometry.isGeometry ) {

var vertices = geometry.vertices;

for ( i = 0, l = vertices.length; i < l; i ++ ) {

v1.copy( vertices[ i ] );
v1.applyMatrix4( node.matrixWorld );

v1.applyMatrix4( m1 );
scope.expandByPoint( v1 );

}
Expand All @@ -246,10 +273,9 @@ Object.assign( Box3.prototype, {

for ( i = 0, l = attribute.count; i < l; i ++ ) {

v1.fromBufferAttribute( attribute, i ).applyMatrix4( node.matrixWorld );

v1.fromBufferAttribute( attribute, i ).applyMatrix4( m1 );
scope.expandByPoint( v1 );

}

}
Expand Down