calculates bounds of array of 3D vertices stored in flat array - Java java.lang

Java examples for java.lang:Math Array Function

Description

calculates bounds of array of 3D vertices stored in flat array

Demo Code

/*****************************************************************************
 *                        Shapeways, Inc Copyright (c) 2011
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 ****************************************************************************/
import javax.vecmath.Vector3d;
import javax.vecmath.Vector4d;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Quat4d;
import javax.vecmath.Matrix3d;
import javax.vecmath.SingularMatrixException;
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import static java.lang.Math.max;
import static abfab3d.core.Output.fmt;

public class Main{
    /**/* w w  w .java2s. co  m*/
       calculates bounds of array of 3D vertices stored in flat array 
     */
    public static double[] calculateBounds(double vertices[]) {
        double bounds[] = new double[] { Double.MAX_VALUE,
                -Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE,
                Double.MAX_VALUE, -Double.MAX_VALUE };
        int vcount = vertices.length / 3;

        for (int i = 0; i < vcount; i++) {
            int ind = i * 3;
            for (int k = 0; k < 3; k++) {
                double v = vertices[ind + k];
                if (v < bounds[2 * k])
                    bounds[2 * k] = v;
                if (v > bounds[2 * k + 1])
                    bounds[2 * k + 1] = v;
            }
        }
        return bounds;
    }
}

Related Tutorials