From f4c926ea82771de22a32150a8b0b1a502faf34b2 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sat, 26 Apr 2014 16:55:11 +0000 Subject: [PATCH] Build properly empty polygons for equal min/max box. JIRA: MATH-1117 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1590251 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 4 ++++ .../geometry/euclidean/twod/PolygonsSet.java | 14 ++++++++++++ .../euclidean/twod/PolygonsSetTest.java | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 084fb6f21..7dc0ebf34 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,10 @@ If the output is not quite correct, check for invisible trailing spaces! + + Build properly empty polygons set when given equal min/max boundaries. Also explained + better in the javadoc about some wrong usage of PolygonsSet constructor. + Added a fast single-step method for fixed-step Runge-Kutta integrators. diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSet.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSet.java index 3c1b26cc3..4d0c9d8cc 100644 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSet.java +++ b/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSet.java @@ -64,6 +64,16 @@ public class PolygonsSet extends AbstractRegion { * cells). In order to avoid building too many small objects, it is * recommended to use the predefined constants * {@code Boolean.TRUE} and {@code Boolean.FALSE}

+ *

+ * This constructor is aimed at expert use, as building the tree may + * be a difficult taks. It is not intended for general use and for + * performances reasons does not check thoroughly its input, as this would + * require walking the full tree each time. Failing to provide a tree with + * the proper attributes, will therefore generate problems like + * {@link NullPointerException} or {@link ClassCastException} only later on. + * This limitation is known and explains why this constructor is for expert + * use only. The caller does have the responsibility to provided correct arguments. + *

* @param tree inside/outside BSP tree representing the region * @param tolerance tolerance below which points are considered identical * @since 3.3 @@ -219,6 +229,10 @@ public class PolygonsSet extends AbstractRegion { private static Line[] boxBoundary(final double xMin, final double xMax, final double yMin, final double yMax, final double tolerance) { + if ((xMin >= xMax - tolerance) || (yMin >= yMax - tolerance)) { + // too thin box, build an empty polygons set + return null; + } final Vector2D minMin = new Vector2D(xMin, yMin); final Vector2D minMax = new Vector2D(xMin, yMax); final Vector2D maxMin = new Vector2D(xMax, yMin); diff --git a/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSetTest.java b/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSetTest.java index 2bac0922b..94b51bad9 100644 --- a/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSetTest.java +++ b/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSetTest.java @@ -1066,6 +1066,28 @@ public class PolygonsSetTest { Assert.assertEquals(1, verticies.length); } + @Test + public void testTooThinBox() { + Assert.assertEquals(0.0, + new PolygonsSet(0.0, 0.0, 0.0, 10.3206397147574, 1.0e-10).getSize(), + 1.0e-10); + } + + @Test + public void testWrongUsage() { + // the following is a wrong usage of the constructor. + // as explained in the javadoc, the failure is NOT detected at construction + // time but occurs later on + PolygonsSet ps = new PolygonsSet(new BSPTree(), 1.0e-10); + Assert.assertNotNull(ps); + try { + ps.getSize(); + Assert.fail("an exception should have been thrown"); + } catch (NullPointerException npe) { + // this is expected + } + } + private PolygonsSet buildSet(Vector2D[][] vertices) { ArrayList> edges = new ArrayList>(); for (int i = 0; i < vertices.length; ++i) {