View Full Version : How to get the bounding box' size?
MrSparkle 07-02-2006, 09:44 PM Hi,
I'm looking for a method to get the positions of the 8 corners of an object's bounding box. The $.min and $.max only return the coordinates for two of them. How can I get the positions of the other 6 corners?
Thanks,
Christian
|
|
HalfVector
07-02-2006, 10:10 PM
That's easy. You have all you need with the object min and max coordinates:
fn getBBoxPoints obj = (
local bbPoints = #()
local bbMax = obj.max
local bbMin = obj.min
append bbPoints (Point3 bbMax.x bbMax.y bbMax.z) -- top-right-front corner
append bbPoints (Point3 bbMax.x bbMin.y bbMax.z) -- top-right-back corner
append bbPoints (Point3 bbMin.x bbMin.y bbMax.z) -- top-left-back corner
append bbPoints (Point3 bbMin.x bbMax.y bbMax.z) -- top-left-front corner
append bbPoints (Point3 bbMax.x bbMax.y bbMin.z) -- bottom-right-front corner
append bbPoints (Point3 bbMax.x bbMin.y bbMin.z) -- bottom-right-back corner
append bbPoints (Point3 bbMin.x bbMin.y bbMin.z) -- bottom-left-back corner
append bbPoints (Point3 bbMin.x bbMax.y bbMin.z) -- bottom-left-front corner
return bbPoints
)
Greets.
MrSparkle
07-02-2006, 10:45 PM
Thank you for your posting. Your function works fine for objects with bounding boxes that are aligned to the axes of the current coordinate system. The problem is that somehow I have to consider the orientation of the bounding box...
HalfVector
07-02-2006, 11:56 PM
Thank you for your posting. Your function works fine for objects with bounding boxes that are aligned to the axes of the current coordinate system. The problem is that somehow I have to consider the orientation of the bounding box...
Ok, so what you want is the oriented bounding box of the object.
Here's a simple way to do this. I don't know if it works the 100% of the cases but... :)
-- Calculates the axis-aligned bounding box corners of an object
fn getAABBoxPoints obj = (
local aabbPoints = #()
-- Get minimum and maximum bounding box coordinates
local bbMax = obj.max
local bbMin = obj.min
-- Build the axis-aligned bounding box corners
append aabbPoints (Point3 bbMax.x bbMax.y bbMax.z) -- top-right-front corner
append aabbPoints (Point3 bbMax.x bbMin.y bbMax.z) -- top-right-back corner
append aabbPoints (Point3 bbMin.x bbMin.y bbMax.z) -- top-left-back corner
append aabbPoints (Point3 bbMin.x bbMax.y bbMax.z) -- top-left-front corner
append aabbPoints (Point3 bbMax.x bbMax.y bbMin.z) -- bottom-right-front corner
append aabbPoints (Point3 bbMax.x bbMin.y bbMin.z) -- bottom-right-back corner
append aabbPoints (Point3 bbMin.x bbMin.y bbMin.z) -- bottom-left-back corner
append aabbPoints (Point3 bbMin.x bbMax.y bbMin.z) -- bottom-left-front corner
return aabbPoints
)
-- Calculates the oriented bounding box corners of an object
fn getOBBoxPoints obj = (
local obbPoints = #()
-- World-space transform
local worldSpaceXform = obj.transform
-- World-space to object-space transform
local objectSpaceXform = inverse worldSpaceXform
-- Transform the object from world-space to object-space
obj.transform *= objectSpaceXform
-- Get the object-space axis-aligned bounding box corners
aabbPoints = getAABBoxPoints obj
-- Transform back to world-space
obj.transform = worldSpaceXform
-- Transform the eight corners from object-space to world-space
for pt in aabbPoints do (
append obbPoints (pt * worldSpaceXform)
)
return obbPoints
)
I've only tested this with a cube.
Hope it helps.
MrSparkle
07-03-2006, 01:06 AM
Thank you again for your help! This script works with a cube and also if the cube is rotated in the scene.
But some objects don't have a bounding box that is aligned to the object's axes, so I have to find out the orientation of the bounding box first. But how can I find this out while I only have the center point and the min and max distances?
Moosley
07-03-2006, 12:27 PM
Not sure if it's quite what you're after, but you could always find your own bounding box by finding the point which is furthest from the objects center in each direction in local space...
MrSparkle
07-03-2006, 01:21 PM
I thought about that, too. But wouldn't that be the "bounding sphere" of an object? You have to go though all vertices and then calculate the center point and the point with the greatest distance to the center point, which is the radius of the sphere.
But I'm looking for the 8 corner points of the 3ds max' bounding box, as shown in the attached picture. You can see that the bounding box doesn't have to be aligned to the object's local coordinate system, so I have to find out the orientation of the bounding box first.
CGTalk Moderation
07-03-2006, 01:21 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.