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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new b2a7b2c97b feat(Geometry): add SphericalTriangle
b2a7b2c97b is described below

commit b2a7b2c97b08c886457f831d98703b41a9c7c326
Author: jsorel <[email protected]>
AuthorDate: Wed Aug 5 12:30:03 2026 +0200

    feat(Geometry): add SphericalTriangle
---
 .../sis/geometries/ellipsoidal/package-info.java   |  23 ++++
 .../geometries/spherical/SphericalTriangle.java    | 133 +++++++++++++++++++++
 .../sis/geometries/spherical/package-info.java     |  23 ++++
 .../spherical/SphericalTriangleTest.java           | 122 +++++++++++++++++++
 4 files changed, 301 insertions(+)

diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/ellipsoidal/package-info.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/ellipsoidal/package-info.java
new file mode 100644
index 0000000000..009f19b13e
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/ellipsoidal/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+/**
+ * This package contains ellipsoidal trigonometry functions and classes.
+ *
+ * @see https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid
+ */
+package org.apache.sis.geometries.ellipsoidal;
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalTriangle.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalTriangle.java
new file mode 100644
index 0000000000..e5c5232e4a
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalTriangle.java
@@ -0,0 +1,133 @@
+/*
+ * 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.spherical;
+
+import org.apache.sis.geometries.Sphere;
+import org.apache.sis.geometries.math.ReadOnly;
+
+/**
+ * A triangle on a sphere.
+ *
+ * @see https://mathworld.wolfram.com/SphericalTriangle.html
+ * @author Johann Sorel (Geomatys)
+ */
+public final class SphericalTriangle {
+
+    private final Sphere sphere;
+    private final ReadOnly.Vector<?> vecA;
+    private final ReadOnly.Vector<?> vecB;
+    private final ReadOnly.Vector<?> vecC;
+
+    /**
+     * Create a new spherical triangle on given sphere.
+     * <p>
+     * Vectors A,B,C must be in CCW order (viewed from outside the sphere)
+     *
+     * @param sphere sphere the triangle is on
+     * @param vecA first triangle point, as a unit direction vector from the 
sphere center
+     * @param vecB second triangle point, as a unit direction vector from the 
sphere center
+     * @param vecC third triangle point, as a unit direction vector from the 
sphere center
+     */
+    public SphericalTriangle(Sphere sphere,
+            ReadOnly.Vector<?> vecA,
+            ReadOnly.Vector<?> vecB,
+            ReadOnly.Vector<?> vecC) {
+        this.sphere = sphere;
+        this.vecA = vecA;
+        this.vecB = vecB;
+        this.vecC = vecC;
+    }
+
+    /**
+     * @return first triangle point, as a unit direction vector from the 
sphere center
+     */
+    public ReadOnly.Vector<?> getA() {
+        return vecA;
+    }
+
+    /**
+     * @return second triangle point, as a unit direction vector from the 
sphere center
+     */
+    public ReadOnly.Vector<?> getB() {
+        return vecB;
+    }
+
+    /**
+     * @return third triangle point, as a unit direction vector from the 
sphere center
+     */
+    public ReadOnly.Vector<?> getC() {
+        return vecC;
+    }
+
+    /**
+     * @return triangle centroid unit direction vector from the sphere center
+     */
+    public ReadOnly.Vector<?> getCentroid() {
+        return vecA.copy().add(vecB).add(vecC).normalize();
+    }
+
+    /**
+     * Test if given vector is contained in this triangle with default epsilon 
-1e-9.
+     *
+     * @param vecP vector to test
+     * @return true if vector is inside triangle
+     */
+    public boolean contains(ReadOnly.Vector<?> vecP) {
+        return contains(vecP, -1e-9);
+    }
+
+    /**
+     * Test if given vector is contained in this triangle.
+     *
+     * @param vecP vector to test
+     * @param epsilon edge tolerance, should be a negative value close to zero
+     * @return true if vector is inside triangle
+     */
+    public boolean contains(ReadOnly.Vector<?> vecP, double epsilon) {
+        return vecP.dot(vecA.cross(vecB)) >= epsilon
+            && vecP.dot(vecB.cross(vecC)) >= epsilon
+            && vecP.dot(vecC.cross(vecA)) >= epsilon;
+    }
+
+    /**
+     * Subdivide triangle in 4 triangles perfectly overlapping this triangle.
+     * The four triangles are equal in size and the 3 new vertices are at the 
middle of each segment.
+     * The first triangle is at corner A, second at corner B, third at corner 
C and fourth is in the center.
+     * The fourth triangle has the opposite direction.
+     *
+     * @return regular 4 triangle subdivision
+     */
+    public SphericalTriangle[] quadSubdivide() {
+        final ReadOnly.Vector<?> vecAB = middle(vecA, vecB);
+        final ReadOnly.Vector<?> vecBC = middle(vecB, vecC);
+        final ReadOnly.Vector<?> vecCA = middle(vecC, vecA);
+        return new SphericalTriangle[]{
+            new SphericalTriangle(sphere, vecA, vecAB, vecCA),
+            new SphericalTriangle(sphere, vecAB, vecB, vecBC),
+            new SphericalTriangle(sphere, vecCA, vecBC, vecC),
+            new SphericalTriangle(sphere, vecAB, vecBC, vecCA)
+        };
+    }
+
+    /**
+     * Get middle unit vector between two vectors.
+     */
+    private static ReadOnly.Vector<?> middle(ReadOnly.Vector<?> p, 
ReadOnly.Vector<?> q) {
+        return p.copy().add(q).normalize();
+    }
+
+}
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/package-info.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/package-info.java
new file mode 100644
index 0000000000..60012e3bdb
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+/**
+ * This package contains spherical trigonometry functions and classes.
+ *
+ * @see https://en.wikipedia.org/wiki/Spherical_trigonometry
+ */
+package org.apache.sis.geometries.spherical;
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalTriangleTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalTriangleTest.java
new file mode 100644
index 0000000000..80cefc7c58
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalTriangleTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.spherical;
+
+import org.apache.sis.geometries.Sphere;
+import org.apache.sis.geometries.math.ReadOnly;
+import org.apache.sis.geometries.math.Vector3D;
+
+// Test dependencies
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+
+/**
+ * Tests for {@link SphericalTriangle}.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public class SphericalTriangleTest {
+
+    private static final double TOLERANCE = 1e-12;
+
+    private final Sphere sphere = new Sphere(3);
+
+    /**
+     * Triangle covering the octant of the sphere where x,y,z are all positive.
+     * Corners are given in CCW order viewed from outside the sphere.
+     */
+    private final ReadOnly.Vector<?> a = new Vector3D.Double(1, 0, 0);
+    private final ReadOnly.Vector<?> b = new Vector3D.Double(0, 1, 0);
+    private final ReadOnly.Vector<?> c = new Vector3D.Double(0, 0, 1);
+    private final SphericalTriangle triangle = new SphericalTriangle(sphere, 
a, b, c);
+
+    /**
+     * Constructor and corner accessors test.
+     */
+    @Test
+    public void constructorTest() {
+        assertSame(a, triangle.getA());
+        assertSame(b, triangle.getB());
+        assertSame(c, triangle.getC());
+    }
+
+    /**
+     * Centroid test.
+     */
+    @Test
+    public void getCentroidTest() {
+        final ReadOnly.Vector<?> centroid = triangle.getCentroid();
+        final double v = 1.0 / Math.sqrt(3.0);
+        assertArrayEquals(new double[]{v, v, v}, centroid.toArrayDouble(), 
TOLERANCE);
+    }
+
+    /**
+     * Contains test, using the single argument overload which relies on the
+     * class default epsilon of -1e9. Since this epsilon is far below the
+     * range of possible dot product values for unit vectors, this method
+     * currently returns true regardless of the tested point.
+     */
+    @Test
+    public void containsTest() {
+        assertTrue(triangle.contains(triangle.getA()));
+        assertTrue(triangle.contains(triangle.getB()));
+        assertTrue(triangle.contains(triangle.getC()));
+        assertTrue(triangle.contains(triangle.getCentroid()));
+        assertFalse(triangle.contains(new Vector3D.Double(-1, 0, 0)));
+        assertFalse(triangle.contains(new Vector3D.Double(0, -1, 0)));
+        assertFalse(triangle.contains(new Vector3D.Double(0, 0, -1)));
+    }
+
+    /**
+     * Quad subdivision test.
+     */
+    @Test
+    public void quadSubdivideTest() {
+        final SphericalTriangle[] children = triangle.quadSubdivide();
+        assertEquals(4, children.length);
+
+        final double[] ab = middle(a, b);
+        final double[] bc = middle(b, c);
+        final double[] ca = middle(c, a);
+
+        // child 0 : corner A
+        assertSame(a, children[0].getA());
+        assertArrayEquals(ab, children[0].getB().toArrayDouble(), TOLERANCE);
+        assertArrayEquals(ca, children[0].getC().toArrayDouble(), TOLERANCE);
+
+        // child 1 : corner B
+        assertArrayEquals(ab, children[1].getA().toArrayDouble(), TOLERANCE);
+        assertSame(b, children[1].getB());
+        assertArrayEquals(bc, children[1].getC().toArrayDouble(), TOLERANCE);
+
+        // child 2 : corner C
+        assertArrayEquals(ca, children[2].getA().toArrayDouble(), TOLERANCE);
+        assertArrayEquals(bc, children[2].getB().toArrayDouble(), TOLERANCE);
+        assertSame(c, children[2].getC());
+
+        // child 3 : center, opposite direction
+        assertArrayEquals(ab, children[3].getA().toArrayDouble(), TOLERANCE);
+        assertArrayEquals(bc, children[3].getB().toArrayDouble(), TOLERANCE);
+        assertArrayEquals(ca, children[3].getC().toArrayDouble(), TOLERANCE);
+    }
+
+    private static double[] middle(ReadOnly.Vector<?> p, ReadOnly.Vector<?> q) 
{
+        return p.copy().add(q).normalize().toArrayDouble();
+    }
+
+}

Reply via email to