You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2021/01/07 18:08:31 UTC
[sis] 02/04: Let `Polyline` computes the bounds itself.
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
commit 34f7b23ac1e9d696250f25bb1d178067efca1b4c
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Fri Jan 1 14:36:43 2021 +0100
Let `Polyline` computes the bounds itself.
---
.../apache/sis/internal/feature/j2d/FlatShape.java | 22 ++++++++++++++++++++++
.../sis/internal/feature/j2d/PathBuilder.java | 20 +++-----------------
.../apache/sis/internal/feature/j2d/Polygon.java | 7 ++-----
.../apache/sis/internal/feature/j2d/Polyline.java | 14 +++++++-------
.../sis/internal/feature/j2d/FlatShapeTest.java | 9 +++------
5 files changed, 37 insertions(+), 35 deletions(-)
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/FlatShape.java b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/FlatShape.java
index b3ed57b..9c5e738 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/FlatShape.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/FlatShape.java
@@ -54,6 +54,28 @@ abstract class FlatShape extends AbstractGeometry implements Shape {
}
/**
+ * Creates a shape with bounds initialized to minimum and maximum coordinates of given array.
+ * The {@code coordinates} array shall not be empty.
+ *
+ * @param coordinates the coordinate values as (x,y) tuples.
+ * @param size number of valid value in {@code coordinates} array.
+ */
+ FlatShape(final double[] coordinates, final int size) {
+ double xmax, ymax;
+ double xmin = xmax = coordinates[0];
+ double ymin = ymax = coordinates[1];
+ for (int i=2; i<size;) {
+ final double x = coordinates[i++];
+ final double y = coordinates[i++];
+ if (x < xmin) xmin = x;
+ if (x > xmax) xmax = x;
+ if (y < ymin) ymin = y;
+ if (y > ymax) ymax = y;
+ }
+ bounds = new IntervalRectangle(xmin, ymin, xmax, ymax);
+ }
+
+ /**
* Returns an integer rectangle that completely encloses the shape.
* There is no guarantee that the rectangle is the smallest bounding box that encloses the shape.
*/
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PathBuilder.java b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PathBuilder.java
index 830509c..faf54a8 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PathBuilder.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/PathBuilder.java
@@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.ArrayList;
import java.awt.Shape;
import org.opengis.referencing.operation.TransformException;
-import org.apache.sis.internal.referencing.j2d.IntervalRectangle;
/**
@@ -192,26 +191,13 @@ public class PathBuilder {
* be equal because `append(…)` filtered repetitive points.
*/
if (size >= 2*DIMENSION) {
- double xmin = coordinates[0];
- double ymin = coordinates[1];
- if (xmin == coordinates[size - 2] &&
- ymin == coordinates[size - 1])
+ if (coordinates[0] == coordinates[size - 2] &&
+ coordinates[1] == coordinates[size - 1])
{
size -= DIMENSION;
close = true;
}
- double xmax = xmin;
- double ymax = ymin;
- for (int i=DIMENSION; i<size;) {
- final double x = coordinates[i++];
- final double y = coordinates[i++];
- if (x < xmin) xmin = x;
- if (x > xmax) xmax = x;
- if (y < ymin) ymin = y;
- if (y > ymax) ymax = y;
- }
- final IntervalRectangle bounds = new IntervalRectangle(xmin, ymin, xmax, ymax);
- polylines.add(close ? new Polygon(bounds, coordinates, size) : new Polyline(bounds, coordinates, size));
+ polylines.add(close ? new Polygon(coordinates, size) : new Polyline(coordinates, size));
}
size = 0;
}
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polygon.java b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polygon.java
index ad8e022..0184b83 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polygon.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polygon.java
@@ -16,8 +16,6 @@
*/
package org.apache.sis.internal.feature.j2d;
-import org.apache.sis.internal.referencing.j2d.IntervalRectangle;
-
/**
* A polygons as a Java2D {@link java.awt.Shape}.
@@ -32,11 +30,10 @@ final class Polygon extends Polyline {
* Creates a new polygon with the given coordinates.
* The {@code coordinates} array shall not be empty.
*
- * @param bounds the polygon bounds (not cloned).
* @param coordinates the coordinate values as (x,y) tuples.
* @param size number of valid value in {@code coordinates} array.
*/
- Polygon(final IntervalRectangle bounds, final double[] coordinates, final int size) {
- super(bounds, coordinates, size);
+ Polygon(final double[] coordinates, final int size) {
+ super(coordinates, size);
}
}
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polyline.java b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polyline.java
index 70d69f8..9c19b7e 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polyline.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/j2d/Polyline.java
@@ -23,7 +23,7 @@ import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.PathIterator;
import java.awt.geom.AffineTransform;
-import org.apache.sis.internal.referencing.j2d.IntervalRectangle;
+import org.apache.sis.internal.util.Numerics;
/**
@@ -78,13 +78,11 @@ class Polyline extends FlatShape {
* Creates a new polylines with the given coordinates.
* The {@code coordinates} array shall not be empty.
*
- * @param bounds the polyline bounds (not cloned).
* @param coordinates the coordinate values as (x,y) tuples.
* @param size number of valid value in {@code coordinates} array.
*/
- Polyline(final IntervalRectangle bounds, final double[] coordinates, final int size) {
- super(bounds);
- assert size >= 2 : size; // Required by our PathIterator.
+ Polyline(final double[] coordinates, final int size) {
+ super(coordinates, size);
this.coordinates = new float[size];
final double tx = round(bounds.getCenterX(), bounds.xmin, bounds.xmax);
final double ty = round(bounds.getCenterY(), bounds.ymin, bounds.ymax);
@@ -93,11 +91,13 @@ class Polyline extends FlatShape {
}
/**
- * Rounds the translation to an arbitrary number of bits (currently 8).
+ * Rounds the translation to an arbitrary number of bits (currently 20).
* The intent is to avoid that zero values become something like 1E-9.
+ * The number of bits that we kept should be less that the number of bits
+ * in the significand (mantissa) of {@code float} type.
*/
private static double round(final double center, final double min, final double max) {
- final int e = Math.getExponent(Math.max(Math.abs(min), Math.abs(max))) - 8;
+ final int e = Math.getExponent(Math.max(Math.abs(min), Math.abs(max))) - (Numerics.SIGNIFICAND_SIZE_OF_FLOAT - 3);
return Math.scalb(Math.round(Math.scalb(center, -e)), e);
}
diff --git a/core/sis-feature/src/test/java/org/apache/sis/internal/feature/j2d/FlatShapeTest.java b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/j2d/FlatShapeTest.java
index 88b1550..9066908 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/internal/feature/j2d/FlatShapeTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/j2d/FlatShapeTest.java
@@ -18,7 +18,6 @@ package org.apache.sis.internal.feature.j2d;
import java.awt.geom.Path2D;
import org.opengis.referencing.operation.TransformException;
-import org.apache.sis.internal.referencing.j2d.IntervalRectangle;
import org.apache.sis.test.TestCase;
import org.junit.Test;
@@ -57,9 +56,8 @@ public final strictfp class FlatShapeTest extends TestCase {
final double[] coordinates = {
4,5, 6,3, 8,5, -2,5, 10,4
};
- final IntervalRectangle bounds = new IntervalRectangle(-2,3, 19,5);
- final Polyline p = closed ? new Polygon (bounds, coordinates, coordinates.length)
- : new Polyline(bounds, coordinates, coordinates.length);
+ final Polyline p = closed ? new Polygon (coordinates, coordinates.length)
+ : new Polyline(coordinates, coordinates.length);
final Path2D.Double r = new Path2D.Double(Path2D.WIND_NON_ZERO);
createReferenceShape(r, coordinates, closed);
@@ -96,11 +94,10 @@ public final strictfp class FlatShapeTest extends TestCase {
{9,3, 7,5, -4,3},
{3,5, 6,1, -2,7, 3,8}
};
- final IntervalRectangle bounds = new IntervalRectangle(); // Dummy bounds for this test.
final Polyline[] polylines = new Polyline[coordinates.length];
final Path2D.Double r = new Path2D.Double(Path2D.WIND_NON_ZERO);
for (int i=0; i < polylines.length; i++) {
- polylines[i] = new Polyline(bounds, coordinates[i], coordinates[i].length);
+ polylines[i] = new Polyline(coordinates[i], coordinates[i].length);
createReferenceShape(r, coordinates[i], false);
}
final MultiPolylines p = new MultiPolylines(polylines);