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 c97816a0cb4e0a33478deddf30a37fa78ca7ce58 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:49:57 2026 +0100 feat(Geometry): add Sphere.toIcosphere --- .../org/apache/sis/geometries/surface/Sphere.java | 90 ++++++++++++++++++++++ .../apache/sis/geometries/surface/SphereTest.java | 32 ++++++++ 2 files changed, 122 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java index e1f439cfa1..a9bbf705e1 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java @@ -16,12 +16,16 @@ */ package org.apache.sis.geometries.surface; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.measure.Quantity; import static org.opengis.annotation.Specification.ISO_19107; import org.opengis.annotation.UML; import org.opengis.geometry.Envelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.apache.sis.geometries.DataPoints; import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.Geometry; @@ -31,10 +35,17 @@ import org.apache.sis.geometries.Point; import org.apache.sis.geometries.Representation; import org.apache.sis.geometries.SurfaceInterpolation; import org.apache.sis.geometries.internal.shared.DefaultSphere; +import org.apache.sis.geometries.mesh.MeshPrimitive; +import org.apache.sis.geometries.solid.polyhedron.Icosahedron; +import org.apache.sis.geometries.spherical.GreatCircleArc; +import org.apache.sis.maths.DataType; import org.apache.sis.maths.Maths; +import org.apache.sis.maths.NDArrays; import org.apache.sis.maths.NDRange; +import org.apache.sis.maths.SampleSystem; import org.apache.sis.maths.Vector; import org.apache.sis.measure.Quantities; +import org.apache.sis.util.ArgumentChecks; /** @@ -171,6 +182,85 @@ public sealed interface Sphere extends ParametricCurveSurface return position.distance(getCenter()) - getRadius(); } + /** + * Returns a triangle mesh of this sphere made by subdividing an icosahedron, with the normals of the vertices. + * Each subdivision splits every triangle in four, its new corners being moved on the sphere. + * The mesh has 20⋅4<sup><var>n</var></sup> triangles for <var>n</var> subdivisions. + * + * <ul> + * <li>The triangles are counterclockwise when seen from outside the sphere.</li> + * <li>The vertices are shared by the triangles, contrarily to the {@linkplain #tessellate tessellation} + * of the parametric form which has more vertices near the poles.</li> + * </ul> + * + * @param subdivisions number of subdivisions, 0 for the icosahedron. + * @return the triangles of the icosphere. + * @throws UnsupportedOperationException if this sphere is not 3-dimensional. + * + * @see Icosahedron + */ + default MeshPrimitive.Triangles toIcosphere(final int subdivisions) { + ArgumentChecks.ensurePositive("subdivisions", subdivisions); + if (getDimension() != 3) { + throw new UnsupportedOperationException("The icosphere is computed in 3 dimensions only."); + } + // Unit vectors of the vertices, and indices of the triangle corners. + final List<Vector<?>> vertices = new ArrayList<>(); + List<int[]> triangles = new ArrayList<>(); + final Icosahedron icosahedron = new Icosahedron(); + for (int f = 0; f < icosahedron.getFaceCount(); f++) { + final DataPoints corners = icosahedron.getFace(f).getExteriorRing().getDataPoints(); + final int[] triangle = new int[3]; + for (int k = 0; k < 3; k++) { + final Vector<?> corner = corners.getPosition(k); + int index = vertices.indexOf(corner); + if (index < 0) { + index = vertices.size(); + vertices.add(corner); + } + triangle[k] = index; + } + triangles.add(triangle); + } + for (int level = 0; level < subdivisions; level++) { + // Middle of each edge, shared by the two triangles of the edge. + final Map<Long, Integer> middles = new HashMap<>(); + final List<int[]> split = new ArrayList<>(triangles.size() * 4); + for (final int[] t : triangles) { + final int[] m = new int[3]; + for (int k = 0; k < 3; k++) { + final int a = t[k], b = t[(k + 1) % 3]; + m[k] = middles.computeIfAbsent(Math.min(a, b) * (long) Integer.MAX_VALUE + Math.max(a, b), (key) -> { + vertices.add(GreatCircleArc.interpolate(vertices.get(a), vertices.get(b), 0.5)); + return vertices.size() - 1; + }); + } + split.add(new int[] {t[0], m[0], m[2]}); + split.add(new int[] {t[1], m[1], m[0]}); + split.add(new int[] {t[2], m[2], m[1]}); + split.add(m); + } + triangles = split; + } + final Vector<?> center = getCenter(); + final double radius = getRadius(); + final List<Vector<?>> positions = new ArrayList<>(vertices.size()); + for (final Vector<?> vertex : vertices) { + positions.add(center.copy(DataType.DOUBLE).combine(1, vertex, radius)); + } + final List<Integer> index = new ArrayList<>(triangles.size() * 3); + for (final int[] t : triangles) { + for (final int corner : t) { + index.add(corner); + } + } + final MeshPrimitive.Triangles mesh = new MeshPrimitive.Triangles(); + mesh.setPositions(NDArrays.of(positions, SampleSystem.of(getCoordinateReferenceSystem()), DataType.DOUBLE)); + mesh.setNormals(NDArrays.of(vertices, SampleSystem.ofSize(3), DataType.FLOAT)); + mesh.setIndex(NDArrays.ofUnsigned(1, index)); + return mesh; + } + /** * Returns the {@linkplain #getCenter() center} of this sphere, with the attributes of its data point. * diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java index c4a334dd09..403dd063c2 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java @@ -26,6 +26,7 @@ import org.apache.sis.geometries.GeometryType; import org.apache.sis.geometries.Point; import org.apache.sis.geometries.curve.Arc; import org.apache.sis.geometries.curve.Circle; +import org.apache.sis.geometries.mesh.MeshPrimitive; import org.apache.sis.maths.Vector; // Test dependencies @@ -173,4 +174,35 @@ public class SphereTest extends GeometryTest { assertEquals( 3, sphere().signedDistance(Vector.of(1, 2, 8)), TOLERANCE); assertEquals(-2, sphere().signedDistance(Vector.of(1, 2, 3)), TOLERANCE); } + + /** + * Tests {@link Sphere#toIcosphere(int)}: the number of vertices and triangles of each subdivision level, + * the vertices on the sphere and the triangles counterclockwise when seen from outside. + */ + @Test + public void testToIcosphere() { + final int[] vertexCounts = {12, 42, 162}; + for (int level = 0; level < vertexCounts.length; level++) { + final MeshPrimitive mesh = sphere().toIcosphere(level); + final double[] xyz = mesh.getPositions().toArrayDouble(); + final int[] index = mesh.getIndex().toArrayInt(); + assertEquals(vertexCounts[level] * 3, xyz.length, "vertices"); + assertEquals(20 * (1 << (2 * level)) * 3, index.length, "triangles"); + for (int v = 0; v < xyz.length; v += 3) { + assertEquals(2, Math.sqrt(sq(xyz[v] - 1) + sq(xyz[v+1] - 2) + sq(xyz[v+2] - 3)), TOLERANCE, "Vertex not on the sphere."); + } + for (int t = 0; t < index.length; t += 3) { + final int a = index[t] * 3, b = index[t+1] * 3, c = index[t+2] * 3; + final double ux = xyz[b] - xyz[a], uy = xyz[b+1] - xyz[a+1], uz = xyz[b+2] - xyz[a+2]; + final double vx = xyz[c] - xyz[a], vy = xyz[c+1] - xyz[a+1], vz = xyz[c+2] - xyz[a+2]; + final double outward = (uy*vz - uz*vy) * (xyz[a] - 1) + (uz*vx - ux*vz) * (xyz[a+1] - 2) + (ux*vy - uy*vx) * (xyz[a+2] - 3); + assertTrue(outward > 0, "Triangle not counterclockwise from outside."); + } + } + assertThrows(IllegalArgumentException.class, () -> sphere().toIcosphere(-1)); + } + + private static double sq(final double x) { + return x * x; + } }
