This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 168148c9d946a3a37f979feee9e1ec4db382ce8b Author: jsorel <[email protected]> AuthorDate: Wed Nov 25 14:46:39 2026 +0100 feat(Geometry): add area,volume, centroid on sphere,capsule,bbox,obbox --- .../main/org/apache/sis/geometries/BBox.java | 85 ++++++++++ .../main/org/apache/sis/geometries/OBBox.java | 46 ++++++ .../geometries/internal/shared/DefaultCapsule.java | 6 - .../org/apache/sis/geometries/surface/Capsule.java | 135 +++++++++++++++ .../apache/sis/geometries/surface/Cylinder.java | 57 +++++++ .../apache/sis/geometries/surface/Ellipsoid.java | 37 +++++ .../org/apache/sis/geometries/surface/Sphere.java | 41 ++++- .../main/org/apache/sis/maths/Maths.java | 27 +++ .../main/org/apache/sis/maths/Vector.java | 18 ++ .../test/org/apache/sis/geometries/BBoxTest.java | 42 +++++ .../test/org/apache/sis/geometries/OBBoxTest.java | 68 ++++++++ .../apache/sis/geometries/surface/CapsuleTest.java | 182 +++++++++++++++++++++ .../sis/geometries/surface/CylinderTest.java | 122 ++++++++++++++ .../sis/geometries/surface/EllipsoidTest.java | 18 ++ .../apache/sis/geometries/surface/SphereTest.java | 25 +++ .../org/apache/sis/maths/AbstractVectorTest.java | 7 + .../test/org/apache/sis/maths/MathsTest.java | 14 ++ 17 files changed, 915 insertions(+), 15 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java index 6931866991..d145faff17 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java @@ -19,6 +19,7 @@ package org.apache.sis.geometries; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import javax.measure.Quantity; import org.opengis.coordinate.MismatchedDimensionException; import org.opengis.geometry.Envelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; @@ -30,6 +31,7 @@ import org.apache.sis.maths.NDArrays; import org.apache.sis.maths.SampleSystem; import org.apache.sis.maths.Vectors; import org.apache.sis.maths.Vector; +import org.apache.sis.measure.Quantities; /** @@ -256,6 +258,89 @@ public final class BBox extends GeneralEnvelope implements Geometry { return factory.createMultiPolygon(crs, polygons); } + /** + * Returns the median position of this box, or {@code null} if this box is empty. + * + * <ul> + * <li>On an axis with wraparound, the median of a box crossing the anti-meridian + * is on the anti-meridian side.</li> + * </ul> + * + * @see "ISO 19107:2019 section 6.4.4.8" + */ + @Override + public Point getCentroid() { + if (isEmpty()) { + return null; + } + final double[] median = new double[getDimension()]; + for (int i = 0; i < median.length; i++) { + median[i] = getMedian(i); + } + return GeometryFactory.DEFAULT.createPoint(getCoordinateReferenceSystem(), median); + } + + /** + * Returns the area of this box: the interior area in 2 dimensions, + * or the sum of the areas of the six faces in 3 dimensions. + * + * <ul> + * <li>An axis whose span is zero, negative or undefined contributes a length of zero.</li> + * <li>The area is planar, in the units of the coordinate system axes.</li> + * </ul> + * + * @return area of this box. + * @throws UnsupportedOperationException if this box has not 2 or 3 dimensions. + * + * @see "ISO 19107:2019 section 6.4.25.7" + * @see "ISO 19107:2019 section 6.4.28.3" + */ + public Quantity<?> getArea() { + final double area; + switch (getDimension()) { + case 2: { + area = length(0) * length(1); + break; + } + case 3: { + final double a = length(0), b = length(1), c = length(2); + area = 2 * (a*b + b*c + c*a); + break; + } + default: { + throw new UnsupportedOperationException("The area of a box is computed in 2 or 3 dimensions only."); + } + } + return Quantities.create(area, Geometries.getAreaUnit(this)); + } + + /** + * Returns the volume of this box. + * + * <ul> + * <li>An axis whose span is zero, negative or undefined contributes a length of zero.</li> + * </ul> + * + * @return volume of this box. + * @throws UnsupportedOperationException if this box has not 3 dimensions. + * + * @see "ISO 19107:2019 section 6.4.28.4" + */ + public Quantity<?> getVolume() { + if (getDimension() != 3) { + throw new UnsupportedOperationException("The volume of a box is computed in 3 dimensions only."); + } + return Quantities.create(length(0) * length(1) * length(2), Geometries.getVolumeUnit(this)); + } + + /** + * Returns the span of this box on the given axis, or zero if that span is not positive. + */ + private double length(final int dimension) { + final double span = getSpan(dimension); + return (span > 0) ? span : 0; + } + @Override public synchronized Map<String, Object> userProperties() { if (properties == null) { diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java index 92b15eb47e..384775e226 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries; +import javax.measure.Quantity; import org.opengis.geometry.Envelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.apache.sis.geometries.internal.shared.AbstractGeometry; @@ -24,6 +25,7 @@ import org.apache.sis.maths.DataType; import org.apache.sis.maths.NDRange; import org.apache.sis.maths.Vector; import org.apache.sis.maths.Vector3D; +import org.apache.sis.measure.Quantities; /** @@ -203,6 +205,50 @@ public final class OBBox extends AbstractGeometry { return BBox.boxFaces(getCoordinateReferenceSystem(), corners, leftHanded); } + /** + * Returns the {@linkplain #getCenter() center} of this box. + * + * @see "ISO 19107:2019 section 6.4.4.8" + */ + @Override + public Point getCentroid() { + return GeometryFactory.DEFAULT.createPoint(crs, center.toArrayDouble()); + } + + /** + * Returns the sum of the areas of the six faces of this box. + * + * <ul> + * <li>The axes need not be orthogonal: the faces are then parallelograms.</li> + * </ul> + * + * @return area of this box. + * + * @see "ISO 19107:2019 section 6.4.28.3" + */ + public Quantity<?> getArea() { + final double area = 8 * (xAxis.cross(yAxis).length() + + yAxis.cross(zAxis).length() + + zAxis.cross(xAxis).length()); + return Quantities.create(area, Geometries.getAreaUnit(this)); + } + + /** + * Returns the volume of this box. + * + * <ul> + * <li>The axes need not be orthogonal: the box is then a parallelepiped.</li> + * </ul> + * + * @return volume of this box. + * + * @see "ISO 19107:2019 section 6.4.28.4" + */ + public Quantity<?> getVolume() { + final double volume = 8 * Math.abs(xAxis.dot(yAxis.cross(zAxis))); + return Quantities.create(volume, Geometries.getVolumeUnit(this)); + } + /** * Build oriented bbox from given envelope. * @param env diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultCapsule.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultCapsule.java index f3c6d0d750..278f228041 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultCapsule.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultCapsule.java @@ -17,7 +17,6 @@ package org.apache.sis.geometries.internal.shared; import java.util.Objects; -import javax.measure.Quantity; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.apache.sis.geometries.DataPoints; import org.apache.sis.geometries.Geometries; @@ -97,11 +96,6 @@ public non-sealed class DefaultCapsule extends AbstractGeometry implements Capsu this.radiusBottom = radius; } - @Override - public Quantity<?> getArea() { - throw new UnsupportedOperationException("Not supported yet."); - } - @Override public boolean equals(Object obj) { if (obj == null) { diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java index 734f9eb402..fb66c8cc67 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java @@ -17,14 +17,17 @@ package org.apache.sis.geometries.surface; import java.util.List; +import javax.measure.Quantity; import static org.opengis.annotation.Specification.ISO_12113; import org.opengis.annotation.UML; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.geometry.Envelope; import org.apache.sis.geometries.DataPointsType; +import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.GeometryType; +import org.apache.sis.geometries.Point; import org.apache.sis.geometries.Representation; import org.apache.sis.geometries.Surface; import org.apache.sis.geometries.SurfaceInterpolation; @@ -32,6 +35,7 @@ import org.apache.sis.geometries.internal.shared.DefaultCapsule; import org.apache.sis.maths.DataType; import org.apache.sis.maths.NDRange; import org.apache.sis.maths.Vector; +import org.apache.sis.measure.Quantities; /** @@ -117,6 +121,137 @@ public sealed interface Capsule extends Surface return getDataPoints().getPosition(1).copy(DataType.DOUBLE).subtract(bottom).length(); } + /** + * Returns the area of this capsule. + * A capsule is the convex hull of its two spheres: a band of cone tangent to both spheres, + * between a cap of each sphere. With <var>d</var> the {@linkplain #getHeight() height} and + * <var>s</var> = (r₁ − r₂) ∕ <var>d</var> the sine of the cone half-angle, the area is the sum of: + * + * <ul> + * <li>the bottom cap, 2πr₁²(1 + <var>s</var>),</li> + * <li>the top cap, 2πr₂²(1 − <var>s</var>),</li> + * <li>the band, π(r₁ + r₂)<var>d</var>(1 − <var>s</var>²).</li> + * </ul> + * + * <p>A capsule whose larger sphere contains the smaller one is that larger sphere.</p> + * + * @throws UnsupportedOperationException if this capsule is not 3-dimensional. + */ + @Override + default Quantity<?> getArea() { + final double r1 = getRadiusBottom(); + final double r2 = getRadiusTop(); + final double[] areas = areas(r1, r2, getHeight()); + final double area; + if (areas != null) { + area = areas[0] + areas[1] + areas[2]; + } else { + final double r = Math.max(r1, r2); + area = 4 * r*r; + } + return Quantities.create(Math.PI * area, Geometries.getAreaUnit(this)); + } + + /** + * Returns the volume enclosed by this capsule. With the notation of {@link #getArea()}, + * the volume is the sum of: + * + * <ul> + * <li>the bottom spherical cap, πr₁³(1 + <var>s</var>)²(2 − <var>s</var>)∕3,</li> + * <li>the top spherical cap, πr₂³(1 − <var>s</var>)²(2 + <var>s</var>)∕3,</li> + * <li>the frustum between them, π<var>d</var>(1 − <var>s</var>²)²(r₁² + r₁r₂ + r₂²)∕3.</li> + * </ul> + * + * <p>Not in ISO 19107, which defines volumes on solids only. + * A capsule whose larger sphere contains the smaller one encloses the ball of that larger sphere.</p> + * + * @return volume enclosed by this capsule. + * @throws UnsupportedOperationException if this capsule is not 3-dimensional. + */ + default Quantity<?> getEnclosedVolume() { + final double r1 = getRadiusBottom(); + final double r2 = getRadiusTop(); + final double d = getHeight(); + final double volume; + if (areas(r1, r2, d) != null) { // Also verifies that this capsule is 3-dimensional. + final double s = (r1 - r2) / d; + final double c = 1 - s*s; + volume = r1*r1*r1 * (1 + s) * (1 + s) * (2 - s) + + r2*r2*r2 * (1 - s) * (1 - s) * (2 + s) + + d * c * c * (r1*r1 + r1*r2 + r2*r2); + } else { + final double r = Math.max(r1, r2); + volume = 4 * r*r*r; + } + return Quantities.create(Math.PI * volume / 3, Geometries.getVolumeUnit(this)); + } + + /** + * Returns the centroid of this surface, which is on the axis of this capsule. + * The two data points are interpolated, so the centroid gets their attributes. + * + * <ul> + * <li>The centroid is the mean of the centroids of the two caps and of the band + * described in {@link #getArea()}, weighted by their areas.</li> + * <li>It may be outside the segment joining the two data points when the radii differ.</li> + * <li>A capsule whose larger sphere contains the smaller one has the center of that sphere as centroid.</li> + * <li>A capsule without area has its centroid at the middle of its axis.</li> + * </ul> + * + * @throws UnsupportedOperationException if this capsule is not 3-dimensional. + * + * @see "ISO 19107:2019 section 6.4.4.8" + */ + @Override + default Point getCentroid() { + final double r1 = getRadiusBottom(); + final double r2 = getRadiusTop(); + final double d = getHeight(); + final double[] areas = areas(r1, r2, d); + final double ratio; + if (areas == null) { + ratio = (r1 >= r2) ? 0 : 1; + } else { + final double area = areas[0] + areas[1] + areas[2]; + if (area > 0) { + /* + * Positions along the axis, from the bottom center, of the centroids of each part. + * The area of a spherical zone is uniform along the axis, so the centroid of a cap + * is in the middle of its extent. The band is the lateral surface of a frustum. + */ + final double s = (r1 - r2) / d; + final double bottom = r1 * (s - 1) / 2; + final double top = d + r2 * (s + 1) / 2; + final double band = r1 * s + d * (1 - s*s) * (r1 + 2*r2) / (3 * (r1 + r2)); + ratio = (areas[0] * bottom + areas[1] * top + areas[2] * band) / (area * d); + } else { + ratio = 0.5; + } + } + return getDataPoints().getInterpolatedPoint(0, 1, ratio); + } + + /** + * Returns the areas divided by π of the bottom cap, the top cap and the band joining them, + * or {@code null} if the larger sphere contains the smaller one. + * + * @throws UnsupportedOperationException if this capsule is not 3-dimensional. + */ + private double[] areas(final double r1, final double r2, final double d) { + if (getDataPoints().getDimension() != 3) { + throw new UnsupportedOperationException("The measures of a capsule are computed in 3 dimensions only."); + } + if (d <= Math.abs(r1 - r2)) { + return null; + } + final double s = (r1 - r2) / d; + return new double[] { + 2 * r1*r1 * (1 + s), + 2 * r2*r2 * (1 - s), + (r1 + r2) * d * (1 - s*s) + }; + } + /** * {@inheritDoc} * diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java index edbf33d7e2..88f2ceacd1 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Cylinder.java @@ -28,6 +28,7 @@ import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.GeometryType; +import org.apache.sis.geometries.Point; import org.apache.sis.geometries.Representation; import org.apache.sis.geometries.Surface; import org.apache.sis.geometries.SurfaceInterpolation; @@ -230,6 +231,62 @@ public sealed interface Cylinder extends Surface return Quantities.create(area, Geometries.getAreaUnit(this)); } + /** + * Returns the volume enclosed by this cylinder, πh(r₁² + r₁r₂ + r₂²)∕3 where <var>h</var> + * is the {@linkplain #getHeight() height}. + * + * <ul> + * <li>Not in ISO 19107, which defines volumes on solids only.</li> + * <li>An open cylinder gives the volume it would enclose if it were {@linkplain #isClosed() closed}.</li> + * </ul> + * + * @return volume enclosed by this cylinder. + * @throws UnsupportedOperationException if this cylinder is not 3-dimensional. + */ + default Quantity<?> getEnclosedVolume() { + if (getDataPoints().getDimension() != 3) { + throw new UnsupportedOperationException("The volume of a cylinder is computed in 3 dimensions only."); + } + final double r1 = getRadiusBottom(); + final double r2 = getRadiusTop(); + final double volume = Math.PI * getHeight() * (r1*r1 + r1*r2 + r2*r2) / 3; + return Quantities.create(volume, Geometries.getVolumeUnit(this)); + } + + /** + * Returns the centroid of this surface, which is on the axis of this cylinder. + * The two data points are interpolated, so the centroid gets their attributes. + * + * <ul> + * <li>The lateral surface has its centroid at the fraction (r₁ + 2r₂) ∕ 3(r₁ + r₂) + * of the axis, from the bottom.</li> + * <li>The end disks of a {@linkplain #isClosed() closed} cylinder are weighted by their areas.</li> + * <li>A cylinder without area has its centroid at the middle of its axis.</li> + * </ul> + * + * @throws UnsupportedOperationException if this cylinder is not 3-dimensional. + * + * @see "ISO 19107:2019 section 6.4.4.8" + */ + @Override + default Point getCentroid() { + if (getDataPoints().getDimension() != 3) { + throw new UnsupportedOperationException("The centroid of a cylinder is computed in 3 dimensions only."); + } + final double r1 = getRadiusBottom(); + final double r2 = getRadiusTop(); + final double slant = Math.hypot(getHeight(), r1 - r2); + // Areas and moments along the axis, all divided by π and by the height. + double area = (r1 + r2) * slant; + double moment = (r1 + 2*r2) * slant / 3; + if (isClosed()) { + area += r1*r1 + r2*r2; + moment += r2*r2; + } + final double ratio = (area > 0) ? moment / area : 0.5; + return getDataPoints().getInterpolatedPoint(0, 1, ratio); + } + /** * Returns the empty geometry if this cylinder is {@linkplain #isClosed() closed}, * or the circles at its ends otherwise. diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Ellipsoid.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Ellipsoid.java index 5f62717af1..d7c8e7ca5c 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Ellipsoid.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Ellipsoid.java @@ -17,19 +17,24 @@ package org.apache.sis.geometries.surface; import java.util.List; +import javax.measure.Quantity; import static org.opengis.annotation.Specification.ISO_19107; import org.opengis.annotation.UML; import org.opengis.geometry.Envelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.apache.sis.geometries.DataPointsType; +import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.GeometryType; +import org.apache.sis.geometries.Point; import org.apache.sis.geometries.Representation; import org.apache.sis.geometries.SurfaceInterpolation; import org.apache.sis.geometries.internal.shared.DefaultEllipsoid; +import org.apache.sis.maths.Maths; import org.apache.sis.maths.NDRange; import org.apache.sis.maths.Vector; +import org.apache.sis.measure.Quantities; /** @@ -153,6 +158,38 @@ public sealed interface Ellipsoid extends ParametricCurveSurface */ void setCenter(Vector<?> position); + /** + * Returns the {@linkplain #getCenter() center} of this ellipsoid, with the attributes of its data point. + * + * @see "ISO 19107:2019 section 6.4.4.8" + */ + @Override + default Point getCentroid() { + return getDataPoints().getInterpolatedPoint(new int[] {0}, new double[] {1}); + } + + /** + * Returns the volume enclosed by this ellipsoid, which is 4π<var>abc</var>∕3 in a 3-dimensional space. + * + * <ul> + * <li>Not in ISO 19107, which defines volumes on solids only.</li> + * <li>In an <var>n</var>-dimensional space, this is the volume of the unit <var>n</var>-ball times + * the product of the semi-axes, expressed in the linear unit raised to the power <var>n</var>.</li> + * </ul> + * + * @return volume enclosed by this ellipsoid. + * + * @see Maths#unitBallVolume(int) + */ + default Quantity<?> getEnclosedVolume() { + final double[] semiAxes = getSemiAxes(); + double volume = Maths.unitBallVolume(semiAxes.length); + for (final double semiAxis : semiAxes) { + volume *= semiAxis; + } + return Quantities.create(volume, Geometries.getLinearUnit(this).pow(semiAxes.length)); + } + /** * Returns the empty geometry: an ellipsoid is a closed surface, so it has no edge. */ diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java index d7ce3bb52b..14541e4796 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java @@ -27,9 +27,11 @@ import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.GeometryType; +import org.apache.sis.geometries.Point; import org.apache.sis.geometries.Representation; import org.apache.sis.geometries.SurfaceInterpolation; import org.apache.sis.geometries.internal.shared.DefaultSphere; +import org.apache.sis.maths.Maths; import org.apache.sis.maths.NDRange; import org.apache.sis.maths.Vector; import org.apache.sis.measure.Quantities; @@ -135,18 +137,39 @@ public sealed interface Sphere extends ParametricCurveSurface @Override default Quantity<?> getArea() { final int n = getDimension(); - /* - * Measure of the unit (n−1)-sphere, from S(1) = 2 and S(2) = 2π with the recurrence - * S(n+2) = 2π⋅S(n)/n, which avoids computing the Gamma function. - */ - int k = (n % 2 == 0) ? 2 : 1; - double measure = (k == 2) ? 2*Math.PI : 2; - for (; k + 2 <= n; k += 2) { - measure *= 2*Math.PI / k; - } + final double measure = n * Maths.unitBallVolume(n); // Measure of the unit (n−1)-sphere. return Quantities.create(measure * Math.pow(getRadius(), n - 1), Geometries.getLinearUnit(this).pow(n - 1)); } + /** + * Returns the volume of the ball bounded by this sphere, which is 4πr³∕3 in a 3-dimensional space. + * + * <ul> + * <li>Not in ISO 19107, which defines volumes on solids only.</li> + * <li>In an <var>n</var>-dimensional space, this is the volume of the <var>n</var>-ball + * expressed in the linear unit raised to the power <var>n</var>. + * In a 2-dimensional space, it is therefore the area πr² of the disk.</li> + * </ul> + * + * @return volume enclosed by this sphere. + * + * @see Maths#unitBallVolume(int) + */ + default Quantity<?> getEnclosedVolume() { + final int n = getDimension(); + return Quantities.create(Maths.unitBallVolume(n) * Math.pow(getRadius(), n), Geometries.getLinearUnit(this).pow(n)); + } + + /** + * Returns the {@linkplain #getCenter() center} of this sphere, with the attributes of its data point. + * + * @see "ISO 19107:2019 section 6.4.4.8" + */ + @Override + default Point getCentroid() { + return getDataPoints().getInterpolatedPoint(new int[] {0}, new double[] {1}); + } + /** * @return radius of the sphere. */ diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java index 14f5a35a56..faea9f74f1 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java @@ -59,6 +59,33 @@ public final class Maths { */ private static final double INCIRCLE_ERROR_FACTOR = 8.0; + /** + * Returns the volume of the ball of radius 1 in a space of the given dimension: + * 2 in 1 dimension, π in 2 dimensions, 4π∕3 in 3 dimensions. + * + * <ul> + * <li>The measure of the unit sphere bounding that ball is <var>n</var> times this volume.</li> + * <li>Computed by the recurrence <var>V</var>(<var>n</var>) = 2π∕<var>n</var>⋅<var>V</var>(<var>n</var>−2) + * from <var>V</var>(0) = 1 and <var>V</var>(1) = 2, which avoids the Gamma function.</li> + * </ul> + * + * @param dimension number of dimensions of the space, zero or positive. + * @return volume of the unit ball. + * @throws IllegalArgumentException if the dimension is negative. + */ + public static double unitBallVolume(final int dimension) { + if (dimension < 0) { + throw new IllegalArgumentException("Negative dimension: " + dimension); + } + int n = (dimension % 2 == 0) ? 0 : 1; + double volume = (n == 0) ? 1 : 2; + while (n < dimension) { + n += 2; + volume *= 2 * Math.PI / n; + } + return volume; + } + /** * Calculate normal of triangle made of given 3 points. * 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 d5596061fa..d983382adc 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,24 @@ public sealed interface Vector<T extends Vector<T>> permits AbstractVector { return sum; } + /** + * Rectilinear distance between this vector and the other vector, both seen as points. + * This is the sum of the absolute coordinate differences, also known as the Manhattan + * or taxicab distance. + * + * @param other second point, of the same dimension + * @return rectilinear distance + * @throws IllegalArgumentException if vectors do not have the same dimension + */ + default double distanceRectilinear(Vector<?> other) { + AbstractVector.ensureDimension(other, getDimension()); + double sum = 0; + for (int i = 0, n = getDimension(); i < n; i++) { + sum += Math.abs(get(i) - other.get(i)); + } + return sum; + } + /** * Computes the shortest angle between this vector and the other vector. * See {@link Vectors#shortestAngle(double[], double[])} for the formula, which keeps diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java index 556b32a158..0c952d68e9 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java @@ -16,8 +16,12 @@ */ package org.apache.sis.geometries; +import org.apache.sis.maths.Vector; + // Test dependencies import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -45,4 +49,42 @@ public class BBoxTest extends GeometryTest { final BBox bbox = new BBox(CRS_2D); assertEquals(-1, bbox.getTopologicDimension()); } + + /** + * The centroid of a box is its median, including across the anti-meridian. + */ + @Test + public void testGetCentroid() { + final Vector<?> centroid = new BBox(CRS_3D, 0,10,100, 10,30,200).getCentroid().getPosition(); + assertEquals( 5, centroid.get(0), TOLERANCE); + assertEquals( 20, centroid.get(1), TOLERANCE); + assertEquals(150, centroid.get(2), TOLERANCE); + final Vector<?> crossing = new BBox(CRS_2D, 170,0, -170,10).getCentroid().getPosition(); + assertEquals(180, Math.abs(crossing.get(0)), TOLERANCE); + assertEquals( 5, crossing.get(1), TOLERANCE); + assertNull(new BBox(CRS_2D).getCentroid()); + } + + /** + * The area of a box is its interior in 2 dimensions and its six faces in 3 dimensions. + */ + @Test + public void testGetArea() { + assertEquals(20, new BBox(CRS_2D, 0,0, 4,5).getArea().getValue().doubleValue(), TOLERANCE); + assertEquals(200, new BBox(CRS_2D, 170,0, -170,10).getArea().getValue().doubleValue(), TOLERANCE); + assertEquals(0, new BBox(CRS_2D).getArea().getValue().doubleValue()); + assertEquals(2 * (2*3 + 3*4 + 4*2), new BBox(CRS_3D, 0,0,0, 2,3,4).getArea().getValue().doubleValue(), TOLERANCE); + assertEquals(2 * 2*3, new BBox(CRS_3D, 0,0,0, 2,3,0).getArea().getValue().doubleValue(), TOLERANCE); + assertThrows(UnsupportedOperationException.class, () -> new BBox(4).getArea()); + } + + /** + * The volume of a box is defined in 3 dimensions only. + */ + @Test + public void testGetVolume() { + assertEquals(24, new BBox(CRS_3D, 0,0,0, 2,3,4).getVolume().getValue().doubleValue(), TOLERANCE); + assertEquals(0, new BBox(CRS_3D, 0,0,0, 2,3,0).getVolume().getValue().doubleValue()); + assertThrows(UnsupportedOperationException.class, () -> new BBox(CRS_2D, 0,0, 4,5).getVolume()); + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/OBBoxTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/OBBoxTest.java new file mode 100644 index 0000000000..83aed0c804 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/OBBoxTest.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.geometries; + +import org.apache.sis.maths.Vector; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link OBBox}. + * + * @author Johann Sorel (Geomatys) + */ +public class OBBoxTest extends GeometryTest { + + /** + * Creates a box of size 2√2 × 2√2 × 4 centered on (1, 2, 3), turned by 45° around the third axis. + */ + private static OBBox box() { + final OBBox box = new OBBox(Geometries.getUndefinedCRS(3)); + box.setCenter(Vector.of(1, 2, 3)); + box.setXAxis(Vector.of(1, 1, 0)); + box.setYAxis(Vector.of(-1, 1, 0)); + box.setZAxis(Vector.of(0, 0, 2)); + return box; + } + + @Test + public void testGetCentroid() { + final Vector<?> centroid = box().getCentroid().getPosition(); + assertEquals(1, centroid.get(0), TOLERANCE); + assertEquals(2, centroid.get(1), TOLERANCE); + assertEquals(3, centroid.get(2), TOLERANCE); + } + + @Test + public void testGetArea() { + assertEquals(2 * (8 + 2 * 8 * Math.sqrt(2)), box().getArea().getValue().doubleValue(), TOLERANCE); + } + + /** + * The volume is the same whether the axes are orthogonal or sheared. + */ + @Test + public void testGetVolume() { + final OBBox box = box(); + assertEquals(32, box.getVolume().getValue().doubleValue(), TOLERANCE); + box.setZAxis(Vector.of(1, 0, 2)); + assertEquals(32, box.getVolume().getValue().doubleValue(), TOLERANCE); + } +} diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CapsuleTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CapsuleTest.java new file mode 100644 index 0000000000..7934bca7ec --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CapsuleTest.java @@ -0,0 +1,182 @@ +/* + * 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.surface; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.apache.sis.geometries.Geometries; +import org.apache.sis.geometries.GeometryFactory; +import org.apache.sis.geometries.Point; +import org.apache.sis.maths.Vector; +import static org.apache.sis.geometries.surface.SphereTest.assertPosition; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import org.apache.sis.geometries.GeometryTest; + + +/** + * Tests the area and the centroid of {@link Capsule}. + * + * @author Johann Sorel (Geomatys) + */ +public class CapsuleTest extends GeometryTest { + + private static final CoordinateReferenceSystem CRS = Geometries.getUndefinedCRS(3); + + /** + * Relative tolerance on the measures computed by numerical integration. + */ + static final double INTEGRATION_TOLERANCE = 1E-5; + + /** + * Number of samples on each circle of the capsule profile. + */ + private static final int SAMPLES = 20000; + + private static Capsule capsule(final double r1, final double r2, final double... axis) { + final GeometryFactory f = GeometryFactory.DEFAULT; + final Capsule capsule = f.createCapsule(f.createDataPoints(CRS, axis)); + capsule.setRadiusBottom(r1); + capsule.setRadiusTop(r2); + return capsule; + } + + /** + * Returns the area and the position along the axis of the centroid of the surface of revolution + * of the given profile, by the midpoint rule on pieces of the profile segments. + * + * @param profile positions (<var>z</var>, ρ) along the axis and distance from the axis. + * @return the area, the position along the axis of the centroid and the enclosed volume. + */ + static double[] revolve(final List<double[]> profile) { + final double step = 1E-3; + double area = 0, moment = 0, volume = 0; + for (int i = 1; i < profile.size(); i++) { + final double[] a = profile.get(i - 1); + final double[] b = profile.get(i); + final double length = Math.hypot(b[0] - a[0], b[1] - a[1]); + final int n = Math.max(1, (int) Math.ceil(length / step)); + for (int k = 0; k < n; k++) { + final double t = (k + 0.5) / n; + final double z = a[0] + (b[0] - a[0]) * t; + final double rho = a[1] + (b[1] - a[1]) * t; + final double part = 2 * Math.PI * rho * length / n; + area += part; + moment += part * z; + volume += Math.PI * rho * rho * (b[0] - a[0]) / n; + } + } + return new double[] {area, moment / area, volume}; + } + + /** + * Returns the profile of a capsule: the upper convex hull of points sampled on the two circles + * of radius {@code r1} centered at <var>z</var> = 0 and of radius {@code r2} centered at {@code d}. + */ + private static List<double[]> profile(final double r1, final double r2, final double d) { + final List<double[]> points = new ArrayList<>(); + for (int i = 0; i <= SAMPLES; i++) { + final double t = Math.PI * i / SAMPLES; + points.add(new double[] {r1 * Math.cos(t), r1 * Math.sin(t)}); + points.add(new double[] {d + r2 * Math.cos(t), r2 * Math.sin(t)}); + } + points.sort(Comparator.<double[]>comparingDouble(p -> p[0]).thenComparingDouble(p -> p[1])); + final List<double[]> hull = new ArrayList<>(); + for (final double[] p : points) { + while (hull.size() >= 2) { + final double[] a = hull.get(hull.size() - 2); + final double[] b = hull.get(hull.size() - 1); + if ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]) >= 0) { + hull.remove(hull.size() - 1); // Not a right turn, so not on the upper hull. + } else { + break; + } + } + hull.add(p); + } + return hull; + } + + /** + * Verifies the area and the centroid of the given capsule against a numerical integration. + */ + private static void assertIntegrated(final Capsule capsule) { + final Vector<?> bottom = capsule.getDataPoints().getPosition(0); + final Vector<?> top = capsule.getDataPoints().getPosition(1); + final double d = capsule.getHeight(); + final double[] expected = revolve(profile(capsule.getRadiusBottom(), capsule.getRadiusTop(), d)); + assertEquals(expected[0], capsule.getArea().getValue().doubleValue(), expected[0] * INTEGRATION_TOLERANCE, "area"); + assertEquals(expected[2], capsule.getEnclosedVolume().getValue().doubleValue(), expected[2] * INTEGRATION_TOLERANCE, "volume"); + final Point centroid = capsule.getCentroid(); + for (int k = 0; k < 3; k++) { + final double e = bottom.get(k) + (top.get(k) - bottom.get(k)) * expected[1] / d; + assertEquals(e, centroid.getPosition().get(k), d * INTEGRATION_TOLERANCE, "centroid"); + } + } + + /** + * A capsule of equal radii is a cylinder of height <var>d</var> between two half spheres. + */ + @Test + public void testEqualRadii() { + final Capsule capsule = capsule(1, 1, 0,0,0, 0,0,2); + assertEquals(8 * Math.PI, capsule.getArea().getValue().doubleValue(), TOLERANCE); + assertEquals(10 * Math.PI / 3, capsule.getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + assertPosition(0, 0, 1, capsule.getCentroid().getPosition()); + assertIntegrated(capsule); + } + + /** + * Capsules of different radii, on axes which are not coordinate axes. + * The expected area of the first one is 2π⋅4⋅1.5 + 2π⋅1⋅0.5 + π⋅3⋅2⋅0.75 with <var>s</var> = ½. + */ + @Test + public void testDifferentRadii() { + final Capsule capsule = capsule(2, 1, 1,1,1, 1,1,3); + assertEquals(17.5 * Math.PI, capsule.getArea().getValue().doubleValue(), TOLERANCE); + assertIntegrated(capsule); + assertIntegrated(capsule(1.5, 0.5, 0,0,0, 2,1,3)); + assertIntegrated(capsule(0.2, 1.4, 0,0,0, 1,-1,1)); + } + + /** + * A capsule whose larger sphere contains the smaller one is that larger sphere. + */ + @Test + public void testContainedSphere() { + final Capsule capsule = capsule(1, 3, 0,0,0, 0,0,1); + assertEquals(36 * Math.PI, capsule.getArea().getValue().doubleValue(), TOLERANCE); + assertEquals(36 * Math.PI, capsule.getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + assertPosition(0, 0, 1, capsule.getCentroid().getPosition()); + } + + /** + * The measures are computed in 3 dimensions only. + */ + @Test + public void testNot3D() { + final GeometryFactory f = GeometryFactory.DEFAULT; + final Capsule capsule = f.createCapsule(f.createDataPoints(Geometries.getUndefinedCRS(2), 0,0, 0,2)); + assertThrows(UnsupportedOperationException.class, capsule::getArea); + assertThrows(UnsupportedOperationException.class, capsule::getCentroid); + assertThrows(UnsupportedOperationException.class, capsule::getEnclosedVolume); + } +} diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CylinderTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CylinderTest.java new file mode 100644 index 0000000000..8b5dc30aa7 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CylinderTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.geometries.surface; + +import java.util.ArrayList; +import java.util.List; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.apache.sis.geometries.Geometries; +import org.apache.sis.geometries.GeometryFactory; +import org.apache.sis.maths.Vector; +import static org.apache.sis.geometries.surface.CapsuleTest.INTEGRATION_TOLERANCE; +import static org.apache.sis.geometries.surface.SphereTest.assertPosition; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import org.apache.sis.geometries.GeometryTest; + + +/** + * Tests the centroid of {@link Cylinder}. + * + * @author Johann Sorel (Geomatys) + */ +public class CylinderTest extends GeometryTest { + + private static final CoordinateReferenceSystem CRS = Geometries.getUndefinedCRS(3); + + private static Cylinder cylinder(final double r1, final double r2, final boolean closed, final double... axis) { + final GeometryFactory f = GeometryFactory.DEFAULT; + final Cylinder cylinder = f.createCylinder(f.createDataPoints(CRS, axis)); + cylinder.setRadiusBottom(r1); + cylinder.setRadiusTop(r2); + cylinder.setClosed(closed); + return cylinder; + } + + /** + * A cylinder of equal radii has its centroid in the middle of its axis. + */ + @Test + public void testEqualRadii() { + assertPosition(1, 2, 4, cylinder(1, 1, true, 1,2,3, 1,2,5).getCentroid().getPosition()); + assertPosition(1, 2, 4, cylinder(1, 1, false, 1,2,3, 1,2,5).getCentroid().getPosition()); + } + + /** + * The lateral surface of an open cone has its centroid at one third of the height from the base. + */ + @Test + public void testOpenCone() { + assertPosition(0, 0, 1, cylinder(2, 0, false, 0,0,0, 0,0,3).getCentroid().getPosition()); + } + + /** + * Frustums, open and closed, on an axis which is not a coordinate axis. + * The expected values are integrated on a fine subdivision of the profile. + */ + @Test + public void testFrustum() { + final double r1 = 2, r2 = 0.5; + for (final boolean closed : new boolean[] {false, true}) { + final Cylinder cylinder = cylinder(r1, r2, closed, 0,0,0, 2,1,3); + final double h = cylinder.getHeight(); + final List<double[]> profile = new ArrayList<>(); + final int n = 10000; + if (closed) { + for (int i = 0; i < n; i++) profile.add(new double[] {0, r1 * i / n}); + } + for (int i = 0; i <= n; i++) { + profile.add(new double[] {h * i / n, r1 + (r2 - r1) * i / n}); + } + if (closed) { + for (int i = n - 1; i >= 0; i--) profile.add(new double[] {h, r2 * i / n}); + } + final double[] expected = CapsuleTest.revolve(profile); + assertEquals(expected[0], cylinder.getArea().getValue().doubleValue(), expected[0] * INTEGRATION_TOLERANCE, "area"); + assertEquals(expected[2], cylinder.getEnclosedVolume().getValue().doubleValue(), expected[2] * INTEGRATION_TOLERANCE, "volume"); + final double ratio = expected[1] / h; + final Vector<?> centroid = cylinder.getCentroid().getPosition(); + assertEquals(2 * ratio, centroid.get(0), h * INTEGRATION_TOLERANCE, "x"); + assertEquals( ratio, centroid.get(1), h * INTEGRATION_TOLERANCE, "y"); + assertEquals(3 * ratio, centroid.get(2), h * INTEGRATION_TOLERANCE, "z"); + } + } + + /** + * The enclosed volume is π<var>r</var>²<var>h</var> for a right cylinder and a third of it for a cone, + * whether the cylinder is closed or not. + */ + @Test + public void testGetEnclosedVolume() { + assertEquals(2 * Math.PI, cylinder(1, 1, true, 0,0,0, 0,0,2).getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + assertEquals(2 * Math.PI, cylinder(1, 1, false, 0,0,0, 0,0,2).getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + assertEquals(4 * Math.PI, cylinder(2, 0, false, 0,0,0, 0,0,3).getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + final GeometryFactory f = GeometryFactory.DEFAULT; + final Cylinder flat = f.createCylinder(f.createDataPoints(Geometries.getUndefinedCRS(2), 0,0, 0,2)); + assertThrows(UnsupportedOperationException.class, flat::getEnclosedVolume); + } + + /** + * A cylinder without area has its centroid in the middle of its axis. + */ + @Test + public void testNoArea() { + assertPosition(0, 0, 1, cylinder(0, 0, true, 0,0,0, 0,0,2).getCentroid().getPosition()); + } +} diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/EllipsoidTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/EllipsoidTest.java index 487a29e4ae..46752fce58 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/EllipsoidTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/EllipsoidTest.java @@ -119,4 +119,22 @@ public class EllipsoidTest extends GeometryTest { } assertEnvelope(curve.getEnvelope(), 0, 0, -1, 0, 2, 1); } + + /** + * The centroid of an ellipsoid is its center. + */ + @Test + public void testGetCentroid() { + final Ellipsoid ellipsoid = GeometryFactory.DEFAULT.createEllipsoid( + GeometryFactory.DEFAULT.createDataPoints(CRS, 1, 2, 3), 3, 2, 1); + assertPosition(1, 2, 3, ellipsoid.getCentroid().getPosition()); + } + + /** + * The enclosed volume of an ellipsoid is 4π<var>abc</var>∕3. + */ + @Test + public void testGetEnclosedVolume() { + assertEquals(8 * Math.PI, ellipsoid().getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java index 9c49cd6439..3e61c41c00 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java @@ -23,6 +23,7 @@ import org.apache.sis.geometries.DataPoints; import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.GeometryType; +import org.apache.sis.geometries.Point; import org.apache.sis.geometries.curve.Arc; import org.apache.sis.geometries.curve.Circle; import org.apache.sis.maths.Vector; @@ -139,4 +140,28 @@ public class SphereTest extends GeometryTest { } assertEnvelope(curve.getEnvelope(), 1, 2, 1, 1, 4, 5); } + + /** + * The enclosed volume is the volume of the ball, or the area of the disk in 2 dimensions. + */ + @Test + public void testGetEnclosedVolume() { + assertEquals(32 * Math.PI / 3, sphere().getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + final Sphere circle = GeometryFactory.DEFAULT.createSphere( + GeometryFactory.DEFAULT.createDataPoints(Geometries.getUndefinedCRS(2), 1, 2), 3); + assertEquals(9 * Math.PI, circle.getEnclosedVolume().getValue().doubleValue(), TOLERANCE); + assertEquals(6 * Math.PI, circle.getArea().getValue().doubleValue(), TOLERANCE); + } + + /** + * The centroid of a sphere is its center, as a point independent of the sphere. + */ + @Test + public void testGetCentroid() { + final Sphere sphere = sphere(); + final Point centroid = sphere.getCentroid(); + assertPosition(1, 2, 3, centroid.getPosition()); + sphere.setCenter(Vector.of(0, 0, 0)); + assertPosition(1, 2, 3, centroid.getPosition()); + } } 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 40186eaa0b..2abd49c0a6 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 @@ -418,6 +418,13 @@ public abstract class AbstractVectorTest { assertEquals(Vectors.length(diff), vector.distance(second), TOLERANCE); assertEquals(0, vector.distance(vector), TOLERANCE); assertThrows(IllegalArgumentException.class, () -> vector.distance(Vector.of(new double[vector.getDimension() + 1]))); + double rectilinear = 0; + for (final double d : diff) { + rectilinear += Math.abs(d); + } + assertEquals(rectilinear, vector.distanceRectilinear(second), TOLERANCE); + assertEquals(0, vector.distanceRectilinear(vector), TOLERANCE); + assertThrows(IllegalArgumentException.class, () -> vector.distanceRectilinear(Vector.of(new double[vector.getDimension() + 1]))); } /** diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/MathsTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/MathsTest.java index 5f0018062a..9a0a159c2b 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/MathsTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/MathsTest.java @@ -100,6 +100,20 @@ public class MathsTest { } + /** + * Tests the volume of the unit ball in 0 to 5 dimensions. + */ + @Test + public void testUnitBallVolume() { + assertEquals(1, Maths.unitBallVolume(0), EPSILON); + assertEquals(2, Maths.unitBallVolume(1), EPSILON); + assertEquals(Math.PI, Maths.unitBallVolume(2), EPSILON); + assertEquals(4 * Math.PI / 3, Maths.unitBallVolume(3), EPSILON); + assertEquals(Math.PI * Math.PI / 2, Maths.unitBallVolume(4), EPSILON); + assertEquals(8 * Math.PI * Math.PI / 15, Maths.unitBallVolume(5), EPSILON); + assertThrows(IllegalArgumentException.class, () -> Maths.unitBallVolume(-1)); + } + @Test public void testDistanceSquare() {
