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