diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/hull/ConvexHull3D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/hull/ConvexHull3D.java index 3688c8012..8e6f38a68 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/hull/ConvexHull3D.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/hull/ConvexHull3D.java @@ -151,6 +151,9 @@ public static class Builder { /** Precision context used to compare floating point numbers. */ private final DoubleEquivalence precision; + /** Simplex for testing new points and starting the algorithm. */ + private Simplex simplex; + /** * A map which contains all the vertices of the current hull as keys and the * associated facets as values. @@ -186,7 +189,11 @@ public Builder append(Vector3D point) { * @return this instance. */ public Builder append(Collection points) { - points.forEach(this::append); + simplex = createSimplex(points); + candidates.addAll(points); + if (!simplex.isDegenerate()) { + distributePoints(simplex); + } return this; } @@ -196,13 +203,10 @@ public Builder append(Collection points) { * @return a convex hull containing all appended points. */ public ConvexHull3D build() { - if (candidates.size() < 4) { + if (simplex == null) { return new ConvexHull3D(candidates, true); } - // Construct an initial simplex with extreme properties. - Simplex simplex = createSimplex(candidates); - // The simplex is degenerate. if (simplex.isDegenerate()) { return new ConvexHull3D(candidates, true);