0

I really cant find any resource to know how to compute the bounding box of a set of points.

I have a float/int array of points for which I wish to compute the bounding box ( I want to know all the four corners of the rectangle of the bounding box ). How do I accomplish this?

Krythic
  • 4,184
  • 5
  • 26
  • 67
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47

3 Answers3

1

You could loop through the array:

int minX = Integer.MAX_VALUE, minY, maxX, maxY = Integer.MAX_VALUE;

for (int i=0;i<myArray.length;i++){
    if (myArray[i].x > maxX){
        maxX = myArray[i].x;
    } else if (myArray[i].x < minX) {
        minX = myArray[i].x;
    } else if (myArray[i].y > maxY){
        maxY = myArray[i].y;
    } else (myArray[i].y < minY) {
        minY = myArray[i].y;(
    }
}

You didn't say what kind of list you are using (array of points or whatever) so you'll need to adjust myArray[i].y and maxY = Integer.MAX_VALUE as required.

Simon
  • 14,407
  • 8
  • 46
  • 61
1

Computing AABB (Axis aligned bounding box) is fairly trivial. Just sort the points in each axis, find the min max on each axis. The intersection of the 4 lines from these points is your AAB rectangle.

Computing OBB (oriented bounding box) is slightly non trivial. Luckily there is a method on GestureUtils that does exactly that, namely :

GestureUtils.computeOrientedBoundingBox(float[] points)

pass it your float array of points and life is good :)

numan salati
  • 19,394
  • 9
  • 63
  • 66
0

Although you didn't really specify what sort of points you were referring to, this bit of code should still work. I use it for creating a bounding box around the vertices of a model. It should also be noted that rotation should happen separate to loading the vertices. I'm actually fairly certain you can't detect rotation unless it's explicitly stated, or serialized with the vertex data. Also, AABB's and OBB's are technically—from a mathematical perspective—the same thing, as I proved here: https://stackoverflow.com/a/63094985/3214889. So even though your question specifically states Oriented Bounding Box, the following code will work for both. However, you will need to rotate the box afterwards; unless you serialize the rotation somehow.

        public void FromVertices(Vertex[] vertices)
        {
            // Calculate Bounding Box
            float minX = float.PositiveInfinity;
            float maxX = float.NegativeInfinity;
            float minY = float.PositiveInfinity;
            float maxY = float.NegativeInfinity;
            float minZ = float.PositiveInfinity;
            float maxZ = float.NegativeInfinity;
            for (int i = 0; i < vertices.Length; i++)
            {
                Vector3 vertex = vertices[i].Location;
                // Check for maximum
                if (vertex.X > maxX)
                {
                    maxX = vertex.X;
                }
                if (vertex.Y > maxY)
                {
                    maxY = vertex.Y;
                }
                if (vertex.Z > maxZ)
                {
                    maxZ = vertex.Z;
                }
                // Check for Minimum
                if (vertex.X < minX)
                {
                    minX = vertex.X;
                }
                if (vertex.Y < minY)
                {
                    minY = vertex.Y;
                }
                if (vertex.Z < minZ)
                {
                    minZ = vertex.Z;
                }
            }
            this.Minimum = new Vector3(minX, minY, minZ);
            this.Maximum = new Vector3(maxX, maxY, maxZ);
        }
Krythic
  • 4,184
  • 5
  • 26
  • 67