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 0519183e9923a7f0b73c1a5a8685ca25ad1a41d0 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:23:06 2026 +0100 feat(Math): add Affines methods placing a box in another box --- .../main/org/apache/sis/maths/Affines.java | 106 +++++++++++++++++++++ .../test/org/apache/sis/maths/AffinesTest.java | 94 ++++++++++++++++++ 2 files changed, 200 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affines.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affines.java index bc0cfed1a1..590963fa9b 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affines.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affines.java @@ -16,8 +16,12 @@ */ package org.apache.sis.maths; +import org.opengis.geometry.Envelope; +import org.apache.sis.util.ArgumentChecks; + /** + * Affine utilities, including the affines placing a box in another box. * * @author Johann Sorel */ @@ -29,4 +33,106 @@ public final class Affines { return affine.toMatrix().transform(t.extend(0.0)).shrink(3); } + /** + * Returns the affine scaling the source box by the given factor and moving its center on the center of the target box. + * + * @param source the box to place. + * @param target the box to place it in, of the same dimension. + * @param scale the scale factor, 1 for a translation only. + * @return the affine from source to target coordinates. + */ + public static Affine<?> centered(final Envelope source, final Envelope target, final double scale) { + final int dimension = source.getDimension(); + ArgumentChecks.ensureDimensionMatches("target", dimension, target); + final Affine<?> affine = AffineND.create(dimension); + for (int i = 0; i < dimension; i++) { + affine.set(i, i, scale); + affine.set(i, dimension, target.getMedian(i) - scale * source.getMedian(i)); + } + return affine; + } + + /** + * Returns the affine fitting the whole source box inside the target box, centered. + * The scale is the same on all axes, so the source box touches the target box on one axis + * and leaves margins on the other ones. + * + * @param source the box to place. + * @param target the box to place it in, of the same dimension. + * @return the affine from source to target coordinates. + */ + public static Affine<?> fitInside(final Envelope source, final Envelope target) { + double scale = Double.POSITIVE_INFINITY; + for (int i = 0; i < source.getDimension(); i++) { + scale = Math.min(scale, target.getSpan(i) / source.getSpan(i)); + } + return centered(source, target, scale); + } + + /** + * Returns the affine scaling the source box to cover the whole target box, centered. + * The scale is the same on all axes, so the source box matches the target box on one axis + * and goes beyond it on the other ones. + * + * @param source the box to place. + * @param target the box to cover, of the same dimension. + * @return the affine from source to target coordinates. + */ + public static Affine<?> cover(final Envelope source, final Envelope target) { + double scale = 0; + for (int i = 0; i < source.getDimension(); i++) { + scale = Math.max(scale, target.getSpan(i) / source.getSpan(i)); + } + return centered(source, target, scale); + } + + /** + * Returns the affine scaling the source box to the span of the target box on the given axis, centered. + * The scale is the same on all axes. + * + * @param source the box to place. + * @param target the box to place it in, of the same dimension. + * @param axis index of the axis on which the spans shall match. + * @return the affine from source to target coordinates. + */ + public static Affine<?> fitAxis(final Envelope source, final Envelope target, final int axis) { + return centered(source, target, target.getSpan(axis) / source.getSpan(axis)); + } + + /** + * Returns the affine mapping the source box exactly on the target box. + * The scale differs on each axis, so the proportions of the source box are not kept. + * + * @param source the box to place. + * @param target the box to map it on, of the same dimension. + * @return the affine from source to target coordinates. + * + * @see org.apache.sis.referencing.operation.matrix.Matrices#createTransform(Envelope, Envelope) + */ + public static Affine<?> stretch(final Envelope source, final Envelope target) { + final int dimension = source.getDimension(); + return AffineND.create(dimension).setFromMatrix(MatrixND.create(dimension + 1, dimension + 1) + .setFromGeoapi(org.apache.sis.referencing.operation.matrix.Matrices.createTransform(source, target))); + } + + /** + * Returns the affine translating the source box inside the target box, without scale. + * On each axis, a ratio of 0 aligns the minimums of the boxes, 1 their maximums and 0.5 their centers. + * + * @param source the box to place. + * @param target the box to place it in, of the same dimension. + * @param ratios position of the source box in the target box, one value per axis. + * @return the affine from source to target coordinates. + */ + public static Affine<?> align(final Envelope source, final Envelope target, final double... ratios) { + final int dimension = source.getDimension(); + ArgumentChecks.ensureDimensionMatches("target", dimension, target); + ArgumentChecks.ensureDimensionMatches("ratios", dimension, ratios); + final Affine<?> affine = AffineND.create(dimension); + for (int i = 0; i < dimension; i++) { + affine.set(i, dimension, target.getMinimum(i) - source.getMinimum(i) + + ratios[i] * (target.getSpan(i) - source.getSpan(i))); + } + return affine; + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AffinesTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AffinesTest.java new file mode 100644 index 0000000000..81159d9c48 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AffinesTest.java @@ -0,0 +1,94 @@ +/* + * 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.assertThrows; +import org.junit.jupiter.api.Test; + + +/** + * Tests the affines of {@link Affines} placing a box in another box. + * The source box is 4 × 2 with its lower corner at the origin, + * the target box is 10 × 10 with its lower corner at the origin. + * + * @author Johann Sorel (Geomatys) + */ +public class AffinesTest { + + private static final double TOLERANCE = 1E-12; + + private static final NDRange SOURCE = new NDRange(Vector.of(0, 0), Vector.of(4, 2)); + + private static final NDRange TARGET = new NDRange(Vector.of(0, 0), Vector.of(10, 10)); + + /** + * Verifies where the lower and upper corners of {@link #SOURCE} go. + */ + private static void assertCorners(final Affine<?> affine, final double... expected) { + assertArrayEquals(new double[] {expected[0], expected[1]}, affine.transform(Vector.of(0, 0)).toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {expected[2], expected[3]}, affine.transform(Vector.of(4, 2)).toArrayDouble(), TOLERANCE); + } + + @Test + public void testCentered() { + assertCorners(Affines.centered(SOURCE, TARGET, 1), 3, 4, 7, 6); + assertCorners(Affines.centered(SOURCE, TARGET, 2), 1, 3, 9, 7); + } + + @Test + public void testFitInside() { + assertCorners(Affines.fitInside(SOURCE, TARGET), 0, 2.5, 10, 7.5); + } + + @Test + public void testCover() { + assertCorners(Affines.cover(SOURCE, TARGET), -5, 0, 15, 10); + } + + @Test + public void testFitAxis() { + assertCorners(Affines.fitAxis(SOURCE, TARGET, 0), 0, 2.5, 10, 7.5); + assertCorners(Affines.fitAxis(SOURCE, TARGET, 1), -5, 0, 15, 10); + } + + @Test + public void testStretch() { + assertCorners(Affines.stretch(SOURCE, TARGET), 0, 0, 10, 10); + } + + @Test + public void testAlign() { + assertCorners(Affines.align(SOURCE, TARGET, 0, 1), 0, 8, 4, 10); + assertCorners(Affines.align(SOURCE, TARGET, 0.5, 0.5), 3, 4, 7, 6); + assertThrows(IllegalArgumentException.class, () -> Affines.align(SOURCE, TARGET, 0.5)); + } + + /** + * Tests boxes of 5 dimensions, for which the affine has no specialized class. + */ + @Test + public void testFiveDimensions() { + final NDRange source = new NDRange(Vector.of(0, 0, 0, 0, 0), Vector.of(1, 1, 1, 1, 1)); + final NDRange target = new NDRange(Vector.of(0, 0, 0, 0, 0), Vector.of(2, 2, 2, 2, 4)); + assertArrayEquals(new double[] {2, 2, 2, 2, 4}, + Affines.stretch(source, target).transform(Vector.of(1, 1, 1, 1, 1)).toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {1.5, 1.5, 1.5, 1.5, 2.5}, + Affines.centered(source, target, 1).transform(Vector.of(1, 1, 1, 1, 1)).toArrayDouble(), TOLERANCE); + } +}
