sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1521927 - in /sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix: Matrices.java Matrix1.java MatrixSIS.java
Date Wed, 11 Sep 2013 16:46:55 GMT
Author: desruisseaux
Date: Wed Sep 11 16:46:55 2013
New Revision: 1521927

URL: http://svn.apache.org/r1521927
Log:
Ported the factory methods.

Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java?rev=1521927&r1=1521926&r2=1521927&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
[UTF-8] Wed Sep 11 16:46:55 2013
@@ -17,6 +17,7 @@
 package org.apache.sis.referencing.operation.matrix;
 
 import org.opengis.referencing.operation.Matrix;
+import org.opengis.referencing.operation.MathTransform;
 import org.apache.sis.util.Static;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.ComparisonMode;
@@ -48,6 +49,102 @@ public final class Matrices extends Stat
     }
 
     /**
+     * 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.
+     *
+     * <p>For an affine transform, the {@code size} is the number of
+     * {@linkplain MathTransform#getSourceDimensions() source} and
+     * {@linkplain MathTransform#getTargetDimensions() target} dimensions + 1.</p>
+     *
+     * @param size Numbers of row and columns.
+     * @return An identity matrix of the given size.
+     */
+    public static MatrixSIS create(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();
+        }
+        return new GeneralMatrix(size, size);
+    }
+
+    /**
+     * Creates an identity matrix of size {@code numRow} × {@code numCol}.
+     * Elements on the diagonal (<var>j</var> == <var>i</var>) are
set to 1.
+     *
+     * @param numRow For an affine transform, this is the number of {@linkplain MathTransform#getTargetDimensions()
target dimensions} + 1.
+     * @param numCol For an affine transform, this is the number of {@linkplain MathTransform#getSourceDimensions()
source dimensions} + 1.
+     * @return An identity matrix of the given size.
+     */
+    public static MatrixSIS create(final int numRow, final int numCol) {
+        if (numRow == numCol) {
+            return create(numRow);
+        } else {
+            return new GeneralMatrix(numRow, numCol);
+        }
+    }
+
+    /**
+     * Creates a matrix of size {@code numRow} × {@code numCol} initialized to the given
elements.
+     * The elements array size must be equals to {@code numRow*numCol}. Column indices vary
fastest.
+     *
+     * @param  numRow   Number of rows.
+     * @param  numCol   Number of columns.
+     * @param  elements The matrix elements in a row-major array. Column indices vary fastest.
+     * @return A matrix initialized to the given elements.
+     *
+     * @see MatrixSIS#setElements(double[])
+     */
+    public static MatrixSIS create(final int numRow, final int numCol, final double[] elements)
{
+        if (numRow == numCol) switch (numRow) {
+            case 1: return new Matrix1(elements);
+            case 2: return new Matrix2(elements);
+            case 3: return new Matrix3(elements);
+            case 4: return new Matrix4(elements);
+        }
+        return new GeneralMatrix(numRow, numCol, elements);
+    }
+
+    /**
+     * Creates a new matrix which is a copy of the given matrix.
+     *
+     * @param matrix The matrix to copy, or {@code null}.
+     * @return A copy of the given matrix, or {@code null} if the given matrix was null.
+     */
+    public static MatrixSIS copy(final Matrix matrix) {
+        if (matrix == null) {
+            return null;
+        }
+        final int size = matrix.getNumRow();
+        if (size == matrix.getNumCol()) {
+            switch (size) {
+                case 1: return new Matrix1(matrix);
+                case 2: return new Matrix2(matrix);
+                case 3: return new Matrix3(matrix);
+                case 4: return new Matrix4(matrix);
+            }
+        }
+        return new GeneralMatrix(matrix);
+    }
+
+    /**
+     * Casts or copies the given matrix to a SIS implementation. If {@code matrix} is already
+     * an instance of {@code MatrixSIS}, then it is returned unchanged. Otherwise all elements
+     * are copied in a new {@code MatrixSIS} 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.
+     */
+    public static MatrixSIS castOrCopy(final Matrix matrix) {
+        if (matrix instanceof MatrixSIS) {
+            return (MatrixSIS) matrix;
+        }
+        return copy(matrix);
+    }
+
+    /**
      * Returns {@code true} if the given matrix is close to an identity matrix, given a tolerance
threshold.
      * This method is equivalent to computing the difference between the given matrix and
an identity matrix
      * of identical size, and returning {@code true} if and only if all differences are smaller
than or equal

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java?rev=1521927&r1=1521926&r2=1521927&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
[UTF-8] Wed Sep 11 16:46:55 2013
@@ -73,6 +73,17 @@ public final class Matrix1 extends Matri
     }
 
     /**
+     * Creates a new matrix initialized to the specified values.
+     * The length of the given array must be 1.
+     *
+     * @param elements Elements of the matrix.
+     * @throws IllegalArgumentException If the given array does not have the expected length.
+     */
+    public Matrix1(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}.

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java?rev=1521927&r1=1521926&r2=1521927&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
(original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
Wed Sep 11 16:46:55 2013
@@ -103,6 +103,8 @@ public abstract class MatrixSIS implemen
      *
      * @param elements The new matrix elements in a row-major array.
      * @throws IllegalArgumentException If the given array does not have the expected length.
+     *
+     * @see Matrices#create(int, int, double[])
      */
     public abstract void setElements(final double[] elements);
 



Mime
View raw message