简体   繁体   中英

Sorting 3D polygon vertices in anticlockwise direction

I have to find the normal of a polygon, but in order to do that I first have to order the vertices (x,y,z) in anticlockwise direction. Lets say I am given n number of vertices points entered by the user, how do I order the vertices in anticlockwise direction to turn them into a polygon?

You can't order your points without knowing the normal, and vice-versa. If you say "anti-clockwise from the front" then you need to know what side is the front, and thus you need to know the normal. So your question doesn't have an answer, as it stands, because it is a bit circular: You don't have enough information to determine what you are looking for.

You need a little bit more information. For example, if this polygon is part of a convex surface, you can compute the geometric center of the surface. Then pick any 3 non-colinear points a, b, c on your polygon, calculate the normal ( norm((a - b) cross (c - b)) ), calculate the vector from the centroid to one of your points (eg b - centroid ), then check if the normal faces the same (or opposite, whatever) direction as that vector (sign of normal dot (b - centroid) ) and flip it if it's the wrong way. Essentially you just calculate a perpendicular unit vector (there are two, remember), then make sure it's pointing towards the inside / outside of your object.

If your face is part of a concave surface, you can make all the normals point the same general direction by taking the dot product of your normal with an adjacent face's normal and flipping it if it's negative. You'd still need to decide on a way to determine which side of the face was on the inside vs. outside of the surface, but at least all the normals would be pointing towards one or the other.

If you don't have information about the general direction the normal should be facing, you'll have to get your anti-clockwise info from elsewhere. You can sort the points around the center of your polygon to get them in order, but to determine if that order is clockwise vs. anti-clockwise requires additional info (eg ask the user, etc.).

Note that the atan -based 2D solutions implicitly rely on some extra information: The rely on the fact that the normal faces out of the screen / off the paper and towards you, the viewer, and thus you can sort in a known direction around that normal. That assumption is generally made without anybody ever actually speaking of it. However, in reality, you need to decide which side is the front and the back - not always easy for 3D (if you are rendering to the screen and you have a viewpoint set up, you could use that as the extra info - say the normal faces the camera, then order around that normal).


Another way to phrase the above is, in general, we have 3 unknowns:

  1. What is the winding order when viewed from the "front"?
  2. Which of the two perpendicular unit vectors to the plane do we choose to be on the "front" or "which side is the front"?
  3. What is the sort order of the given points that corresponds to the winding order?

We need to have two of these to determine the third.

In response to Andon's comments below: For the rasterizer, it defines #1 itself (or takes it from configuration), and #3 is assumed by API requirements, and so #2 can be inferred.

In the 2D cases that you often see described (which I mentioned above), #1 is specified as a requirement, #2 is assumed to be "the side facing the viewer is the front", and so #3 can be determined with atan and sorting in increasing/decreasing order.

In your original question, you know #1 (you've defined it), but you don't know #2 (which side is the front?), and therefore you cannot determine #3. You need some way to determine #2 (eg facing away from centroid, facing towards viewer, whatever).

See atan .

To find the angle of a point (X,Y), you just need to do atan(Y/X) . Here are some caveats:

http://upload.wikimedia.org/math/5/7/1/571efb70f630041f9e2b00019025171e.png Atan网域

Finally, order your results from lowest to highest (add 360 to negative numbers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM