View Full Version : Bounding Box w/ Coffee

08 August 2006, 12:03 PM
Is there a faster way to get the boundingbox of a polyobject then evaluating all points individually? Here's the code I wrote, but it is way too slow on large scenes:

var ptCount = obj->GetPointCount();
var ptArray = (obj->GetPoints());
var maxPoint = ptArray[0];

var i;
for (i = 1; i < ptCount; i++)
if (ptArray[i].x > maxPoint.x) {
maxPoint.x = ptArray[i].x; }
if (ptArray[i].y > maxPoint.y) {
maxPoint.y = ptArray[i].y; }
if (ptArray[i].z > maxPoint.z) {
maxPoint.z = ptArray[i].z; }
return maxPoint;


08 August 2006, 12:46 PM
uh, nevermind. Somewhere in my other code I compared two vectors for equality, but due to rounding errors they would never be the same! So now I changed the code to accomodate for a small percentage error, et voilą: it works fine.

08 August 2006, 05:15 AM
Not even rounding errors - floating point values need to be compared for equality on a significance basis - using an epsilon. The C++ SDK has a VectorEqual() method that compares with an epsilon value. COFFEE doesn't, but the compare is rather simple:

var VectorEqual(var v1, var v2, var epsilon)
// Not Equivalent = FALSE
// Equivalent = TRUE
return (Abs(v1.x-v2.x)<epsilon && Abs(v1.y-v2.y)<epsilon && Abs(v1.z-v2.z)<epsilon);

A good value for epsilon depends on the circumstances, but usually something like 0.001 (for instance) is a good epsilon value.

08 August 2006, 08:10 AM
Thanks Robert for that! I think your way of testing is faster than mine. I used vlen() to do the percentage error thing. But that probably is more expensive and less accurate.

BTW, there's an error in my code above, I'll fix it right now.

CGTalk Moderation
08 August 2006, 08:10 AM
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.