public void rearrangeTriangles ( int [ ] indices ) { assert indices . length % 3 == 0 ; // Step 1: Make sure that the first index of each triangle is the smallest // one (rotate triangle nodes if necessary) for ( int off = 0 ; off < indices . length ; off += 3 ) { if ( ( indices [ off + 1 ] < indices [ off ] ) && ( indices [ off + 1 ] < indices [ off + 2 ] ) ) { int tmp = indices [ off ] ; indices [ off ] = indices [ off + 1 ] ; indices [ off + 1 ] = indices [ off + 2 ] ; indices [ off + 2 ] = tmp ; } else if ( ( indices [ off + 2 ] < indices [ off ] ) && ( indices [ off + 2 ] < indices [ off + 1 ] ) ) { int tmp = indices [ off ] ; indices [ off ] = indices [ off + 2 ] ; indices [ off + 2 ] = indices [ off + 1 ] ; indices [ off + 1 ] = tmp ; } } // Step 2: Sort the triangles based on the first triangle index Triangle [ ] tris = new Triangle [ indices . length / 3 ] ; for ( int i = 0 ; i < tris . length ; i ++ ) { int off = i * 3 ; tris [ i ] = new Triangle ( indices , off ) ; } Arrays . sort ( tris ) ; for ( int i = 0 ; i < tris . length ; i ++ ) { int off = i * 3 ; tris [ i ] . copyBack ( indices , off ) ; } }
Re - arrange all triangles for optimal compression .