views:

77

answers:

3

I have an array with vertices representing a triangular strip. I need to convert it into polygon. There are many solution to do the reverse, but I failed to find one for the above problem. Or it could be too easy and I just cannot see it. Please help.

OpenGL=compatible, see http://en.wikipedia.org/wiki/Triangle_strip

Example: for this strip http://en.wikipedia.org/wiki/File:Triangle_Strip_Small.png I need output A B D F E C or A C E F D B

A: 

There may be a shortcut if your shape has a particularly simple structure, but in general I think you want to do the following

  • Create a list of vertices and edges from your list of triangles. This involves detecting shared points (hopefully they are exact matches so you can find them by hashing instead of needing some sort of fuzzy search) and shared lines (that's easy--just don't draw two edges between the same pair of shared vertices).
  • Find the upper-left-most point.
  • Find the edge that travels as upper-left-most as possible, and is in the counterclockwise direction.
  • Walk to the next vertex.
  • Find the next edge that doubles back on the previous one as much as possible.
  • Keep going until you hit the upper-left-most point again.
Rex Kerr
+5  A: 

I believe the following should work:

Walk through the list of vertices. Add the first point to your polygon. Push the second point on the stack. Add the third point to the polygon. Continue alternating between pushing points on the stack and adding them to the polygon until you reach the end of the list. When you get to the end of the list, pop the points of the stack and add them to the polygon.

Jerry Coffin
I think this does not account for interior points. Does it?
belisarius
@belisarius: It doesn't try to "simplify" the polygon, if that's what you mean -- i.e., it's going to create a polygon that matches the outline of the triangle strip exactly. If (for example) you've tessellated a surface into a long triangle strip that winds around a lot, it's going to produce a polygon that winds around exactly the same way. At least if you're using 3 dimensions, you can't do much else though -- the interior points often have differing Z values that need to be preserved.
Jerry Coffin
Yep. I thought that was the intention of the OP. May be I'm wrong. Tnx.
belisarius
In order to simplify the polygon, unify the names of equal vertices, then go through your circular list, replacing the pattern ABA with A, until no such replacement is possible any more.
Svante
@belisarius - there are no interior points on a triangle strip.
dash-tom-bang
@dash-tom-bang Yes, I understood that long after posting the comment. Thank you.
belisarius
+1  A: 

alt text

I'll assume your triangle strip is always connected the same way (which I believe is true for OpenGL).

  • The "bottom" vertices are always two apart: A, C, E, ...
  • The "top" vertices are always two apart: B, D, F, ...

Take the "bottom" list and append the reverse of the "top" list. (ACEFDB for the example)


Or, more directly, using a zero-based index instead of letters:

// do "bottom"
for ( i = 0; i < N; i += 2 )
  addVertex( i )

// do "top"
largestOddNumberLessThanN = N % 2 == 0 ? N - 1 : N - 2;
for ( i = largestOddNumberLessThanN; i >= 0; i -= 2 )
  addVertex( i )
Tom Sirgedas