/** * Casts or copies the given matrix to a {@code Matrix4} implementation. If the given {@code matrix} * is already an instance of {@code Matrix4}, then it is returned unchanged. Otherwise this method * verifies the matrix size, then copies all elements in a new {@code Matrix4} object. * * @param matrix the matrix to cast or copy, or {@code null}. * @return the matrix argument if it can be safely casted (including {@code null} argument), * or a copy of the given matrix otherwise. * @throws MismatchedMatrixSizeException if the size of the given matrix is not {@value #SIZE}×{@value #SIZE}. */ public static Matrix4 castOrCopy(final Matrix matrix) throws MismatchedMatrixSizeException { if (matrix == null || matrix instanceof Matrix4) { return (Matrix4) matrix; } ensureSizeMatch(SIZE, SIZE, matrix); return new Matrix4(matrix); }
/** * Returns all matrix elements in a flat, row-major (column indices vary fastest) array. * The array length is 16. * * @return {@inheritDoc} */ @Override public final double[] getElements() { final double[] elements = new double[SIZE*SIZE]; getElements(elements); return elements; }
/** * Sets all matrix elements from a flat, row-major (column indices vary fastest) array. * The array length shall be 16. */ @Override public final void setElements(final double[] elements) { ensureLengthMatch(SIZE*SIZE, elements); m00 = elements[ 0]; m01 = elements[ 1]; m02 = elements[ 2]; m03 = elements[ 3]; m10 = elements[ 4]; m11 = elements[ 5]; m12 = elements[ 6]; m13 = elements[ 7]; m20 = elements[ 8]; m21 = elements[ 9]; m22 = elements[10]; m23 = elements[11]; m30 = elements[12]; m31 = elements[13]; m32 = elements[14]; m33 = elements[15]; }
/** * Creates a dummy transform for testing purpose. * The transform has the following properties: * * <ul> * <li>The source and target dimensions are 3.</li> * <li>The transform contains 3 step.</li> * <li>The second step is a {@link PassThroughTransform}.</li> * <li>The transform in the middle (at dimension 1) is non-linear.</li> * </ul> * * @return the dummy math transform. */ public static MathTransform createConcatenateAndPassThrough() { return createConcatenateAndPassThrough(new Matrix4(), new Matrix4()); }
/** * Tests the {@link Matrix4#Matrix4(double, double, double, double, double, double, double, * double, double, double, double, double, double, double, double, double)} constructor. * This constructor is specific to the implementation class. */ @Test public void testConstructor() { initialize(-7053945420932915425L); final double[] elements = createRandomPositiveValues(SIZE * SIZE); final Matrix4 matrix = new Matrix4( elements[ 0], elements[ 1], elements[ 2], elements[ 3], elements[ 4], elements[ 5], elements[ 6], elements[ 7], elements[ 8], elements[ 9], elements[10], elements[11], elements[12], elements[13], elements[14], elements[15]); validate(matrix); assertArrayEquals(elements, matrix.getElements(), STRICT); }
final Matrix4 m1 = new Matrix4( 0.5, 0, 0, -179.5, 0, 0.25, 0, -89.5, 0, 0, 0, 1); final Matrix4 m2 = new Matrix4( 4, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1); assertMatrixEquals("Multiplication with NaN", new Matrix4( 2.0, 0, 0, -718.0, 0, 1.5, 0, -537.0, 0, 0, NaN, 39251.5, 0, 0, 0, 1), m2.multiply(m1), STRICT); assertMatrixEquals("Multiplication with NaN", new Matrix4( 2.0, 0, 0, -179.5, 0, 1.5, 0, -89.5, 0, 0, NaN, NaN, 0, 0, 0, 1), m1.multiply(m2), STRICT);
throw indexOutOfBounds(row, column);
/** * {@inheritDoc} * * @return {@inheritDoc} */ @Override public final boolean isIdentity() { return m00 == 1 && m01 == 0 && m02 == 0 && m03 == 0 && m10 == 0 && m11 == 1 && m12 == 0 && m13 == 0 && m20 == 0 && m21 == 0 && m22 == 1 && m23 == 0 && isAffine(); }
/** * Creates a new matrix initialized to the specified values. * The length of the given array must be 16 and the values in the same order than the above constructor. * * @param elements elements of the matrix. Column indices vary fastest. * @throws IllegalArgumentException if the given array does not have the expected length. * * @see #setElements(double[]) * @see Matrices#create(int, int, double[]) */ public Matrix4(final double[] elements) throws IllegalArgumentException { setElements(elements); }
/** * Creates a new matrix initialized to the same value than the specified one. * The specified matrix size must be {@value #SIZE}×{@value #SIZE}. * This is not verified by this constructor, since it shall be verified by {@link Matrices}. * * @param matrix the matrix to copy. * @throws IllegalArgumentException if the given matrix is not of the expected size. */ Matrix4(final Matrix matrix) throws IllegalArgumentException { for (int j=0; j<SIZE; j++) { for (int i=0; i<SIZE; i++) { setElement(j,i, matrix.getElement(j,i)); } } }
/** * Creates a square identity matrix of size {@code size} × {@code size}. * Elements on the diagonal (<var>j</var> == <var>i</var>) are set to 1. * * <div class="note"><b>Implementation note:</b> * For sizes between {@value org.apache.sis.referencing.operation.matrix.Matrix1#SIZE} and * {@value org.apache.sis.referencing.operation.matrix.Matrix4#SIZE} inclusive, the matrix * is guaranteed to be an instance of one of {@link Matrix1} … {@link Matrix4} subtypes.</div> * * @param size numbers of row and columns. For an affine transform matrix, this is the number of * {@linkplain MathTransform#getSourceDimensions() source} and * {@linkplain MathTransform#getTargetDimensions() target} dimensions + 1. * @return an identity matrix of the given size. */ public static MatrixSIS createIdentity(final int size) { switch (size) { case 1: return new Matrix1(); case 2: return new Matrix2(); case 3: return new Matrix3(); case 4: return new Matrix4(); default: return new GeneralMatrix(size, size, true, 1); } }
throw indexOutOfBounds(row, column);
/** * {@inheritDoc} * * @return {@inheritDoc} */ @Override public final boolean isIdentity() { return m00 == 1 && m01 == 0 && m02 == 0 && m03 == 0 && m10 == 0 && m11 == 1 && m12 == 0 && m13 == 0 && m20 == 0 && m21 == 0 && m22 == 1 && m23 == 0 && isAffine(); }
/** * Creates a new matrix initialized to the specified values. * The length of the given array must be 16 and the values in the same order than the above constructor. * * @param elements elements of the matrix. Column indices vary fastest. * @throws IllegalArgumentException if the given array does not have the expected length. * * @see #setElements(double[]) * @see Matrices#create(int, int, double[]) */ public Matrix4(final double[] elements) throws IllegalArgumentException { setElements(elements); }
/** * Creates a new matrix initialized to the same value than the specified one. * The specified matrix size must be {@value #SIZE}×{@value #SIZE}. * This is not verified by this constructor, since it shall be verified by {@link Matrices}. * * @param matrix the matrix to copy. * @throws IllegalArgumentException if the given matrix is not of the expected size. */ Matrix4(final Matrix matrix) throws IllegalArgumentException { for (int j=0; j<SIZE; j++) { for (int i=0; i<SIZE; i++) { setElement(j,i, matrix.getElement(j,i)); } } }
/** * Creates a square identity matrix of size {@code size} × {@code size}. * Elements on the diagonal (<var>j</var> == <var>i</var>) are set to 1. * * <div class="note"><b>Implementation note:</b> * For sizes between {@value org.apache.sis.referencing.operation.matrix.Matrix1#SIZE} and * {@value org.apache.sis.referencing.operation.matrix.Matrix4#SIZE} inclusive, the matrix * is guaranteed to be an instance of one of {@link Matrix1} … {@link Matrix4} subtypes.</div> * * @param size numbers of row and columns. For an affine transform matrix, this is the number of * {@linkplain MathTransform#getSourceDimensions() source} and * {@linkplain MathTransform#getTargetDimensions() target} dimensions + 1. * @return an identity matrix of the given size. */ public static MatrixSIS createIdentity(final int size) { switch (size) { case 1: return new Matrix1(); case 2: return new Matrix2(); case 3: return new Matrix3(); case 4: return new Matrix4(); default: return new GeneralMatrix(size, size, true, 1); } }
/** * Casts or copies the given matrix to a {@code Matrix4} implementation. If the given {@code matrix} * is already an instance of {@code Matrix4}, then it is returned unchanged. Otherwise this method * verifies the matrix size, then copies all elements in a new {@code Matrix4} object. * * @param matrix the matrix to cast or copy, or {@code null}. * @return the matrix argument if it can be safely casted (including {@code null} argument), * or a copy of the given matrix otherwise. * @throws MismatchedMatrixSizeException if the size of the given matrix is not {@value #SIZE}×{@value #SIZE}. */ public static Matrix4 castOrCopy(final Matrix matrix) throws MismatchedMatrixSizeException { if (matrix == null || matrix instanceof Matrix4) { return (Matrix4) matrix; } ensureSizeMatch(SIZE, SIZE, matrix); return new Matrix4(matrix); }
/** * Returns all matrix elements in a flat, row-major (column indices vary fastest) array. * The array length is 16. * * @return {@inheritDoc} */ @Override public final double[] getElements() { final double[] elements = new double[SIZE*SIZE]; getElements(elements); return elements; }