RecastBBTreeBox RemoveBox (RecastBBTreeBox c, RecastMeshObj mesh, Rect bounds, ref bool found) { if (!RectIntersectsRect (c.rect,bounds)) { return c; } if (c.mesh == mesh) { found = true; return null; } if (c.mesh == null && !found) { c.c1 = RemoveBox (c.c1, mesh, bounds, ref found); if (c.c1 == null) { return c.c2; } if (!found) { c.c2 = RemoveBox (c.c2, mesh, bounds, ref found); if (c.c2 == null) { return c.c1; } } if (found) { c.rect = ExpandToContain (c.c1.rect, c.c2.rect); } } return c; }
/** Removes the specified mesh from the tree. * Assumes that it has the correct bounds information. * * \returns True if the mesh was removed from the tree, false otherwise. */ public bool Remove (RecastMeshObj mesh) { if (mesh == null) throw new ArgumentNullException("mesh"); if (root == null) { return false; } bool found = false; Bounds b = mesh.GetBounds(); //Convert to top down rect Rect r = Rect.MinMaxRect (b.min.x, b.min.z, b.max.x, b.max.z); root = RemoveBox (root, mesh, r, ref found); return found; }
public RecastBBTreeBox (RecastMeshObj mesh) { this.mesh = mesh; Vector3 min = mesh.bounds.min; Vector3 max = mesh.bounds.max; rect = Rect.MinMaxRect (min.x,min.z,max.x,max.z); }
/** Inserts a RecastMeshObj in the tree at its current position */ public void Insert (RecastMeshObj mesh) { var box = new RecastBBTreeBox (mesh); if (root == null) { root = box; return; } RecastBBTreeBox c = root; while (true) { c.rect = ExpandToContain (c.rect,box.rect); if (c.mesh != null) { //Is Leaf c.c1 = box; var box2 = new RecastBBTreeBox (c.mesh); c.c2 = box2; c.mesh = null; return; } else { float e1 = ExpansionRequired (c.c1.rect,box.rect); float e2 = ExpansionRequired (c.c2.rect,box.rect); // Choose the rect requiring the least expansion to contain box.rect if (e1 < e2) { c = c.c1; } else if (e2 < e1) { c = c.c2; } else { // Equal, Choose the one with the smallest area c = RectArea (c.c1.rect) < RectArea (c.c2.rect) ? c.c1 : c.c2; } } } }