Example #1
0
        void FinalizeConstruction()
        {
            if (bvs.Count == 1)
            {
                // this node is leaf
                box    = bvs[0];
                isLeaf = true;
                return;
            }

            // find bounding box for bvs
            box = new kDOP24();
            foreach (kDOP24 bv in bvs)
            {
                box.Expand(bv);
            }
            double dX, dY, dZ;

            box.Dimensions(out dX, out dY, out dZ);

            // arrays where left and right portions will be stored
            List <kDOP24> left  = new List <kDOP24>(bvs.Count);
            List <kDOP24> right = new List <kDOP24>(bvs.Count);

            // identify splitting axis and split
            if (dX >= dY && dX >= dZ)
            {
                double center = box.centerX;
                foreach (kDOP24 bv in bvs)
                {
                    if (bv.centerX < center)
                    {
                        left.Add(bv);
                    }
                    else
                    {
                        right.Add(bv);
                    }
                }
                // make sure that there is at least one element on each side
                if (left.Count == 0)
                {
                    kDOP24 selected = null;
                    double min1     = double.MaxValue;
                    foreach (kDOP24 bv in right)
                    {
                        if (min1 >= bv.centerX)
                        {
                            min1 = bv.centerX; selected = bv;
                        }
                    }
                    left.Add(selected);
                    right.Remove(selected);
                }
                else if (right.Count == 0)
                {
                    kDOP24 selected = null;
                    double min1     = double.MaxValue;
                    foreach (kDOP24 bv in left)
                    {
                        if (min1 >= bv.centerX)
                        {
                            min1 = bv.centerX; selected = bv;
                        }
                    }
                    right.Add(selected);
                    left.Remove(selected);
                }
            }
            else if (dY >= dX && dY >= dZ)
            {
                double center = box.centerY;
                foreach (kDOP24 bv in bvs)
                {
                    if (bv.centerY < center)
                    {
                        left.Add(bv);
                    }
                    else
                    {
                        right.Add(bv);
                    }
                }
                if (left.Count == 0)
                {
                    kDOP24 selected = null;
                    double min1     = double.MaxValue;
                    foreach (kDOP24 bv in right)
                    {
                        if (min1 >= bv.centerY)
                        {
                            min1 = bv.centerY; selected = bv;
                        }
                    }
                    left.Add(selected);
                    right.Remove(selected);
                }
                else if (right.Count == 0)
                {
                    kDOP24 selected = null;
                    double min1     = double.MaxValue;
                    foreach (kDOP24 bv in left)
                    {
                        if (min1 >= bv.centerY)
                        {
                            min1 = bv.centerY; selected = bv;
                        }
                    }
                    right.Add(selected);
                    left.Remove(selected);
                }
            }
            else
            {
                double center = box.centerZ;
                foreach (kDOP24 bv in bvs)
                {
                    if (bv.centerZ < center)
                    {
                        left.Add(bv);
                    }
                    else
                    {
                        right.Add(bv);
                    }
                }
                if (left.Count == 0)
                {
                    kDOP24 selected = null;
                    double min1     = double.MaxValue;
                    foreach (kDOP24 bv in right)
                    {
                        if (min1 >= bv.centerZ)
                        {
                            min1 = bv.centerZ; selected = bv;
                        }
                    }
                    left.Add(selected);
                    right.Remove(selected);
                }
                else if (right.Count == 0)
                {
                    kDOP24 selected = null;
                    double min1     = double.MaxValue;
                    foreach (kDOP24 bv in left)
                    {
                        if (min1 >= bv.centerZ)
                        {
                            min1 = bv.centerZ; selected = bv;
                        }
                    }
                    right.Add(selected);
                    left.Remove(selected);
                }
            }
            left.TrimExcess();
            right.TrimExcess();

            child1 = new BVHN(this, left, level + 1);
            child2 = new BVHN(this, right, level + 1);
        }