Esempio n. 1
0
        private static void insertleaf(Dbvt pdbvt, Node leaf)
        {
            if (pdbvt.root == null)
            {
                pdbvt.root  = leaf;
                leaf.parent = null;
            }
            else
            {
                Node node = pdbvt.root;
                while (!node.isleaf())
                {
                    if (DbvtAabbMm.Proximity(node.childs[0].volume, leaf.volume) <
                        DbvtAabbMm.Proximity(node.childs[1].volume, leaf.volume))
                    {
                        node = node.childs[0];
                    }
                    else
                    {
                        node = node.childs[1];
                    }
                }
                Node sibling   = node;
                Node oldParent = node.parent;
                Node newParent = createnode(pdbvt, oldParent, merge(leaf.volume, sibling.volume, new DbvtAabbMm()), null);
                if (oldParent != null)
                {
                    oldParent.childs[indexof(sibling)] = newParent;
                    newParent.childs[0] = sibling;
                    newParent.childs[1] = leaf;
                    sibling.parent      = newParent;
                    leaf.parent         = newParent;
                }
                else
                {
                    newParent.childs[0] = sibling;
                    newParent.childs[1] = leaf;
                    sibling.parent      = newParent;
                    leaf.parent         = newParent;
                    pdbvt.root          = newParent;
                }

                node = leaf.parent;
                while (node != null)
                {
                    node = Balance(pdbvt, node);
                    Node child0 = node.childs[0];
                    Node child1 = node.childs[1];
                    node.height = Math.Max(child0.height, child1.height) + 1;
                    node.volume = merge(child0.volume, child1.volume, new DbvtAabbMm());
                    node        = node.parent;
                }
            }
        }