Пример #1
0
        public QuadtreeNode(ArrayList sceneNodes, int depth, int maxDepth)
        {
            this.sceneNodes = sceneNodes;

            depth++;
            if (depth == maxDepth)
            {
                return;
            }

            nodeBox = new BoundingBox(-1000000, -1000000, -1000000, 1000000, 1000000, 1000000);
            foreach (SceneNode node in sceneNodes)
            {
                BoundingBox tempBB = node.GetBoundingBox();

                if (tempBB.min.x < nodeBox.min.x)
                {
                    nodeBox.min.x = tempBB.min.x;
                }
                if (tempBB.min.y < nodeBox.min.y)
                {
                    nodeBox.min.y = tempBB.min.y;
                }
                if (tempBB.min.z < nodeBox.min.z)
                {
                    nodeBox.min.z = tempBB.min.z;
                }

                if (tempBB.max.x > nodeBox.max.x)
                {
                    nodeBox.max.x = tempBB.max.x;
                }
                if (tempBB.max.y > nodeBox.max.y)
                {
                    nodeBox.max.y = tempBB.max.y;
                }
                if (tempBB.max.z > nodeBox.max.z)
                {
                    nodeBox.max.z = tempBB.max.z;
                }
            }

            ArrayList[] childSceneNodes = new ArrayList[4];
            for (int i = 0; i < 4; i++)
            {
                childSceneNodes[i] = new ArrayList();
            }

            double halfX = ((nodeBox.max.x - nodeBox.min.x) / 2) + nodeBox.min.x;
            double halfY = ((nodeBox.max.y - nodeBox.min.y) / 2) + nodeBox.min.y;

            foreach (SceneNode node in sceneNodes)
            {
                BoundingBox tempBB = node.GetBoundingBox();

                if (tempBB.min.x <= halfX && tempBB.min.y <= halfY)
                {
                    childSceneNodes[0].Add(node);
                }

                if (tempBB.min.x > halfX && tempBB.min.y <= halfY)
                {
                    childSceneNodes[1].Add(node);
                }

                if (tempBB.min.x <= halfX && tempBB.min.y > halfY)
                {
                    childSceneNodes[2].Add(node);
                }

                if (tempBB.min.x > halfX && tempBB.min.y > halfY)
                {
                    childSceneNodes[3].Add(node);
                }
            }

            for (int i = 0; i < 4; i++)
            {
                if (childSceneNodes[i].Count > 0)
                {
                    QuadtreeNode tempNode = new QuadtreeNode(childSceneNodes[i], depth, maxDepth);
                    childNodes.Add(tempNode);
                }
            }
        }
Пример #2
0
 public void BuildTree(int maxDepth)
 {
     rootNode    = new QuadtreeNode(nodes, 0, maxDepth);
     boundingBox = rootNode.GetBoundingBox();
 }