You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "agoss94 (via GitHub)" <gi...@apache.org> on 2023/07/20 20:09:08 UTC

[GitHub] [commons-geometry] agoss94 commented on a diff in pull request #218: Refactor hull

agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269915290


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########


Review Comment:
   Old file has been retrieved and moved to the new location as suggested.



##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,469 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.geometry.euclidean.twod.hull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.geometry.core.ConvexHull;
+import org.apache.commons.geometry.euclidean.EuclideanCollections;
+import org.apache.commons.geometry.euclidean.twod.ConvexArea;
+import org.apache.commons.geometry.euclidean.twod.Lines;
+import org.apache.commons.geometry.euclidean.twod.Vector2D;
+import org.apache.commons.geometry.euclidean.twod.path.LinePath;
+import org.apache.commons.numbers.core.Precision;
+
+/**
+ * This class represents a convex hull in two-dimensional Euclidean space.
+ */
+public final class ConvexHull2D implements ConvexHull<Vector2D> {
+
+    /** Vertices for the convex hull, in order. */
+    private final List<Vector2D> vertices;
+
+    /** Polyline path for the convex hull. */
+    private final LinePath path;
+
+    /** Simple constructor; no validation is performed.
+     * @param vertices the vertices of the convex hull; callers are responsible for ensuring that
+     *      the given vertices are in order, unique, and define a convex hull.
+     * @param precision precision context used to compare floating point numbers
+     */
+    ConvexHull2D(final Collection<Vector2D> vertices, final Precision.DoubleEquivalence precision) {
+        this.vertices = Collections.unmodifiableList(new ArrayList<>(vertices));
+        this.path = buildHullPath(vertices, precision);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public List<Vector2D> getVertices() {
+        return vertices;
+    }
+
+    /** Get a path defining the convex hull. The path will contain
+     * <ul>
+     *      <li>zero segments if the hull consists of only a single point,</li>
+     *      <li>one segment if the hull consists of two points,</li>
+     *      <li>three or more segments defining a closed loop if the hull consists of more than
+     *          two non-collinear points.</li>
+     * </ul>
+     * @return polyline path defining the convex hull
+     */
+    public LinePath getPath() {
+        return path;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public ConvexArea getRegion() {
+        return path.isClosed() ?
+                ConvexArea.convexPolygonFromPath(path) :
+                null;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append(getClass().getSimpleName())
+            .append("[vertices= ")
+            .append(getVertices())
+            .append(']');
+
+        return sb.toString();
+    }
+
+    /** Build a polyline representing the path for a convex hull.
+     * @param vertices convex hull vertices
+     * @param precision precision context used to compare floating point values
+     * @return path for the convex hull defined by the given vertices
+     */
+    private static LinePath buildHullPath(final Collection<Vector2D> vertices,
+            final Precision.DoubleEquivalence precision) {
+        if (vertices.size() < 2) {
+            return LinePath.empty();
+        }
+
+        final boolean closeLoop = vertices.size() > 2;
+
+        return LinePath.builder(precision)
+                .appendVertices(vertices)
+                .build(closeLoop);
+    }
+
+    /** Class used to build convex hulls. The builder is based on the Akl-Toussaint
+     * heuristic to construct the hull. The heuristic is based on the idea of a
+     * convex quadrilateral, which is formed by four points with the lowest and
+     * highest x / y coordinates. Any point that lies inside this quadrilateral can
+     * not be part of the convex hull and can thus be safely discarded before
+     * generating the convex hull itself.
+     * <p>
+     * The complexity of the operation is O(n), and may greatly improve the time it
+     * takes to construct the convex hull afterwards, depending on the point
+     * distribution.
+     *
+     * @see <a href=
+     *      "http://en.wikipedia.org/wiki/Convex_hull_algorithms#Akl-Toussaint_heuristic">
+     *      Akl-Toussaint heuristic (Wikipedia)</a>
+     */
+    public static final class Builder {
+
+        /** Corner of triangle with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of triangle with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of triangle with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of triangle with maximal y coordinate. */
+        private Vector2D maxY;
+
+        /** Collection of all remaining candidates for a convex hull. */
+        private final Collection<Vector2D> candidates;
+
+        /** A precision context for comparing points. */
+        private final Precision.DoubleEquivalence precision;
+
+        /** Indicates if collinear points on the hull shall be present in the output.
+         * If {@code false}, only the extreme points are added to the hull.
+         */
+        private final boolean includeCollinearPoints;
+
+        /**Return a {@link Builder} instance configured with the given precision
+         * context. The precision context is used when comparing points.
+         *
+         * @param builderPrecision       precision context to use when building a convex
+         *                               hull from raw vertices; may be null if raw
+         *                               vertices are not used.
+         * @param includeCollinearPoints whether collinear points shall be added as hull
+         *                               vertices
+         */
+        public Builder(final boolean includeCollinearPoints, final Precision.DoubleEquivalence builderPrecision) {
+            this.precision = builderPrecision;
+            this.includeCollinearPoints = includeCollinearPoints;
+            candidates = EuclideanCollections.pointSet2D(builderPrecision);
+        }
+
+        /** Appends the given point to a collection of possible hull points, if and only
+         * if the given point is outside of a constructed quadrilateral of extreme properties.
+         *
+         * @param point a given point.
+         * @return this instance.
+         */
+        public Builder append(Vector2D point) {
+
+            //Checks if the given point supersedes one of the corners.
+            checkCorners(point);
+
+            //Only proceed if the quadrilateral is complete.
+            if (candidates.size() < 4) {
+                return this;
+            }
+
+            final List<Vector2D> quadrilateral = buildQuadrilateral(minY, maxX, maxY, minX);
+            // if the quadrilateral is not well formed, e.g. only 2 points, do not attempt to reduce
+            if (quadrilateral.size() < 3) {
+                return this;
+            }
+
+            // check all points if they are within the quadrilateral
+            // in which case they can not be part of the convex hull
+            if (!insideQuadrilateral(point, quadrilateral)) {
+                candidates.add(point);
+            }
+
+            return this;
+        }
+
+        /** Appends the given points to a collection of possible hull points, if and only
+         * if the given points are outside of a constructed quadrilateral of extreme
+         * properties.
+         *
+         * @param points a given collection of points.
+         * @throws NullPointerException if points is {@code null}.
+         * @return this instance.
+         */
+        public Builder append(Collection<Vector2D> points) {
+            points.forEach(this::append);
+            return this;
+        }
+
+        /**
+         * Build a convex hull from the set appended points.
+         *
+         * @return the convex hull
+         * @throws IllegalStateException if generator fails to generate a convex hull for
+         *      the given set of input points
+         */
+        public ConvexHull2D build() {
+            Collection<Vector2D> hullVertices;
+            if (candidates.size() < 2) {
+                hullVertices = candidates;
+            } else {
+                hullVertices = findHullVertices(candidates);
+            }
+
+            if (!isConvex(hullVertices)) {
+                throw new IllegalStateException("Convex hull algorithm failed to generate solution");
+            }
+
+            return new ConvexHull2D(hullVertices, precision);
+        }
+
+        /** Build the convex quadrilateral with the found corner points (with min/max x/y
+         * coordinates).
+         *
+         * @param points the respective points with min/max x/y coordinate
+         * @return the quadrilateral
+         */
+        private static List<Vector2D> buildQuadrilateral(final Vector2D... points) {
+            final List<Vector2D> quadrilateral = new ArrayList<>();
+            for (final Vector2D p : points) {
+                if (!quadrilateral.contains(p)) {
+                    quadrilateral.add(p);
+                }
+            }
+            return quadrilateral;
+        }
+
+        /** Checks if the given point supersedes one of the corners. If it does the old
+         * corner is removed and the point added to the collection of points.
+         *
+         * @param point a given point.
+         */
+        private void checkCorners(Vector2D point) {
+            if (minX == null || point.getX() < minX.getX()) {
+                minX = point;
+                candidates.add(point);
+            }
+            if (maxX == null || point.getX() > maxX.getX()) {
+                maxX = point;
+                candidates.add(point);
+            }
+            if (minY == null || point.getY() < minY.getY()) {
+                minY = point;
+                candidates.add(point);
+            }
+            if (maxY == null || point.getY() > maxY.getY()) {
+                maxY = point;
+                candidates.add(point);
+            }
+        }
+
+        /** Checks if the given point is located within the convex quadrilateral.
+         * @param point the point to check
+         * @param quadrilateralPoints the convex quadrilateral, represented by 4 points
+         * @return {@code true} if the point is inside the quadrilateral, {@code false} otherwise
+         */
+        private boolean insideQuadrilateral(final Vector2D point,
+                                                   final List<? extends Vector2D> quadrilateralPoints) {

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org