/** * Returns this transform as an affine transform matrix. */ @Override public Matrix getMatrix() { return new Matrix2(scale, offset, 0, 1); }
/** * Returns all matrix elements in a flat, row-major (column indices vary fastest) array. * The array length is 4. * * @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 4. */ @Override public final void setElements(final double[] elements) { ensureLengthMatch(SIZE*SIZE, elements); m00 = elements[0]; m01 = elements[1]; m10 = elements[2]; m11 = elements[3]; }
/** * Casts or copies the given matrix to a {@code Matrix2} implementation. If the given {@code matrix} * is already an instance of {@code Matrix2}, then it is returned unchanged. Otherwise this method * verifies the matrix size, then copies all elements in a new {@code Matrix2} 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 Matrix2 castOrCopy(final Matrix matrix) throws MismatchedMatrixSizeException { if (matrix == null || matrix instanceof Matrix2) { return (Matrix2) matrix; } ensureSizeMatch(SIZE, SIZE, matrix); return new Matrix2(matrix); }
/** * Tests the {@link Matrix2#Matrix2(double, double, double, double)} constructor. * This constructor is specific to the implementation class. */ @Test public void testConstructor() { initialize(-8453835559080304420L); final double[] elements = createRandomPositiveValues(SIZE * SIZE); final Matrix2 matrix = new Matrix2( elements[0], elements[1], elements[2], elements[3]); validate(matrix); assertArrayEquals(elements, matrix.getElements(), STRICT); }
/** * Retrieves the value at the specified row and column of this matrix. * This method can be invoked when the matrix size or type is unknown. * If the matrix is known to be an instance of {@code Matrix2}, * then the {@link #m00} … {@link #m11} fields can be read directly for efficiency. * * @param row the row index, which can only be 0 or 1. * @param column the column index, which can only be 0 or 1. * @return the current value at the given row and column. */ @Override public final double getElement(final int row, final int column) { if (row >= 0 && row < SIZE && column >= 0 && column < SIZE) { switch (row*SIZE + column) { case 0: return m00; case 1: return m01; case 2: return m10; case 3: return m11; } } throw indexOutOfBounds(row, column); }
/** * Creates a new matrix initialized to the specified values. * The length of the given array must be 4 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 Matrix2(final double[] elements) throws IllegalArgumentException { setElements(elements); }
/** * Casts or copies the given matrix to a {@code Matrix2} implementation. If the given {@code matrix} * is already an instance of {@code Matrix2}, then it is returned unchanged. Otherwise this method * verifies the matrix size, then copies all elements in a new {@code Matrix2} 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 Matrix2 castOrCopy(final Matrix matrix) throws MismatchedMatrixSizeException { if (matrix == null || matrix instanceof Matrix2) { return (Matrix2) matrix; } ensureSizeMatch(SIZE, SIZE, matrix); return new Matrix2(matrix); }
/** * Retrieves the value at the specified row and column of this matrix. * This method can be invoked when the matrix size or type is unknown. * If the matrix is known to be an instance of {@code Matrix2}, * then the {@link #m00} … {@link #m11} fields can be read directly for efficiency. * * @param row the row index, which can only be 0 or 1. * @param column the column index, which can only be 0 or 1. * @return the current value at the given row and column. */ @Override public final double getElement(final int row, final int column) { if (row >= 0 && row < SIZE && column >= 0 && column < SIZE) { switch (row*SIZE + column) { case 0: return m00; case 1: return m01; case 2: return m10; case 3: return m11; } } throw indexOutOfBounds(row, column); }
/** * Creates a new matrix initialized to the specified values. * The length of the given array must be 4 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 Matrix2(final double[] elements) throws IllegalArgumentException { setElements(elements); }
/** * Returns this transform as an affine transform matrix. */ @Override public Matrix getMatrix() { return new Matrix2(scale, offset, 0, 1); }
/** * Returns all matrix elements in a flat, row-major (column indices vary fastest) array. * The array length is 4. * * @return {@inheritDoc} */ @Override public final double[] getElements() { final double[] elements = new double[SIZE*SIZE]; getElements(elements); return elements; }
/** * Modifies the value at the specified row and column of this matrix. * This method can be invoked when the matrix size or type is unknown. * If the matrix is known to be an instance of {@code Matrix2}, * then the {@link #m00} … {@link #m11} fields can be set directly for efficiency. * * @param row the row index, which can only be 0 or 1. * @param column the column index, which can only be 0 or 1. * @param value the new value to set at the given row and column. */ @Override public final void setElement(final int row, final int column, final double value) { if (row >= 0 && row < SIZE && column >= 0 && column < SIZE) { switch (row*SIZE + column) { case 0: m00 = value; return; case 1: m01 = value; return; case 2: m10 = value; return; case 3: m11 = value; return; } } throw indexOutOfBounds(row, column); }
/** * Sets all matrix elements from a flat, row-major (column indices vary fastest) array. * The array length shall be 4. */ @Override public final void setElements(final double[] elements) { ensureLengthMatch(SIZE*SIZE, elements); m00 = elements[0]; m01 = elements[1]; m10 = elements[2]; m11 = elements[3]; }
/** * {@inheritDoc} */ @Override public Matrix transform(final double[] srcPts, final int srcOff, final double[] dstPts, final int dstOff, final boolean derivate) throws ProjectionException { final double φ = srcPts[srcOff+1]; if (dstPts != null) { dstPts[dstOff ] = srcPts[srcOff]; dstPts[dstOff+1] = sin(φ); } return derivate ? new Matrix2(1, 0, 0, cos(φ)) : null; }
/** * Modifies the value at the specified row and column of this matrix. * This method can be invoked when the matrix size or type is unknown. * If the matrix is known to be an instance of {@code Matrix2}, * then the {@link #m00} … {@link #m11} fields can be set directly for efficiency. * * @param row the row index, which can only be 0 or 1. * @param column the column index, which can only be 0 or 1. * @param value the new value to set at the given row and column. */ @Override public final void setElement(final int row, final int column, final double value) { if (row >= 0 && row < SIZE && column >= 0 && column < SIZE) { switch (row*SIZE + column) { case 0: m00 = value; return; case 1: m01 = value; return; case 2: m10 = value; return; case 3: m11 = value; return; } } throw indexOutOfBounds(row, column); }
/** * {@inheritDoc} */ @Override public Matrix transform(final double[] srcPts, final int srcOff, final double[] dstPts, final int dstOff, final boolean derivate) throws ProjectionException { final double φ = srcPts[srcOff+1]; if (dstPts != null) { dstPts[dstOff ] = srcPts[srcOff]; dstPts[dstOff+1] = sin(φ); } return derivate ? new Matrix2(1, 0, 0, cos(φ)) : null; }
/** * Converts a single coordinate and optionally computes the derivative. */ @Override public Matrix transform(final double[] srcPts, final int srcOff, final double[] dstPts, final int dstOff, final boolean derivate) { final double r = srcPts[srcOff ]; final double θ = srcPts[srcOff+1]; final double cosθ = cos(θ); final double sinθ = sin(θ); if (dstPts != null) { dstPts[dstOff ] = r*cosθ; dstPts[dstOff+1] = r*sinθ; } if (!derivate) { return null; } return new Matrix2(cosθ, -r*sinθ, sinθ, r*cosθ); }
/** * Converts a single coordinate and optionally computes the derivative. */ @Override public Matrix transform(final double[] srcPts, final int srcOff, final double[] dstPts, final int dstOff, final boolean derivate) { final double x = srcPts[srcOff ]; final double y = srcPts[srcOff+1]; final double r = hypot(x, y); if (dstPts != null) { dstPts[dstOff ] = r; dstPts[dstOff+1] = atan2(y, x); } if (!derivate) { return null; } final double r2 = r*r; return new Matrix2(x/r, y/r, -y/r2, x/r2); }
/** * Converts a single coordinate and optionally computes the derivative. */ @Override public Matrix transform(final double[] srcPts, final int srcOff, final double[] dstPts, final int dstOff, final boolean derivate) { final double r = srcPts[srcOff ]; final double θ = srcPts[srcOff+1]; final double cosθ = cos(θ); final double sinθ = sin(θ); if (dstPts != null) { dstPts[dstOff ] = r*cosθ; dstPts[dstOff+1] = r*sinθ; } if (!derivate) { return null; } return new Matrix2(cosθ, -r*sinθ, sinθ, r*cosθ); }