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 4e2ed7adaa feat(Geometry): add parametrize, discretize, tesselate
methods on Geometry
4e2ed7adaa is described below
commit 4e2ed7adaa418912c54612b3f9f336301fe752aa
Author: jsorel <[email protected]>
AuthorDate: Fri Nov 20 14:23:05 2026 +0100
feat(Geometry): add parametrize, discretize, tesselate methods on Geometry
---
.../main/org/apache/sis/geometries/Geometry.java | 95 +++++++++
.../org/apache/sis/geometries/Representation.java | 8 +-
.../apache/sis/geometries/curve/CompoundCurve.java | 2 +-
.../sis/geometries/internal/shared/Centroids.java | 6 +-
.../apache/sis/geometries/mesh/MeshPrimitive.java | 32 +--
.../geometries/operation/GeometryProcessor.java | 223 +++++++++++++++++----
.../sis/geometries/operation/Intersection.java | 2 +-
.../{ToPrimitive.java => Tessellate.java} | 25 +--
.../sis/geometries/operation/DiscretizeTest.java | 156 ++++++++++++++
.../sis/geometries/operation/ParametrizeTest.java | 99 +++++++++
.../{ToPrimitiveTest.java => TessellateTest.java} | 94 ++++++---
11 files changed, 636 insertions(+), 106 deletions(-)
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java
index 756a9d8bd6..d85eadb662 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java
@@ -1107,4 +1107,99 @@ public sealed interface Geometry
return new GeometryProcessor().withinDistance(this, other, distance);
}
+ // ////////////////////////////////////////////////////////////////////////
+ // Representation changes
+ // ////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Returns an exact representation of this geometry at the {@linkplain
Representation#PARAMETRIC parametric}
+ * level or below.
+ *
+ * <p>Constraints:</p>
+ * <ul>
+ * <li>The returned geometry has the same positions as this geometry: no
precision is lost.</li>
+ * <li>The {@linkplain #getRepresentation() representation} of the
returned geometry is
+ * {@code PARAMETRIC}, {@code DISCRETE} or {@code TESSELLATED}.</li>
+ * <li>A geometry which is already at one of those levels, or empty, is
returned unchanged.</li>
+ * </ul>
+ *
+ * <p>Difference with ISO 19107: this operation has no equivalent in the
standard.</p>
+ *
+ * @return this geometry at the parametric level or below.
+ * @throws UnsupportedOperationException if no exact parametric form is
known for this geometry.
+ * @throws OperationException if the conversion cannot be computed.
+ *
+ * @see GeometryProcessor#parametrize(org.apache.sis.geometries.Geometry)
+ */
+ default Geometry parametrize() throws OperationException {
+ return new GeometryProcessor().parametrize(this);
+ }
+
+ /**
+ * Returns an approximation of this geometry at the {@linkplain
Representation#DISCRETE discrete}
+ * level or below, whose vertices lie on this geometry.
+ *
+ * <p>Constraints:</p>
+ * <ul>
+ * <li>All the vertices of the returned geometry lie on this geometry,
within the accuracy of the
+ * numerical representation of the positions.</li>
+ * <li>If {@code spacing} is non-zero, the distance along this geometry
between two adjacent vertices
+ * is at most {@code spacing}.</li>
+ * <li>If {@code offset} is non-zero, the distance between this geometry
and the returned geometry
+ * is at most {@code offset}.</li>
+ * <li>If both arguments are zero or {@code null}, the implementation
may apply its own precision limit,
+ * and the returned geometry contains at least all the {@linkplain
#getDataPoints() data points}
+ * of this geometry.</li>
+ * <li>Curves are converted with {@link Curve#asLine(Quantity,
Quantity)}, so a line string
+ * is densified when a spacing is given.</li>
+ * <li>Other geometries which are already discrete or tessellated, or
empty, are returned unchanged.</li>
+ * <li>Limitation: the spacing and the offset are applied to curves only.
+ * Polygons and polyhedral patches are not subdivided.</li>
+ * </ul>
+ *
+ * <p>Difference with ISO 19107: this operation generalizes {@link
Curve#asLine(Quantity, Quantity)}
+ * to all geometries.</p>
+ *
+ * @param spacing maximal distance along this geometry between two
adjacent vertices, or zero or {@code null} for no limit.
+ * @param offset maximal distance between this geometry and the result,
or zero or {@code null} for no limit.
+ * @return this geometry at the discrete level or below.
+ * @throws UnsupportedOperationException if the discretization of this
geometry type is not supported.
+ * @throws OperationException if the approximation cannot be computed.
+ *
+ * @see GeometryProcessor#discretize(org.apache.sis.geometries.Geometry,
javax.measure.Quantity, javax.measure.Quantity)
+ * @see "ISO-19107:2019 section 6.4.18.17"
+ */
+ default Geometry discretize(Quantity<?> spacing, Quantity<?> offset)
throws OperationException {
+ return new GeometryProcessor().discretize(this, spacing, offset);
+ }
+
+ /**
+ * Returns this geometry as mesh primitives, for use in GPU geometric
models.
+ *
+ * <p>Constraints:</p>
+ * <ul>
+ * <li>The result is a {@link MeshPrimitive} or a collection of them,
whose
+ * {@linkplain #getRepresentation() representation} is {@code
TESSELLATED}.</li>
+ * <li>Points become points primitives, curves become line strips or
lines,
+ * surfaces and solids become triangles. All attributes are
copied.</li>
+ * <li>Geometries above the discrete level are first {@linkplain
#discretize(Quantity, Quantity) discretized}
+ * with the given spacing and offset, which have the same meaning as
in that method.</li>
+ * <li>Mesh primitives and empty geometries are returned unchanged.</li>
+ * </ul>
+ *
+ * <p>Difference with ISO 19107: this operation has no equivalent in the
standard.</p>
+ *
+ * @param spacing maximal distance along this geometry between two
adjacent vertices, or zero or {@code null} for no limit.
+ * @param offset maximal distance between this geometry and the result,
or zero or {@code null} for no limit.
+ * @return a mesh primitive or a collection of mesh primitives.
+ * @throws UnsupportedOperationException if the tessellation of this
geometry type is not supported.
+ * @throws OperationException if the tessellation cannot be computed.
+ *
+ * @see GeometryProcessor#tessellate(org.apache.sis.geometries.Geometry,
javax.measure.Quantity, javax.measure.Quantity)
+ * @see "ISO-19107:2019 section 6.4.18.17"
+ */
+ default Geometry tessellate(Quantity<?> spacing, Quantity<?> offset)
throws OperationException {
+ return new GeometryProcessor().tessellate(this, spacing, offset);
+ }
+
}
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Representation.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Representation.java
index 020e0e11af..bfca6da51c 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Representation.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Representation.java
@@ -17,6 +17,7 @@
package org.apache.sis.geometries;
import java.util.stream.Stream;
+import javax.measure.Quantity;
/**
* A geometry can be represented by several forms.
@@ -24,13 +25,14 @@ import java.util.stream.Stream;
*
* Each representation gain or loss some capabilities.
* It is possible to downgrade a Geometry representation from one type to
another using the methods :
- * parametrize, discretize and tesselate.
+ * {@link Geometry#parametrize()}, {@link Geometry#discretize(Quantity,
Quantity)}
+ * and {@link Geometry#tessellate(Quantity, Quantity)}.
*
* For example : a Sphere.
* A sphere is an analytic geometry with a center and a radius, it is an exact
geometric definition but it can not hold attributes on it's surface.
* The sphere can be parametrize in a number of Nurb surfaces without lossing
exactness, but also gaining attributes on it's surface.
* It can then be discretize to a polyhedron, lossing in surface precision but
having attributes attached to exact vertices.
- * And it can be tesselated to triangles for GPU processing.
+ * And it can be tessellated to triangles for GPU processing.
*
* @author Johann Sorel (Geomatys)
*/
@@ -39,7 +41,7 @@ public enum Representation {
ANALYTIC,
PARAMETRIC,
DISCRETE,
- TESSELATED;
+ TESSELLATED;
/**
* @return the most abstract representation not empty
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/CompoundCurve.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/CompoundCurve.java
index e600024abc..22d81c90a6 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/CompoundCurve.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/CompoundCurve.java
@@ -59,7 +59,7 @@ public sealed interface CompoundCurve extends Curve
*/
@Override
default Representation getRepresentation() {
- Representation r = Representation.TESSELATED;
+ Representation r = Representation.TESSELLATED;
for (int i = 0, n = getNumCurves(); i < n; i++) {
r = Representation.mostAbstract(r,
getCurveN(i).getRepresentation());
}
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/Centroids.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/Centroids.java
index 95fc7a615f..515eb692c6 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/Centroids.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/Centroids.java
@@ -33,7 +33,7 @@ import org.apache.sis.geometries.Point;
import org.apache.sis.geometries.Surface;
import org.apache.sis.geometries.curve.LineString;
import org.apache.sis.geometries.curve.LinearRing;
-import org.apache.sis.geometries.operation.ToPrimitive;
+import org.apache.sis.geometries.operation.Tessellate;
import org.apache.sis.geometries.surface.CurvePolygon;
import org.apache.sis.geometries.surface.Polygon;
import org.apache.sis.geometries.surface.PolyhedralSurface;
@@ -141,7 +141,7 @@ public final class Centroids {
* @return the centroid of the polygon.
*/
public static Point polygon(final Polygon polygon) {
- final Point centroid = ToPrimitive.toPrimitive(polygon).getCentroid();
+ final Point centroid = Tessellate.tessellate(polygon).getCentroid();
return (centroid != null) ? centroid :
polyline(polygon.getExteriorRing().getDataPoints());
}
@@ -153,7 +153,7 @@ public final class Centroids {
* @return a point inside the polygon.
*/
public static Point pointOnPolygon(final Polygon polygon) {
- final Point point =
ToPrimitive.toPrimitive(polygon).getRepresentativePoint();
+ final Point point =
Tessellate.tessellate(polygon).getRepresentativePoint();
return (point != null) ? point :
polyline(polygon.getExteriorRing().getDataPoints());
}
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java
index 6d6ddc0a47..4a2872c6c5 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/mesh/MeshPrimitive.java
@@ -127,11 +127,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
default Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
/**
@@ -1079,11 +1079,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
public Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
@Override
@@ -1120,11 +1120,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
public Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
@Override
@@ -1155,11 +1155,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
public Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
/**
@@ -1181,11 +1181,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
public Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
/**
@@ -1207,11 +1207,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
public Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
@Override
@@ -1245,11 +1245,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
public Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
@Override
@@ -1285,11 +1285,11 @@ public sealed interface MeshPrimitive extends Geometry
}
/**
- * @return a Mesh is TESSELATED
+ * @return a Mesh is TESSELLATED
*/
@Override
public Representation getRepresentation() {
- return Representation.TESSELATED;
+ return Representation.TESSELLATED;
}
@Override
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java
index ef86b745c5..a15e2db417 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java
@@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.function.Function;
+import java.util.function.UnaryOperator;
import javax.measure.Quantity;
import static org.opengis.annotation.Specification.ISO_19107;
import org.opengis.annotation.UML;
@@ -40,6 +41,7 @@ import org.apache.sis.geometries.GeometryFactory;
import org.apache.sis.geometries.GeometryType;
import org.apache.sis.geometries.Point;
import org.apache.sis.geometries.Prism;
+import org.apache.sis.geometries.Representation;
import org.apache.sis.geometries.curve.LineString;
import org.apache.sis.geometries.curve.LinearRing;
import org.apache.sis.geometries.curve.OffsetCurve;
@@ -49,6 +51,7 @@ import org.apache.sis.geometries.internal.shared.Vertex;
import org.apache.sis.geometries.mesh.MeshPrimitive;
import org.apache.sis.geometries.mesh.MeshPrimitiveVisitor;
import org.apache.sis.geometries.solid.Polyhedron;
+import org.apache.sis.geometries.surface.CurvePolygon;
import org.apache.sis.geometries.surface.Polygon;
import org.apache.sis.geometries.surface.PolyhedralSurface;
import org.apache.sis.geometries.surface.Triangle;
@@ -531,50 +534,196 @@ public final class GeometryProcessor {
}
/**
- * Convert this geometry to a Primitive geometry type.
- * This method is provided as a conversion to GPU geometric model.
+ * Returns an exact representation of the geometry at the {@linkplain
Representation#PARAMETRIC parametric}
+ * level or below.
*
- * <p>Curves are converted to line strips with {@link
Curve#asLine(Quantity, Quantity)},
- * which receives the given spacing and offset.</p>
+ * <p>Constraints:</p>
+ * <ul>
+ * <li>Geometries which are not {@linkplain Representation#ANALYTIC
analytic} are returned unchanged.</li>
+ * <li>A prism is replaced by its {@linkplain Prism#toExtrusion()
extrusion}.</li>
+ * <li>The elements of a collection are converted one by one.</li>
+ * </ul>
+ *
+ * @param geom the geometry to convert.
+ * @return the geometry at the parametric level or below.
+ * @throws UnsupportedOperationException if no exact parametric form is
known for the geometry.
+ *
+ * @see Geometry#parametrize()
+ */
+ public Geometry parametrize(Geometry geom) throws OperationException {
+ if (geom.getRepresentation() != Representation.ANALYTIC) {
+ return geom;
+ }
+ return switch (geom) {
+ case Prism cdt -> cdt.toExtrusion();
+ case GeometryCollection<?> cdt -> convertElements(cdt,
this::parametrize);
+ default -> throw new UnsupportedOperationException(
+ "No exact parametric form is known for " +
geom.getGeometryType() + " geometries.");
+ };
+ }
+
+ /**
+ * Returns an approximation of the geometry at the {@linkplain
Representation#DISCRETE discrete} level or below.
+ *
+ * <p>Constraints:</p>
+ * <ul>
+ * <li>Curves are replaced by their {@linkplain Curve#asLine(Quantity,
Quantity) linear approximation}.</li>
+ * <li>Curve polygons are replaced by polygons whose rings are linear
approximations of the curves.</li>
+ * <li>A prism is replaced by the {@linkplain Prism#toExtrusion()
extrusion} of its discretized base.</li>
+ * <li>The elements of a collection are converted one by one.</li>
+ * <li>Other geometries already at the discrete level or below are
returned unchanged.</li>
+ * </ul>
*
* @param geom the geometry to convert.
- * @param spacing maximal distance along a curve between two points of
its line strip,
- * or zero or {@code null} for no limit.
- * @param offset maximal distance between a curve and its line strip,
or zero or {@code null} for no limit.
- * If both the spacing and the offset are zero, a default
precision is used.
- * @return equivalent primitive, all attributes are copied.
- * can be a Primitive or MultiPrimitive
+ * @param spacing maximal distance along the geometry between two
adjacent vertices, or zero or {@code null}.
+ * @param offset maximal distance between the geometry and the result,
or zero or {@code null}.
+ * @return the geometry at the discrete level or below.
+ * @throws UnsupportedOperationException if the discretization of the
geometry type is not supported.
+ *
+ * @see Geometry#discretize(Quantity, Quantity)
*/
- public Geometry toPrimitive(Geometry geom, Quantity<?> spacing,
Quantity<?> offset) {
- if (geom instanceof MeshPrimitive) {
- //Does nothing, geometry is already a Primitive.
- //Collections of primitives are handled by the collection case
below.
+ public Geometry discretize(Geometry geom, Quantity<?> spacing, Quantity<?>
offset) throws OperationException {
+ if (geom.isEmpty()) {
return geom;
- } else if (geom instanceof Point cdt) {
- return ToPrimitive.toPrimitive(cdt);
- } else if (geom instanceof Curve cdt) {
- //also handles line strings, which are densified if a spacing is
given.
- return ToPrimitive.toPrimitive(cdt.asLine(spacing, offset));
- } else if (geom instanceof Polygon cdt) {
- return ToPrimitive.toPrimitive(cdt);
- } else if (geom instanceof PolyhedralSurface<?> cdt) {
- //also handles TIN, which is a polyhedral surface of triangles.
- return ToPrimitive.toPrimitive(cdt);
- } else if (geom instanceof Polyhedron cdt) {
- return ToPrimitive.toPrimitive(cdt);
- } else if (geom instanceof Prism cdt) {
- //a prism is converted to the line string, surface or solid
covered by its extrusion.
- return toPrimitive(cdt.toExtrusion(), spacing, offset);
- } else if (geom instanceof GeometryCollection<?> cdt) {
- final GeometryType elementType = cdt.getElementType();
- switch (elementType) {
- case LINESTRING : return
ToPrimitive.toPrimitiveLine((GeometryCollection<LineString>) cdt, spacing,
offset);
- case POINT : return
ToPrimitive.toPrimitivePoint((GeometryCollection<Point>) cdt);
- default : return ToPrimitive.toPrimitive(cdt, spacing, offset);
- }
}
+ /*
+ * Case order matters: the first matching case is selected.
+ * Mesh primitives are checked before curves and collections
+ * because the points and lines primitives are also of those types.
+ */
+ return switch (geom) {
+ case MeshPrimitive cdt -> cdt;
+ case Curve cdt -> cdt.asLine(spacing, offset);
+ case CurvePolygon cdt -> discretize(cdt, spacing, offset);
+ case Prism cdt -> discretize(cdt, spacing, offset);
+ case GeometryCollection<?> cdt -> convertElements(cdt, (element)
-> discretize(element, spacing, offset));
+ default -> switch (geom.getRepresentation()) {
+ case EMPTY, DISCRETE, TESSELLATED -> geom;
+ default -> throw new UnsupportedOperationException(
+ "Discretization of " + geom.getGeometryType() + "
geometries is not supported yet.");
+ };
+ };
+ }
- throw new UnsupportedOperationException();
+ /**
+ * Returns a polygon whose rings are the linear approximations of the
rings of the given curve polygon.
+ */
+ private static Polygon discretize(CurvePolygon geom, Quantity<?> spacing,
Quantity<?> offset) {
+ final LinearRing exterior = discretizeRing(geom.getExteriorRing(),
spacing, offset);
+ final List<LinearRing> interiors = new ArrayList<>();
+ for (final Curve ring : geom.getInteriorRings()) {
+ interiors.add(discretizeRing(ring, spacing, offset));
+ }
+ return GeometryFactory.DEFAULT.createPolygon(exterior, interiors);
+ }
+
+ /**
+ * Returns the linear approximation of the given closed curve as a linear
ring.
+ */
+ private static LinearRing discretizeRing(Curve ring, Quantity<?> spacing,
Quantity<?> offset) {
+ final LineString line = ring.asLine(spacing, offset);
+ if (line instanceof LinearRing cdt) {
+ return cdt;
+ }
+ return GeometryFactory.DEFAULT.createLinearRing(line.getDataPoints());
+ }
+
+ /**
+ * Returns the extrusion of the given prism after the discretization of
its base.
+ */
+ private Geometry discretize(Prism geom, Quantity<?> spacing, Quantity<?>
offset) {
+ final Geometry base = geom.getBase();
+ final Geometry discreteBase = discretize(base, spacing, offset);
+ final Prism prism = (discreteBase == base) ? geom
+ : GeometryFactory.DEFAULT.createPrism(discreteBase,
geom.getExtrusionRange(), geom.getExtrusionCrs());
+ return prism.toExtrusion();
+ }
+
+ /**
+ * Converts the given geometry to mesh primitives, for use in GPU
geometric models.
+ *
+ * <p>Constraints:</p>
+ * <ul>
+ * <li>Mesh primitives and empty geometries are returned unchanged.</li>
+ * <li>Curves are converted to line strips with {@link
Curve#asLine(Quantity, Quantity)},
+ * which receives the given spacing and offset.</li>
+ * <li>Geometries above the {@linkplain Representation#DISCRETE
discrete} level which are not curves
+ * are {@linkplain #discretize(Geometry, Quantity, Quantity)
discretized} first.</li>
+ * <li>All attributes are copied.</li>
+ * </ul>
+ *
+ * @param geom the geometry to convert.
+ * @param spacing maximal distance along the geometry between two
adjacent vertices, or zero or {@code null}.
+ * @param offset maximal distance between the geometry and the result,
or zero or {@code null}.
+ * @return a mesh primitive or a collection of mesh primitives.
+ * @throws UnsupportedOperationException if the tessellation of the
geometry type is not supported.
+ *
+ * @see Geometry#tessellate(Quantity, Quantity)
+ */
+ public Geometry tessellate(Geometry geom, Quantity<?> spacing, Quantity<?>
offset) throws OperationException {
+ if (geom.isEmpty()) {
+ return geom;
+ }
+ /*
+ * Case order matters: the first matching case is selected.
+ * Mesh primitives are checked before curves and collections
+ * because the points and lines primitives are also of those types.
+ */
+ return switch (geom) {
+ case MeshPrimitive cdt -> cdt;
+ case Point cdt -> Tessellate.tessellate(cdt);
+ case Curve cdt ->
Tessellate.tessellate(cdt.asLine(spacing, offset));
+ case Polygon cdt -> Tessellate.tessellate(cdt);
+ case PolyhedralSurface<?> cdt -> Tessellate.tessellate(cdt);
+ case Polyhedron cdt -> Tessellate.tessellate(cdt);
+ case GeometryCollection<?> cdt -> tessellate(cdt, spacing, offset);
+ default -> switch (geom.getRepresentation()) {
+ case ANALYTIC, PARAMETRIC -> tessellate(discretize(geom,
spacing, offset), spacing, offset);
+ default -> throw new UnsupportedOperationException(
+ "Tessellation of " + geom.getGeometryType() + "
geometries is not supported yet.");
+ };
+ };
+ }
+
+ /**
+ * Converts the given collection to mesh primitives.
+ * Collections of points or line strings are merged in a single primitive
when possible.
+ */
+ @SuppressWarnings("unchecked")
+ private static Geometry tessellate(GeometryCollection<?> geom, Quantity<?>
spacing, Quantity<?> offset) {
+ return switch (geom.getElementType()) {
+ case LINESTRING ->
Tessellate.tessellateLines((GeometryCollection<LineString>) geom, spacing,
offset);
+ case POINT ->
Tessellate.tessellatePoints((GeometryCollection<Point>) geom);
+ default -> Tessellate.tessellate(geom, spacing, offset);
+ };
+ }
+
+ /**
+ * Applies the given conversion on each element of the given collection.
+ * The element type of the collection is kept if all converted elements
are still of that type.
+ *
+ * @param geom the collection to convert.
+ * @param converter the conversion to apply on each element.
+ * @return the collection of converted elements, or the given collection
if no element changed.
+ */
+ private static Geometry convertElements(GeometryCollection<?> geom,
UnaryOperator<Geometry> converter) {
+ final int count = geom.getNumGeometries();
+ final Geometry[] elements = new Geometry[count];
+ GeometryType elementType = geom.getElementType();
+ boolean changed = false;
+ for (int i = 0; i < count; i++) {
+ final Geometry element = geom.getGeometryN(i);
+ final Geometry converted = converter.apply(element);
+ changed |= (converted != element);
+ if (!elementType.isInstance(converted)) {
+ elementType = GeometryType.GEOMETRY;
+ }
+ elements[i] = converted;
+ }
+ if (!changed) {
+ return geom;
+ }
+ return GeometryFactory.DEFAULT.createGeometryCollection(elementType,
geom.getCoordinateReferenceSystem(), elements);
}
/**
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
index 47539c4f32..620e0abdb7 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
@@ -207,7 +207,7 @@ public final class Intersection {
}
final GeometryCollection<LineString> mline =
GeometryFactory.DEFAULT.createMultiLineString(segments.toArray(LineString[]::new));
- final MeshPrimitive intersection = (MeshPrimitive) new
GeometryProcessor().toPrimitive(mline, null, null);
+ final MeshPrimitive intersection = (MeshPrimitive) new
GeometryProcessor().tessellate(mline, null, null);
return intersection;
}
diff --git
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/ToPrimitive.java
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Tessellate.java
similarity index 90%
rename from
incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/ToPrimitive.java
rename to
incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Tessellate.java
index 459f13393a..757d215108 100644
---
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/ToPrimitive.java
+++
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Tessellate.java
@@ -42,12 +42,13 @@ import org.apache.sis.maths.NDArrays;
/**
+ * Conversions of discrete geometries to mesh primitives, used by {@link
GeometryProcessor#tessellate}.
*
* @author Johann Sorel (Geomatys)
*/
-public final class ToPrimitive {
+public final class Tessellate {
- private ToPrimitive(){}
+ private Tessellate(){}
private static ArrayDataPoints toArraySequence(DataPoints points) {
final Map<String,Array> attributes = new HashMap<>();
@@ -60,7 +61,7 @@ public final class ToPrimitive {
/**
* Transform Point to Primitive.
*/
- public static MeshPrimitive.Points toPrimitive(Point geometry) throws
OperationException {
+ public static MeshPrimitive.Points tessellate(Point geometry) throws
OperationException {
final MeshPrimitive.Points primitive = new MeshPrimitive.Points();
final DataPointsType attributesType = geometry.getDataPointsType();
for (String name : attributesType.getAttributeNames()) {
@@ -74,7 +75,7 @@ public final class ToPrimitive {
/**
* Transform LineString to Primitive.
*/
- public static MeshPrimitive.LineStrip toPrimitive(LineString geometry)
throws OperationException {
+ public static MeshPrimitive.LineStrip tessellate(LineString geometry)
throws OperationException {
final MeshPrimitive.LineStrip primitive = new
MeshPrimitive.LineStrip();
final ArrayDataPoints array =
toArraySequence(geometry.getDataPoints());
for (String name : array.getAttributeNames()) {
@@ -86,7 +87,7 @@ public final class ToPrimitive {
/**
* Transform Polygon to Primitive.
*/
- public static MeshPrimitive.Triangles toPrimitive(Polygon geometry) throws
OperationException {
+ public static MeshPrimitive.Triangles tessellate(Polygon geometry) throws
OperationException {
//we must triangulate it
return new EarClipping().toMesh(geometry);
}
@@ -95,7 +96,7 @@ public final class ToPrimitive {
* Transform PolyhedralSurface, including TIN, to Primitive.
* Each patch is triangulated and all triangles are merged in a single
non-indexed mesh.
*/
- public static MeshPrimitive.Triangles toPrimitive(PolyhedralSurface<?>
geometry) throws OperationException {
+ public static MeshPrimitive.Triangles tessellate(PolyhedralSurface<?>
geometry) throws OperationException {
final List<Polygon> faces = new ArrayList<>();
for (int i = 0, n = geometry.getNumPatches(); i < n; i++) {
faces.add(geometry.getPatchN(i));
@@ -108,7 +109,7 @@ public final class ToPrimitive {
* The faces of the exterior shell and of all the interior shells are
triangulated together
* in a single mesh, each face keeping its orientation.
*/
- public static MeshPrimitive.Triangles toPrimitive(Polyhedron geometry)
throws OperationException {
+ public static MeshPrimitive.Triangles tessellate(Polyhedron geometry)
throws OperationException {
final List<Polygon> faces = new ArrayList<>();
final List<GeometryCollection<Polygon>> shells = new ArrayList<>();
shells.add(geometry.getExteriorShell());
@@ -178,7 +179,7 @@ public final class ToPrimitive {
/**
* Transform MultiPoint to Primitive.
*/
- public static MeshPrimitive.Points
toPrimitivePoint(GeometryCollection<Point> geometry) throws OperationException {
+ public static MeshPrimitive.Points
tessellatePoints(GeometryCollection<Point> geometry) throws OperationException {
final ArrayDataPoints array =
toArraySequence(geometry.getDataPoints());
final MeshPrimitive.Points primitive = new MeshPrimitive.Points();
for (String name : array.getAttributeNames()) {
@@ -195,7 +196,7 @@ public final class ToPrimitive {
* @param spacing maximal distance between two points of a line strip, or
zero or {@code null} for no limit.
* @param offset ignored for line strings, which are their own linear
approximation.
*/
- public static Geometry toPrimitiveLine(GeometryCollection<LineString>
geometry, Quantity<?> spacing, Quantity<?> offset)
+ public static Geometry tessellateLines(GeometryCollection<LineString>
geometry, Quantity<?> spacing, Quantity<?> offset)
throws OperationException
{
boolean allLines = (spacing == null ||
spacing.getValue().doubleValue() == 0);
@@ -224,7 +225,7 @@ public final class ToPrimitive {
} else {
final List<MeshPrimitive> primitives = new
ArrayList<>(numGeometries);
for (int i = 0; i < numGeometries; i++) {
- addPrimitives(primitives,
toPrimitive(geometry.getGeometryN(i).asLine(spacing, offset)));
+ addPrimitives(primitives,
tessellate(geometry.getGeometryN(i).asLine(spacing, offset)));
}
return
GeometryFactory.DEFAULT.createGeometryCollection(geometry.getCoordinateReferenceSystem(),
primitives.toArray(MeshPrimitive[]::new));
}
@@ -236,13 +237,13 @@ public final class ToPrimitive {
* @param spacing maximal distance along a curve between two points of its
line strip, or zero or {@code null}.
* @param offset maximal distance between a curve and its line strip, or
zero or {@code null}.
*/
- public static GeometryCollection<MeshPrimitive>
toPrimitive(GeometryCollection<?> geometry, Quantity<?> spacing, Quantity<?>
offset)
+ public static GeometryCollection<MeshPrimitive>
tessellate(GeometryCollection<?> geometry, Quantity<?> spacing, Quantity<?>
offset)
throws OperationException
{
final GeometryProcessor processor = new GeometryProcessor();
final List<MeshPrimitive> primitives = new ArrayList<>();
for (int i = 0, n = geometry.getNumGeometries(); i < n; i++) {
- addPrimitives(primitives,
processor.toPrimitive(geometry.getGeometryN(i), spacing, offset));
+ addPrimitives(primitives,
processor.tessellate(geometry.getGeometryN(i), spacing, offset));
}
return
GeometryFactory.DEFAULT.createGeometryCollection(geometry.getCoordinateReferenceSystem(),
primitives.toArray(MeshPrimitive[]::new));
}
diff --git
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DiscretizeTest.java
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DiscretizeTest.java
new file mode 100644
index 0000000000..9b03630bc2
--- /dev/null
+++
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DiscretizeTest.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.operation;
+
+import javax.measure.Quantity;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.apache.sis.geometries.DataPoints;
+import org.apache.sis.geometries.Geometry;
+import org.apache.sis.geometries.GeometryCollection;
+import org.apache.sis.geometries.GeometryFactory;
+import org.apache.sis.geometries.GeometryType;
+import org.apache.sis.geometries.Point;
+import org.apache.sis.geometries.Prism;
+import org.apache.sis.geometries.Representation;
+import org.apache.sis.geometries.curve.CircularString;
+import org.apache.sis.geometries.curve.LineString;
+import org.apache.sis.geometries.solid.Polyhedron;
+import org.apache.sis.geometries.surface.CurvePolygon;
+import org.apache.sis.geometries.surface.Polygon;
+import org.apache.sis.maths.DataType;
+import org.apache.sis.maths.Vector;
+import org.apache.sis.maths.Vectors;
+import org.apache.sis.measure.NumberRange;
+import org.apache.sis.measure.Quantities;
+import org.apache.sis.measure.Units;
+import org.apache.sis.referencing.CommonCRS;
+
+// Test dependencies
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+
+/**
+ * Tests {@link GeometryProcessor#discretize(Geometry, Quantity, Quantity)}.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public class DiscretizeTest {
+
+ private static final CoordinateReferenceSystem CRS2D =
CommonCRS.WGS84.normalizedGeographic();
+
+ private static final GeometryFactory F = GeometryFactory.DEFAULT;
+
+ /**
+ * Maximal distance between a curve and its linear approximation used in
the tests.
+ */
+ private static final Quantity<?> OFFSET = Quantities.create(0.001,
Units.DEGREE);
+
+ /**
+ * Creates a half circle of radius 5 centered on (5,0).
+ */
+ private static CircularString arc() {
+ return F.createCircularString(F.createDataPoints(CRS2D, 0,0, 5,5,
10,0));
+ }
+
+ /**
+ * Asserts that all the positions are on the circle of radius 5 centered
on (5,0).
+ */
+ private static void assertOnCircle(final DataPoints points) {
+ for (int i = 0; i < points.size(); i++) {
+ final double[] p = points.getPosition(i).toArrayDouble();
+ assertEquals(5, Math.hypot(p[0] - 5, p[1]), 1E-9, "Point not on
the circle.");
+ }
+ }
+
+ /**
+ * A curve becomes a line string whose points are on the curve.
+ */
+ @Test
+ public void testCurve() {
+ final Geometry result = arc().discretize(null, OFFSET);
+ assertInstanceOf(LineString.class, result);
+ assertEquals(Representation.DISCRETE, result.getRepresentation());
+ assertTrue(result.getDataPoints().size() > 3, "The arc shall be
densified.");
+ assertOnCircle(result.getDataPoints());
+ }
+
+ /**
+ * A curve polygon becomes a polygon whose rings are linear approximations
of the curves.
+ */
+ @Test
+ public void testCurvePolygon() {
+ final CircularString circle =
F.createCircularString(F.createDataPoints(CRS2D, 0,0, 5,5, 10,0, 5,-5, 0,0));
+ final CurvePolygon disk = F.createCurvePolygon(circle, null);
+ final Geometry result = disk.discretize(null, OFFSET);
+ assertInstanceOf(Polygon.class, result);
+ assertEquals(Representation.DISCRETE, result.getRepresentation());
+ assertOnCircle(((Polygon) result).getExteriorRing().getDataPoints());
+ }
+
+ /**
+ * The prism of a polygon becomes a polyhedron.
+ */
+ @Test
+ public void testPrism() {
+ final Polygon square =
F.createPolygon(F.createLinearRing(F.createDataPoints(CRS2D, 0,0, 2,0, 2,2,
0,2, 0,0)), null);
+ final Prism prism = F.createPrism(square, NumberRange.create(0., true,
3., true), null);
+ final Geometry result = prism.discretize(null, null);
+ assertInstanceOf(Polyhedron.class, result);
+ assertEquals(Representation.DISCRETE, result.getRepresentation());
+ }
+
+ /**
+ * A geometry which is already discrete is returned unchanged.
+ */
+ @Test
+ public void testDiscrete() {
+ final Polygon square =
F.createPolygon(F.createLinearRing(F.createDataPoints(CRS2D, 0,0, 2,0, 2,2,
0,2, 0,0)), null);
+ assertSame(square, square.discretize(null, OFFSET));
+ final Point point = F.createPoint(CRS2D, 1, 2);
+ assertSame(point, point.discretize(null, OFFSET));
+ }
+
+ /**
+ * The elements of a collection are discretized one by one, and the
element type is kept when possible.
+ */
+ @Test
+ public void testCollection() {
+ final Point point = F.createPoint(CRS2D, 1, 2);
+ final GeometryCollection<Geometry> mixed =
F.createGeometryCollection(CRS2D, arc(), point);
+ final GeometryCollection<?> result =
assertInstanceOf(GeometryCollection.class, mixed.discretize(null, OFFSET));
+ assertEquals(2, result.getNumGeometries());
+ assertInstanceOf(LineString.class, result.getGeometryN(0));
+ assertSame(point, result.getGeometryN(1));
+ assertEquals(Representation.DISCRETE, result.getRepresentation());
+
+ final GeometryCollection<CircularString> curves =
F.createMultiCurve(CRS2D, arc(), arc());
+ final GeometryCollection<?> lines =
assertInstanceOf(GeometryCollection.class, curves.discretize(null, OFFSET));
+ assertEquals(GeometryType.CURVE, lines.getElementType());
+ }
+
+ /**
+ * A geometry without discrete form causes an exception.
+ */
+ @Test
+ public void testUnsupported() {
+ final Vector<?> origin = Vectors.create(new double[] {0, 0},
DataType.DOUBLE);
+ final Vector<?> direction = Vectors.create(new double[] {1, 0},
DataType.DOUBLE);
+ final Geometry ray = F.createRay(CRS2D, origin, direction);
+ assertThrows(UnsupportedOperationException.class, () ->
ray.discretize(null, OFFSET));
+ }
+}
diff --git
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ParametrizeTest.java
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ParametrizeTest.java
new file mode 100644
index 0000000000..740b3f3e31
--- /dev/null
+++
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ParametrizeTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.operation;
+
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.apache.sis.geometries.BBox;
+import org.apache.sis.geometries.Geometry;
+import org.apache.sis.geometries.GeometryCollection;
+import org.apache.sis.geometries.GeometryFactory;
+import org.apache.sis.geometries.Prism;
+import org.apache.sis.geometries.Representation;
+import org.apache.sis.geometries.curve.CircularString;
+import org.apache.sis.geometries.curve.LineString;
+import org.apache.sis.geometries.solid.Polyhedron;
+import org.apache.sis.geometries.surface.Polygon;
+import org.apache.sis.geometries.surface.PolyhedralSurface;
+import org.apache.sis.measure.NumberRange;
+import org.apache.sis.referencing.CommonCRS;
+
+// Test dependencies
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+
+/**
+ * Tests {@link GeometryProcessor#parametrize(Geometry)}.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public class ParametrizeTest {
+
+ private static final CoordinateReferenceSystem CRS2D =
CommonCRS.WGS84.normalizedGeographic();
+
+ private static final GeometryFactory F = GeometryFactory.DEFAULT;
+
+ private static final NumberRange<Double> RANGE = NumberRange.create(0.,
true, 3., true);
+
+ /**
+ * Geometries which are not analytic are returned unchanged.
+ */
+ @Test
+ public void testNotAnalytic() {
+ final CircularString arc =
F.createCircularString(F.createDataPoints(CRS2D, 0,0, 5,5, 10,0));
+ assertSame(arc, arc.parametrize());
+ final Polygon square =
F.createPolygon(F.createLinearRing(F.createDataPoints(CRS2D, 0,0, 2,0, 2,2,
0,2, 0,0)), null);
+ assertSame(square, square.parametrize());
+ }
+
+ /**
+ * A prism is replaced by its extrusion, which is exact.
+ */
+ @Test
+ public void testPrism() {
+ final LineString line = F.createLineString(F.createDataPoints(CRS2D,
0,0, 2,0, 2,1));
+ final Geometry walls = F.createPrism(line, RANGE, null).parametrize();
+ assertInstanceOf(PolyhedralSurface.class, walls);
+ assertEquals(Representation.DISCRETE, walls.getRepresentation());
+
+ final Polygon square =
F.createPolygon(F.createLinearRing(F.createDataPoints(CRS2D, 0,0, 2,0, 2,2,
0,2, 0,0)), null);
+ assertInstanceOf(Polyhedron.class, F.createPrism(square, RANGE,
null).parametrize());
+ }
+
+ /**
+ * The elements of a collection are converted one by one.
+ */
+ @Test
+ public void testCollection() {
+ final Polygon square =
F.createPolygon(F.createLinearRing(F.createDataPoints(CRS2D, 0,0, 2,0, 2,2,
0,2, 0,0)), null);
+ final Prism prism = F.createPrism(square, RANGE, null);
+ final GeometryCollection<Geometry> mixed =
F.createGeometryCollection(prism, square);
+ assertEquals(Representation.ANALYTIC, mixed.getRepresentation());
+ final GeometryCollection<?> result =
assertInstanceOf(GeometryCollection.class, mixed.parametrize());
+ assertInstanceOf(Polyhedron.class, result.getGeometryN(0));
+ assertSame(square, result.getGeometryN(1));
+ }
+
+ /**
+ * A geometry without known exact parametric form causes an exception.
+ */
+ @Test
+ public void testUnsupported() {
+ final BBox box = new BBox(CRS2D, 0,0, 10,10);
+ assertThrows(UnsupportedOperationException.class, box::parametrize);
+ }
+}
diff --git
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ToPrimitiveTest.java
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TessellateTest.java
similarity index 88%
rename from
incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ToPrimitiveTest.java
rename to
incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TessellateTest.java
index 9f83e4597e..a31f3b1b45 100644
---
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ToPrimitiveTest.java
+++
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/TessellateTest.java
@@ -26,9 +26,13 @@ import org.apache.sis.geometries.Point;
import org.apache.sis.geometries.DataPoints;
import org.apache.sis.geometries.DataPointsType;
import org.apache.sis.geometries.GeometryCollection;
+import org.apache.sis.geometries.Empty;
import org.apache.sis.geometries.Prism;
+import org.apache.sis.geometries.Representation;
+import org.apache.sis.geometries.curve.CircularString;
import org.apache.sis.geometries.curve.LineString;
import org.apache.sis.geometries.curve.LinearRing;
+import org.apache.sis.geometries.surface.CurvePolygon;
import org.apache.sis.geometries.surface.Polygon;
import org.apache.sis.geometries.solid.Polyhedron;
import org.apache.sis.geometries.solid.polyhedron.Hexahedron;
@@ -48,17 +52,18 @@ import org.junit.jupiter.api.Test;
/**
+ * Tests {@link GeometryProcessor#tessellate(Geometry, javax.measure.Quantity,
javax.measure.Quantity)}.
*
* @author Johann Sorel (Geomatys)
*/
-public class ToPrimitiveTest {
+public class TessellateTest {
private static final CoordinateReferenceSystem CRS2D =
CommonCRS.WGS84.normalizedGeographic();
@Test
public void testPoint() {
final Point point = GeometryFactory.DEFAULT.createPoint(CRS2D, 1, 2);
- final Geometry result = new GeometryProcessor().toPrimitive(point,
null, null);
+ final Geometry result = point.tessellate(null, null);
assertTrue(result instanceof MeshPrimitive);
final MeshPrimitive primitive = (MeshPrimitive) result;
assertEquals(MeshPrimitive.Type.POINTS, primitive.getType());
@@ -69,7 +74,7 @@ public class ToPrimitiveTest {
final Array positions = NDArrays.of(CRS2D, new double[]{0,1,2,3});
final DataPoints points =
GeometryFactory.DEFAULT.createDataPoints(positions);
final LineString line =
GeometryFactory.DEFAULT.createLineString(points);
- final Geometry result = new GeometryProcessor().toPrimitive(line,
null, null);
+ final Geometry result = new GeometryProcessor().tessellate(line, null,
null);
assertTrue(result instanceof MeshPrimitive);
final MeshPrimitive primitive = (MeshPrimitive) result;
assertEquals(MeshPrimitive.Type.LINE_STRIP, primitive.getType());
@@ -83,8 +88,8 @@ public class ToPrimitiveTest {
public void testCurve() {
final GeometryFactory f = GeometryFactory.DEFAULT;
final var arc = f.createCircularString(f.createDataPoints(CRS2D, 0,0,
5,5, 10,0));
- final MeshPrimitive coarse = (MeshPrimitive) new
GeometryProcessor().toPrimitive(arc, null, Quantities.create(0.1,
Units.DEGREE));
- final MeshPrimitive fine = (MeshPrimitive) new
GeometryProcessor().toPrimitive(arc, null, Quantities.create(0.001,
Units.DEGREE));
+ final MeshPrimitive coarse = (MeshPrimitive) new
GeometryProcessor().tessellate(arc, null, Quantities.create(0.1, Units.DEGREE));
+ final MeshPrimitive fine = (MeshPrimitive) arc.tessellate(null,
Quantities.create(0.001, Units.DEGREE));
assertEquals(MeshPrimitive.Type.LINE_STRIP, coarse.getType());
final Array positions = fine.getPositions();
assertTrue(positions.getLength() > coarse.getPositions().getLength(),
"A smaller offset shall give more points.");
@@ -102,7 +107,7 @@ public class ToPrimitiveTest {
final GeometryFactory f = GeometryFactory.DEFAULT;
final var arc1 = f.createCircularString(f.createDataPoints(CRS2D, 0,0,
5,5, 10,0));
final var arc2 = f.createCircularString(f.createDataPoints(CRS2D,
20,0, 25,5, 30,0));
- final Geometry result = new
GeometryProcessor().toPrimitive(f.createMultiCurve(CRS2D, arc1, arc2),
+ final Geometry result = new
GeometryProcessor().tessellate(f.createMultiCurve(CRS2D, arc1, arc2),
Quantities.create(1, Units.DEGREE), null);
assertTrue(result instanceof GeometryCollection);
for (final Object component : (GeometryCollection<?>) result) {
@@ -121,8 +126,8 @@ public class ToPrimitiveTest {
final var lines = f.createMultiLineString(CRS2D,
f.createLineString(f.createDataPoints(CRS2D, 0,0, 10,0)),
f.createLineString(f.createDataPoints(CRS2D, 0,5, 10,5)));
- assertEquals(MeshPrimitive.Type.LINES, ((MeshPrimitive) new
GeometryProcessor().toPrimitive(lines, null, null)).getType());
- final Geometry result = new GeometryProcessor().toPrimitive(lines,
Quantities.create(2.5, Units.DEGREE), null);
+ assertEquals(MeshPrimitive.Type.LINES, ((MeshPrimitive) new
GeometryProcessor().tessellate(lines, null, null)).getType());
+ final Geometry result = new GeometryProcessor().tessellate(lines,
Quantities.create(2.5, Units.DEGREE), null);
assertTrue(result instanceof GeometryCollection);
for (final Object component : (GeometryCollection<?>) result) {
assertEquals(5, ((MeshPrimitive)
component).getPositions().getLength());
@@ -133,7 +138,7 @@ public class ToPrimitiveTest {
public void testPolygon() {
final LinearRing exterior = ring(0,0, 2,0, 2,2, 0,2, 0,0);
final Polygon polygon =
GeometryFactory.DEFAULT.createPolygon(exterior, null);
- final Geometry result = new GeometryProcessor().toPrimitive(polygon,
null, null);
+ final Geometry result = new GeometryProcessor().tessellate(polygon,
null, null);
assertTrue(result instanceof MeshPrimitive);
final MeshPrimitive primitive = (MeshPrimitive) result;
assertEquals(MeshPrimitive.Type.TRIANGLES, primitive.getType());
@@ -150,14 +155,14 @@ public class ToPrimitiveTest {
public void testPolygonAttributes() {
final GeometryFactory f = GeometryFactory.DEFAULT;
final Polygon square = f.createPolygon(attributedRing(0,0, 2,0, 2,2,
0,2, 0,0), null);
- final MeshPrimitive mesh = (MeshPrimitive) new
GeometryProcessor().toPrimitive(square, null, null);
+ final MeshPrimitive mesh = (MeshPrimitive) new
GeometryProcessor().tessellate(square, null, null);
assertEquals(4, mesh.getPositions().getLength(), "The closing position
shall not be a separate vertex.");
assertEquals(6, mesh.getIndex().getLength(), "Expected 2 triangles.");
assertAttribute(mesh);
final Polygon holed = f.createPolygon(attributedRing(0,0, 10,0, 10,10,
0,10, 0,0),
List.of(attributedRing(2,3, 2,5, 4,5, 4,3, 2,3)));
- final MeshPrimitive holedMesh = (MeshPrimitive) new
GeometryProcessor().toPrimitive(holed, null, null);
+ final MeshPrimitive holedMesh = (MeshPrimitive) new
GeometryProcessor().tessellate(holed, null, null);
assertEquals(96, trianglesArea(holedMesh), 1E-9);
assertAttribute(holedMesh);
}
@@ -202,7 +207,7 @@ public class ToPrimitiveTest {
final LinearRing exterior = ring(0,0, 10,0, 10,10, 0,10, 0,0);
final LinearRing hole = ring(2,2, 2,4, 4,4, 4,2, 2,2);
final Polygon polygon =
GeometryFactory.DEFAULT.createPolygon(exterior, java.util.List.of(hole));
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(polygon, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(polygon, null, null);
assertEquals(MeshPrimitive.Type.TRIANGLES, primitive.getType());
assertEquals(96, trianglesArea(primitive), 1E-9);
}
@@ -216,7 +221,7 @@ public class ToPrimitiveTest {
final LinearRing exterior = ring(0,0, 10,0, 10,10, 0,10, 0,0);
final LinearRing hole = ring(4,4, 4,2, 2,2, 2,4, 4,4);
final Polygon polygon =
GeometryFactory.DEFAULT.createPolygon(exterior, java.util.List.of(hole));
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(polygon, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(polygon, null, null);
assertEquals(96, trianglesArea(primitive), 1E-9);
}
@@ -228,7 +233,7 @@ public class ToPrimitiveTest {
final LinearRing exterior =
GeometryFactory.DEFAULT.createLinearRing(GeometryFactory.DEFAULT.createDataPoints(
NDArrays.of(CommonCRS.WGS84.geographic3D(), 0,0,5, 2,0,5,
2,2,7, 0,2,7, 0,0,5)));
final Polygon polygon =
GeometryFactory.DEFAULT.createPolygon(exterior, null);
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(polygon, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(polygon, null, null);
assertEquals(MeshPrimitive.Type.TRIANGLES, primitive.getType());
assertEquals(3, primitive.getPositions().getDimension());
assertEquals(6, primitive.getIndex().getLength(), "Expected 2
triangles.");
@@ -248,7 +253,7 @@ public class ToPrimitiveTest {
final LinearRing exterior = ring3D(crs, 0,0,0, 6,0,0, 6,0,4, 0,0,4,
0,0,0);
final LinearRing window = ring3D(crs, 2,0,1, 2,0,3, 4,0,3, 4,0,1,
2,0,1);
final Polygon polygon =
GeometryFactory.DEFAULT.createPolygon(exterior, java.util.List.of(window));
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(polygon, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(polygon, null, null);
assertEquals(20, trianglesArea3D(primitive), 1E-9);
final double[] c = primitive.getPositions().toArrayDouble();
for (int i = 1; i < c.length; i += 3) {
@@ -265,7 +270,7 @@ public class ToPrimitiveTest {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.geographic3D();
final LinearRing exterior = ring3D(crs, 3,0,0, 3,5,0, 3,5,2, 3,0,2,
3,0,0);
final Polygon polygon =
GeometryFactory.DEFAULT.createPolygon(exterior, null);
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(polygon, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(polygon, null, null);
assertEquals(10, trianglesArea3D(primitive), 1E-9);
final double[] c = primitive.getPositions().toArrayDouble();
for (int i = 0; i < c.length; i += 3) {
@@ -283,7 +288,7 @@ public class ToPrimitiveTest {
final LinearRing exterior = ring3D(crs, 0,0,5, 10,0,5, 10,10,5,
0,10,5, 0,0,5);
final LinearRing hole = ring3D(crs, 2,2,5, 2,4,5, 4,4,5, 4,2,5,
2,2,5);
final Polygon polygon =
GeometryFactory.DEFAULT.createPolygon(exterior, java.util.List.of(hole));
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(polygon, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(polygon, null, null);
assertEquals(96, trianglesArea3D(primitive), 1E-9);
final double[] c = primitive.getPositions().toArrayDouble();
for (int i = 2; i < c.length; i += 3) {
@@ -301,7 +306,7 @@ public class ToPrimitiveTest {
final org.apache.sis.geometries.surface.TIN tin = f.createTIN(crs, new
org.apache.sis.geometries.surface.Triangle[] {
f.createTriangle(ring3D(crs, 0,0,0, 2,0,0, 0,2,1, 0,0,0)),
f.createTriangle(ring3D(crs, 2,0,0, 2,2,1, 0,2,1, 2,0,0))});
- final Geometry result = new GeometryProcessor().toPrimitive(tin, null,
null);
+ final Geometry result = new GeometryProcessor().tessellate(tin, null,
null);
assertTrue(result instanceof MeshPrimitive.Triangles);
final MeshPrimitive primitive = (MeshPrimitive) result;
assertEquals(3, primitive.getPositions().getDimension());
@@ -323,7 +328,7 @@ public class ToPrimitiveTest {
f.createPolygon(ring3D(crs, 0,0,0, 2,0,0, 2,0,2, 0,0,2, 0,0,0),
null),
f.createPolygon(ring3D(crs, 0,2,0, 0,2,2, 2,2,2, 2,2,0, 0,2,0),
null),
f.createPolygon(ring3D(crs, 0,0,0, 0,2,0, 2,2,0, 2,0,0, 0,0,0),
null)};
- final Geometry result = new
GeometryProcessor().toPrimitive(f.createPolyhedralSurface(crs, faces), null,
null);
+ final Geometry result = new
GeometryProcessor().tessellate(f.createPolyhedralSurface(crs, faces), null,
null);
assertTrue(result instanceof MeshPrimitive.Triangles);
final MeshPrimitive primitive = (MeshPrimitive) result;
assertEquals(30, primitive.getIndex().getLength(), "Expected 2
triangles per face.");
@@ -353,7 +358,7 @@ public class ToPrimitiveTest {
public void testPolyhedron() {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.geographic3D();
final Polyhedron cube =
GeometryFactory.DEFAULT.createPolyhedron(cube(crs, 0,0,0, 2,2,2), List.of());
- final Geometry result = new GeometryProcessor().toPrimitive(cube,
null, null);
+ final Geometry result = new GeometryProcessor().tessellate(cube, null,
null);
assertTrue(result instanceof MeshPrimitive.Triangles);
final MeshPrimitive primitive = (MeshPrimitive) result;
assertEquals(crs, primitive.getCoordinateReferenceSystem());
@@ -368,7 +373,7 @@ public class ToPrimitiveTest {
public void testPolyhedronWithInteriorShell() {
final CoordinateReferenceSystem crs = CommonCRS.WGS84.geographic3D();
final Polyhedron hollow =
GeometryFactory.DEFAULT.createPolyhedron(cube(crs, 0,0,0, 4,4,4),
List.of(cube(crs, 1,1,1, 3,3,3)));
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(hollow, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(hollow, null, null);
assertEquals(72, primitive.getIndex().getLength(), "Expected 2
triangles per face of both shells.");
assertEquals(96 + 24, trianglesArea3D(primitive), 1E-9);
}
@@ -379,7 +384,7 @@ public class ToPrimitiveTest {
@Test
public void testRegularPolyhedron() {
for (final Polyhedron polyhedron : new Polyhedron[] {new
Tetrahedron(), new Hexahedron(), new Icosahedron()}) {
- final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().toPrimitive(polyhedron, null, null);
+ final MeshPrimitive primitive = (MeshPrimitive) new
GeometryProcessor().tessellate(polyhedron, null, null);
double expected = 0;
final GeometryCollection<Polygon> shell =
polyhedron.getExteriorShell();
for (int i = 0; i < shell.getNumGeometries(); i++) {
@@ -398,7 +403,7 @@ public class ToPrimitiveTest {
final Polygon square =
f.createPolygon(f.createLinearRing(f.createDataPoints(
NDArrays.of(CommonCRS.WGS84.normalizedGeographic(), new
double[] {0,0, 2,0, 2,2, 0,2, 0,0}))), null);
final Prism prism = f.createPrism(square, NumberRange.create(0., true,
3., true), null);
- final Geometry result = new GeometryProcessor().toPrimitive(prism,
null, null);
+ final Geometry result = prism.tessellate(null, null);
assertTrue(result instanceof MeshPrimitive.Triangles);
final MeshPrimitive primitive = (MeshPrimitive) result;
assertEquals(3,
primitive.getCoordinateReferenceSystem().getCoordinateSystem().getDimension());
@@ -415,17 +420,40 @@ public class ToPrimitiveTest {
final CoordinateReferenceSystem crs =
CommonCRS.WGS84.normalizedGeographic();
final NumberRange<Double> range = NumberRange.create(0., true, 3.,
true);
- final Geometry strip = new
GeometryProcessor().toPrimitive(f.createPrism(f.createPoint(crs, 1, 2), range,
null), null, null);
+ final Geometry strip = new
GeometryProcessor().tessellate(f.createPrism(f.createPoint(crs, 1, 2), range,
null), null, null);
assertTrue(strip instanceof MeshPrimitive.LineStrip);
assertEquals(2, ((MeshPrimitive) strip).getPositions().getLength());
final LineString line =
f.createLineString(f.createDataPoints(NDArrays.of(crs, new double[] {0,0, 2,0,
2,1})));
- final Geometry walls = new
GeometryProcessor().toPrimitive(f.createPrism(line, range, null), null, null);
+ final Geometry walls = new
GeometryProcessor().tessellate(f.createPrism(line, range, null), null, null);
assertTrue(walls instanceof MeshPrimitive.Triangles);
assertEquals(12, ((MeshPrimitive) walls).getIndex().getLength(),
"Expected 2 triangles per segment.");
assertEquals(2*3 + 1*3, trianglesArea3D((MeshPrimitive) walls), 1E-9);
}
+ /**
+ * A curve polygon is discretized, then triangulated.
+ */
+ @Test
+ public void testCurvePolygon() {
+ final GeometryFactory f = GeometryFactory.DEFAULT;
+ final CircularString circle =
f.createCircularString(f.createDataPoints(CRS2D, 0,0, 5,5, 10,0, 5,-5, 0,0));
+ final CurvePolygon disk = f.createCurvePolygon(circle, null);
+ final Geometry result = disk.tessellate(null, Quantities.create(0.001,
Units.DEGREE));
+ assertTrue(result instanceof MeshPrimitive.Triangles);
+ assertEquals(Representation.TESSELLATED, result.getRepresentation());
+ assertEquals(Math.PI * 25, trianglesArea((MeshPrimitive) result), 0.1);
+ }
+
+ /**
+ * An empty geometry is returned unchanged.
+ */
+ @Test
+ public void testEmpty() {
+ final Empty empty = GeometryFactory.DEFAULT.createEmpty(CRS2D);
+ assertSame(empty, empty.tessellate(null, null));
+ }
+
private static LinearRing ring3D(final CoordinateReferenceSystem crs,
final double... coordinates) {
return GeometryFactory.DEFAULT.createLinearRing(
GeometryFactory.DEFAULT.createDataPoints(NDArrays.of(crs,
coordinates)));
@@ -497,7 +525,7 @@ public class ToPrimitiveTest {
final GeometryCollection<LineString> mlines =
GeometryFactory.DEFAULT.createMultiLineString(line1, line2);
- final Geometry result = new
GeometryProcessor().toPrimitive(mlines, null, null);
+ final Geometry result = new GeometryProcessor().tessellate(mlines,
null, null);
assertTrue(result instanceof GeometryCollection);
final GeometryCollection<?> mp = (GeometryCollection<?>) result;
assertEquals(2, mp.getNumGeometries());
@@ -521,7 +549,7 @@ public class ToPrimitiveTest {
final GeometryCollection<LineString> mlines =
GeometryFactory.DEFAULT.createMultiLineString(line1, line2);
- final Geometry result = new
GeometryProcessor().toPrimitive(mlines, null, null);
+ final Geometry result = new GeometryProcessor().tessellate(mlines,
null, null);
assertTrue(result instanceof MeshPrimitive.Lines);
final MeshPrimitive.Lines mp = (MeshPrimitive.Lines) result;
assertEquals(2, mp.getNumGeometries());
@@ -538,7 +566,7 @@ public class ToPrimitiveTest {
final DataPoints points =
GeometryFactory.DEFAULT.createDataPoints(positions1);
final GeometryCollection<Point> mpoints =
GeometryFactory.DEFAULT.createMultiPoint(points);
- final Geometry result = new GeometryProcessor().toPrimitive(mpoints,
null, null);
+ final Geometry result = new GeometryProcessor().tessellate(mpoints,
null, null);
assertTrue(result instanceof MeshPrimitive);
final MeshPrimitive primitive1 = (MeshPrimitive) result;
assertEquals(MeshPrimitive.Type.POINTS, primitive1.getType());
@@ -574,23 +602,23 @@ public class ToPrimitiveTest {
final GeometryFactory f = GeometryFactory.DEFAULT;
final GeometryProcessor processor = new GeometryProcessor();
- Geometry result =
processor.toPrimitive(f.createPoint(attributedPoints(1, 2)), null, null);
+ Geometry result =
processor.tessellate(f.createPoint(attributedPoints(1, 2)), null, null);
assertAttributes((MeshPrimitive) result, 1);
- result =
processor.toPrimitive(f.createMultiPoint(attributedPoints(0,0, 1,1, 2,2)),
null, null);
+ result = processor.tessellate(f.createMultiPoint(attributedPoints(0,0,
1,1, 2,2)), null, null);
assertAttributes((MeshPrimitive) result, 3);
- result =
processor.toPrimitive(f.createLineString(attributedPoints(0,0, 1,1, 2,0)),
null, null);
+ result = processor.tessellate(f.createLineString(attributedPoints(0,0,
1,1, 2,0)), null, null);
assertAttributes((MeshPrimitive) result, 3);
//only lines of 2 points, a single primitive of lines
- result = processor.toPrimitive(f.createMultiLineString(CRS2D,
+ result = processor.tessellate(f.createMultiLineString(CRS2D,
f.createLineString(attributedPoints(0,0, 1,1)),
f.createLineString(attributedPoints(2,0, 3,1))), null, null);
assertAttributes((MeshPrimitive) result, 4);
//longer lines, one line strip per line
- result = processor.toPrimitive(f.createMultiLineString(CRS2D,
+ result = processor.tessellate(f.createMultiLineString(CRS2D,
f.createLineString(attributedPoints(0,0, 1,1, 2,0)),
f.createLineString(attributedPoints(3,0, 4,1, 5,0))), null,
null);
final GeometryCollection<?> multi =
assertInstanceOf(GeometryCollection.class, result);