Пример #1
0
        //对象删除或更新后,需要对旧的结点进行合并操作
        private void MergeNode(QuadNode mergedNode)
        {
            if (mergedNode == null || mergedNode.ObjectCount >= m_NodeSplitThreshold)
            {
                return;
            }

            QuadNode targetNode = mergedNode;

            while (targetNode != null)
            {
                if (targetNode.ParentNode != null && targetNode.ParentNode.GetTotalObjectCount() >= m_NodeSplitThreshold)
                {
                    break;
                }

                targetNode = targetNode.ParentNode;
            }

            if (targetNode != null && !targetNode.IsLeaf)
            {
                List <QuadNode> nodeList = QuadPool.GetNodeList();
                targetNode.GetTotalChildNodes(ref nodeList);
                List <IQuadObject> objectList = QuadPool.GetObjectList();
                targetNode.GetTotalObjects(ref objectList);

                targetNode[QuadNodeDirection.LB] = null;
                targetNode[QuadNodeDirection.RB] = null;
                targetNode[QuadNodeDirection.LT] = null;
                targetNode[QuadNodeDirection.RT] = null;

                foreach (var childNode in nodeList)
                {
                    m_NodePool.Release(childNode);
                }

                targetNode.ClearObjects();
                foreach (var obj in objectList)
                {
                    m_ObjectToNodeDic.Remove(obj);
                    InsertObjectToNode(targetNode, obj);
                }

                QuadPool.ReleaseNodeList(nodeList);
                QuadPool.ReleaseObjectList(objectList);
            }
        }
Пример #2
0
        //结点分裂后重新整理原来的对象
        private void ResetNodeObjects(QuadNode node)
        {
            List <IQuadObject> nodeObjects = QuadPool.GetObjectList();

            node.GetObjects(ref nodeObjects);

            node.ClearObjects();

            List <IQuadObject> uninsertToChildObjects = QuadPool.GetObjectList();

            foreach (var obj in nodeObjects)
            {
                bool isInsertToChildNode = false;

                QuadNode[] childNodes = node.ChildNodes;
                foreach (var childNode in childNodes)
                {
                    if (childNode.TryInsertObject(obj))
                    {
                        m_ObjectToNodeDic[obj] = childNode;
                        isInsertToChildNode    = true;
                        break;
                    }
                }

                if (!isInsertToChildNode)
                {
                    uninsertToChildObjects.Add(obj);
                }
            }

            if (uninsertToChildObjects.Count > 0)
            {
                foreach (var obj in uninsertToChildObjects)
                {
                    node.InsertObject(obj);
                }
            }
            QuadPool.ReleaseObjectList(nodeObjects);
            QuadPool.ReleaseObjectList(uninsertToChildObjects);
        }