question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need `Box3.setFromObject` to return an AABB in local space of specified object.

See original GitHub issue

When I use Box3.setFromObject to compute the bounding box of the an Object3D, I was expecting it to return the AABB in local coordinates aligned to the local frame within the specified object, but instead it returns an AABB in world coordinates. I was wondering if would be good to add an option to this function to allow either local or world AABB to be returned?

I made a wrapper function that implements this option by temporarily setting the local matrix to the inverse of the worldMatrix of its parent object, but it seems like a messy and inefficient solution.

	public getBounds(coords: CoordinateSystem): Box3 {
		this._object3D.updateMatrix();
		let oldMatrix = this._object3D.matrix;
		let oldMatrixAutoUpdate = this._object3D.matrixAutoUpdate;

		if (coords === CoordinateSystem.LOCAL) {
			this.updateMatrixWorld();			
			let m = new Matrix4();
			if (this.getParent() !== null) 
			{
				m.getInverse(this._object3D.parent.matrixWorld, false);
			}
			this._object3D.matrix = m;
			// to prevent matrix being reassigned 
			this._object3D.matrixAutoUpdate = false; 
			this.updateMatrixWorld();
		}

		var bounds = new Box3();
		bounds.setFromObject(this._object3D);

		if (coords === CoordinateSystem.LOCAL) {
			this._object3D.matrix = oldMatrix;
			this._object3D.matrixAutoUpdate = oldMatrixAutoUpdate;
			this.updateMatrixWorld();
		}

		return bounds;
	}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
Mugen87commented, Aug 31, 2017

Some time ago i’ve planned to develop an Oriented Bounding Box class for three.js. Most algorithms calculate in the first step the convex hull of the considered geometry. The good thing is that we already have a QuickHull implementation that calculates the convex hull with good time complexity (average-case O(nlogn)).

In the next step you can use a method that is called rotating calipers to compute the oriented minimum bounding box for the given convex hull. Once the OBB is computed, you can use the SAT algorithm for fast collision detection. This would be a proper solution for even more complex requirements.

The API could look like this:

var convexHull = new THREE.QuickHull().setFromObject( mesh ); // already possible
var obb = new THREE.OBB().setFromConvexHull( convexHull ); // new class

// methods

obb.intersectsBox( box ); // OBB-AABB intersection test based on SAT
obb.intersectsOBB( obb2 ); // OBB-OBB (also SAT)
obb.intersectsSphere( sphere ); // OBB-Bounding Sphere via closest point 

Although there are more efficient algorithms for OBB creation (like this one), the mentioned technique should be feasible to implement. And yeah, i know three.js is not a library for computational geometry but it’s always handy to have these algorithms around 😇 . At least in the examples directory.

If somebody wants to try an OBB implementation, i would definitely support this 😊 .

5reactions
ChrisDenhamcommented, Aug 16, 2017

Yes, that’s correct. In effect, what I want is to be able to get the bounding box of a subtree of my hierarchy. i.e. the bounding box of all the contained geometry aligned within the coordinate space of the root of the subtree. Perhaps I’m thinking about this wrong, but it seemed like something a lot of people might want, for example to show a box around the bounds of the subtree, where the box is aligned to the orientation of the selected node.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setFromObject to align with the object, not the world - Questions
Need `Box3.setFromObject` to return an AABB in local space of specified object. ... When I use Box3.setFromObject to compute the bounding box of ......
Read more >
three.js box3 of a imported sphere - javascript - Stack Overflow
var box = new THREE.Box3().setFromObject(obj);. With this i can compute boxes for my objects and i can merge them together if i want....
Read more >
Bounding volume collision detection with THREE.js
This article shows how to implement collision detection between bounding boxes and spheres using the Three.js library.
Read more >
Three.Js Obb Implementation - ADocLib
getCenter target is now required' ; target new Vector3;. } return ... box new Need Box3.setFromObject to return an AABB in local space...
Read more >
Bounding volume - Wikipedia
In computer graphics and computational geometry, a bounding volume for a set of objects is a closed volume that completely contains the union...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found