views:

754

answers:

8
+3  Q: 

Polygon math

Given a list of points that form a simple 2d polygon oriented in 3d space and a normal for that polygon, what is a good way to determine which points are specific 'corner' points?

For example, which point is at the lower left, or the lower right, or the top most point? The polygon may be oriented in any 3d orientation, so I'm pretty sure I need to do something with the normal, but I'm having trouble getting the math right.

Thanks!

A: 

Are you looking for a bounding box?

I'm not sure the normal has anything to do with what you are asking.

To get a Bounding box, keep 4 variables: MinX, MaxX, MinY, MaxY

Then loop through all of your points, checking the X values against MaxX and MinX, and your Y values against MaxY and MinY, updating them as needed.

When looping is complete, your box is defined as MinX,MinY as the upper left, MinX, MaxY as upper right, and so on...

Response to your comment:

If you want your box after a projection, what you need is to get the "transformed" points. Then apply bounding box loop as stated above.

Transformed usually implies 2D screen coordinates after a projection(scene render) but it could also mean the 2D points on any plane that you projected on to.

Neil N
Sorry, I should have been more clear. It's a face in 3d space, but it's really just a 2d polygon. So I want to know the specific corners if you were facing the polygon directly aligned with the normal (so it would appear as a 2d polygon). No wonder I can't solve it, I can't even explain it. :)
kevin42
ahh, then you are looking for the "transformed" points
Neil N
A: 

Project it onto a plane and get the bounding box.

plinth
+1  A: 

A possible algorithm would be

  1. Find the normal, which you can do by using the cross product of vectors connecting two pairs of different corners

  2. Create a transformation matrix to rotate the polygon so that it is planer in XY space (i.e. normal alligned along the Z axis)

  3. Calculate the coordinates of the bounding box or whatever other definition of corners you are using (as the polygon is now aligned in 2D space this is a considerably simpler problem)

  4. Apply the inverse of the transformation matrix used in step 2 to transform these coordinates back to 3D space.

Cruachan
+2  A: 

You would need more information in order to make that decision. A set of (co-planar) points and a normal is not enough to give you a concept of "lower left" or "top right" or any such relative identification.

Viewing the polygon from the direction of the normal (so that it appears as a simple 2D shape) is a good start, but that shape could be rotated to any arbitrary angle.

  1. Is there some other information in the 3D world that you can use to obtain a coordinate-system reference?

  2. What are you trying to accomplish by knowing the extreme corners of the shape?

e.James
+1  A: 

I believe that your question requires some additional information - namely the coordinate system with respect to which any point could be considered "topmost", or "leftmost".

Don't forget that whilst the normal tells you which way the polygon is facing, it doesn't on its own tell you which way is "up". It's possible to rotate (or "roll") around the normal vector and still be facing in the same direction.

This is why most 3D rendering systems have a camera which contains not only a "view" vector, but also "up" and "right" vectors. Changes to the latter two achieve the effect of the camera "rolling" around the view vector.

Alnitak
A: 

I have a silly idea, but at the risk of gaining a negative a point, I'll give it a try:

  1. Get the minimum/maximum value from each three-dimensional axis of each point on your 2d polygon. A single pass with a loop/iterator over the list of values for every point will suffice, simply replacing the minimum and maximum values as you go. The end result is a list that has the "lowest" X, Y, Z coordinates and "highest" X, Y, Z coordinates.
  2. Iterate through this list of min/max values to create each point ("corner") of a "bounding box" around the object. The result should be a box that always contains the object regardless of axis examined or orientation (no point on the polygon will ever exceed the maximum or minimums you collect).
  3. Then get the distance of each "2d polygon" point to each corner location on the "bounding box"; the shorter the distance between points, the "closer" it is to that "corner".

Far from optimal, certainly crummy, but certainly quick. You could probably post-capture this during the object's rotation, by simply looking for the min/max of each rotated x/y/z value, and retaining a list of those values ahead of time.

Avery Payne
A: 

If you can assume that there is some constraints regarding the shapes, then you might be able to get away with knowing less information. For example, if your shape was the composition of a small square with a long thin triangle on one side (i.e. a simple symmetrical geometry), then you could compare the distance from each list point to the "center of mass." The largest distance would identify the tip of the cone, the second largest would be the two points farthest from the tip of the cone, etc... If there was some order to the list, like points are entered in counter clockwise order (about the normal), you could identify all the points. This sounds like a bit of computation, so it might be reasonable to try to include some extra info with your shapes, like the "center of mass" and a reference point that is located "up" above the COM (but not along the normal). This will give you an "up" vector that you can cross with the normal to define some body coordinates, for example. Also, the normal can be defined by an ordering of the point list. If you can't assume anything about the shapes (or even if the shapes were symmetrical, for example), then you will need more data. It depends on your constraints.

Derek E
A: 

If you know that the polygon in 3D is "flat" you can use the normal to transform all 3D-points of the vertices to a 2D-representation (of the points with respect to the plan in which the polygon is located) - but this still leaves you with defining the origin of this coordinate-system (but this don't really matter for your problem) and with the orientation of at least one of the axes (if you want orthogonal axes you can still rotate them around your choosen origin) - and this is where the trouble starts. I would recommend using the Y-axis of your 3D-coordinate system, project this on your plane and use the resulting direction as "up" - but then you are in trouble in case your plan is orthogonal to the Y-axis (now you might want to use the projected Z-Axis as "up").

The math is rather simple (you can use the inner product (a.k.a. scalar product) for projection to your plane and some matrix stuff to convert to the 2D-coordinate system - you can get all of it by googling for raytracer algorithms for polygons.

CKoenig