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 6c0aca6588 feat(Math): add Vector.combine method, update spherical
geometries
6c0aca6588 is described below
commit 6c0aca6588480a5cd9d2d6c762d08fbd532061c2
Author: jsorel <[email protected]>
AuthorDate: Tue Nov 24 12:18:31 2026 +0100
feat(Math): add Vector.combine method, update spherical geometries
---
.../sis/geometries/spherical/GreatCircleArc.java | 75 +++++++-
.../spherical/SphericalConvexPolygon.java | 124 ++++++++++++-
.../sis/geometries/spherical/SphericalPolygon.java | 194 +++++++++++++++++++++
.../geometries/spherical/SphericalTriangle.java | 56 ++++--
.../main/org/apache/sis/maths/AbstractVector.java | 12 +-
.../main/org/apache/sis/maths/Affine1D.java | 2 +-
.../main/org/apache/sis/maths/Affine2D.java | 2 +-
.../main/org/apache/sis/maths/Affine3D.java | 2 +-
.../main/org/apache/sis/maths/Affine4D.java | 2 +-
.../main/org/apache/sis/maths/Matrix2D.java | 2 +-
.../main/org/apache/sis/maths/Matrix3D.java | 2 +-
.../main/org/apache/sis/maths/Matrix4D.java | 2 +-
.../main/org/apache/sis/maths/Vector.java | 41 ++++-
.../main/org/apache/sis/maths/Vector1D.java | 57 ++----
.../main/org/apache/sis/maths/Vector2D.java | 57 ++----
.../main/org/apache/sis/maths/Vector3D.java | 57 ++----
.../main/org/apache/sis/maths/Vector4D.java | 57 ++----
.../main/org/apache/sis/maths/VectorND.java | 57 ++----
.../main/org/apache/sis/maths/Vectors.java | 47 +++--
.../geometries/spherical/GreatCircleArcTest.java | 32 ++++
.../spherical/SphericalConvexPolygonTest.java | 41 +++++
.../geometries/spherical/SphericalPolygonTest.java | 156 +++++++++++++++++
.../spherical/SphericalTriangleTest.java | 55 ++++++
.../org/apache/sis/maths/AbstractVectorTest.java | 2 +
.../test/org/apache/sis/maths/VectorsTest.java | 55 ++++++
25 files changed, 938 insertions(+), 251 deletions(-)
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/GreatCircleArc.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/GreatCircleArc.java
index 7a2b987b0b..3a57fc6eda 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/GreatCircleArc.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/GreatCircleArc.java
@@ -17,18 +17,25 @@
package org.apache.sis.geometries.spherical;
import org.apache.sis.geometries.surface.Sphere;
-import org.apache.sis.maths.Maths;
import org.apache.sis.maths.Vector;
/**
* A great circle arc on a sphere.
+ * <p>
+ * The angle between the arc ends is computed once, so interpolating many
points along the arc
+ * costs only two sines per point.
*
* @see https://mathworld.wolfram.com/GreatCircle.html
* @author Johann Sorel (Geomatys)
*/
public final class GreatCircleArc {
+ /**
+ * Below this sine of the arc angle, the arc is indistinguishable from the
straight line joining its ends.
+ */
+ private static final double PARALLEL_SINE = 1E-9;
+
/**
* base sphere.
*/
@@ -41,11 +48,21 @@ public final class GreatCircleArc {
* second arc point, as a unit direction vector from the sphere center
*/
private final Vector<?> vecB;
+ /**
+ * Angle between the arc ends, in radians.
+ */
+ private final double angle;
+ /**
+ * Sine of {@link #angle}.
+ */
+ private final double sinAngle;
public GreatCircleArc(Sphere sphere, Vector<?> vecA, Vector<?> vecB) {
this.sphere = sphere;
this.vecA = vecA;
this.vecB = vecB;
+ this.angle = vecA.shortestAngle(vecB);
+ this.sinAngle = Math.sin(angle);
}
/**
@@ -70,11 +87,13 @@ public final class GreatCircleArc {
}
/**
+ * Computes the arc length from the {@linkplain
Vector#shortestAngle(Vector) shortest angle} between the arc ends,
+ * which keeps full precision for short arcs.
+ *
* @return arc length, using the sphere radius
*/
public double getLength() {
- final double cosAngle = Maths.clamp(vecA.dot(vecB),-1,1);
- return sphere.getRadius() * Math.acos(cosAngle);
+ return sphere.getRadius() * angle;
}
/**
@@ -90,7 +109,24 @@ public final class GreatCircleArc {
* not define a unique great circle
*/
public Vector<?> pointAt(double fraction) {
- return interpolate(vecA, vecB, fraction);
+ return interpolate(vecA, vecB, angle, sinAngle, fraction);
+ }
+
+ /**
+ * Returns points along the arc, spaced evenly and at most {@code spacing}
apart.
+ * The arc ends are not included.
+ *
+ * @param spacing maximal distance between consecutive points, in the
units of the sphere radius
+ * @return interior points as unit direction vectors from the sphere
center, possibly empty
+ * @throws IllegalArgumentException if the two arc ends are antipodal
+ */
+ public Vector<?>[] sample(double spacing) {
+ final double segments = Math.max(1, Math.ceil(getLength() / spacing));
+ final Vector<?>[] samples = new Vector<?>[(int) segments - 1];
+ for (int i = 0; i < samples.length; i++) {
+ samples[i] = pointAt((i + 1) / segments);
+ }
+ return samples;
}
/**
@@ -109,12 +145,33 @@ public final class GreatCircleArc {
* @see Vector#slerp(Vector, double)
*/
public static Vector<?> interpolate(Vector<?> vecA, Vector<?> vecB, double
fraction) {
+ final double angle = vecA.shortestAngle(vecB);
+ return interpolate(vecA, vecB, angle, Math.sin(angle), fraction);
+ }
+
+ /**
+ * Spherical linear interpolation with the angle between the two vectors
already known.
+ */
+ private static Vector<?> interpolate(Vector<?> vecA, Vector<?> vecB,
double angle, double sinAngle, double fraction) {
+ if (fraction <= 0) {
+ return vecA;
+ } else if (fraction >= 1) {
+ return vecB;
+ }
/*
- * Slerp already keeps the length of unit vectors, but normalizing
removes the
- * rounding it leaves behind. Worth the cost because subdividing a
cell feeds the
- * result back in, so the drift would otherwise pile up over the
levels.
+ * The result is normalized to remove the rounding left by the
interpolation. Worth the cost
+ * because subdividing a cell feeds the result back in, so the drift
would otherwise pile up.
*/
- return vecA.slerp(vecB, fraction).normalize();
+ if (sinAngle < PARALLEL_SINE) {
+ if (angle > Math.PI / 2) {
+ throw new IllegalArgumentException("Cannot interpolate between
antipodal vectors, "
+ + "they define no unique great circle.");
+ }
+ // Nearly parallel vectors: the arc is the straight line joining
them
+ return vecA.lerp(vecB, fraction).normalize();
+ }
+ final double weightA = Math.sin((1 - fraction) * angle) / sinAngle;
+ final double weightB = Math.sin(fraction * angle) / sinAngle;
+ return vecA.combine(weightA, vecB, weightB).normalize();
}
-
}
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
index aed5522a5b..9fa2599f03 100644
---
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
@@ -16,13 +16,17 @@
*/
package org.apache.sis.geometries.spherical;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.sis.geometries.surface.Sphere;
import org.apache.sis.maths.Array;
+import org.apache.sis.maths.DataType;
+import org.apache.sis.maths.NDArrays;
import org.apache.sis.maths.Vector;
-
+import org.apache.sis.util.ArgumentChecks;
/**
- * A polygon on a sphere.
+ * A convex polygon on a sphere.
*
* @author Johann Sorel (Geomatys)
*/
@@ -37,10 +41,27 @@ public final class SphericalConvexPolygon {
* First point equels last point
*/
private final Array points;
+ /**
+ * The polygon corners, without the closing point.
+ */
+ private final Vector<?>[] vertices;
+ /**
+ * @param sphere base sphere
+ * @param points corners as unit direction vectors from the sphere center,
counter-clockwise
+ * seen from outside the sphere, the first point being repeated at
the end
+ */
public SphericalConvexPolygon(Sphere sphere, Array points) {
this.sphere = sphere;
this.points = points;
+ this.vertices = vertices(points);
+ }
+
+ /**
+ * Returns the corners of a closed ring, without the closing point.
+ */
+ static Vector<?>[] vertices(Array ring) {
+ return ring.toArray(0, Math.max(0, ring.getLength() - 1));
}
/**
@@ -76,5 +97,104 @@ public final class SphericalConvexPolygon {
return true;
}
+ /**
+ * Computes how far the given vector is inside this polygon.
+ * At each corner, the directions toward the two adjacent corners and
toward the vector are
+ * projected on the plane tangent to the sphere. The result is the
smallest sine of the angles
+ * between the vector direction and the edge directions, signed by the
side of the edge.
+ *
+ * @param vecP unit vector to test
+ * @return positive inside, zero on the boundary, negative outside;
+ * the further from zero, the further from the boundary
+ * @see "Locating a point on a spherical surface relative to a spherical
polygon, condition of necessary strike"
+ */
+ public double margin(Vector<?> vecP) {
+ return margin(vertices, vecP);
+ }
+
+ /**
+ * Computes the area of this polygon, as a fan of triangles around its
center.
+ *
+ * @return polygon area, in the sphere radius squared units
+ */
+ public double getArea() {
+ final double radius = sphere.getRadius();
+ return fanExcess(vertices) * radius * radius;
+ }
+ /**
+ * Returns the boundary of this polygon, each edge divided in arcs of
equal length.
+ *
+ * @param segments number of arcs per edge, must be 1 or more
+ * @return closed ring of unit vectors, starting and ending on the first
corner
+ * @throws IllegalArgumentException if {@code segments} is less than 1
+ */
+ public Array getBoundary(int segments) {
+ return boundary(vertices, segments);
+ }
+
+ /**
+ * Implementation of {@link #margin(Vector)} on the given convex ring
corners.
+ */
+ static double margin(Vector<?>[] vertices, Vector<?> vecP) {
+ final int n = vertices.length;
+ double margin = Double.POSITIVE_INFINITY;
+ for (int i = 0; i < n; i++) {
+ final Vector<?> v = vertices[i];
+ // A vector on the corner has no direction from it, and lies on
the boundary
+ Vector<?> vp = vecP.subtract(v);
+ if (vp.lengthSquare() > 0) {
+ vp = vp.normalize();
+ }
+ final Vector<?> va = vertices[(i + 1) % n].subtract(v).normalize();
+ final Vector<?> vb = vertices[(i + n - 1) %
n].subtract(v).normalize();
+ margin = Math.min(Math.min(margin, v.dot(va.cross(vp))),
v.dot(vp.cross(vb)));
+ }
+ return margin;
+ }
+
+ /**
+ * Unsigned spherical excess of the given convex ring corners, as a fan of
triangles around their center.
+ */
+ static double fanExcess(Vector<?>[] vertices) {
+ final int n = vertices.length;
+ if (n < 3) {
+ return 0;
+ }
+ if (n == 3) {
+ return Math.abs(SphericalTriangle.signedExcess(vertices[0],
vertices[1], vertices[2]));
+ }
+ Vector<?> center = vertices[0];
+ for (int i = 1; i < n; i++) {
+ center = center.add(vertices[i]);
+ }
+ center = center.normalize();
+ double excess = 0;
+ for (int i = 0; i < n; i++) {
+ final double triangle = SphericalTriangle.signedExcess(center,
vertices[i], vertices[(i + 1) % n]);
+ if (!Double.isNaN(triangle)) {
+ excess += triangle;
+ }
+ }
+ return Math.abs(excess);
+ }
+
+ /**
+ * Implementation of {@link #getBoundary(int)} on the given ring corners.
+ */
+ static Array boundary(Vector<?>[] vertices, int segments) {
+ ArgumentChecks.ensureStrictlyPositive("segments", segments);
+ final int n = vertices.length;
+ final List<Vector<?>> points = new ArrayList<>(n * segments + 1);
+ for (int i = 0; i < n; i++) {
+ final Vector<?> start = vertices[i];
+ final Vector<?> end = vertices[(i + 1) % n];
+ points.add(start);
+ for (int j = 1; j < segments; j++) {
+ points.add(GreatCircleArc.interpolate(start, end, j / (double)
segments));
+ }
+ }
+ points.add(vertices[0]);
+ return NDArrays.of(points, vertices[0].getDimension(),
DataType.DOUBLE);
+ }
}
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalPolygon.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalPolygon.java
new file mode 100644
index 0000000000..5fe2a5bbeb
--- /dev/null
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/spherical/SphericalPolygon.java
@@ -0,0 +1,194 @@
+/*
+ * 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.surface.Sphere;
+import org.apache.sis.maths.Array;
+import org.apache.sis.maths.Vector;
+
+
+/**
+ * A polygon on a sphere, possibly concave and with holes, whose edges are
great circle arcs.
+ * <p>
+ * Rings are given as closed arrays of unit direction vectors from the sphere
center. The static methods
+ * work on ring corners without the closing point, for callers which already
have them as vectors.
+ * <p>
+ * Containment is decided by the sum of the angles under which the ring edges
are seen from the tested point.
+ * Like every test based on angles, it cannot distinguish a point from its
antipode when the polygon is larger
+ * than a hemisphere: callers testing such polygons should reject far points
first, for example with a cap.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public final class SphericalPolygon {
+
+ /**
+ * base sphere.
+ */
+ private final Sphere sphere;
+ /**
+ * Exterior ring corners, without the closing point.
+ */
+ private final Vector<?>[] exterior;
+ /**
+ * Interior rings corners (holes), without the closing points.
+ */
+ private final Vector<?>[][] interiors;
+
+ /**
+ * @param sphere base sphere
+ * @param exterior exterior ring, unit direction vectors from the sphere
center, the first point repeated at the end
+ * @param interiors holes, as closed rings like the exterior
+ */
+ public SphericalPolygon(Sphere sphere, Array exterior, Array... interiors)
{
+ this.sphere = sphere;
+ this.exterior = SphericalConvexPolygon.vertices(exterior);
+ this.interiors = new Vector<?>[interiors.length][];
+ for (int i = 0; i < interiors.length; i++) {
+ this.interiors[i] = SphericalConvexPolygon.vertices(interiors[i]);
+ }
+ }
+
+ /**
+ * @return the base sphere
+ */
+ public Sphere getSphere() {
+ return sphere;
+ }
+
+ /**
+ * @return exterior ring corners, without the closing point
+ */
+ public Vector<?>[] getExterior() {
+ return exterior.clone();
+ }
+
+ /**
+ * @return holes corners, without the closing points
+ */
+ public Vector<?>[][] getInteriors() {
+ final Vector<?>[][] copy = new Vector<?>[interiors.length][];
+ for (int i = 0; i < copy.length; i++) {
+ copy[i] = interiors[i].clone();
+ }
+ return copy;
+ }
+
+ /**
+ * Test if given vector is inside the exterior ring and outside every hole.
+ *
+ * @param vecP unit vector to test
+ * @return true if vector is inside the polygon
+ */
+ public boolean contains(Vector<?> vecP) {
+ if (!ringContains(exterior, vecP)) {
+ return false;
+ }
+ for (final Vector<?>[] hole : interiors) {
+ if (ringContains(hole, vecP)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Computes the area of this polygon : the area of the exterior ring minus
the areas of the holes.
+ * Concave rings are measured correctly, whatever their winding.
+ *
+ * @return polygon area, in the sphere radius squared units
+ */
+ public double getArea() {
+ double excess = Math.abs(excess(exterior));
+ for (final Vector<?>[] hole : interiors) {
+ excess -= Math.abs(excess(hole));
+ }
+ final double radius = sphere.getRadius();
+ return excess * radius * radius;
+ }
+
+ /**
+ * Test if given vector is inside the given ring, by summing the angles
under which the ring edges
+ * are seen from the vector: about ±2π inside, about zero outside. Works
for concave rings, whatever
+ * their winding.
+ *
+ * @param ring ring corners as unit vectors, without the closing point
+ * @param vecP unit vector to test
+ * @return true if the vector is inside the ring
+ */
+ public static boolean ringContains(Vector<?>[] ring, Vector<?> vecP) {
+ double angleSum = 0;
+ for (int i = 0; i < ring.length; i++) {
+ final Vector<?> a = ring[i];
+ final Vector<?> b = ring[(i + 1) % ring.length];
+ // Projections of the corners on the plane tangent to the sphere
at the tested vector
+ final Vector<?> ap = a.combine(1.0, vecP, -vecP.dot(a));
+ final Vector<?> bp = b.combine(1.0, vecP, -vecP.dot(b));
+ angleSum += Math.atan2(ap.cross(bp).dot(vecP), ap.dot(bp));
+ }
+ return Math.abs(angleSum) > Math.PI;
+ }
+
+ /**
+ * Returns the winding of the given ring around its center:
+ * the sum of (v<sub>i</sub> × v<sub>i+1</sub>) · center over the ring
edges.
+ *
+ * @param ring ring corners as unit vectors, without the closing point
+ * @return 1 if the ring is counter-clockwise seen from outside the
sphere, -1 otherwise
+ */
+ public static int orientation(Vector<?>[] ring) {
+ Vector<?> center = ring[0];
+ for (int i = 1; i < ring.length; i++) {
+ center = center.add(ring[i]);
+ }
+ center = center.normalize();
+ double sum = 0;
+ for (int i = 0; i < ring.length; i++) {
+ sum += center.dot(ring[i].cross(ring[(i + 1) % ring.length]));
+ }
+ return sum > 0 ? 1 : -1;
+ }
+
+ /**
+ * Returns the normals of the great circle planes of the ring edges.
+ *
+ * @param ring ring corners as unit vectors, without the closing point
+ * @return one normal per edge (not normalized), edge i going from corner
i to corner i+1
+ */
+ public static Vector<?>[] edgeNormals(Vector<?>[] ring) {
+ final Vector<?>[] normals = new Vector<?>[ring.length];
+ for (int i = 0; i < ring.length; i++) {
+ normals[i] = ring[i].cross(ring[(i + 1) % ring.length]);
+ }
+ return normals;
+ }
+
+ /**
+ * Computes the signed spherical excess of the given ring, as a fan of
triangles from its first corner.
+ * Concave rings are measured correctly.
+ *
+ * @param ring ring corners as unit vectors, without the closing point
+ * @return spherical excess in radians, positive if the ring is
counter-clockwise seen from outside the sphere
+ * @see SphericalTriangle#signedExcess(Vector, Vector, Vector)
+ */
+ public static double excess(Vector<?>[] ring) {
+ double excess = 0;
+ for (int i = 1; i + 1 < ring.length; i++) {
+ excess += SphericalTriangle.signedExcess(ring[0], ring[i], ring[i
+ 1]);
+ }
+ return excess;
+ }
+}
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 f0bcebf70e..b081db50e0 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
@@ -17,7 +17,7 @@
package org.apache.sis.geometries.spherical;
import org.apache.sis.geometries.surface.Sphere;
-import org.apache.sis.maths.Maths;
+import org.apache.sis.maths.Array;
import org.apache.sis.maths.Vector;
import org.apache.sis.util.ArgumentChecks;
@@ -105,21 +105,33 @@ public final class SphericalTriangle {
/**
* Compute the spherical excess of this triangle : the sum of its three
* angles minus PI.
+ * <p>
+ * Computed with the Van Oosterom–Strackee formula tan(E/2) = |A·(B×C)| /
(1 + A·B + B·C + C·A),
+ * which keeps full precision for small triangles, where summing the
angles loses most digits.
+ * The excess is positive whatever the winding of the corners.
*
* @see <a
href="https://mathworld.wolfram.com/SphericalExcess.html">Spherical Excess</a>
+ * @see "Van Oosterom and Strackee, The Solid Angle of a Plane Triangle,
IEEE Trans. Biomed. Eng. 30(2), 1983"
* @return spherical excess in radians
*/
public double getSphericalExcess() {
- final double cosA = Maths.clamp(vecB.dot(vecC),-1,1);
- final double cosB = Maths.clamp(vecC.dot(vecA),-1,1);
- final double cosC = Maths.clamp(vecA.dot(vecB),-1,1);
- final double sinA = Math.sqrt(1 - cosA * cosA);
- final double sinB = Math.sqrt(1 - cosB * cosB);
- final double sinC = Math.sqrt(1 - cosC * cosC);
- final double angleA = Math.acos(Maths.clamp((cosA - cosB * cosC) /
(sinB * sinC),-1,1));
- final double angleB = Math.acos(Maths.clamp((cosB - cosC * cosA) /
(sinC * sinA),-1,1));
- final double angleC = Math.acos(Maths.clamp((cosC - cosA * cosB) /
(sinA * sinB),-1,1));
- return angleA + angleB + angleC - Math.PI;
+ return Math.abs(signedExcess(vecA, vecB, vecC));
+ }
+
+ /**
+ * Computes the signed spherical excess of the triangle of the given unit
vectors,
+ * with the Van Oosterom–Strackee formula tan(E/2) = A·(B×C) / (1 + A·B +
B·C + C·A).
+ * The excess is positive when the corners are counter-clockwise seen from
outside the sphere.
+ *
+ * @param a first corner, as a unit direction vector from the sphere center
+ * @param b second corner, as a unit direction vector from the sphere
center
+ * @param c third corner, as a unit direction vector from the sphere center
+ * @return signed spherical excess in radians, in the [-2π … 2π] range
+ * @see #getSphericalExcess()
+ */
+ public static double signedExcess(Vector<?> a, Vector<?> b, Vector<?> c) {
+ final double norm = 1 + a.dot(b) + b.dot(c) + c.dot(a);
+ return 2 * Math.atan2(a.dot(b.cross(c)), norm);
}
/**
@@ -155,6 +167,28 @@ public final class SphericalTriangle {
&& vecP.dot(vecC.cross(vecA)) >= epsilon;
}
+ /**
+ * Computes how far the given vector is inside this triangle.
+ * See {@link SphericalConvexPolygon#margin(Vector)} for the meaning of
the value.
+ *
+ * @param vecP unit vector to test
+ * @return positive inside, zero on the boundary, negative outside
+ */
+ public double margin(Vector<?> vecP) {
+ return SphericalConvexPolygon.margin(new Vector<?>[] {vecA, vecB,
vecC}, vecP);
+ }
+
+ /**
+ * Returns the boundary of this triangle, each edge divided in arcs of
equal length.
+ *
+ * @param segments number of arcs per edge, must be 1 or more
+ * @return closed ring of unit vectors, starting and ending on corner A
+ * @throws IllegalArgumentException if {@code segments} is less than 1
+ */
+ public Array getBoundary(int segments) {
+ return SphericalConvexPolygon.boundary(new Vector<?>[] {vecA, vecB,
vecC}, segments);
+ }
+
/**
* 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.
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AbstractVector.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AbstractVector.java
index 6ae498907e..c0d3f17949 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AbstractVector.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/AbstractVector.java
@@ -137,19 +137,17 @@ abstract sealed class AbstractVector<T extends
AbstractVector<T>> implements Vec
}
/*
- * Angle between the two directions. Dividing by the lengths keeps the
angle right
- * for vectors which are not unit vectors. A vector of length zero has
no direction,
- * which the parallel case below handles as a linear interpolation.
+ * Angle between the two directions, with full precision for nearly
parallel and nearly
+ * opposite vectors. A vector of length zero has no direction, which
the parallel case
+ * below handles as a linear interpolation.
*/
final double lengthStart = start.length();
final double lengthEnd = end.length();
- final double lengths = lengthStart * lengthEnd;
- final double cosAngle = (lengths > 0) ? Maths.clamp(start.dot(end) /
lengths, -1, 1) : 1;
- final double angle = Math.acos(cosAngle);
+ final double angle = (lengthStart > 0 && lengthEnd > 0) ?
start.shortestAngle(end) : 0;
final double sinAngle = Math.sin(angle);
if (sinAngle < 1E-9) {
- if (cosAngle < 0) {
+ if (angle > Math.PI / 2) {
throw new IllegalArgumentException("Cannot interpolate between
vectors pointing in "
+ "opposite directions, they define no unique arc.");
}
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine1D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine1D.java
index 28db690b53..4eb9e27a29 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine1D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine1D.java
@@ -119,7 +119,7 @@ public final class Affine1D extends
AbstractAffine<Affine1D> implements Transfor
}
@Override
- public Vector<?> transform(Vector<?> source) {
+ public Vector1D.Double transform(Vector<?> source) {
return new Vector1D.Double(m00*source.get(0) + m01);
}
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 e9d4eb1f71..02eb9f480c 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
@@ -143,7 +143,7 @@ public final class Affine2D extends
AbstractAffine<Affine2D> {
}
@Override
- public Vector<?> transform(Vector<?> source) {
+ public Vector2D.Double transform(Vector<?> source) {
final double x = source.get(0);
final double y = source.get(1);
return new Vector2D.Double(
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine3D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine3D.java
index 85c8c69c0b..85fdced776 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine3D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine3D.java
@@ -227,7 +227,7 @@ public final class Affine3D extends
AbstractAffine<Affine3D> {
}
@Override
- public Vector<?> transform(Vector<?> source) {
+ public Vector3D.Double transform(Vector<?> source) {
final double x = source.get(0);
final double y = source.get(1);
final double z = source.get(2);
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine4D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine4D.java
index 4f558c926a..db5c710615 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine4D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Affine4D.java
@@ -331,7 +331,7 @@ public final class Affine4D extends
AbstractAffine<Affine4D> {
}
@Override
- public Vector<?> transform(Vector<?> source) {
+ public Vector4D.Double transform(Vector<?> source) {
final double x = source.get(0);
final double y = source.get(1);
final double z = source.get(2);
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix2D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix2D.java
index 0c86e8288b..df5aac443c 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix2D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix2D.java
@@ -205,7 +205,7 @@ public class Matrix2D extends AbstractMatrix<Matrix2D> {
}
@Override
- public Vector<?> transform(Vector<?> vector) {
+ public Vector2D.Double transform(Vector<?> vector) {
final double x = vector.get(0);
final double y = vector.get(1);
return new Vector2D.Double(
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java
index e41c764146..05dcbc4f55 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix3D.java
@@ -267,7 +267,7 @@ public class Matrix3D extends AbstractMatrix<Matrix3D> {
}
@Override
- public Vector<?> transform(Vector<?> vector) {
+ public Vector3D.Double transform(Vector<?> vector) {
final double x = vector.get(0);
final double y = vector.get(1);
final double z = vector.get(2);
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix4D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix4D.java
index 45f64f2261..682b61ccdc 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix4D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Matrix4D.java
@@ -456,7 +456,7 @@ public class Matrix4D extends AbstractMatrix<Matrix4D> {
}
@Override
- public Vector<?> transform(Vector<?> vector) {
+ public Vector4D.Double transform(Vector<?> vector) {
final double x = vector.get(0);
final double y = vector.get(1);
final double z = vector.get(2);
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java
index b1112eb65a..d5596061fa 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector.java
@@ -751,6 +751,33 @@ public sealed interface Vector<T extends Vector<T>>
permits AbstractVector {
return sum;
}
+ /**
+ * Computes the shortest angle between this vector and the other vector.
+ * See {@link Vectors#shortestAngle(double[], double[])} for the formula,
which keeps
+ * full precision for nearly parallel and nearly opposite vectors.
+ *
+ * @param other second vector, of the same dimension
+ * @return shortest angle in radians, or NaN if a vector has a length of
zero
+ * @throws IllegalArgumentException if vectors do not have the same
dimension
+ */
+ default double shortestAngle(Vector<?> other) {
+ AbstractVector.ensureDimension(other, getDimension());
+ final double lengthThis = length();
+ final double lengthOther = other.length();
+ if (lengthThis == 0 || lengthOther == 0) {
+ return Double.NaN;
+ }
+ double difference = 0;
+ double sum = 0;
+ for (int i = 0, n = getDimension(); i < n; i++) {
+ final double a = get(i) * lengthOther;
+ final double b = other.get(i) * lengthThis;
+ difference += (a - b) * (a - b);
+ sum += (a + b) * (a + b);
+ }
+ return 2 * Math.atan2(Math.sqrt(difference), Math.sqrt(sum));
+ }
+
/**
* Increase size of the tuple by one value.
*
@@ -805,7 +832,7 @@ public sealed interface Vector<T extends Vector<T>> permits
AbstractVector {
* @param b coefficient of the other vector
* @return new vector
*/
- private T combination(double a, Vector<?> other, double b) {
+ default T combine(double a, Vector<?> other, double b) {
final double[] values = new double[getDimension()];
for (int i = 0; i < values.length; i++) {
values[i] = a * get(i) + b * other.get(i);
@@ -814,9 +841,9 @@ public sealed interface Vector<T extends Vector<T>> permits
AbstractVector {
}
/**
- * Same as {@link #combination(double, Vector, double)} but computed with
compensated arithmetic.
+ * Same as {@link #combine(double, Vector, double)} but computed with
compensated arithmetic.
*/
- private T combinationAccurate(double a, Vector<?> other, double b) {
+ default T combineAccurate(double a, Vector<?> other, double b) {
final double[] values = new double[getDimension()];
for (int i = 0; i < values.length; i++) {
values[i] = LinearCombination.of(a, get(i), b, other.get(i));
@@ -931,7 +958,7 @@ public sealed interface Vector<T extends Vector<T>> permits
AbstractVector {
*/
default T perpendicular(Vector<?> unit) {
AbstractVector.ensureDimension(unit, getDimension());
- return combination(1, unit, -dot(unit));
+ return combine(1, unit, -dot(unit));
}
/**
@@ -944,7 +971,7 @@ public sealed interface Vector<T extends Vector<T>> permits
AbstractVector {
*/
default T vectorTo(Vector<?> other) {
AbstractVector.ensureDimension(other, getDimension());
- return combination(-1, other, 1);
+ return combine(-1, other, 1);
}
/**
@@ -977,7 +1004,7 @@ public sealed interface Vector<T extends Vector<T>>
permits AbstractVector {
*/
default T lerpAccurate(Vector<?> other, double ratio) {
AbstractVector.ensureDimension(other, getDimension());
- return combinationAccurate(1 - ratio, other, ratio);
+ return combineAccurate(1 - ratio, other, ratio);
}
/**
@@ -1005,7 +1032,7 @@ public sealed interface Vector<T extends Vector<T>>
permits AbstractVector {
* which case they define no unique arc to interpolate along
*/
default T slerp(Vector<?> other, double ratio) {
- return AbstractVector.slerp(this, other, ratio, (a, b) ->
combination(a, other, b));
+ return AbstractVector.slerp(this, other, ratio, (a, b) -> combine(a,
other, b));
}
/**
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java
index db2b4db63b..97f1095106 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java
@@ -40,27 +40,6 @@ public abstract sealed class Vector1D<T extends Vector1D<T>>
extends AbstractVec
return 1;
}
- /**
- * Create a new vector, linear combination of this vector and the other
vector : a*this + b*other.
- * The other vector is expected to have the same dimension as this vector.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combine(double a, Vector<?> other, double b);
-
- /**
- * Same as {@link #combine(double, Vector, double)} but computed with
compensated arithmetic.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combineAccurate(double a, Vector<?> other, double b);
-
@Override
public T normalize() {
return scale(1.0 / length());
@@ -221,12 +200,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected Byte combine(double a, Vector<?> other, double b) {
+ public Byte combine(double a, Vector<?> other, double b) {
return new Byte((byte) (a * x + b * other.get(0)));
}
@Override
- protected Byte combineAccurate(double a, Vector<?> other, double b) {
+ public Byte combineAccurate(double a, Vector<?> other, double b) {
return new Byte((byte) (LinearCombination.of(a, x, b,
other.get(0))));
}
@@ -365,12 +344,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected UByte combine(double a, Vector<?> other, double b) {
+ public UByte combine(double a, Vector<?> other, double b) {
return new UByte((byte) (a * (x & 0xFF) + b * other.get(0)));
}
@Override
- protected UByte combineAccurate(double a, Vector<?> other, double b) {
+ public UByte combineAccurate(double a, Vector<?> other, double b) {
return new UByte((byte) (LinearCombination.of(a, (x & 0xFF), b,
other.get(0))));
}
@@ -509,12 +488,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected Short combine(double a, Vector<?> other, double b) {
+ public Short combine(double a, Vector<?> other, double b) {
return new Short((short) (a * x + b * other.get(0)));
}
@Override
- protected Short combineAccurate(double a, Vector<?> other, double b) {
+ public Short combineAccurate(double a, Vector<?> other, double b) {
return new Short((short) (LinearCombination.of(a, x, b,
other.get(0))));
}
@@ -653,12 +632,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected UShort combine(double a, Vector<?> other, double b) {
+ public UShort combine(double a, Vector<?> other, double b) {
return new UShort((short) (a * (x & 0xFFFF) + b * other.get(0)));
}
@Override
- protected UShort combineAccurate(double a, Vector<?> other, double b) {
+ public UShort combineAccurate(double a, Vector<?> other, double b) {
return new UShort((short) (LinearCombination.of(a, (x & 0xFFFF),
b, other.get(0))));
}
@@ -797,12 +776,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected Int combine(double a, Vector<?> other, double b) {
+ public Int combine(double a, Vector<?> other, double b) {
return new Int((int) (a * x + b * other.get(0)));
}
@Override
- protected Int combineAccurate(double a, Vector<?> other, double b) {
+ public Int combineAccurate(double a, Vector<?> other, double b) {
return new Int((int) (LinearCombination.of(a, x, b,
other.get(0))));
}
@@ -941,12 +920,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected UInt combine(double a, Vector<?> other, double b) {
+ public UInt combine(double a, Vector<?> other, double b) {
return new UInt((int) (long) (a * Integer.toUnsignedLong(x) + b *
other.get(0)));
}
@Override
- protected UInt combineAccurate(double a, Vector<?> other, double b) {
+ public UInt combineAccurate(double a, Vector<?> other, double b) {
return new UInt((int) (long) (LinearCombination.of(a,
Integer.toUnsignedLong(x), b, other.get(0))));
}
@@ -1085,12 +1064,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected Long combine(double a, Vector<?> other, double b) {
+ public Long combine(double a, Vector<?> other, double b) {
return new Long((long) (a * x + b * other.get(0)));
}
@Override
- protected Long combineAccurate(double a, Vector<?> other, double b) {
+ public Long combineAccurate(double a, Vector<?> other, double b) {
return new Long((long) (LinearCombination.of(a, x, b,
other.get(0))));
}
@@ -1247,12 +1226,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected Float combine(double a, Vector<?> other, double b) {
+ public Float combine(double a, Vector<?> other, double b) {
return new Float((float) (a * x + b * other.get(0)));
}
@Override
- protected Float combineAccurate(double a, Vector<?> other, double b) {
+ public Float combineAccurate(double a, Vector<?> other, double b) {
return new Float((float) (LinearCombination.of(a, x, b,
other.get(0))));
}
@@ -1423,12 +1402,12 @@ public abstract sealed class Vector1D<T extends
Vector1D<T>> extends AbstractVec
}
@Override
- protected Double combine(double a, Vector<?> other, double b) {
+ public Double combine(double a, Vector<?> other, double b) {
return new Double(a * x + b * other.get(0));
}
@Override
- protected Double combineAccurate(double a, Vector<?> other, double b) {
+ public Double combineAccurate(double a, Vector<?> other, double b) {
return new Double(LinearCombination.of(a, x, b, other.get(0)));
}
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java
index fadaec3c37..743460bf5a 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java
@@ -69,27 +69,6 @@ public abstract sealed class Vector2D<T extends Vector2D<T>>
extends AbstractVec
return 2;
}
- /**
- * Create a new vector, linear combination of this vector and the other
vector : a*this + b*other.
- * The other vector is expected to have the same dimension as this vector.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combine(double a, Vector<?> other, double b);
-
- /**
- * Same as {@link #combine(double, Vector, double)} but computed with
compensated arithmetic.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combineAccurate(double a, Vector<?> other, double b);
-
@Override
public T normalize() {
return scale(1.0 / length());
@@ -271,14 +250,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected Byte combine(double a, Vector<?> other, double b) {
+ public Byte combine(double a, Vector<?> other, double b) {
return new Byte(
(byte) (a * x + b * other.get(0)),
(byte) (a * y + b * other.get(1)));
}
@Override
- protected Byte combineAccurate(double a, Vector<?> other, double b) {
+ public Byte combineAccurate(double a, Vector<?> other, double b) {
return new Byte(
(byte) (LinearCombination.of(a, x, b, other.get(0))),
(byte) (LinearCombination.of(a, y, b, other.get(1))));
@@ -446,14 +425,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected UByte combine(double a, Vector<?> other, double b) {
+ public UByte combine(double a, Vector<?> other, double b) {
return new UByte(
(byte) (a * (x & 0xFF) + b * other.get(0)),
(byte) (a * (y & 0xFF) + b * other.get(1)));
}
@Override
- protected UByte combineAccurate(double a, Vector<?> other, double b) {
+ public UByte combineAccurate(double a, Vector<?> other, double b) {
return new UByte(
(byte) (LinearCombination.of(a, (x & 0xFF), b,
other.get(0))),
(byte) (LinearCombination.of(a, (y & 0xFF), b,
other.get(1))));
@@ -621,14 +600,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected Short combine(double a, Vector<?> other, double b) {
+ public Short combine(double a, Vector<?> other, double b) {
return new Short(
(short) (a * x + b * other.get(0)),
(short) (a * y + b * other.get(1)));
}
@Override
- protected Short combineAccurate(double a, Vector<?> other, double b) {
+ public Short combineAccurate(double a, Vector<?> other, double b) {
return new Short(
(short) (LinearCombination.of(a, x, b, other.get(0))),
(short) (LinearCombination.of(a, y, b, other.get(1))));
@@ -796,14 +775,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected UShort combine(double a, Vector<?> other, double b) {
+ public UShort combine(double a, Vector<?> other, double b) {
return new UShort(
(short) (a * (x & 0xFFFF) + b * other.get(0)),
(short) (a * (y & 0xFFFF) + b * other.get(1)));
}
@Override
- protected UShort combineAccurate(double a, Vector<?> other, double b) {
+ public UShort combineAccurate(double a, Vector<?> other, double b) {
return new UShort(
(short) (LinearCombination.of(a, (x & 0xFFFF), b,
other.get(0))),
(short) (LinearCombination.of(a, (y & 0xFFFF), b,
other.get(1))));
@@ -971,14 +950,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected Int combine(double a, Vector<?> other, double b) {
+ public Int combine(double a, Vector<?> other, double b) {
return new Int(
(int) (a * x + b * other.get(0)),
(int) (a * y + b * other.get(1)));
}
@Override
- protected Int combineAccurate(double a, Vector<?> other, double b) {
+ public Int combineAccurate(double a, Vector<?> other, double b) {
return new Int(
(int) (LinearCombination.of(a, x, b, other.get(0))),
(int) (LinearCombination.of(a, y, b, other.get(1))));
@@ -1146,14 +1125,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected UInt combine(double a, Vector<?> other, double b) {
+ public UInt combine(double a, Vector<?> other, double b) {
return new UInt(
(int) (long) (a * Integer.toUnsignedLong(x) + b *
other.get(0)),
(int) (long) (a * Integer.toUnsignedLong(y) + b *
other.get(1)));
}
@Override
- protected UInt combineAccurate(double a, Vector<?> other, double b) {
+ public UInt combineAccurate(double a, Vector<?> other, double b) {
return new UInt(
(int) (long) (LinearCombination.of(a,
Integer.toUnsignedLong(x), b, other.get(0))),
(int) (long) (LinearCombination.of(a,
Integer.toUnsignedLong(y), b, other.get(1))));
@@ -1321,14 +1300,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected Long combine(double a, Vector<?> other, double b) {
+ public Long combine(double a, Vector<?> other, double b) {
return new Long(
(long) (a * x + b * other.get(0)),
(long) (a * y + b * other.get(1)));
}
@Override
- protected Long combineAccurate(double a, Vector<?> other, double b) {
+ public Long combineAccurate(double a, Vector<?> other, double b) {
return new Long(
(long) (LinearCombination.of(a, x, b, other.get(0))),
(long) (LinearCombination.of(a, y, b, other.get(1))));
@@ -1523,14 +1502,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected Float combine(double a, Vector<?> other, double b) {
+ public Float combine(double a, Vector<?> other, double b) {
return new Float(
(float) (a * x + b * other.get(0)),
(float) (a * y + b * other.get(1)));
}
@Override
- protected Float combineAccurate(double a, Vector<?> other, double b) {
+ public Float combineAccurate(double a, Vector<?> other, double b) {
return new Float(
(float) (LinearCombination.of(a, x, b, other.get(0))),
(float) (LinearCombination.of(a, y, b, other.get(1))));
@@ -1741,14 +1720,14 @@ public abstract sealed class Vector2D<T extends
Vector2D<T>> extends AbstractVec
}
@Override
- protected Double combine(double a, Vector<?> other, double b) {
+ public Double combine(double a, Vector<?> other, double b) {
return new Double(
a * x + b * other.get(0),
a * y + b * other.get(1));
}
@Override
- protected Double combineAccurate(double a, Vector<?> other, double b) {
+ public Double combineAccurate(double a, Vector<?> other, double b) {
return new Double(
LinearCombination.of(a, x, b, other.get(0)),
LinearCombination.of(a, y, b, other.get(1)));
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java
index e878c15baa..c74a4d0429 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java
@@ -143,27 +143,6 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
return 3;
}
- /**
- * Create a new vector, linear combination of this vector and the other
vector : a*this + b*other.
- * The other vector is expected to have the same dimension as this vector.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combine(double a, Vector<?> other, double b);
-
- /**
- * Same as {@link #combine(double, Vector, double)} but computed with
compensated arithmetic.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combineAccurate(double a, Vector<?> other, double b);
-
@Override
public T normalize() {
return scale(1.0 / length());
@@ -385,7 +364,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Byte combine(double a, Vector<?> other, double b) {
+ public Byte combine(double a, Vector<?> other, double b) {
return new Byte(
(byte) (a * x + b * other.get(0)),
(byte) (a * y + b * other.get(1)),
@@ -393,7 +372,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Byte combineAccurate(double a, Vector<?> other, double b) {
+ public Byte combineAccurate(double a, Vector<?> other, double b) {
return new Byte(
(byte) (LinearCombination.of(a, x, b, other.get(0))),
(byte) (LinearCombination.of(a, y, b, other.get(1))),
@@ -579,7 +558,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected UByte combine(double a, Vector<?> other, double b) {
+ public UByte combine(double a, Vector<?> other, double b) {
return new UByte(
(byte) (a * (x & 0xFF) + b * other.get(0)),
(byte) (a * (y & 0xFF) + b * other.get(1)),
@@ -587,7 +566,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected UByte combineAccurate(double a, Vector<?> other, double b) {
+ public UByte combineAccurate(double a, Vector<?> other, double b) {
return new UByte(
(byte) (LinearCombination.of(a, (x & 0xFF), b,
other.get(0))),
(byte) (LinearCombination.of(a, (y & 0xFF), b,
other.get(1))),
@@ -773,7 +752,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Short combine(double a, Vector<?> other, double b) {
+ public Short combine(double a, Vector<?> other, double b) {
return new Short(
(short) (a * x + b * other.get(0)),
(short) (a * y + b * other.get(1)),
@@ -781,7 +760,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Short combineAccurate(double a, Vector<?> other, double b) {
+ public Short combineAccurate(double a, Vector<?> other, double b) {
return new Short(
(short) (LinearCombination.of(a, x, b, other.get(0))),
(short) (LinearCombination.of(a, y, b, other.get(1))),
@@ -979,7 +958,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected UShort combine(double a, Vector<?> other, double b) {
+ public UShort combine(double a, Vector<?> other, double b) {
return new UShort(
(short) (a * (x & 0xFFFF) + b * other.get(0)),
(short) (a * (y & 0xFFFF) + b * other.get(1)),
@@ -987,7 +966,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected UShort combineAccurate(double a, Vector<?> other, double b) {
+ public UShort combineAccurate(double a, Vector<?> other, double b) {
return new UShort(
(short) (LinearCombination.of(a, (x & 0xFFFF), b,
other.get(0))),
(short) (LinearCombination.of(a, (y & 0xFFFF), b,
other.get(1))),
@@ -1173,7 +1152,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Int combine(double a, Vector<?> other, double b) {
+ public Int combine(double a, Vector<?> other, double b) {
return new Int(
(int) (a * x + b * other.get(0)),
(int) (a * y + b * other.get(1)),
@@ -1181,7 +1160,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Int combineAccurate(double a, Vector<?> other, double b) {
+ public Int combineAccurate(double a, Vector<?> other, double b) {
return new Int(
(int) (LinearCombination.of(a, x, b, other.get(0))),
(int) (LinearCombination.of(a, y, b, other.get(1))),
@@ -1375,7 +1354,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected UInt combine(double a, Vector<?> other, double b) {
+ public UInt combine(double a, Vector<?> other, double b) {
return new UInt(
(int) (long) (a * Integer.toUnsignedLong(x) + b *
other.get(0)),
(int) (long) (a * Integer.toUnsignedLong(y) + b *
other.get(1)),
@@ -1383,7 +1362,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected UInt combineAccurate(double a, Vector<?> other, double b) {
+ public UInt combineAccurate(double a, Vector<?> other, double b) {
return new UInt(
(int) (long) (LinearCombination.of(a,
Integer.toUnsignedLong(x), b, other.get(0))),
(int) (long) (LinearCombination.of(a,
Integer.toUnsignedLong(y), b, other.get(1))),
@@ -1569,7 +1548,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Long combine(double a, Vector<?> other, double b) {
+ public Long combine(double a, Vector<?> other, double b) {
return new Long(
(long) (a * x + b * other.get(0)),
(long) (a * y + b * other.get(1)),
@@ -1577,7 +1556,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Long combineAccurate(double a, Vector<?> other, double b) {
+ public Long combineAccurate(double a, Vector<?> other, double b) {
return new Long(
(long) (LinearCombination.of(a, x, b, other.get(0))),
(long) (LinearCombination.of(a, y, b, other.get(1))),
@@ -1791,7 +1770,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Float combine(double a, Vector<?> other, double b) {
+ public Float combine(double a, Vector<?> other, double b) {
return new Float(
(float) (a * x + b * other.get(0)),
(float) (a * y + b * other.get(1)),
@@ -1799,7 +1778,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Float combineAccurate(double a, Vector<?> other, double b) {
+ public Float combineAccurate(double a, Vector<?> other, double b) {
return new Float(
(float) (LinearCombination.of(a, x, b, other.get(0))),
(float) (LinearCombination.of(a, y, b, other.get(1))),
@@ -2061,7 +2040,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Double combine(double a, Vector<?> other, double b) {
+ public Double combine(double a, Vector<?> other, double b) {
return new Double(
a * x + b * other.get(0),
a * y + b * other.get(1),
@@ -2069,7 +2048,7 @@ public abstract sealed class Vector3D<T extends
Vector3D<T>> extends AbstractVec
}
@Override
- protected Double combineAccurate(double a, Vector<?> other, double b) {
+ public Double combineAccurate(double a, Vector<?> other, double b) {
return new Double(
LinearCombination.of(a, x, b, other.get(0)),
LinearCombination.of(a, y, b, other.get(1)),
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java
index 432334b528..39ba1d9417 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java
@@ -40,27 +40,6 @@ public abstract sealed class Vector4D<T extends Vector4D<T>>
extends AbstractVec
return 4;
}
- /**
- * Create a new vector, linear combination of this vector and the other
vector : a*this + b*other.
- * The other vector is expected to have the same dimension as this vector.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combine(double a, Vector<?> other, double b);
-
- /**
- * Same as {@link #combine(double, Vector, double)} but computed with
compensated arithmetic.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combineAccurate(double a, Vector<?> other, double b);
-
@Override
public T normalize() {
return scale(1.0 / length());
@@ -278,7 +257,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Byte combine(double a, Vector<?> other, double b) {
+ public Byte combine(double a, Vector<?> other, double b) {
return new Byte(
(byte) (a * x + b * other.get(0)),
(byte) (a * y + b * other.get(1)),
@@ -287,7 +266,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Byte combineAccurate(double a, Vector<?> other, double b) {
+ public Byte combineAccurate(double a, Vector<?> other, double b) {
return new Byte(
(byte) (LinearCombination.of(a, x, b, other.get(0))),
(byte) (LinearCombination.of(a, y, b, other.get(1))),
@@ -486,7 +465,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected UByte combine(double a, Vector<?> other, double b) {
+ public UByte combine(double a, Vector<?> other, double b) {
return new UByte(
(byte) (a * (x & 0xFF) + b * other.get(0)),
(byte) (a * (y & 0xFF) + b * other.get(1)),
@@ -495,7 +474,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected UByte combineAccurate(double a, Vector<?> other, double b) {
+ public UByte combineAccurate(double a, Vector<?> other, double b) {
return new UByte(
(byte) (LinearCombination.of(a, (x & 0xFF), b,
other.get(0))),
(byte) (LinearCombination.of(a, (y & 0xFF), b,
other.get(1))),
@@ -694,7 +673,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Short combine(double a, Vector<?> other, double b) {
+ public Short combine(double a, Vector<?> other, double b) {
return new Short(
(short) (a * x + b * other.get(0)),
(short) (a * y + b * other.get(1)),
@@ -703,7 +682,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Short combineAccurate(double a, Vector<?> other, double b) {
+ public Short combineAccurate(double a, Vector<?> other, double b) {
return new Short(
(short) (LinearCombination.of(a, x, b, other.get(0))),
(short) (LinearCombination.of(a, y, b, other.get(1))),
@@ -917,7 +896,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected UShort combine(double a, Vector<?> other, double b) {
+ public UShort combine(double a, Vector<?> other, double b) {
return new UShort(
(short) (a * (x & 0xFFFF) + b * other.get(0)),
(short) (a * (y & 0xFFFF) + b * other.get(1)),
@@ -926,7 +905,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected UShort combineAccurate(double a, Vector<?> other, double b) {
+ public UShort combineAccurate(double a, Vector<?> other, double b) {
return new UShort(
(short) (LinearCombination.of(a, (x & 0xFFFF), b,
other.get(0))),
(short) (LinearCombination.of(a, (y & 0xFFFF), b,
other.get(1))),
@@ -1125,7 +1104,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Int combine(double a, Vector<?> other, double b) {
+ public Int combine(double a, Vector<?> other, double b) {
return new Int(
(int) (a * x + b * other.get(0)),
(int) (a * y + b * other.get(1)),
@@ -1134,7 +1113,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Int combineAccurate(double a, Vector<?> other, double b) {
+ public Int combineAccurate(double a, Vector<?> other, double b) {
return new Int(
(int) (LinearCombination.of(a, x, b, other.get(0))),
(int) (LinearCombination.of(a, y, b, other.get(1))),
@@ -1343,7 +1322,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected UInt combine(double a, Vector<?> other, double b) {
+ public UInt combine(double a, Vector<?> other, double b) {
return new UInt(
(int) (long) (a * Integer.toUnsignedLong(x) + b *
other.get(0)),
(int) (long) (a * Integer.toUnsignedLong(y) + b *
other.get(1)),
@@ -1352,7 +1331,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected UInt combineAccurate(double a, Vector<?> other, double b) {
+ public UInt combineAccurate(double a, Vector<?> other, double b) {
return new UInt(
(int) (long) (LinearCombination.of(a,
Integer.toUnsignedLong(x), b, other.get(0))),
(int) (long) (LinearCombination.of(a,
Integer.toUnsignedLong(y), b, other.get(1))),
@@ -1551,7 +1530,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Long combine(double a, Vector<?> other, double b) {
+ public Long combine(double a, Vector<?> other, double b) {
return new Long(
(long) (a * x + b * other.get(0)),
(long) (a * y + b * other.get(1)),
@@ -1560,7 +1539,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Long combineAccurate(double a, Vector<?> other, double b) {
+ public Long combineAccurate(double a, Vector<?> other, double b) {
return new Long(
(long) (LinearCombination.of(a, x, b, other.get(0))),
(long) (LinearCombination.of(a, y, b, other.get(1))),
@@ -1788,7 +1767,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Float combine(double a, Vector<?> other, double b) {
+ public Float combine(double a, Vector<?> other, double b) {
return new Float(
(float) (a * x + b * other.get(0)),
(float) (a * y + b * other.get(1)),
@@ -1797,7 +1776,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Float combineAccurate(double a, Vector<?> other, double b) {
+ public Float combineAccurate(double a, Vector<?> other, double b) {
return new Float(
(float) (LinearCombination.of(a, x, b, other.get(0))),
(float) (LinearCombination.of(a, y, b, other.get(1))),
@@ -2045,7 +2024,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Double combine(double a, Vector<?> other, double b) {
+ public Double combine(double a, Vector<?> other, double b) {
return new Double(
a * x + b * other.get(0),
a * y + b * other.get(1),
@@ -2054,7 +2033,7 @@ public abstract sealed class Vector4D<T extends
Vector4D<T>> extends AbstractVec
}
@Override
- protected Double combineAccurate(double a, Vector<?> other, double b) {
+ public Double combineAccurate(double a, Vector<?> other, double b) {
return new Double(
LinearCombination.of(a, x, b, other.get(0)),
LinearCombination.of(a, y, b, other.get(1)),
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java
index 77164c1d7d..c0eed22d2c 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/VectorND.java
@@ -35,27 +35,6 @@ public abstract sealed class VectorND<T extends VectorND<T>>
extends AbstractVec
public VectorND() {
}
- /**
- * Create a new vector, linear combination of this vector and the other
vector : a*this + b*other.
- * The other vector is expected to have the same dimension as this vector.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combine(double a, Vector<?> other, double b);
-
- /**
- * Same as {@link #combine(double, Vector, double)} but computed with
compensated arithmetic.
- *
- * @param a coefficient of this vector
- * @param other second vector
- * @param b coefficient of the other vector
- * @return new vector
- */
- protected abstract T combineAccurate(double a, Vector<?> other, double b);
-
@Override
public T normalize() {
return scale(1.0 / length());
@@ -214,7 +193,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Byte combine(double a, Vector<?> other, double b) {
+ public Byte combine(double a, Vector<?> other, double b) {
final Byte result = new Byte(values.length);
final byte[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -224,7 +203,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Byte combineAccurate(double a, Vector<?> other, double b) {
+ public Byte combineAccurate(double a, Vector<?> other, double b) {
final Byte result = new Byte(values.length);
final byte[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -381,7 +360,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected UByte combine(double a, Vector<?> other, double b) {
+ public UByte combine(double a, Vector<?> other, double b) {
final UByte result = new UByte(values.length);
final byte[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -391,7 +370,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected UByte combineAccurate(double a, Vector<?> other, double b) {
+ public UByte combineAccurate(double a, Vector<?> other, double b) {
final UByte result = new UByte(values.length);
final byte[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -548,7 +527,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Short combine(double a, Vector<?> other, double b) {
+ public Short combine(double a, Vector<?> other, double b) {
final Short result = new Short(values.length);
final short[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -558,7 +537,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Short combineAccurate(double a, Vector<?> other, double b) {
+ public Short combineAccurate(double a, Vector<?> other, double b) {
final Short result = new Short(values.length);
final short[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -715,7 +694,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected UShort combine(double a, Vector<?> other, double b) {
+ public UShort combine(double a, Vector<?> other, double b) {
final UShort result = new UShort(values.length);
final short[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -725,7 +704,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected UShort combineAccurate(double a, Vector<?> other, double b) {
+ public UShort combineAccurate(double a, Vector<?> other, double b) {
final UShort result = new UShort(values.length);
final short[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -882,7 +861,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Int combine(double a, Vector<?> other, double b) {
+ public Int combine(double a, Vector<?> other, double b) {
final Int result = new Int(values.length);
final int[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -892,7 +871,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Int combineAccurate(double a, Vector<?> other, double b) {
+ public Int combineAccurate(double a, Vector<?> other, double b) {
final Int result = new Int(values.length);
final int[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1049,7 +1028,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected UInt combine(double a, Vector<?> other, double b) {
+ public UInt combine(double a, Vector<?> other, double b) {
final UInt result = new UInt(values.length);
final int[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1059,7 +1038,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected UInt combineAccurate(double a, Vector<?> other, double b) {
+ public UInt combineAccurate(double a, Vector<?> other, double b) {
final UInt result = new UInt(values.length);
final int[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1216,7 +1195,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Long combine(double a, Vector<?> other, double b) {
+ public Long combine(double a, Vector<?> other, double b) {
final Long result = new Long(values.length);
final long[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1226,7 +1205,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Long combineAccurate(double a, Vector<?> other, double b) {
+ public Long combineAccurate(double a, Vector<?> other, double b) {
final Long result = new Long(values.length);
final long[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1394,7 +1373,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Float combine(double a, Vector<?> other, double b) {
+ public Float combine(double a, Vector<?> other, double b) {
final Float result = new Float(values.length);
final float[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1404,7 +1383,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Float combineAccurate(double a, Vector<?> other, double b) {
+ public Float combineAccurate(double a, Vector<?> other, double b) {
final Float result = new Float(values.length);
final float[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1607,7 +1586,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Double combine(double a, Vector<?> other, double b) {
+ public Double combine(double a, Vector<?> other, double b) {
final Double result = new Double(values.length);
final double[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
@@ -1617,7 +1596,7 @@ public abstract sealed class VectorND<T extends
VectorND<T>> extends AbstractVec
}
@Override
- protected Double combineAccurate(double a, Vector<?> other, double b) {
+ public Double combineAccurate(double a, Vector<?> other, double b) {
final Double result = new Double(values.length);
final double[] dest = result.values;
for (int i = 0; i < dest.length; i++) {
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vectors.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vectors.java
index 906ffc1d1f..234619be81 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vectors.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vectors.java
@@ -443,26 +443,49 @@ public final class Vectors {
/**
* Computes the shortest angle between given vectors.
- * formula : acos(dot(vector,other)/(length(vector)*length(other)))
+ * formula : 2·atan2(‖a·|b| − b·|a|‖, ‖a·|b| + b·|a|‖), which is
2·atan2(‖a−b‖, ‖a+b‖) for unit vectors.
+ * Unlike the arc cosine of the normalized dot product, it keeps full
precision over the whole [0 … π] range,
+ * including nearly parallel and nearly opposite vectors.
*
* @param vector input vector
* @param other second vector
- * @return shortest angle in radian
+ * @return shortest angle in radian, or NaN if a vector has a length of
zero
+ * @see "Kahan, How Futile are Mindless Assessments of Roundoff in
Floating-Point Computation?, section 12"
*/
public static double shortestAngle(final double[] vector, final double[]
other){
- return Math.acos(cos( vector, other ));
+ if (vector.length != other.length) {
+ throw new IllegalArgumentException("Both vectors must have same
length.");
+ }
+ final double lengthVector = length(vector);
+ final double lengthOther = length(other);
+ if (lengthVector == 0 || lengthOther == 0) {
+ return Double.NaN;
+ }
+ double difference = 0;
+ double sum = 0;
+ for (int i = 0; i < vector.length; i++) {
+ final double a = vector[i] * lengthOther;
+ final double b = other[i] * lengthVector;
+ difference += (a - b) * (a - b);
+ sum += (a + b) * (a + b);
+ }
+ return 2 * Math.atan2(Math.sqrt(difference), Math.sqrt(sum));
}
/**
* Computes the shortest angle between given vectors.
- * formula : acos(dot(vector,other)/(length(vector)*length(other)))
+ * See {@link #shortestAngle(double[], double[])} for the formula,
computed with {@code double} precision.
*
* @param vector input vector
* @param other second vector
- * @return shortest angle in radian
+ * @return shortest angle in radian, or NaN if a vector has a length of
zero
*/
public static float shortestAngle(final float[] vector, final float[]
other){
- return (float)Math.acos(cos( vector, other ));
+ final double[] v = new double[vector.length];
+ final double[] o = new double[other.length];
+ for (int i = 0; i < v.length; i++) v[i] = vector[i];
+ for (int i = 0; i < o.length; i++) o[i] = other[i];
+ return (float) shortestAngle(v, o);
}
/**
@@ -1271,19 +1294,17 @@ public final class Vectors {
}
/*
- * Angle between the two directions. Dividing by the lengths keeps the
angle right
- * for vectors which are not unit vectors. A vector of length zero has
no direction,
- * which the parallel case below handles as a linear interpolation.
+ * Angle between the two directions, with full precision for nearly
parallel and nearly
+ * opposite vectors. A vector of length zero has no direction, which
the parallel case
+ * below handles as a linear interpolation.
*/
final double lengthStart = length(start);
final double lengthEnd = length(end);
- final double lengths = lengthStart * lengthEnd;
- final double cosAngle = (lengths > 0) ? Maths.clamp(dot(start, end) /
lengths, -1, 1) : 1;
- final double angle = Math.acos(cosAngle);
+ final double angle = (lengthStart > 0 && lengthEnd > 0) ?
shortestAngle(start, end) : 0;
final double sinAngle = Math.sin(angle);
if (sinAngle < 1E-9) {
- if (cosAngle < 0) {
+ if (angle > Math.PI / 2) {
throw new IllegalArgumentException("Cannot interpolate between
vectors pointing in "
+ "opposite directions, they define no unique arc.");
}
diff --git
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/GreatCircleArcTest.java
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/GreatCircleArcTest.java
index bb1e57be79..96a4424e84 100644
---
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/GreatCircleArcTest.java
+++
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/GreatCircleArcTest.java
@@ -59,6 +59,20 @@ public class GreatCircleArcTest {
assertEquals(0, emptyArc.getLength(), TOLERANCE);
}
+ /**
+ * Length of short arcs on the Earth, where the arc cosine of the dot
product loses most digits.
+ */
+ @Test
+ public void getLengthShortTest() {
+ final double radius = 6371007.2;
+ for (final double length : new double[] {1000, 10, 0.1}) {
+ final double angle = length / radius;
+ final GreatCircleArc arc = new GreatCircleArc(new DefaultSphere(3,
radius), a,
+ new Vector3D.Double(Math.cos(angle), Math.sin(angle), 0));
+ assertEquals(length, arc.getLength(), length * 1e-12);
+ }
+ }
+
/**
* Interpolation must land on the arc ends, on the arc middle, and space
the points
* evenly in angle along the way.
@@ -109,4 +123,22 @@ public class GreatCircleArcTest {
assertThrows(IllegalArgumentException.class, () ->
antipodal.pointAt(0.5));
assertThrows(IllegalArgumentException.class, () ->
antipodal.pointAt(0.25));
}
+
+ /**
+ * Sampling returns the interior points at regular fractions of the arc,
at most the spacing apart.
+ */
+ @Test
+ public void sampleTest() {
+ final GreatCircleArc arc = new GreatCircleArc(new DefaultSphere(3), a,
b);
+ final Vector<?>[] samples = arc.sample(Math.PI / 8);
+ assertEquals(3, samples.length);
+ for (int i = 0; i < samples.length; i++) {
+ final double angle = (i + 1) * Math.PI / 8;
+ assertArrayEquals(new double[] {Math.cos(angle), Math.sin(angle),
0}, samples[i].toArrayDouble(), TOLERANCE);
+ }
+ // a spacing longer than the arc gives no interior point
+ assertEquals(0, arc.sample(Math.PI).length);
+ // the spacing is in the units of the sphere radius
+ assertEquals(3, new GreatCircleArc(new DefaultSphere(3, 2), a,
b).sample(Math.PI / 4).length);
+ }
}
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
index 06a77a4696..effe302564 100644
---
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
@@ -86,4 +86,45 @@ public class SphericalConvexPolygonTest {
assertFalse(polygon.contains(new Vector3D.Double(0, -1, 0)));
}
+
+ /**
+ * The margin is positive inside, zero on a corner and negative outside.
+ */
+ @Test
+ public void marginTest() {
+ assertTrue(polygon.margin(new Vector3D.Double(0, 0, 1)) > 0);
+ assertEquals(0, polygon.margin(a), 1e-15);
+ assertTrue(polygon.margin(new Vector3D.Double(0, 0, -1)) < 0);
+ assertTrue(polygon.margin(new Vector3D.Double(1, 1, 0).normalize()) <
0);
+ // a point nearer to the center is further inside
+ assertTrue(polygon.margin(new Vector3D.Double(0, 0, 1)) >
polygon.margin(new Vector3D.Double(0.2, 0, 1).normalize()));
+ }
+
+ /**
+ * Area of the square cap : four right triangles at the pole with legs of
45°,
+ * each having an excess E with tan(E/2) = tan²(PI/8).
+ */
+ @Test
+ public void getAreaTest() {
+ final double t = Math.tan(Math.PI / 8);
+ assertEquals(4 * 2 * Math.atan(t * t), polygon.getArea(), 1e-14);
+ final SphericalConvexPolygon bigger = new SphericalConvexPolygon(new
DefaultSphere(3, 3), points);
+ assertEquals(9 * polygon.getArea(), bigger.getArea(), 1e-13);
+ }
+
+ /**
+ * Densified boundary : corners at every second point, edge middles in
between, closed ring.
+ */
+ @Test
+ public void getBoundaryTest() {
+ final Array boundary = polygon.getBoundary(2);
+ assertEquals(9, boundary.getLength());
+ final Vector3D.Double[] corners = {a, b, c, d, a};
+ for (int i = 0; i < 9; i++) {
+ final double[] expected = (i % 2 == 0)
+ ? corners[i / 2].toArrayDouble()
+ : corners[i / 2].add(corners[i / 2 +
1]).normalize().toArrayDouble();
+ assertArrayEquals(expected, boundary.get(i).toArrayDouble(),
1e-15);
+ }
+ }
}
diff --git
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalPolygonTest.java
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalPolygonTest.java
new file mode 100644
index 0000000000..d1a8e59680
--- /dev/null
+++
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/spherical/SphericalPolygonTest.java
@@ -0,0 +1,156 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+import org.apache.sis.geometries.internal.shared.DefaultSphere;
+import org.apache.sis.maths.Array;
+import org.apache.sis.maths.DataType;
+import org.apache.sis.maths.NDArrays;
+import org.apache.sis.maths.Vector;
+import org.apache.sis.maths.Vector3D;
+
+// Test dependencies
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+
+/**
+ * Tests for {@link SphericalPolygon}.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public class SphericalPolygonTest {
+
+ private static final double TOLERANCE = 1e-12;
+
+ /**
+ * Unit vector of a position given in degrees.
+ */
+ private static Vector3D.Double point(double lon, double lat) {
+ return Vector3D.fromLatLon(Math.toRadians(lat), Math.toRadians(lon));
+ }
+
+ /**
+ * Vertices of a ring given as (lon, lat) pairs in degrees, not closed.
+ */
+ private static Vector3D.Double[] vertices(double... lonLat) {
+ final Vector3D.Double[] vertices = new Vector3D.Double[lonLat.length /
2];
+ for (int i = 0; i < vertices.length; i++) {
+ vertices[i] = point(lonLat[2 * i], lonLat[2 * i + 1]);
+ }
+ return vertices;
+ }
+
+ /**
+ * Closed ring array of the given vertices.
+ */
+ private static Array ring(Vector<?>... vertices) {
+ final List<Vector<?>> points = new ArrayList<>(List.of(vertices));
+ points.add(vertices[0]);
+ return NDArrays.of(points, 3, DataType.DOUBLE);
+ }
+
+ /**
+ * Square of 10° with a notch from its top side: concave,
counter-clockwise seen from outside.
+ */
+ private static final Vector3D.Double[] NOTCHED = vertices(0, 0, 10, 0, 10,
10, 6, 10, 6, 4, 4, 4, 4, 10, 0, 10);
+
+ /**
+ * Containment in a concave ring : inside both arms and the base, outside
the notch.
+ */
+ @Test
+ public void ringContainsConcaveTest() {
+ assertTrue (SphericalPolygon.ringContains(NOTCHED, point(2, 5)));
+ assertTrue (SphericalPolygon.ringContains(NOTCHED, point(8, 5)));
+ assertTrue (SphericalPolygon.ringContains(NOTCHED, point(5, 2)));
+ assertFalse(SphericalPolygon.ringContains(NOTCHED, point(5, 7)),
"notch");
+ assertFalse(SphericalPolygon.ringContains(NOTCHED, point(20, 5)));
+ assertFalse(SphericalPolygon.ringContains(NOTCHED, point(5, -5)));
+ }
+
+ /**
+ * Containment in a polygon with a hole.
+ */
+ @Test
+ public void containsWithHoleTest() {
+ final SphericalPolygon polygon = new SphericalPolygon(new
DefaultSphere(3),
+ ring(vertices(0, 0, 10, 0, 10, 10, 0, 10)),
+ ring(vertices(4, 4, 6, 4, 6, 6, 4, 6)));
+ assertTrue (polygon.contains(point(2, 2)));
+ assertFalse(polygon.contains(point(5, 5)), "in the hole");
+ assertFalse(polygon.contains(point(15, 5)));
+ assertEquals(1, polygon.getInteriors().length);
+ }
+
+ /**
+ * Orientation of a ring, and the same ring reversed.
+ */
+ @Test
+ public void orientationTest() {
+ assertEquals(1, SphericalPolygon.orientation(NOTCHED));
+ final Vector3D.Double[] reversed = new Vector3D.Double[NOTCHED.length];
+ for (int i = 0; i < reversed.length; i++) {
+ reversed[i] = NOTCHED[NOTCHED.length - 1 - i];
+ }
+ assertEquals(-1, SphericalPolygon.orientation(reversed));
+ }
+
+ /**
+ * Edge normals are the cross products of consecutive vertices, the last
edge closing the ring.
+ */
+ @Test
+ public void edgeNormalsTest() {
+ final Vector<?>[] ring = {new Vector3D.Double(1, 0, 0), new
Vector3D.Double(0, 1, 0), new Vector3D.Double(0, 0, 1)};
+ final Vector<?>[] normals = SphericalPolygon.edgeNormals(ring);
+ assertArrayEquals(new double[] {0, 0, 1}, normals[0].toArrayDouble(),
TOLERANCE);
+ assertArrayEquals(new double[] {1, 0, 0}, normals[1].toArrayDouble(),
TOLERANCE);
+ assertArrayEquals(new double[] {0, 1, 0}, normals[2].toArrayDouble(),
TOLERANCE);
+ }
+
+ /**
+ * Area of the octant, of a polygon with a hole, and on a sphere of radius
2.
+ */
+ @Test
+ public void getAreaTest() {
+ final Vector3D.Double a = new Vector3D.Double(1, 0, 0);
+ final Vector3D.Double b = new Vector3D.Double(0, 1, 0);
+ final Vector3D.Double c = new Vector3D.Double(0, 0, 1);
+ assertEquals(Math.PI / 2, new SphericalPolygon(new DefaultSphere(3),
ring(a, b, c)).getArea(), TOLERANCE);
+ assertEquals(Math.PI / 2, new SphericalPolygon(new DefaultSphere(3),
ring(c, b, a)).getArea(), TOLERANCE, "winding");
+ assertEquals(2 * Math.PI, new SphericalPolygon(new DefaultSphere(3,
2), ring(a, b, c)).getArea(), TOLERANCE);
+
+ final Vector3D.Double[] hole = vertices(30, 30, 60, 30, 45, 50);
+ final double holeArea = new SphericalTriangle(new DefaultSphere(3),
hole[0], hole[1], hole[2]).getSphericalExcess();
+ final SphericalPolygon withHole = new SphericalPolygon(new
DefaultSphere(3), ring(a, b, c), ring(hole));
+ assertEquals(Math.PI / 2 - holeArea, withHole.getArea(), TOLERANCE);
+ }
+
+ /**
+ * The signed excess of a ring is positive counter-clockwise and sums the
fan triangles.
+ */
+ @Test
+ public void excessTest() {
+ final Vector3D.Double[] square = vertices(0, 0, 10, 0, 10, 10, 0, 10);
+ final double expected = SphericalTriangle.signedExcess(square[0],
square[1], square[2])
+ + SphericalTriangle.signedExcess(square[0],
square[2], square[3]);
+ assertEquals(expected, SphericalPolygon.excess(square), 0);
+ assertTrue(expected > 0);
+ assertEquals(-expected, SphericalPolygon.excess(new Vector3D.Double[]
{square[3], square[2], square[1], square[0]}), TOLERANCE);
+ }
+}
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
index b31a282805..54d2e04ca0 100644
---
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
@@ -17,6 +17,7 @@
package org.apache.sis.geometries.spherical;
import org.apache.sis.geometries.internal.shared.DefaultSphere;
+import org.apache.sis.maths.Array;
import org.apache.sis.maths.Vector;
import org.apache.sis.maths.Vector3D;
@@ -75,6 +76,24 @@ public class SphericalTriangleTest {
assertEquals(Math.PI / 2, triangle.getSphericalExcess(), TOLERANCE);
}
+ /**
+ * Spherical excess of small triangles, where summing the three angles and
subtracting PI loses
+ * most digits. A right triangle at the pole with two legs of angle s has
an excess E with
+ * tan(E/2) = tan²(s/2). The excess is positive whatever the winding of
the corners.
+ */
+ @Test
+ public void getSphericalExcessSmallTest() {
+ for (final double s : new double[] {1e-2, 1e-4, 1e-6}) {
+ final Vector<?> pole = new Vector3D.Double(0, 0, 1);
+ final Vector<?> east = new Vector3D.Double(Math.sin(s), 0,
Math.cos(s));
+ final Vector<?> north = new Vector3D.Double(0, Math.sin(s),
Math.cos(s));
+ final double t = Math.tan(s / 2);
+ final double expected = 2 * Math.atan(t * t);
+ assertEquals(expected, new SphericalTriangle(new DefaultSphere(3),
pole, east, north).getSphericalExcess(), expected * 1e-9);
+ assertEquals(expected, new SphericalTriangle(new DefaultSphere(3),
pole, north, east).getSphericalExcess(), expected * 1e-9);
+ }
+ }
+
/**
* Area test.
* The octant triangle covers 1/8th of the sphere surface (4*PI*r^2),
@@ -210,4 +229,40 @@ public class SphericalTriangleTest {
assertThrows(IllegalArgumentException.class, () ->
triangle.subdivide(0));
assertThrows(IllegalArgumentException.class, () ->
triangle.subdivide(-1));
}
+
+ /**
+ * The margin is positive inside, zero on a corner and negative outside.
+ */
+ @Test
+ public void marginTest() {
+ final SphericalTriangle triangle = new SphericalTriangle(new
DefaultSphere(3), a, b, c);
+ assertTrue(triangle.margin(triangle.getCentroidVector()) > 0);
+ assertEquals(0, triangle.margin(a), 1e-15);
+ assertTrue(triangle.margin(new Vector3D.Double(-1, -1,
-1).normalize()) < 0);
+ }
+
+ /**
+ * Densified boundary : the corners, then the points at thirds of each
edge, closed ring.
+ */
+ @Test
+ public void getBoundaryTest() {
+ final SphericalTriangle triangle = new SphericalTriangle(new
DefaultSphere(3), a, b, c);
+ final Array boundary = triangle.getBoundary(3);
+ assertEquals(10, boundary.getLength());
+ assertArrayEquals(a.toArrayDouble(), boundary.get(0).toArrayDouble(),
TOLERANCE);
+ final double third = Math.PI / 6;
+ assertArrayEquals(new double[] {Math.cos(third), Math.sin(third), 0},
boundary.get(1).toArrayDouble(), TOLERANCE);
+ assertArrayEquals(b.toArrayDouble(), boundary.get(3).toArrayDouble(),
TOLERANCE);
+ assertArrayEquals(c.toArrayDouble(), boundary.get(6).toArrayDouble(),
TOLERANCE);
+ assertArrayEquals(a.toArrayDouble(), boundary.get(9).toArrayDouble(),
TOLERANCE);
+ }
+
+ /**
+ * The signed excess is negative for clockwise corners.
+ */
+ @Test
+ public void signedExcessTest() {
+ assertEquals(Math.PI / 2, SphericalTriangle.signedExcess(a, b, c),
TOLERANCE);
+ assertEquals(-Math.PI / 2, SphericalTriangle.signedExcess(a, c, b),
TOLERANCE);
+ }
}
diff --git
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java
index f64ebb7d0f..40186eaa0b 100644
---
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java
+++
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/AbstractVectorTest.java
@@ -510,6 +510,8 @@ public abstract class AbstractVectorTest {
assertNewVector(vector, original, vector.multiply(second),
Vectors.multiply(v, s), TOLERANCE);
assertNewVector(vector, original, vector.divide(second),
Vectors.divide(v, s), TOLERANCE);
assertNewVector(vector, original, vector.scale(3),
Vectors.scale(v, 3), TOLERANCE);
+ assertNewVector(vector, original, vector.combine(1.0, second, 3),
Vectors.add(v, Vectors.scale(s, 3)), TOLERANCE);
+ assertEquals(Vectors.shortestAngle(v, s),
vector.shortestAngle(second), 1e-15, "shortestAngle");
assertNewVector(vector, original, vector.perpendicular(unit),
perpendicular, TOLERANCE);
assertNewVector(vector, original, vector.lerp(second, 0.5),
Vectors.lerp(v, s, 0.5, null), TOLERANCE);
assertNewVector(vector, original,
vector.transform(MathTransforms.scale(factors)), Vectors.scale(v, 3),
TOLERANCE);
diff --git
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorsTest.java
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorsTest.java
index 39f7f76e5e..619ac461f7 100644
---
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorsTest.java
+++
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorsTest.java
@@ -231,4 +231,59 @@ public class VectorsTest {
assertArrayEquals(new double[] {1, 0, 0}, start.toArrayDouble(),
SLERP_TOLERANCE);
assertArrayEquals(new double[] {0, 1, 0}, end.toArrayDouble(),
SLERP_TOLERANCE);
}
+
+ /**
+ * The shortest angle must be exact for usual angles and vectors of any
length.
+ */
+ @Test
+ public void shortestAngleTest() {
+ assertEquals(Math.PI / 2, Vectors.shortestAngle(new double[] {1, 0},
new double[] {0, 1}), 1e-15);
+ assertEquals(Math.PI / 2, Vectors.shortestAngle(new double[] {2, 0},
new double[] {0, 3}), 1e-15);
+ assertEquals(Math.PI / 4, Vectors.shortestAngle(new double[] {1, 0,
0}, new double[] {5, 5, 0}), 1e-15);
+ assertEquals(Math.PI, Vectors.shortestAngle(new double[] {1, 0,
0}, new double[] {-4, 0, 0}), 1e-15);
+ assertEquals(0, Vectors.shortestAngle(new double[] {1, 0,
0}, new double[] {7, 0, 0}), 0);
+ assertEquals((float) (Math.PI / 2), Vectors.shortestAngle(new float[]
{2, 0}, new float[] {0, 3}), 1e-6f);
+ assertTrue(Double.isNaN(Vectors.shortestAngle(new double[] {0, 0}, new
double[] {0, 1})), "a zero vector has no direction");
+ }
+
+ /**
+ * The shortest angle must keep its precision for nearly parallel and
nearly opposite vectors,
+ * where the arc cosine of the normalized dot product loses half of its
digits.
+ */
+ @Test
+ public void shortestAngleAccuracyTest() {
+ final double angle = 1e-10;
+ final double[] parallel = {3 * Math.cos(angle), 3 * Math.sin(angle),
0};
+ final double[] opposite = {-2 * Math.cos(angle), 2 * Math.sin(angle),
0};
+ assertEquals(angle, Vectors.shortestAngle(new double[] {1, 0, 0},
parallel), angle * 1e-6);
+ assertEquals(Math.PI - angle, Vectors.shortestAngle(new double[] {1,
0, 0}, opposite), 1e-15);
+ }
+
+ /**
+ * The shortest angle on vectors must be the one on arrays, with the same
precision.
+ */
+ @Test
+ public void shortestAngleOnVectorTest() {
+ final double angle = 1e-10;
+ final Vector3D.Double a = new Vector3D.Double(1, 0, 0);
+ final Vector3D.Double b = new Vector3D.Double(3 * Math.cos(angle), 3 *
Math.sin(angle), 0);
+ assertEquals(angle, a.shortestAngle(b), angle * 1e-6);
+ assertEquals(Vectors.shortestAngle(a.toArrayDouble(),
b.toArrayDouble()), a.shortestAngle(b), 0);
+ assertTrue(Double.isNaN(a.shortestAngle(new Vector3D.Double())), "a
zero vector has no direction");
+ }
+
+ /**
+ * Spherical interpolation between nearly opposite vectors needs the angle
with full precision,
+ * where the arc cosine of the normalized dot product loses digits.
+ */
+ @Test
+ public void slerpNearlyOppositeTest() {
+ final double delta = 1e-6;
+ final double[] start = {1, 0, 0};
+ final double[] end = {-Math.cos(delta), Math.sin(delta), 0};
+ final double half = (Math.PI - delta) / 2;
+ final double[] expected = {Math.cos(half), Math.sin(half), 0};
+ assertArrayEquals(expected, Vectors.slerp(start, end, 0.5, null),
1e-8);
+ assertArrayEquals(expected, new Vector3D.Double(start).slerp(new
Vector3D.Double(end), 0.5).toArrayDouble(), 1e-8);
+ }
}