Ejemplo n.º 1
0
        private void  balanceSegment(Photon[] pbal, Photon[] porg, int index, int start, int end)
        {
            // compute new median
            int median = 1;

            while ((4 * median) <= (end - start + 1))
            {
                median += median;
            }

            if ((3 * median) <= (end - start + 1))
            {
                median += median;
                median += start - 1;
            }
            else
            {
                median = end - median + 1;
            }

            // find axis to split along
            short axis = 2;

            if ((bboxMax.x() - bboxMin.x()) > (bboxMax.y() - bboxMin.y()) && (bboxMax.x() - bboxMin.x()) > (bboxMax.z() - bboxMin.z()))
            {
                axis = 0;
            }
            else if ((bboxMax.y() - bboxMin.y()) > (bboxMax.z() - bboxMin.z()))
            {
                axis = 1;
            }

            // partition photon block around the median

            medianSplit(porg, start, end, median, axis);

            pbal[index]       = porg[median];
            pbal[index].plane = axis;

            // recursively balance the left and right block

            if (median > start)
            {
                // balance left segment
                if (start < (median - 1))
                {
                    double temp = bboxMax.get(axis);

                    bboxMax.set(axis, pbal[index].position.get(axis));
                    balanceSegment(pbal, porg, (2 * index), start, (median - 1));
                    bboxMax.set(axis, temp);
                }
                else
                {
                    pbal[2 * index] = porg[start];
                }
            }

            if (median < end)
            {
                // balance right segment
                if ((median + 1) < end)
                {
                    double temp = bboxMin.get(axis);

                    bboxMin.set(axis, pbal[index].position.get(axis));
                    balanceSegment(pbal, porg, (2 * index) + 1, (median + 1), end);
                    bboxMin.set(axis, temp);
                }
                else
                {
                    pbal[(2 * index) + 1] = porg[end];
                }
            }
        }