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 e66c28200fa792d091eb576521135f2113c899de Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:54:27 2026 +0100 feat(Geometry): add circle, disk and quad factories --- .../org/apache/sis/geometries/GeometryFactory.java | 67 ++++++++++++++++++++ .../apache/sis/geometries/surface/Cylinder.java | 33 ++-------- .../apache/sis/geometries/GeometryFactoryTest.java | 74 ++++++++++++++++++++++ 3 files changed, 145 insertions(+), 29 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/GeometryFactory.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/GeometryFactory.java index 280c08c578..59ca1cbe86 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/GeometryFactory.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/GeometryFactory.java @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -357,6 +358,72 @@ public final class GeometryFactory { return new DefaultCircle(points, controlPoints, radius); } + /** + * Creates a circle of the given center and radius, in the plane of the two given unit vectors. + * The circle starts in the direction of {@code u} and turns toward {@code w}. It is made of four quarter arcs, + * since a single arc of a circle shall stay below a half turn. + * + * @param crs reference system of the circle. + * @param center center of the circle. + * @param radius radius of the circle. + * @param u unit vector toward the first point of the circle. + * @param w unit vector perpendicular to {@code u}, toward the second quarter of the circle. + * @return the circle. + */ + public Circle createCircle(CoordinateReferenceSystem crs, Vector<?> center, double radius, Vector<?> u, Vector<?> w) { + final SampleSystem system = SampleSystem.of(crs); + final List<Vector<?>> points = new ArrayList<>(5); + final List<Vector<?>> centers = new ArrayList<>(4); + for (final Vector<?> direction : new Vector<?>[] {u, w, u.scale(-1), w.scale(-1), u}) { + points.add(center.copy(DataType.DOUBLE).add(direction.scale(radius))); + } + for (int i = 0; i < 4; i++) { + centers.add(center.copy(DataType.DOUBLE)); + } + return createCircle(createDataPoints(NDArrays.of(points, system, DataType.DOUBLE)), + NDArrays.of(centers, system, DataType.DOUBLE), null); + } + + /** + * Creates a disk: the surface bounded by a {@linkplain #createCircle(CoordinateReferenceSystem, Vector, double, + * Vector, Vector) circle} of the given center and radius, in the plane of the two given unit vectors. + * The disk is counterclockwise when seen from the side of <var>u</var> × <var>w</var>. + * + * @param crs reference system of the disk. + * @param center center of the disk. + * @param radius radius of the disk. + * @param u unit vector in the plane of the disk. + * @param w unit vector in the plane of the disk, perpendicular to {@code u}. + * @return the disk. + */ + public CurvePolygon createDisk(CoordinateReferenceSystem crs, Vector<?> center, double radius, Vector<?> u, Vector<?> w) { + return createCurvePolygon(createCircle(crs, center, radius, u, w), List.of()); + } + + /** + * Creates a quadrilateral centered on the given position, whose sides are parallel to the two given half axes. + * The corners are <var>center</var> − <var>u</var> − <var>w</var>, then + <var>u</var> − <var>w</var>, + * + <var>u</var> + <var>w</var> and − <var>u</var> + <var>w</var>: the quadrilateral is counterclockwise + * when seen from the side of <var>u</var> × <var>w</var>. + * + * @param crs reference system of the quadrilateral. + * @param center center of the quadrilateral. + * @param u half of the first side, from the center to the middle of the second side. + * @param w half of the second side, from the center to the middle of the third side. + * @return the quadrilateral. + */ + public Polygon createQuad(CoordinateReferenceSystem crs, Vector<?> center, Vector<?> u, Vector<?> w) { + final Vector<?> c = center.copy(DataType.DOUBLE); + final List<Vector<?>> corners = List.of( + c.combine(1, u, -1).combine(1, w, -1), + c.combine(1, u, 1).combine(1, w, -1), + c.combine(1, u, 1).combine(1, w, 1), + c.combine(1, u, -1).combine(1, w, 1)); + final List<Vector<?>> ring = new ArrayList<>(corners); + ring.add(corners.get(0)); + return createPolygon(createLinearRing(createDataPoints(NDArrays.of(ring, SampleSystem.of(crs), DataType.DOUBLE))), List.of()); + } + /** * Creates a conic without a cross term, therefore an arc of ellipse, each arc being determined * by four data points instead of five. diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java index 88f2ceacd1..64af2df0cb 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java @@ -35,9 +35,7 @@ import org.apache.sis.geometries.SurfaceInterpolation; import org.apache.sis.geometries.curve.Circle; import org.apache.sis.geometries.internal.shared.DefaultCylinder; import org.apache.sis.maths.DataType; -import org.apache.sis.maths.NDArrays; import org.apache.sis.maths.NDRange; -import org.apache.sis.maths.SampleSystem; import org.apache.sis.maths.Vectors; import org.apache.sis.maths.Vector; import org.apache.sis.measure.Quantities; @@ -330,37 +328,14 @@ public sealed interface Cylinder extends Surface final var circles = new ArrayList<Circle>(2); final double radiusBottom = getRadiusBottom(); final double radiusTop = getRadiusTop(); - final SampleSystem ss = SampleSystem.of(crs); + final GeometryFactory factory = GeometryFactory.DEFAULT; if (radiusBottom > 0) { - circles.add(circle(ss, getDataPoints().getPosition(0), radiusBottom, u, w)); + circles.add(factory.createCircle(crs, getDataPoints().getPosition(0), radiusBottom, u, w)); } if (radiusTop > 0) { - circles.add(circle(ss, getDataPoints().getPosition(1), radiusTop, u, w.scale(-1))); - } - return GeometryFactory.DEFAULT.createMultiCurve(crs, circles.toArray(Circle[]::new)); - } - - /** - * Returns the circle of the given center and radius in the plane of the two given unit vectors, - * turning from {@code u} toward {@code w}. The circle is made of four quarter arcs, since a - * single arc of a circle shall stay below a half turn. - */ - private static Circle circle(SampleSystem system, final Vector<?> center, final double radius, - final Vector<?> u, final Vector<?> w) - { - final var points = new ArrayList<Vector<?>>(5); - final var centers = new ArrayList<Vector<?>>(4); - final Vector<?>[] directions = {u, w, u.scale(-1), w.scale(-1), u}; - for (final Vector<?> direction : directions) { - points.add(center.copy(DataType.DOUBLE).add(direction.scale(radius))); + circles.add(factory.createCircle(crs, getDataPoints().getPosition(1), radiusTop, u, w.scale(-1))); } - for (int i = 0; i < 4; i++) { - centers.add(center.copy(DataType.DOUBLE)); - } - final GeometryFactory factory = GeometryFactory.DEFAULT; - return factory.createCircle( - factory.createDataPoints(NDArrays.of(points, system, DataType.DOUBLE)), - NDArrays.of(centers, system, DataType.DOUBLE), null); + return factory.createMultiCurve(crs, circles.toArray(Circle[]::new)); } @Override diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/GeometryFactoryTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/GeometryFactoryTest.java new file mode 100644 index 0000000000..8c17095b11 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/GeometryFactoryTest.java @@ -0,0 +1,74 @@ +/* + * 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.geometries; + +import org.opengis.geometry.Envelope; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.apache.sis.geometries.curve.Circle; +import org.apache.sis.geometries.surface.CurvePolygon; +import org.apache.sis.geometries.surface.Polygon; +import org.apache.sis.maths.Vector; + +// 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.assertInstanceOf; +import org.junit.jupiter.api.Test; + + +/** + * Tests the shape creation methods of {@link GeometryFactory}. + * + * @author Johann Sorel (Geomatys) + */ +public class GeometryFactoryTest extends GeometryTest { + + /** + * Tolerance on the areas computed on a linear approximation of the curves. + */ + private static final double CURVE_TOLERANCE = 1E-3; + + private static final GeometryFactory F = GeometryFactory.DEFAULT; + + private static final CoordinateReferenceSystem CRS2 = Geometries.getUndefinedCRS(2); + + private static void assertEnvelope(final Envelope envelope, final double... bounds) { + assertArrayEquals(new double[] {bounds[0], bounds[1]}, envelope.getLowerCorner().getCoordinates(), CURVE_TOLERANCE); + assertArrayEquals(new double[] {bounds[2], bounds[3]}, envelope.getUpperCorner().getCoordinates(), CURVE_TOLERANCE); + } + + /** + * The quadrilateral turns counterclockwise from the first half axis toward the second one. + */ + @Test + public void testCreateQuad() { + final Polygon quad = F.createQuad(Geometries.getUndefinedCRS(3), Vector.of(1, 2, 3), Vector.of(1, 0, 0), Vector.of(0, 0, 2)); + assertArrayEquals(new double[] {0,2,1, 2,2,1, 2,2,5, 0,2,5, 0,2,1}, + quad.getExteriorRing().getDataPoints().getAttributeArray(DataPointsType.ATT_POSITION).toArrayDouble(), TOLERANCE); + } + + /** + * A disk of radius 2 centered on (1 1) is bounded by a circle. + */ + @Test + public void testCreateDisk() { + final CurvePolygon disk = F.createDisk(CRS2, Vector.of(1, 1), 2, Vector.of(1, 0), Vector.of(0, 1)); + assertInstanceOf(Circle.class, disk.getExteriorRing()); + assertEquals(4 * Math.PI, disk.getArea().getValue().doubleValue(), CURVE_TOLERANCE); + assertEnvelope(disk.getEnvelope(), -1, -1, 3, 3); + } +}
