5 static int s_used[8192]; // same as MD3_MAX_TRIANGLES
\r
8 ** FindNextTriangleInStrip
\r
10 ** Given a surface and triangle this tries to find the next triangle
\r
11 ** in the strip that would continue the strip. The next triangle in
\r
12 ** the strip should have the same winding as this triangle.
\r
14 static int FindNextTriangleInStripOrFan( int mesh[][3], int tri, int orientation, int numTris, int odd )
\r
23 currentTri[0] = mesh[tri][(0+orientation)%3];
\r
24 currentTri[1] = mesh[tri][(1+orientation)%3];
\r
25 currentTri[2] = mesh[tri][(2+orientation)%3];
\r
29 refa = currentTri[1];
\r
30 refb = currentTri[2];
\r
34 refa = currentTri[2];
\r
35 refb = currentTri[0];
\r
38 // go through all triangles and look for sides that match
\r
40 for ( t = 0; t < numTris; t++ )
\r
42 // don't check against self or against previously used triangles
\r
48 // check all three sides of the candidate triangle
\r
49 for ( side = 0; side < 3; side++ )
\r
51 // check only the second (abutting) side
\r
52 if ( ( refa == mesh[t][(side+1)%3] ) &&
\r
53 ( refb == mesh[t][side] ) )
\r
60 // rotate the candidate triangle to align it properly in the strip
\r
67 else if ( side == 2 )
\r
79 Error( "fans not implemented yet" );
\r
81 // check only the third (abutting) side
\r
82 if ( ( currentTri[2] == pSurf->baseTriangles[t].v[side].index ) &&
\r
83 ( currentTri[0] == pSurf->baseTriangles[t].v[(side+1)%3].index ) )
\r
98 static int StripLength( int mesh[][3], int strip[][3], int tri, int orientation, int numInputTris, int fillNo )
\r
100 int stripIndex = 0;
\r
105 strip[stripIndex][0] = mesh[tri][(0+orientation)%3];
\r
106 strip[stripIndex][1] = mesh[tri][(1+orientation)%3];
\r
107 strip[stripIndex][2] = mesh[tri][(2+orientation)%3];
\r
108 s_used[tri] = fillNo;
\r
113 while ( ( next = FindNextTriangleInStripOrFan( mesh, next, orientation, numInputTris, odd ) ) != -1 )
\r
115 s_used[next] = fillNo;
\r
117 strip[stripIndex][0] = mesh[next][0];
\r
118 strip[stripIndex][1] = mesh[next][1];
\r
119 strip[stripIndex][2] = mesh[next][2];
\r
122 // all iterations after first need to be with an unrotated reference triangle
\r
130 ** BuildOptimizedList
\r
132 ** Attempts to build the longest strip/fan possible. Does not adhere
\r
133 ** to pure strip or fan, will intermix between the two so long as some
\r
134 ** type of connectivity can be maintained.
\r
136 #define MAX_ORIENTATIONS 3
\r
137 #define MAX_MATCHED_SIDES 4
\r
138 #define MAX_SEED_TRIANGLES 16
\r
140 static int BuildOptimizedList( int mesh[][3], int strip[][3], int numInputTris )
\r
145 int bestTri = -1, bestLength = 0, bestOrientation = -1;
\r
146 int matchedSides = 0;
\r
147 int orientation = 0;
\r
148 int seedTriangles[MAX_MATCHED_SIDES][MAX_SEED_TRIANGLES];
\r
149 int seedLengths[MAX_ORIENTATIONS][MAX_MATCHED_SIDES][MAX_SEED_TRIANGLES];
\r
150 int numSeeds[MAX_MATCHED_SIDES] = { 0, 0, 0 };
\r
153 // build a ranked list of candidate seed triangles based on
\r
154 // number of offshoot strips. Precedence goes to orphans,
\r
155 // then corners, then edges, and interiors.
\r
156 memset( seedTriangles, 0xff, sizeof( seedTriangles ) );
\r
157 memset( seedLengths, 0xff, sizeof( seedLengths ) );
\r
159 for ( i = 0; i < MAX_MATCHED_SIDES; i++ )
\r
161 // find the triangle with lowest number of child strips
\r
162 for ( t = 0; t < numInputTris; t++ )
\r
170 // try the candidate triangle in three different orientations
\r
172 for ( orientation = 0; orientation < 3; orientation++ )
\r
174 if ( ( n = FindNextTriangleInStripOrFan( mesh, t, orientation, numInputTris, 1 ) ) != -1 )
\r
180 if ( matchedSides == i )
\r
182 seedTriangles[i][numSeeds[i]] = t;
\r
184 if ( numSeeds[i] == MAX_SEED_TRIANGLES )
\r
190 // we have a list of potential seed triangles, so we now go through each
\r
191 // potential candidate and look to see which produces the longest strip
\r
192 // and select our startTri based on this
\r
193 for ( i = 0; i < MAX_MATCHED_SIDES; i++ )
\r
197 for ( j = 0; j < numSeeds[i]; j++ )
\r
199 for ( orientation = 0; orientation < 3; orientation++ )
\r
203 seedLengths[orientation][i][j] = StripLength( mesh, strip, seedTriangles[i][j], orientation, numInputTris, 2 );
\r
205 if ( seedLengths[orientation][i][j] > bestLength )
\r
207 bestTri = seedTriangles[i][j];
\r
208 bestLength = seedLengths[orientation][i][j];
\r
209 bestOrientation = orientation;
\r
212 for ( k = 0; k < numInputTris; k++ )
\r
214 if ( s_used[k] == 2 )
\r
220 if ( bestTri != -1 )
\r
226 // build the strip for real
\r
227 if ( bestTri != -1 )
\r
229 stripLen = StripLength( mesh, strip, bestTri, bestOrientation, numInputTris, 1 );
\r
238 ** Given an input mesh and an output mesh, this routine will reorder
\r
239 ** the triangles within the mesh into strips/fans.
\r
241 void OrderMesh( int input[][3], int output[][3], int numTris )
\r
244 int sumStrippedTriangles = 0;
\r
245 int strippedTriangles;
\r
246 int totalStrips = 0;
\r
247 int strip[8192][3]; // could dump directly into 'output', but
\r
248 // this helps with debugging
\r
250 memset( s_used, 0, sizeof( s_used ) );
\r
253 FILE *fp = fopen( "strip.txt", "wt" );
\r
255 for ( i = 0; i < numTris; i++ )
\r
257 fprintf( fp, "%4d: %3d %3d %3d\n", i, input[i][0], input[i][1], input[i][2] );
\r
262 // while there are still triangles that are not part of a strip
\r
263 while ( sumStrippedTriangles < numTris )
\r
266 strippedTriangles = BuildOptimizedList( input, strip, numTris );
\r
268 for ( i = 0; i < strippedTriangles; i++ )
\r
270 output[sumStrippedTriangles+i][0] = strip[i][0];
\r
271 output[sumStrippedTriangles+i][1] = strip[i][1];
\r
272 output[sumStrippedTriangles+i][2] = strip[i][2];
\r
275 sumStrippedTriangles += strippedTriangles;
\r
279 printf( "Triangles on surface: %d\n", sumStrippedTriangles );
\r
280 printf( "Total strips from surface: %d\n", totalStrips );
\r
281 printf( "Average strip length: %f\n", ( float ) sumStrippedTriangles / totalStrips );
\r