/** * Re-arrange all triangles for optimal compression. */ public void rearrangeTriangles(int[] indices) { if(indices.Length % 3 != 0) throw new Exception(); // 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); } Array.Sort(tris); for (int i = 0; i < tris.Length; i++) { int off = i * 3; tris[i].copyBack(indices, off); } }
public int compareTo(Triangle o) { if (elements[0] != o.elements[0]) { return elements[0] - o.elements[0]; } else if (elements[1] != o.elements[1]) { return elements[1] - o.elements[1]; } return elements[2] - o.elements[2]; }