Ejemplo n.º 1
0
        ///
        /// <summary>
        /// Subdivide a leaf node into 8 equal volumes and distribute the objects within that node across the 8 new children
        /// </summary>
        ///
        /// <param name="node">
        /// The node to break down
        /// </param>
        ///
        private void BreakDownNode(OctreeNode node)
        {
            // We shouldn't ever call this on a node that has already been broken down but if for some reason
            // we do we should fail in a noticable manner so we can debug the problem rather than tracking
            // down the silent error based on some unpredicted side effect.
            if (node.Children != null)
            {
                throw new ArgumentException("Called Octree.BreakDownNode on a node which has already been broken down");
            }

            node.Children = new OctreeNode[8];

            BoundingBox nodeBoundingBox = _masterBoundingBoxList[node.BoundingBoxIndex];

            nodeBoundingBox.Max += node.BoundingBoxPosition;
            nodeBoundingBox.Min += node.BoundingBoxPosition;

            Vector3[] nodeCorners = nodeBoundingBox.GetCorners();
            Vector3   nodeCenter  = (nodeBoundingBox.Max + nodeBoundingBox.Min) / 2.0f;

            // shrink our bounding box by a factor of 2.  We will use the same bounding box for all 8 children
            Vector3[] points = new Vector3[2];
            points[0] = _masterBoundingBoxList[node.BoundingBoxIndex].Max / 2.0f;
            points[1] = _masterBoundingBoxList[node.BoundingBoxIndex].Min / 2.0f;
            BoundingBox newBoundingBox = BoundingBox.CreateFromPoints(points);

            int boundingBoxIndex;

            if (_masterBoundingBoxList.Contains(newBoundingBox))
            {
                boundingBoxIndex = _masterBoundingBoxList.FindIndex(x => x == newBoundingBox);
            }
            else
            {
                _masterBoundingBoxList.Add(newBoundingBox);
                boundingBoxIndex = _masterBoundingBoxList.Count - 1;
            }

            for (int i = 0; i < node.Children.Length; ++i)
            {
                Vector3 subRegionCenter = (nodeCorners[i] + nodeCenter) / 2.0f;

                node.Children[i] = new OctreeNode(boundingBoxIndex, subRegionCenter);
            }

            AddItems(node.InternalObjectPositions.ToArray(), node.InternalObjectBoundingBoxes.ToArray(), node);
            node.ClearObjects();
        }
Ejemplo n.º 2
0
        /// 
        /// <summary>
        /// Subdivide a leaf node into 8 equal volumes and distribute the objects within that node across the 8 new children
        /// </summary>
        /// 
        /// <param name="node">
        /// The node to break down
        /// </param>
        /// 
        private void BreakDownNode(OctreeNode node)
        {
            // We shouldn't ever call this on a node that has already been broken down but if for some reason
            // we do we should fail in a noticable manner so we can debug the problem rather than tracking
            // down the silent error based on some unpredicted side effect.
            if (node.Children != null)
            {
                throw new ArgumentException("Called Octree.BreakDownNode on a node which has already been broken down");
            }

            node.Children = new OctreeNode[8];

            BoundingBox nodeBoundingBox = _masterBoundingBoxList[node.BoundingBoxIndex];
            nodeBoundingBox.Max += node.BoundingBoxPosition;
            nodeBoundingBox.Min += node.BoundingBoxPosition;

            Vector3[] nodeCorners = nodeBoundingBox.GetCorners();
            Vector3 nodeCenter = (nodeBoundingBox.Max + nodeBoundingBox.Min) / 2.0f;

            // shrink our bounding box by a factor of 2.  We will use the same bounding box for all 8 children
            Vector3[] points = new Vector3[2];
            points[0] = _masterBoundingBoxList[node.BoundingBoxIndex].Max / 2.0f;
            points[1] = _masterBoundingBoxList[node.BoundingBoxIndex].Min / 2.0f;
            BoundingBox newBoundingBox = BoundingBox.CreateFromPoints(points);

            int boundingBoxIndex;
            if (_masterBoundingBoxList.Contains(newBoundingBox))
            {
                boundingBoxIndex = _masterBoundingBoxList.FindIndex(x => x == newBoundingBox);
            }
            else
            {
                _masterBoundingBoxList.Add(newBoundingBox);
                boundingBoxIndex =_masterBoundingBoxList.Count - 1;
            }

            for (int i = 0; i < node.Children.Length; ++i)
            {
                Vector3 subRegionCenter = (nodeCorners[i] + nodeCenter) / 2.0f;

                node.Children[i] = new OctreeNode(boundingBoxIndex, subRegionCenter);
            }

            AddItems(node.InternalObjectPositions.ToArray(), node.InternalObjectBoundingBoxes.ToArray(), node);
            node.ClearObjects();
        }