package basics.math.algebra;

/**
 * Basis of an vector space.
 * This class owns the components of a vector space.
 */
public class Basis {

    public Vector[] vectors;

    public Basis(Vector[] vectors) {
        this.vectors = vectors;
    }

    public Basis(Vector a, Vector b, Vector c) {
        this.vectors = new Vector[]{a, b, c};
    }

    /**
     * Constructs an orthonormal basis using the gram-schmidt formula.
     * It will utilize the supplied up vector as the first basis vector,
     * building the other two vectors from swizzling the up vectors components.
     *
     * @param up the first vector of this basis
     * @return an orthonormal basis with up and 2 additional orthonormal vectors
     */
    public static Basis constructOrthonormalBasis(Vector up) {
        Vector n2 = Vector.diagonal(1).sub(up);
        Vector n3 = Vector.diagonal(1).sub(up.shuffle(Vector.SwizzleMask.ZXY));

        Vector w2 = n2.sub(up.project(n2)).normalize();
        Vector w3 = n3.sub(up.project(n3)).sub(w2.project(n3)).normalize();

        return new Basis(up, w2, w3);
    }
}
