diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7dc0ebf34..7c0dda3ed 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 polyhedrons set when given equal min/max boundaries. Also explained + better in the javadoc about some wrong usage of PolyhedronsSet constructor. + Build properly empty polygons set when given equal min/max boundaries. Also explained better in the javadoc about some wrong usage of PolygonsSet constructor. diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSet.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSet.java index 31b78a61a..0c5a9a7e3 100644 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSet.java +++ b/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSet.java @@ -59,6 +59,16 @@ public class PolyhedronsSet 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 @@ -190,6 +200,10 @@ public class PolyhedronsSet extends AbstractRegion { final double yMin, final double yMax, final double zMin, final double zMax, final double tolerance) { + if ((xMin >= xMax - tolerance) || (yMin >= yMax - tolerance) || (zMin >= zMax - tolerance)) { + // too thin box, build an empty polygons set + return new BSPTree(Boolean.FALSE); + } final Plane pxMin = new Plane(new Vector3D(xMin, 0, 0), Vector3D.MINUS_I, tolerance); final Plane pxMax = new Plane(new Vector3D(xMax, 0, 0), Vector3D.PLUS_I, tolerance); final Plane pyMin = new Plane(new Vector3D(0, yMin, 0), Vector3D.MINUS_J, tolerance); diff --git a/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSetTest.java b/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSetTest.java index c131b92ca..b23fc5d41 100644 --- a/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSetTest.java +++ b/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSetTest.java @@ -285,6 +285,28 @@ public class PolyhedronsSetTest { Assert.assertEquals(24.0, polyhedronsSet.getBoundarySize(), 5.0e-6); } + @Test + public void testTooThinBox() { + Assert.assertEquals(0.0, + new PolyhedronsSet(0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 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 + PolyhedronsSet ps = new PolyhedronsSet(new BSPTree(), 1.0e-10); + Assert.assertNotNull(ps); + try { + ps.checkPoint(Vector3D.ZERO); + Assert.fail("an exception should have been thrown"); + } catch (NullPointerException npe) { + // this is expected + } + } + private void checkPoints(Region.Location expected, PolyhedronsSet tree, Vector3D[] points) { for (int i = 0; i < points.length; ++i) { Assert.assertEquals(expected, tree.checkPoint(points[i]));