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; }
void QueryBoxInBounds (RecastBBTreeBox box, Rect bounds, List<RecastMeshObj> boxes) { if (box.mesh != null) { //Leaf node if (RectIntersectsRect (box.rect,bounds)) { // Found a RecastMeshObj, add it to the result boxes.Add (box.mesh); } else { } } else { #if ASTARDEBUG Debug.DrawLine (new Vector3 (box.rect.xMin,0,box.rect.yMin),new Vector3 (box.rect.xMax,0,box.rect.yMin),Color.white); Debug.DrawLine (new Vector3 (box.rect.xMin,0,box.rect.yMax),new Vector3 (box.rect.xMax,0,box.rect.yMax),Color.white); Debug.DrawLine (new Vector3 (box.rect.xMin,0,box.rect.yMin),new Vector3 (box.rect.xMin,0,box.rect.yMax),Color.white); Debug.DrawLine (new Vector3 (box.rect.xMax,0,box.rect.yMin),new Vector3 (box.rect.xMax,0,box.rect.yMax),Color.white); #endif //Search children if (RectIntersectsRect (box.c1.rect,bounds)) { QueryBoxInBounds (box.c1, bounds, boxes); } if (RectIntersectsRect (box.c2.rect,bounds)) { QueryBoxInBounds (box.c2, bounds, boxes); } } }
/** 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 void OnDrawGizmos (RecastBBTreeBox box) { if (box == null) { return; } var min = new Vector3 (box.rect.xMin,0,box.rect.yMin); var max = new Vector3 (box.rect.xMax,0,box.rect.yMax); Vector3 center = (min+max)*0.5F; Vector3 size = (max-center)*2; Gizmos.DrawCube (center,size); OnDrawGizmos (box.c1); OnDrawGizmos (box.c2); }
/** 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; } } } }