0

I'm trying to creat an axis aligned bounding box (aabb) and would like to get the min and max values of all my vertice coordinates.

I'm reading from an obj file to get the vertex coordinates, the print out would be a list of x,y,z coordinates in float numbers.

float xVertices;
float yVertices;
float zVertices;

private void getObjVertices(String fileName)
{       
    BufferedReader meshReader = null;

    try
    {
        meshReader = new BufferedReader(new FileReader(fileName));
        String line;

        while((line = meshReader.readLine()) != null)
        {
            String[] tokens = line.split(" ");
            tokens = Util.RemoveEmptyStrings(tokens);

            if(tokens.length == 0 || tokens[0].equals("#") || 
                    tokens[0].equals("vt") || tokens[0].equals("vn") || tokens[0].equals("f"))
                continue;
            else if(tokens[0].equals("v"))
            {                                               
                xVertices = Float.parseFloat(tokens[1]);
                yVertices = Float.parseFloat(tokens[2]);
                zVertices =  Float.parseFloat(tokens[3]);

                System.out.println("xVertices:" + xVertices);
                System.out.println("yVertices:" + yVertices);
                System.out.println("zVertices:" + zVertices);

        // get min/max x,y,z values, calculatre width, height, depth
            }
        }           
        meshReader.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.exit(1);
    }
}

What I would like to achieve is to get all xVertices, yVertices, zVertices and find out which number in each axis is greates and which is smallest.

With this information I would be able to create the collider.. has someone an idea how I can calclate greates and smallest numbers in my code?

Thanks for any help in advance!

2
  • Why not just maintain a record of the greatest and smallest number? and keeping updating them as you are reading new vertices? Commented Oct 18, 2015 at 0:51
  • oh yea, that's an interesting idea, funny it didn't occur to me.. would you have a concrete suggestion? Commented Oct 18, 2015 at 0:53

1 Answer 1

2

You can maintain records of the greatest and smallest number and updating them as your program is reading the vertices. Below is an example.

float xMin, xMax, yMin, yMax, zMin, zMax;
    xMin = yMin = zMin = Float.MAX_VALUE;
    xMax = yMax = zMax = Float.MIN_VALUE;

    while ((line = meshReader.readLine()) != null) {
        String[] tokens = line.split(" ");
        tokens = Util.RemoveEmptyStrings(tokens);

        if (tokens.length == 0 || tokens[0].equals("#") ||
                tokens[0].equals("vt") || tokens[0].equals("vn") || tokens[0].equals("f"))
            continue;
        else if (tokens[0].equals("v")) {
            xVertices = Float.parseFloat(tokens[1]);
            yVertices = Float.parseFloat(tokens[2]);
            zVertices = Float.parseFloat(tokens[3]);

            if (xMin > xVertices) xMin = xVertices;
            if (yMin > yVertices) yMin = yVertices;
            if (zMin > zVertices) zMin = zVertices;

            if (xMax < xVertices) xMax = xVertices;
            if (yMax < yVertices) yMax = yVertices;
            if (zMax < zVertices) zMax = zVertices;

            System.out.println("xVertices:" + xVertices);
            System.out.println("yVertices:" + yVertices);
            System.out.println("zVertices:" + zVertices);

            // get min/max x,y,z values, calculatre width, height, depth
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.