Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to convert vertices of a triangular strip to polygon

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

like image 393
Yarik Avatar asked Aug 20 '10 14:08

Yarik


2 Answers

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.

like image 119
Jerry Coffin Avatar answered Oct 20 '22 08:10

Jerry Coffin


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 )
like image 30
Tom Sirgedas Avatar answered Oct 20 '22 08:10

Tom Sirgedas