Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » minimum area bounding box

This thread is locked; no one can reply to it. rss feed Print
minimum area bounding box
Steve Terry
Member #1,989
March 2002
avatar

Anyone know of a good C/C++ algorithm for calculating the minimum area bounding box for any type of solid? For example if you have a solid shape made of any number of vertices how would you calculate a rectangle around the shape with a minimum amount of area? I can get the min/max of all the vertices but it only gives you rough box, not rotated in any way such that it touches the maximum amount of vertices and minimizes the extra area.

[edit]
I'll post a pic as soon as I figure out where my upload link went...
[/edit]

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

bamccaig
Member #7,536
July 2006
avatar

:-/ I'm extremely new to collision detection. In fact the only way I know how to do collision detection is tile-based or hard-coded. Anyway, I'm a little confused as to why you would want a rectangular bounding-box to be rotated on any angle...

???

If your detecting it based on a rectangle wouldn't you just use a flush rectangle? In other words, the maximum and minimum x's and y's...? :-/

Steve Terry
Member #1,989
March 2002
avatar

Here is what I want:
{"name":"img","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/a\/fa0f69e588a33c9f536dadb352d16b0d.png","w":292,"h":269,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/a\/fa0f69e588a33c9f536dadb352d16b0d"}img

This shows two methods of bounding boxes on an odd shaped object. There are many reasons to do the minimum area, one is collision detection like you mentioned. The box is not precalculated so I will have to calculate it on the fly. I don't want bounding spheres or bounding elipses, but boxes.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

bamccaig
Member #7,536
July 2006
avatar

Steve Terry said:

The box is not precalculated so I will have to calculate it on the fly.

Wouldn't it be more efficient to calculate only when the shape changes and store the bounding box? ???

:-/ Sorry, Steve, this doesn't help you much.

Steve Terry
Member #1,989
March 2002
avatar

I would like to know the algorithm before I can calculate it :P

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Kitty Cat
Member #2,815
October 2002
avatar

Is there even a valid way to do that? I'd imagine that any method would be hugely intensive since it'd need to check the various angles of boxes and determine which produces the "smallest" box. I'd either go with AABB (Axis-Aligned Bounding Box, as in your first example), or build the bounding box with your shape in an upright position, and just rotate it with the object.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Goalie Ca
Member #2,579
July 2002
avatar

A single equation.. no!

But i can see how to do an iterative solution. Start out with an approximation (such as standard rectangle). Calculate the energy function.. in this case the area. Now you want to find a function that minimizes the area. Now it becomes a standard optimization problem. Might take some work to do a gradient descent but its probably a correct approach. Maybe define area( shape, boxAngle ).

sorry if that's a little obtuse.. but when dealing with hammers soo much everything looks like a nail.

Computationally that is not very efficient. You should probably pre-calculate it!

-------------
Bah weep granah weep nini bong!

bamccaig
Member #7,536
July 2006
avatar

Watch Numb3rs on Friday nights (or record it for later) and hope they eventually tell you the solution!

Is a bounding box even the best solution for collision detection? I would assume when you get into weird shapes there must be a better, more complex way...

???

Steve Terry
Member #1,989
March 2002
avatar

I'm not looking for speed here and if it helps any I do have the axis angle calculated so I know the angle the box will be at, just don't know how to shrink it down.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

bamccaig
Member #7,536
July 2006
avatar

Steve Terry said:

I'm not looking for speed here and if it helps any I do have the axis angle calculated so I know the angle the box will be at, just don't know how to shrink it down.

The only way I can think of is guesses... Make a guess and check if you are too far. If you are, move back by half the distance you moved last time and check again. If you're not, move forward again.

???

Besides, how can you know what angle the box will be at for an oddly shaped object?

SiegeLord
Member #7,827
October 2006
avatar

If you are looking for an algorithm that will do that from scratch, google gives this as the first result:

link

Otherwise, if you know the axis of the box, what's preventing you from rotating the set of points in such a way as to make this axis be, say, vertical and then simply using the standard min/max algorithm to find the box? When you obtain the box you can rotate the box corners back into the original orientation as needed.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Rampage
Member #3,035
December 2002
avatar

I don't really know what I'm talking about, but it seems to me that trying to find such a rectangle by approximations would take a lot of time, which I don't think is too god for any kind of game...

What I can think of doing is: take the standard rectangle bounding box of your example and divide it in smaller rectangles, maybe 32x32. Generate a pixel perfect collision detection mask for each of the rectangles, discard those that are all 0. This might take more time than a mathematical approximation, I don't know, but you would get a pixel perfect mask of the shape.

-R

Goalie Ca
Member #2,579
July 2002
avatar

If you say you know the angle then that's an easy one. Take a side, say the top, and move it in until that line intersects an edge. The check another side..

-------------
Bah weep granah weep nini bong!

bamccaig
Member #7,536
July 2006
avatar

SiegeLord said:

Otherwise, if you know the axis of the box, what's preventing you from rotating the set of points in such a way as to make this axis be, say, vertical and then simply using the standard min/max algorithm to find the box? When you obtain the box you can rotate the box corners back into the original orientation as needed.

That sounds like a great idea. Yes, I think I'll take credit for that one. ;)8-)

CGamesPlay
Member #2,559
July 2002
avatar

Seems like you should just calculate the slope of all of the pairs of vertices and create lines using them. Discard any lines that intersect with any other edges on the shape, of course. Take the line closest to the center of mass of the shape, then use that to find the next three edges.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ImLeftFooted
Member #3,935
October 2003
avatar

I was hoping this was a bounding box check thread :(, I like answering that question.

Onewing
Member #6,152
August 2005
avatar

I think CGames has the right idea. Draw a line from each vertice to all other vertices, discarding any lines that enter the shape. This reduces your shape as follows:

{"name":"591890","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/7\/97a2347d189310d24347d33dc5ffbe82.jpg","w":254,"h":150,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/7\/97a2347d189310d24347d33dc5ffbe82"}591890

This new image will be much easier to find the minimum area bounding box (I would assume).

------------
Solo-Games.org | My Tech Blog: The Digital Helm

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

discarding any lines that enter the shape

I think you would need to find the quickest path around the shape without entering it. I can find a few routes that don't enter the shape and aren't your resulting polygon.

CGamesPlay
Member #2,559
July 2002
avatar

Stupid GIMP can't draw lies, so I booted Windows and I'm doing this from MS Paint.

{"name":"591891","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/7\/579ae9e12f7c48259f01e1ee34e5cd46.png","w":345,"h":453,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/5\/7\/579ae9e12f7c48259f01e1ee34e5cd46"}591891

1. Find all the lines between all the vertices. Discard those that intersect the polygon. The valid lines are shown in blue, some of the invalid ones are shown in red.

2. Find the shortest distance of each of the valid lines to the center of the shape. The shortest is shown in green, the rest in yellow.

3. From the point that the line come closest to the center of the shape (where the green line hits the blue on in step 2), find the first perpendicular line to the left and right that doesn't intersect with the shape.

3 b. Find the first parallel line down that doesn't intersect with the shape.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Goalie Ca
Member #2,579
July 2002
avatar

CGames... why Paint!? Paint sucks at drawing lines too! (esp at changing them)

What you want is inkscape. It does vector drawing.

-------------
Bah weep granah weep nini bong!

Johan Halmén
Member #1,550
September 2001

Stupid Gimp can draw lines. Click on start point. Shift-click on next points (vertices). Works with pencil, paintbrush, airbrush etc.

I would rotate my polygon in both directions and see what happens with the bounding rectangle. Compare rotate +epsilon degrees and -epsilon degrees and see in which direction you want to rotate to make the rectangle smaller. Then some kind of binary search for the minim.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Richard Phipps
Member #1,632
November 2001
avatar

If you are using vector objects, wouldn't it be easier just to check if the vector objects overlap directly?

bamccaig
Member #7,536
July 2006
avatar

If...

Steve Terry said:

...I know the angle the box will be at...

Then it makes sense to me to...

SiegeLord said:

...if you know the axis of the box, what's preventing you from rotating the set of points in such a way as to make this axis be, say, vertical and then simply using the standard min/max algorithm to find the box? When you obtain the box you can rotate the box corners back into the original orientation as needed.

As in...

{"name":"591899","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6d4b18ffc221daeddbf5789624d05009.jpg","w":543,"h":513,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6d4b18ffc221daeddbf5789624d05009"}591899

(Pretend the objects are the same... MSPaint is rotationally challenged and I'm no artist)

Otherwise, I'm out of my league. ;D

Thomas Harte
Member #33
April 2000
avatar

For some reason my vague memory is saying something about the covariance matrix and possibly eigenvectors. I'm off to the 'net...

Go to: