int split_tri_set_sorted(int[] triangles, Vector3d[] centers, int iStart, int iCount, int depth, int minTriCount, boxes_set tris, boxes_set nodes, out AxisAlignedBox3f box) { box = AxisAlignedBox3f.Empty; int iBox = -1; if (iCount < minTriCount) { // append new triangles box iBox = tris.iBoxCur++; tris.box_to_index.insert(tris.iIndicesCur, iBox); tris.index_list.insert(iCount, tris.iIndicesCur++); for (int i = 0; i < iCount; ++i) { tris.index_list.insert(triangles[iStart + i], tris.iIndicesCur++); box.Contain(mesh.GetTriBounds(triangles[iStart + i])); } tris.box_centers.insert(box.Center, iBox); tris.box_extents.insert(box.Extents, iBox); return(-(iBox + 1)); } AxisComp c = new AxisComp() { Axis = depth % 3 }; Array.Sort(centers, triangles, iStart, iCount, c); int mid = iCount / 2; int n0 = mid; int n1 = iCount - mid; // create child boxes AxisAlignedBox3f box1; int child0 = split_tri_set_sorted(triangles, centers, iStart, n0, depth + 1, minTriCount, tris, nodes, out box); int child1 = split_tri_set_sorted(triangles, centers, iStart + mid, n1, depth + 1, minTriCount, tris, nodes, out box1); box.Contain(box1); // append new box iBox = nodes.iBoxCur++; nodes.box_to_index.insert(nodes.iIndicesCur, iBox); nodes.index_list.insert(child0, nodes.iIndicesCur++); nodes.index_list.insert(child1, nodes.iIndicesCur++); nodes.box_centers.insert(box.Center, iBox); nodes.box_extents.insert(box.Extents, iBox); return(iBox); }