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 0a8a7e4da11b6cfa5af2cd84c6b17c1ee2493440 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:19:37 2026 +0100 feat(Math): add Affine2D scaleAt, rotateAt and translate --- .../main/org/apache/sis/maths/Affine2D.java | 45 +++++++++++++ .../test/org/apache/sis/maths/Affine2DTest.java | 73 ++++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine2D.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine2D.java index 158d606fa1..40a49274b8 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine2D.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine2D.java @@ -207,6 +207,51 @@ public final class Affine2D extends AbstractAffine<Affine2D> { return this; } + /** + * Scales the output of this transform around the given position. + * The scale is applied after this transform, in its output space, + * as when a view is zoomed around the mouse position. + * + * @param scale the scale factor. + * @param center the position which stays fixed, in output coordinates. + * @return this affine. + */ + public Affine2D scaleAt(final double scale, final Vector<?> center) { + final double x = center.get(0); + final double y = center.get(1); + return set(new Affine2D(scale, 0, x - scale * x, 0, scale, y - scale * y).multiply(this)); + } + + /** + * Rotates the output of this transform around the given position. + * The rotation is applied after this transform, in its output space. + * + * @param angle rotation angle in radians, from the first axis toward the second axis. + * @param center the position which stays fixed, in output coordinates. + * @return this affine. + */ + public Affine2D rotateAt(final double angle, final Vector<?> center) { + final double cos = Math.cos(angle); + final double sin = Math.sin(angle); + final double x = center.get(0); + final double y = center.get(1); + return set(new Affine2D(cos, -sin, x - cos * x + sin * y, + sin, cos, y - sin * x - cos * y).multiply(this)); + } + + /** + * Translates the output of this transform. + * The translation is applied after this transform, in its output space. + * + * @param translation the translation to add, in output coordinates. + * @return this affine. + */ + public Affine2D translate(final Vector<?> translation) { + m02 += translation.get(0); + m12 += translation.get(1); + return this; + } + @Override public Matrix3D toMatrix() { return new Matrix3D(m00, m01, m02, m10, m11, m12, 0, 0, 1); diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Affine2DTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Affine2DTest.java new file mode 100644 index 0000000000..be85853286 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/Affine2DTest.java @@ -0,0 +1,73 @@ +/* + * 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.assertSame; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link Affine2D}. + * + * @author Johann Sorel (Geomatys) + */ +public class Affine2DTest { + + private static final double TOLERANCE = 1E-12; + + private static void assertTransform(final Affine2D affine, final double x, final double y, final double ex, final double ey) { + assertArrayEquals(new double[] {ex, ey}, affine.transform(Vector.of(x, y)).toArrayDouble(), TOLERANCE); + } + + /** + * The scale keeps the given center fixed and is applied after the existing transform. + */ + @Test + public void testScaleAt() { + final Affine2D affine = new Affine2D(); + assertSame(affine, affine.scaleAt(2, Vector.of(1, 1))); + assertTransform(affine, 1, 1, 1, 1); + assertTransform(affine, 3, 1, 5, 1); + + final Affine2D translated = new Affine2D(1, 0, 10, 0, 1, 0).scaleAt(2, Vector.of(0, 0)); + assertTransform(translated, 1, 0, 22, 0); + } + + /** + * The rotation keeps the given center fixed and is applied after the existing transform. + */ + @Test + public void testRotateAt() { + final Affine2D affine = new Affine2D().rotateAt(Math.PI / 2, Vector.of(1, 0)); + assertTransform(affine, 1, 0, 1, 0); + assertTransform(affine, 2, 0, 1, 1); + + final Affine2D scaled = new Affine2D(2, 0, 0, 0, 2, 0).rotateAt(Math.PI / 2, Vector.of(0, 0)); + assertTransform(scaled, 1, 0, 0, 2); + } + + /** + * The translation is applied after the existing transform. + */ + @Test + public void testTranslate() { + final Affine2D affine = new Affine2D(2, 0, 0, 0, 2, 0).translate(Vector.of(3, 4)); + assertTransform(affine, 1, 1, 5, 6); + } +}
