views:

219

answers:

3

Given an image of a 2-dimensional irregular (non-convex) shape, how would I able to compute all the ways in which it could lie stable on a flat surface? For example, if the shape is a perfect square rectangle, then it will surely have 4 ways in which it is stable. A circle on the other hand either has no stable orientation or every point is a stable orientation.

EDIT: There's this nice little game called Splitter (Beware, addictive game ahead) that seems close to what I want. Noticed that you cut a piece of the wood out it would fall to the ground and lay in a stable manner.

EDIT: In the end, the approach I took is to compute center of mass (of the shape) and compute the convex hull (using OpenCV), and then loop through every pair of vertices. If the center of mass falls on top of the line formed by the 2 vertices, it is deemed stable, else, no.

A: 

I'm sure this is not the most efficient algorithm, but it's an idea.

If you can order the verticles of the polygon (assuming it has a finite number of vertices), then just iterate over adjacent pairs of vertices and record the angle it rests at through some form of simulation. There will be duplicate orientations for it to sit on in the case of weird shapes, like stars, but you can accomodate for that by keeping track of the resting rotation.

Jamie Wong
+8  A: 
Beta
Since I *do* have some drawing tools, I've run up a couple of quick figures to illustrate the [convex hull](http://walkytalky.net/extern/hull.png) and the stable positions (plus one unstable, for the little end of an ellipse) from [your examples](http://walkytalky.net/extern/stable.png). BTW, you take CM and hull calculations as read. This may be fair enough for polygons, but I'm curious as to whether there are efficient ways to calculate the CM and hull for shapes including curves such as Beziers?
walkytalky
@tom10: well, no, look at my ellipse example. Also, if the resting points are *all on one side* of the CM, then the position is not stable.
Beta
@walkytalky: oh, *very* nice! (Although I think the dotted circles are swapped for the two ellipses.) May I embed these? As for finding the CM and hull of complex shapes, I'd fall back on integral calculus and number-crunching. Bezier curves may have tidy exact solutions for all I know, but there are certainly shapes that don't.
Beta
@tom10 - Beta was talking about the (convex) hull. If you take the hull of the original shape, all "resting points" get converted into sides. There are no special cases--Beta covered _all_ cases by first performing the hull operation.
Rex Kerr
@Beta: You may certainly embed. The dotted circles on the ellipses were an attempt to illustrate where the curvature lies with respect to the distance from the CM. Treating the latter as a radius seemed easier to depict, but I can change it. Actually, perhaps having both would be preferable. I'll give it a go.
walkytalky
@Beta and Rex - I either misread the question or it was edited, either way, my previous comments are simply a distraction, so I've removed them.
tom10
+3  A: 

note: this answer assumes your shape is a proper polygon.

For our purposes, we'll define an equilibrium position as one where the Center of Mass is directly above a point that is between the leftmost and rightmost ground-contact points of the object (assuming the ground is a flat surface perpendicular to the force of gravity). This will work in all cases, for all shapes.

Note that, this is actually the physical definition of rotational equilibrium, as a consequence of Newtonian Rotational Kinematics.

For a proper polygon, if we eliminate cases where they stand on a sole vertex, this definition is equivalent to a stable position.

So, if you have a straight downward gravity, first find the left-most and right-most parts of it that are touching the ground.

Then, calculate your Center of Mass. For a polygon with known vertices and uniform density, this problem is reduced to finding the Centroid (relevant section).

Afterwards, drop a line from your CoM; if the intersection of the CoM and the ground is between those two x values, it's at equilibrium.

If your leftmost point and rightmost point match (ie, in a round object), this will still hold; just remember to be careful with your floating point comparisms.

Note that this can also be used to measure "how stable" an object is -- this measure is the maximum y-distance the Center of Mass can move before it is no longer within the range of the two contact points.

EDIT: nifty diagram made hastily

Diagram

So, how can you use this to find all the ways it can sit on a table? See:


EDIT

The programmable approach

Instead of the computationally expensive task of rotating the shape, try this instead.

Your shape's representation in your program should probably have a list of all vertices.

Find the vertices of your shape's convex hull (basically, your shape, but with all concave vertices -- vertices that are "pushed in" -- eliminated).

Then Iterate through each of pair of adjacent vertices on your convex hull (ie, if I had vertices A, B, C, D, I'd iterate through AB, BC, CD, DA)

Do this test:

  1. Draw a line A through the two vertices being tested
  2. Draw a line perpendicular to A, going through CoM C.
  3. Find the intersection of the two lines (simple algebra)
  4. If the intersection's y value is in between the y value of the two vertices, it stable. If the y values are all equal, compare the x values.

That should do the trick.

Here is an example of the test being running on one pair of vertices:

Example test

If your shape is not represented by its vertices in your data structure, then you should try to convert them. If it's something like a circle or an ellipse, you may use heuristics to guess the answer (a circle has infinite equilibrium positions; an ellipse 4, albeit only two "stable" points). If it's a curved wobbly irregular shape, you're going to have to supply your data structure for me be able to help in a program-related way, instead of just providing case-by-case heuristics.

Justin L.
An ellipse does *not* have 4 stable positions.
Beta
An ellipse has four stable positions; it's just that in one of them, the center of mass has only to move an infinitesimal distance before it is outside of the left and right contact points (which are only one point). However, if he is defining the ellipse by vertices, there are no infinitesimal points, and he will definitely have a stable point there (unless it's a vertex).For a physical, real-world demonstration of how an ellipse can have four stable positions, see [this image](http://bit.ly/c1eHHT); however, I will edit my point to emphasize the more precarious stability.
Justin L.
@Justin - You're confusing "equilibrium" with "stable equilibrium". Google the later and you'll see. You do a good job in defining equilibrium, but these aren't necessarily stable. (Your answer makes this mistake clear when you say "we'll define a 'stable position'...", but stable already has a clear physical definition, and is about how the system responds to perturbations, and you're redefining it to mean something else.)
tom10
Thanks; I think I got it now. I did misunderstand the question; I'll amend that.
Justin L.
I've noticed that my answer still applies to stable position if we assume a polygon with nonzero area.
Justin L.
Off-topic question, I'm curious as to what software did you use to draw such pretty illustrations?
Hao Wooi Lim
Any vector art program will do; I'm sure there are a lot of free ones out there. I've even worked once with a browser-based one called Raven, of the [Aviary](http://aviary.com/) online suite. The first diagram was on Aviary (I was playing around with it). The second, more serious one, was on Adobe Illustrator.
Justin L.