abenci
abenci

Reputation: 8651

Bounding box computation

Our bounding box computation algorithm is implemented like this. Is there anything else we can do to improve speed? Or a different approach to tackle the same problem?

Thanks.

boxMin.X = boxMax.X = points[0].X;
boxMin.Y = boxMax.Y = points[0].Y;
boxMin.Z = boxMax.Z = points[0].Z;

for (int i = 1; i < points.Length; i++)
{
    UpdateMinMaxQuick(points[i], boxMin, boxMax);
}

This is the UpdateMinMaxQuick() method source code:

public static void UpdateMinMaxQuick(double x, double y, double z, Point3D min, Point3D max)
{
    if (x < min.X)

        min.X = x;

    else if (x > max.X)

        max.X = x;

    if (y < min.Y)

        min.Y = y;

    else if (y > max.Y)

        max.Y = y;

    if (z < min.Z)

        min.Z = z;

    else if (z > max.Z)

        max.Z = z;
}

Upvotes: 0

Views: 99

Answers (1)

MBo
MBo

Reputation: 80187

You cannot significantly accelerate average time, but can avoid the worst case (for raising coordinate sequence you approach uses about 2n comparisons per coordinate):

For every pair of adjacent elements compare them and check global min and max against pair min and max. This method uses 3n/2 comparisons (per coordinate) always.

 if points[2*i].X > points[2*i+1].X:
       if min.X > points[2*i+1].X:
            min.X = points[2*i+1].X 
       if max.X < points[2*i].X:
            max.X = points[2*i].X 
 else:
       if min.X > points[2*i].X:
            min.X = points[2*i].X 
       if max.X < points[2*i+1].X:
            max.X = points[2*i+1].X 

Upvotes: 1

Related Questions