/// <summary> /// Try to add an object, starting at this node /// </summary> public bool TryAddObj(OctreeObject <T> newObj, bool updateCount) { OctreeNode childNode = this;//Start here int index; while (true) { if (!childNode.ContainsBounds(ref newObj.boundsMin, ref newObj.boundsMax)) { //Doesn't fit in this node. Put it in the parent node. if (childNode.childIndex == -1) { return(false); } childNode.parent.PutObjectInNode(newObj, updateCount); return(true); } index = childNode.BestFitChild(ref newObj.boundsCenter); if (childNode.isLeaf && childNode.localItemCount >= numObjectsAllowed && childNode.ContainsBounds(ref newObj.doubleBoundsMin, ref newObj.doubleBoundsMax)) { childNode.Split();//We hit the limit and we can split. We only split if the object will fit in the new bounds } if (!childNode.isLeaf) { //Drop down another level if we have children childNode = childNode.children[index]; } else { //Place it here. We have no children and we're under the limit childNode.PutObjectInNode(newObj, updateCount); return(true); } } }
/// <summary> /// Find a node that fully contains the given bounds /// </summary> public bool ContainingNode(ref Vector3 center, ref Vector3 minBounds, ref Vector3 maxBounds, out OctreeNode node) { node = null; OctreeNode childNode = this;//Start here int index; while (true) { if (!childNode.ContainsBounds(ref minBounds, ref maxBounds)) { //Doesn't fit in this node. Put it in the parent node. if (childNode.childIndex == -1) { return(false); } node = childNode.parent; return(true); } index = childNode.BestFitChild(ref center); if (!childNode.isLeaf) { //Drop down another level if we have children childNode = childNode.children[index]; } else { //Place it here. No children node = childNode; return(true); } } }