Beispiel #1
0
        private static void BuildTopDown(BVHNode node, Action <string> logger)
        {
            int iterations = 0;

            var queue = new Queue <BVHNode>();

            queue.Enqueue(node);

            BVHNode current = null;

            while (queue.Count > 0)
            {
                ++iterations;

                current = queue.Dequeue();

                if (current.Items.Length > MinPartitionSize)
                {
                    var boundingSphere = current.BoundingSphere;
                    var splitOrder     = GetSplitOrder(boundingSphere.Center);

                    foreach (var split_dim in splitOrder)
                    {
                        var leftSpheres  = new List <Sphere>();
                        var rightSpheres = new List <Sphere>();

                        var split_coord = 0.5f * ValueAt(boundingSphere.Center, split_dim);

                        foreach (var sphere in current.Items)
                        {
                            if (ValueAt(sphere.Center, split_dim) < split_coord)
                            {
                                leftSpheres.Add(sphere);
                            }
                            else
                            {
                                rightSpheres.Add(sphere);
                            }
                        }

                        var split_success
                            = leftSpheres.Count > 0 &&
                              rightSpheres.Count > 0;

                        if (split_success)
                        {
                            current.Left = new BVHNode(leftSpheres.ToArray());
                            queue.Enqueue(current.Left);

                            current.Right = new BVHNode(rightSpheres.ToArray());
                            queue.Enqueue(current.Right);

                            current.ClearItems();

                            break;
                        }
                    }
                }
            }

            logger($"BVH constructed in '{iterations}' iterations.");
        }