From 6b43922648fe2dfcb4cb1aaaf5d795f00a6aa174 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Wed, 5 Feb 2014 22:10:22 +0000 Subject: [PATCH] [MATH-749] Handle case that all points are identical. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1564954 13f79535-47bb-0310-9956-ffa450edef68 --- .../euclidean/twod/hull/GiftWrap.java | 2 +- .../ConvexHullGenerator2DAbstractTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/hull/GiftWrap.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/hull/GiftWrap.java index c46b1cac5..d02889317 100644 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/hull/GiftWrap.java +++ b/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/hull/GiftWrap.java @@ -115,7 +115,7 @@ public class GiftWrap extends AbstractConvexHullGenerator2D { } } pointOnHull = nextPoint; - } while (nextPoint != hullVertices.get(0)); + } while (nextPoint != hullVertices.get(0) && nextPoint != null); return hullVertices; } diff --git a/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/hull/ConvexHullGenerator2DAbstractTest.java b/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/hull/ConvexHullGenerator2DAbstractTest.java index 94557be95..82901466b 100644 --- a/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/hull/ConvexHullGenerator2DAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/hull/ConvexHullGenerator2DAbstractTest.java @@ -87,6 +87,18 @@ public abstract class ConvexHullGenerator2DAbstractTest { Assert.assertTrue(hull.getLineSegments().length == 1); } + @Test + public void testAllIdentical() { + final Collection points = new ArrayList(); + points.add(new Vector2D(1, 1)); + points.add(new Vector2D(1, 1)); + points.add(new Vector2D(1, 1)); + points.add(new Vector2D(1, 1)); + + final ConvexHull2D hull = generator.generate(points); + checkConvexHull(points, hull); + } + @Test public void testConvexHull() { // execute 100 random variations @@ -219,6 +231,10 @@ public abstract class ConvexHullGenerator2DAbstractTest { double sign = 0.0; final Vector2D[] points = hull.getVertices(); + if (points.length < 3) { + return true; + } + for (int i = 0; i < points.length; i++) { Vector2D p1 = points[i == 0 ? points.length - 1 : i - 1]; Vector2D p2 = points[i]; @@ -252,6 +268,9 @@ public abstract class ConvexHullGenerator2DAbstractTest { final boolean includesCollinearPoints) { final Collection hullVertices = Arrays.asList(hull.getVertices()); + if (hullVertices.size() < 3) { + return; + } final Region region = hull.createRegion(); for (final Vector2D p : points) {