This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new 3fd95d9 Add a Matrices.createAffine(Matrix, DirectPosition) method,
to be used by ongoing work on Canvas.
3fd95d9 is described below
commit 3fd95d9fdee5af69097b35e155285d6ceb7d8ef3
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Fri Feb 7 23:45:33 2020 +0100
Add a Matrices.createAffine(Matrix, DirectPosition) method, to be used by
ongoing work on Canvas.
---
.../sis/referencing/operation/matrix/Matrices.java | 57 +++++++++++++++++++++-
.../operation/transform/MathTransforms.java | 2 +
.../referencing/operation/matrix/MatricesTest.java | 21 +++++++-
3 files changed, 78 insertions(+), 2 deletions(-)
diff --git
a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
index 2203d8c..f6c6208 100644
---
a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
+++
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
@@ -37,6 +37,7 @@ import org.apache.sis.internal.util.DoubleDouble;
import org.apache.sis.internal.referencing.AxisDirections;
import org.apache.sis.internal.referencing.Resources;
import org.apache.sis.internal.referencing.ExtendedPrecisionMatrix;
+import org.apache.sis.referencing.operation.transform.MathTransforms; //
For javadoc
/**
@@ -70,7 +71,7 @@ import
org.apache.sis.internal.referencing.ExtendedPrecisionMatrix;
* </ul>
*
* @author Martin Desruisseaux (IRD, Geomatys)
- * @version 1.0
+ * @version 1.1
*
* @see org.apache.sis.parameter.TensorParameters
*
@@ -706,6 +707,60 @@ public final class Matrices extends Static {
}
/**
+ * Creates an affine transform as the given matrix augmented by the given
translation vector and a [0 … 0 1] row.
+ * At least one of {@code derivative} and {@code translation} arguments
shall be non-null. If {@code derivative}
+ * is non-null, the returned matrix will have one more row and one more
column than {@code derivative} with all
+ * {@code derivative} values copied into the new matrix at the same (row,
column) indices. If {@code translation}
+ * is non-null, all its coordinate values are copied in the last column of
the returned matrix.
+ *
+ * <div class="note"><b>Relationship with {@link MathTransform}</b><br>
+ * When used together with {@link MathTransforms#derivativeAndTransform
MathTransforms.derivativeAndTransform(…)},
+ * the {@code derivative} argument is the derivative computed by {@code
derivativeAndTransform(…)} and the
+ * {@code translation} vector is the position computed by that method. The
result is an approximation of the
+ * transform in the vicinity of the position given to {@code
derivativeAndTransform(…)}.</div>
+ *
+ * @param derivative the scale, shear and rotation of the affine
transform.
+ * @param translation the translation vector (the last column) of the
affine transform.
+ * @return an affine transform as the given matrix augmented by the given
column and a a [0 … 0 1] row.
+ * @throws NullPointerException if {@code derivative} and {@code
translation} are both null.
+ * @throws MismatchedMatrixSizeException if {@code derivative} and {@code
translation} are both non-null and
+ * the number of {@code derivative} rows is not equal to the
number of {@code translation} dimensions.
+ *
+ * @see MathTransforms#derivativeAndTransform(MathTransform, double[],
int, double[], int)
+ *
+ * @since 1.1
+ */
+ public static MatrixSIS createAffine(final Matrix derivative, final
DirectPosition translation) {
+ final int numRow, numCol;
+ final MatrixSIS matrix;
+ if (derivative != null) {
+ numRow = derivative.getNumRow();
+ numCol = derivative.getNumCol();
+ if (translation != null) {
+ MatrixSIS.ensureNumRowMatch(translation.getDimension(),
numRow, numCol);
+ }
+ matrix = createZero(numRow + 1, numCol + 1);
+ matrix.setElement(numRow, numCol, 1);
+ for (int j=0; j<numRow; j++) {
+ for (int i=0; i<numCol; i++) {
+ matrix.setElement(j, i, derivative.getElement(j, i));
+ }
+ }
+ } else {
+ // If both arguments are null, report the first one ("derivative")
as the one that should be non-null.
+ ArgumentChecks.ensureNonNull("derivative", translation);
// Intentional mismatch (see above).
+ numRow = numCol = translation.getDimension();
+ matrix = createIdentity(numRow + 1);
+ }
+ if (translation != null) {
+ for (int j=0; j<numRow; j++) {
+ matrix.setElement(j, numCol, translation.getOrdinate(j));
+ }
+ }
+ return matrix;
+ }
+
+ /**
* Returns a matrix with the same content than the given matrix but a
different size, assuming an affine transform.
* This method can be invoked for adding or removing the
<strong>last</strong> dimensions of an affine transform.
* More specifically:
diff --git
a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
index 7379d6c..9e7287c 100644
---
a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
+++
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
@@ -671,6 +671,8 @@ public final class MathTransforms extends Static {
* @return the matrix of the transform derivative at the given source
position.
* @throws TransformException if the point can't be transformed or if a
problem occurred
* while calculating the derivative.
+ *
+ * @see Matrices#createAffine(Matrix, DirectPosition)
*/
public static Matrix derivativeAndTransform(final MathTransform transform,
final double[] srcPts, final
int srcOff,
diff --git
a/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatricesTest.java
b/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatricesTest.java
index 0c16aff..7c75c00 100644
---
a/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatricesTest.java
+++
b/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatricesTest.java
@@ -17,11 +17,13 @@
package org.apache.sis.referencing.operation.matrix;
import org.opengis.geometry.Envelope;
+import org.opengis.geometry.DirectPosition;
import org.opengis.referencing.operation.Matrix;
import org.opengis.referencing.cs.AxisDirection;
import org.apache.sis.internal.util.DoubleDouble;
import org.apache.sis.geometry.Envelope2D;
import org.apache.sis.geometry.GeneralEnvelope;
+import org.apache.sis.geometry.DirectPosition2D;
import org.apache.sis.util.iso.Types;
import org.apache.sis.util.ComparisonMode;
import org.apache.sis.test.DependsOnMethod;
@@ -38,7 +40,7 @@ import static org.opengis.referencing.cs.AxisDirection.*;
* Tests the {@link Matrices} implementation.
*
* @author Martin Desruisseaux (Geomatys)
- * @version 0.7
+ * @version 1.1
* @since 0.4
* @module
*/
@@ -364,6 +366,23 @@ public final strictfp class MatricesTest extends TestCase {
}
/**
+ * Tests {@link Matrices#createAffine(Matrix, DirectPosition)}.
+ */
+ @Test
+ public void testCreateAffine() {
+ MatrixSIS derivative = Matrices.create(2, 3, new double[] {
+ 2, 3, 8,
+ 0, 7, 5
+ });
+ DirectPosition translation = new DirectPosition2D(-3, 9);
+ assertEquals(Matrices.create(3, 4, new double[] {
+ 2, 3, 8, -3,
+ 0, 7, 5, 9,
+ 0, 0, 0, 1
+ }), Matrices.createAffine(derivative, translation));
+ }
+
+ /**
* Tests {@link Matrices#resizeAffine(Matrix, int, int)}.
*/
@Test