/// <summary> /// Constructor. /// </summary> public SphereTree(int numChildrenPerNode) { // Store the number of children per node and clamp _numChildrenPerNode = numChildrenPerNode; if (_numChildrenPerNode < 2) { _numChildrenPerNode = 2; } // Create the root node _root = new SphereTreeNode <T>(default(T), new Sphere(Vector3.zero, 1.0f)); _root.SetFlagsBits(BVHNodeFlags.Root); }
/// <summary> /// Integrates 'node' inside the tree. This is a recursive method which will /// search for the best place where to place this node inside the tree. /// </summary> /// <param name="parent"> /// Keeps track of the current parent node we are processing., Allow us to keep /// going down the tree hierarchy. /// </param> private void IntegrateNodeRecurse(SphereTreeNode <T> node, SphereTreeNode <T> parent) { // Are we dealing with a terminal node? if (!parent.IsFlagBitSet(BVHNodeFlags.Terminal)) { // This is not a terminal node. First thing to do is check if this node has // room for one more child. If it does, we add the node here. Otherwise, we // keep searching. if (parent.NumChildren < _numChildrenPerNode) { node.SetFlagsBits(BVHNodeFlags.Terminal); node.SetParent(parent); parent.EncapsulateChildrenBottomUp(); } else { // Find the child closest to 'node' and recurse down that path SphereTreeNode <T> closestChild = parent.ClosestChild(node); if (closestChild != null) { IntegrateNodeRecurse(node, closestChild); } } } else { // We have reached a terminal node. We have no choice but to create a new non-terminal // node to take the place of the terminal one and then attach the integration node and // the old terminal node as children of this new node. SphereTreeNode <T> newParentNode = new SphereTreeNode <T>(default(T), parent.Sphere); newParentNode.SetParent(parent.Parent); parent.SetParent(newParentNode); node.SetParent(newParentNode); node.SetFlagsBits(BVHNodeFlags.Terminal); newParentNode.EncapsulateChildrenBottomUp(); } }