private static void Subdivide(BVItem[] items, int nitems, int imin, int imax, ref int curNode, ref List <BVNode> nodes) { int inum = imax - imin; int icur = curNode; BVNode node = new BVNode(); nodes.Add(node); curNode++; if (inum == 1) { // Leaf node.BMin = items[imin].BMin; node.BMax = items[imin].BMax; node.I = items[imin].I; } else { // Split CalcExtends(items, nitems, imin, imax, out var bmin, out var bmax); node.BMin = bmin; node.BMax = bmax; int axis = LongestAxis( node.BMax.X - node.BMin.X, node.BMax.Y - node.BMin.Y, node.BMax.Z - node.BMin.Z); if (axis == 0) { // Sort along x-axis Array.Sort(items, imin, inum, BVItem.XComparer); } else if (axis == 1) { // Sort along y-axis Array.Sort(items, imin, inum, BVItem.YComparer); } else { // Sort along z-axis Array.Sort(items, imin, inum, BVItem.ZComparer); } int isplit = imin + inum / 2; // Left Subdivide(items, nitems, imin, isplit, ref curNode, ref nodes); // Right Subdivide(items, nitems, isplit, imax, ref curNode, ref nodes); int iescape = curNode - icur; // Negative index means escape. node.I = -iescape; } }
/// <summary> /// Patch header pointers /// </summary> /// <param name="header">Header</param> public void Patch(MeshHeader header) { Verts = new Vector3[header.VertCount]; Polys = new Poly[header.PolyCount]; Links = new Link[header.MaxLinkCount]; DetailMeshes = new PolyDetail[header.DetailMeshCount]; DetailVerts = new Vector3[header.DetailVertCount]; DetailTris = new Int4[header.DetailTriCount]; BvTree = new BVNode[header.BvNodeCount]; OffMeshCons = new OffMeshConnection[header.OffMeshConCount]; }