public bool Remove(T item, bool debugRender = false) { AABB itemAABB = item.AABB; if (Bounds.Encloses(itemAABB)) { if (!CanHaveChildren || !SubdivisionCanContainItem(item)) { return(ObjectsInNode.Remove(item)); } else { var removed = false; foreach (var child in Children) { if (child.Remove(item, debugRender)) { removed = true; break; } } if (!removed) { throw new NotImplementedException("The item could not be deleted into this tree or any subdivision, this suggests a mistake in choosing to delete the item from this node"); } if (NumObjectsInChildren < MergeThreshold) { ObjectsInNode.AddRange(Children.SelectMany(c => c.ObjectsInNode)); Children.Clear(); } return(removed); } } else if (debugRender) { Bounds.DrawAABB(Color.red); item.AABB.DrawAABB(Color.green); } return(false); }
public bool Insert(T item, bool debugRender = false) { AABB itemAABB = item.AABB; if (Bounds.Encloses(itemAABB)) { if (!CanHaveChildren || !SubdivisionCanContainItem(item)) { ObjectsInNode.Add(item); return(true); } else if (SubdivisionThreshold <= ObjectsInNodeThatCanFitInSubdivision.Count()) { ObjectsInNode.Add(item); return(true); } else { if (Children.Count == 0) { foreach (var extent in Bounds.EnumerateExtents()) { var newCentre = Bounds.center + extent / 2; //Debug.DrawLine(newCentre, Bounds.center, Color.white, 30f); Children.Add(new OctreeNode <T>(MinDimensionOfNode, new AABB(newCentre, Bounds.extents / 2))); } } foreach (var objectToMoveToChild in ObjectsInNodeThatCanFitInSubdivision.ToList()) { ObjectsInNode.Remove(objectToMoveToChild); foreach (var child in Children) { if (child.Insert(objectToMoveToChild, debugRender)) { break; } } } foreach (var child in Children) { if (child.Insert(item, debugRender)) { return(true); } } throw new NotImplementedException("The item could not be inserted into this tree or any subdivision, this suggests a mistake in choosing to insert into this node"); } } else if (debugRender) { Bounds.DrawAABB(Color.red); item.AABB.DrawAABB(Color.green); } return(false); }