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 fad2774533 feat(Geometry): add SphericalConvexPolygon
fad2774533 is described below

commit fad2774533da9be24414d94951a1d019f820b24e
Author: jsorel <[email protected]>
AuthorDate: Thu Aug 6 15:22:24 2026 +0200

    feat(Geometry): add SphericalConvexPolygon
---
 .../spherical/SphericalConvexPolygon.java          | 85 +++++++++++++++++++++
 .../geometries/spherical/SphericalTriangle.java    |  2 +-
 .../spherical/SphericalConvexPolygonTest.java      | 89 ++++++++++++++++++++++
 3 files changed, 175 insertions(+), 1 deletion(-)

diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalConvexPolygon.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalConvexPolygon.java
new file mode 100644
index 0000000000..3646ef64c9
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalConvexPolygon.java
@@ -0,0 +1,85 @@
+/*
+ * 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.Array;
+import org.apache.sis.geometries.math.ReadOnly;
+import org.apache.sis.geometries.math.Vector3D;
+
+/**
+ * A polygon on a sphere.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public final class SphericalConvexPolygon {
+
+    /**
+     * base sphere.
+     */
+    private final Sphere sphere;
+    /**
+     * Serie of points as a unit direction vector from the sphere center.
+     * First point equels last point
+     */
+    private final Array points;
+
+    public SphericalConvexPolygon(Sphere sphere, Array points) {
+        this.sphere = sphere;
+        this.points = points;
+    }
+
+    /**
+     * @return the base sphere
+     */
+    public Sphere getSphere() {
+        return sphere;
+    }
+
+    /**
+     * Test if given vector is contained in this polygon 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 polygon.
+     *
+     * @param vecP vector to test
+     * @param epsilon edge tolerance, should be a negative value close to zero
+     * @return true if vector is inside the polygon
+     */
+    public boolean contains(ReadOnly.Vector<?> vecP, double epsilon) {
+        final Vector3D.Double va = new Vector3D.Double();
+        final Vector3D.Double vb = new Vector3D.Double();
+
+        for (long a = 0, n = points.getLength()-1; a < n; a++) {
+            points.get(a, va);
+            points.get(a + 1, vb);
+            if (vecP.dot(va.cross(vb)) < epsilon) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+
+}
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
index 1dbb4df790..9ebd592dde 100644
--- 
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
@@ -145,7 +145,7 @@ public final class SphericalTriangle {
      *
      * @param vecP vector to test
      * @param epsilon edge tolerance, should be a negative value close to zero
-     * @return true if vector is inside triangle
+     * @return true if vector is inside the triangle
      */
     public boolean contains(ReadOnly.Vector<?> vecP, double epsilon) {
         return vecP.dot(vecA.cross(vecB)) >= epsilon
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalConvexPolygonTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalConvexPolygonTest.java
new file mode 100644
index 0000000000..4aca3ef1f1
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalConvexPolygonTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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 java.util.List;
+import org.apache.sis.geometries.Sphere;
+import org.apache.sis.geometries.math.Array;
+import org.apache.sis.geometries.math.DataType;
+import org.apache.sis.geometries.math.NDArrays;
+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 SphericalConvexPolygon#contains}.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public class SphericalConvexPolygonTest {
+
+    /**
+     * Corners of a spherical square cap centered on the north pole, given in
+     * CCW order viewed from outside the sphere.
+     */
+    private final Vector3D.Double a = new Vector3D.Double(1, 0, 1).normalize();
+    private final Vector3D.Double b = new Vector3D.Double(0, 1, 1).normalize();
+    private final Vector3D.Double c = new Vector3D.Double(-1, 0, 
1).normalize();
+    private final Vector3D.Double d = new Vector3D.Double(0, -1, 
1).normalize();
+
+    private final Array points = NDArrays.of(List.of(a, b, c, d, a), 3, 
DataType.DOUBLE);
+    private final SphericalConvexPolygon polygon = new 
SphericalConvexPolygon(new Sphere(3), points);
+
+    /**
+     * Corner points lie on the polygon boundary and must be considered 
contained.
+     */
+    @Test
+    public void containsCornerTest() {
+        assertTrue(polygon.contains(a));
+        assertTrue(polygon.contains(b));
+        assertTrue(polygon.contains(c));
+        assertTrue(polygon.contains(d));
+    }
+
+    /**
+     * The north pole is at the center of the cap and must be contained.
+     */
+    @Test
+    public void containsInteriorTest() {
+        assertTrue(polygon.contains(new Vector3D.Double(0, 0, 1)));
+    }
+
+    /**
+     * The south pole is diametrically opposed to the cap and must not be 
contained.
+     */
+    @Test
+    public void containsOppositeSideTest() {
+        assertFalse(polygon.contains(new Vector3D.Double(0, 0, -1)));
+    }
+
+    /**
+     * Points on the equator are outside the cap, even though each of them lies
+     * on the "inside" side of some, but not all, edges.
+     */
+    @Test
+    public void containsOutsideEquatorTest() {
+        assertFalse(polygon.contains(new Vector3D.Double(1, 0, 0)));
+        assertFalse(polygon.contains(new Vector3D.Double(0, 1, 0)));
+        assertFalse(polygon.contains(new Vector3D.Double(-1, 0, 0)));
+        assertFalse(polygon.contains(new Vector3D.Double(0, -1, 0)));
+    }
+
+}

Reply via email to