This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 1260cabe5037bebefd81c28b6f5d502a06d5a016 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:22:07 2026 +0100 fix(Math): make AffineND usable --- .../main/org/apache/sis/maths/AffineND.java | 38 ++++++++++--- .../test/org/apache/sis/maths/AffineNDTest.java | 64 ++++++++++++++++++++++ 2 files changed, 95 insertions(+), 7 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AffineND.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AffineND.java index e1725f4c9e..08597e9d34 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AffineND.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AffineND.java @@ -42,18 +42,26 @@ public final class AffineND extends AbstractAffine<AffineND> { return cp; } + /** + * Creates an identity affine of the given dimension. + * + * @param dimension number of input and output dimensions. + */ public AffineND(int dimension) { super(dimension); this.dim = dimension; - this.values = new double[dimension][dimension]; + this.values = new double[dimension][dimension + 1]; + for (int i = 0; i < dimension; i++) { + values[i][i] = 1; + } } @Override public Matrix<?> toMatrix() { - final Matrix<?> buffer = MatrixND.create(dim+1, +1); - for (int y=0;y<dim;y++) { - for (int x=0;x<=dim;x++) { - buffer.set(y, x, values[y][x]); + final Matrix<?> buffer = MatrixND.create(dim + 1, dim + 1); + for (int y = 0; y <= dim; y++) { + for (int x = 0; x <= dim; x++) { + buffer.set(y, x, (y < dim) ? values[y][x] : (x == dim ? 1 : 0)); } } return buffer; @@ -82,12 +90,28 @@ public final class AffineND extends AbstractAffine<AffineND> { @Override protected void transform1(double[] source, int sourceOffset, double[] dest, int destOffset) { - throw new UnsupportedOperationException("Not supported yet."); + final double[] result = new double[dim]; + for (int y = 0; y < dim; y++) { + double sum = values[y][dim]; + for (int x = 0; x < dim; x++) { + sum += values[y][x] * source[sourceOffset + x]; + } + result[y] = sum; + } + System.arraycopy(result, 0, dest, destOffset, dim); } @Override protected void transform1(float[] source, int sourceOffset, float[] dest, int destOffset) { - throw new UnsupportedOperationException("Not supported yet."); + final float[] result = new float[dim]; + for (int y = 0; y < dim; y++) { + double sum = values[y][dim]; + for (int x = 0; x < dim; x++) { + sum += values[y][x] * source[sourceOffset + x]; + } + result[y] = (float) sum; + } + System.arraycopy(result, 0, dest, destOffset, dim); } @Override diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AffineNDTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AffineNDTest.java new file mode 100644 index 0000000000..caba0d3daa --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AffineNDTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.maths; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link AffineND}, the affine of more than 4 dimensions. + * + * @author Johann Sorel (Geomatys) + */ +public class AffineNDTest { + + /** + * A new affine is the identity. + */ + @Test + public void testIdentity() { + final AffineND affine = new AffineND(5); + assertTrue(affine.isIdentity()); + assertArrayEquals(new double[] {1, 2, 3, 4, 5}, affine.transform(Vector.of(1, 2, 3, 4, 5)).toArrayDouble()); + } + + /** + * Tests the transform of a position with a scale and a translation. + */ + @Test + public void testTransform() { + final AffineND affine = new AffineND(5).set(0, 0, 2).set(4, 5, 10); + assertArrayEquals(new double[] {2, 2, 3, 4, 15}, affine.transform(Vector.of(1, 2, 3, 4, 5)).toArrayDouble()); + assertArrayEquals(new float[] {2, 2, 3, 4, 15}, affine.transform(new float[] {1, 2, 3, 4, 5}, null)); + } + + /** + * The matrix of an affine of dimension 5 is a square matrix of size 6 ending with the row (0 0 0 0 0 1). + */ + @Test + public void testToMatrix() { + final Matrix<?> matrix = new AffineND(5).set(4, 5, 10).toMatrix(); + assertEquals(6, matrix.getNumRow()); + assertEquals(6, matrix.getNumCol()); + assertEquals(10, matrix.get(4, 5)); + assertArrayEquals(new double[] {0, 0, 0, 0, 0, 1}, matrix.getRow(5)); + } +}
