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/19 18:47:56 UTC

[GitHub] [commons-geometry] agoss94 opened a new pull request, #218: Refactor hull

agoss94 opened a new pull request, #218:
URL: https://github.com/apache/commons-geometry/pull/218

   Refactoring of commons-geometry-hull module. The ConvexHull interface has been relocated to commons-geometry-core and ConvexHull2D is now part of commons-geometry-euclidean with a builder that combines the logic of the Akl-Toussaint heuristic and the Monotone-Chain to build a convex hull.
   
   Line 291-295 in ConvexHull2D has to be added to the original code in order to allow for collinear points to be considered in all cases. 
   
   All necessary test have been moved to the euclidean module as well. 


-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269930417


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {
+                hullVertices.add(lowerHull.get(idx));
+            }
+            for (int idx = 0; idx < upperHull.size() - 1; idx++) {
+                hullVertices.add(upperHull.get(idx));
+            }
+
+            // special case: if the lower and upper hull may contain only 1 point if all are identical
+            if (hullVertices.isEmpty() && !lowerHull.isEmpty()) {
+                hullVertices.add(lowerHull.get(0));
+            }
+
+            return hullVertices;
+        }
+
+        /**
+         * Update the partial hull with the current point.
+         *
+         * @param point the current point
+         * @param hull the partial hull
+         */
+        private void updateHull(final Vector2D point, final List<Vector2D> hull) {
+            if (hull.size() == 1) {
+                // ensure that we do not add an identical point
+                final Vector2D p1 = hull.get(0);
+                if (p1.eq(point, precision)) {
+                    return;
+                }
+            }
+
+            while (hull.size() >= 2) {
+                final int size = hull.size();
+                final Vector2D p1 = hull.get(size - 2);
+                final Vector2D p2 = hull.get(size - 1);
+
+                final double offset = Lines.fromPoints(p1, p2, precision).offset(point);
+                if (precision.eqZero(offset)) {
+                    // the point is collinear to the line (p1, p2)
+
+                    final double distanceToCurrent = p1.distance(point);
+                    if (precision.eqZero(distanceToCurrent) || precision.eqZero(p2.distance(point))) {
+                        // the point is assumed to be identical to either p1 or p2
+                        return;
+                    }
+
+                    final double distanceToLast = p1.distance(p2);
+                    if (includeCollinearPoints) {
+                        final int index = distanceToCurrent < distanceToLast ? size - 1 : size;
+                        hull.add(index, point);
+                    } else {
+                        if (distanceToCurrent > distanceToLast) {
+                            hull.remove(size - 1);
+                            hull.add(point);
+                        }
+                    }
+                    return;
+                } else if (offset > 0) {
+                    hull.remove(size - 1);
+                } else {
+                    break;
+                }
+            }
+            hull.add(point);
+        }
+
+        /** Return true if the given vertices define a convex hull.
+         * @param vertices the hull vertices
+         * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
+         */
+        private boolean isConvex(final Collection<Vector2D> vertices) {
+            final int size = vertices.size();
+
+            if (size < 3) {
+                // 1 or 2 points always define a convex set
+                return true;
+            }
+
+            final Iterator<Vector2D> it = vertices.iterator();
+
+            Vector2D p1 = it.next();
+            Vector2D p2 = it.next();
+            Vector2D p3;
+
+            Vector2D v1;
+            Vector2D v2;
+
+            while (it.hasNext()) {
+                p3 = it.next();
+
+                v1 = p1.vectorTo(p2);

Review Comment:
   I am going to check that tomorrow .



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1274404792


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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 quadrilateral with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of quadrilateral with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of quadrilateral with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of quadrilateral 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);

Review Comment:
   Or maybe get rid of the list at all. The way I see it. The purpose of the list is to count how many of the points are truly distinct and to go around all sides in an ordered manner. Since we only have 4 corners at all and only proceed if there are  at least 3 sides it would be possible to get rid of the list and just to count and then replace the loop with a method that checks either all three or four sides. 



-- 
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


[GitHub] [commons-geometry] darkma773r merged pull request #218: Refactor hull

Posted by "darkma773r (via GitHub)" <gi...@apache.org>.
darkma773r merged PR #218:
URL: https://github.com/apache/commons-geometry/pull/218


-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269930240


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {

Review Comment:
   I am going to check that tomorrow .



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1646135380

   All review marks have been completed. Please review all subsequent changes made.


-- 
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


[GitHub] [commons-geometry] darkma773r commented on pull request #218: Refactor hull

Posted by "darkma773r (via GitHub)" <gi...@apache.org>.
darkma773r commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1649936856

   Hello! I plan on reviewing this soon, although I might not have time until this weekend. Thanks for the hard work here!


-- 
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


[GitHub] [commons-geometry] aherbert commented on pull request #218: Refactor hull

Posted by "aherbert (via GitHub)" <gi...@apache.org>.
aherbert commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1699834707

   @darkma773r I have no further suggestion for this. If you are OK with the new API then I suggest merging.


-- 
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


[GitHub] [commons-geometry] aherbert commented on pull request #218: Refactor hull

Posted by "aherbert (via GitHub)" <gi...@apache.org>.
aherbert commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1684893275

   I think the cache of the quadrilateral can be improved. It only has to be rebuilt if the 4 points for the extrema have changed. Here the builder is recreating it each time. IIUC if the corners change then you can rebuild the quadrilateral and add to the candidates. The rest of the work in `append` is not relevant and you can simply return. This can be done by returning a `boolean` from `checkCorners`, e.g.
   
   ```java
   public Builder append(Vector2D point) {
       // Checks if the given point supersedes one of the corners.
       if (checkCorners(point)) {
           buildQuadrilateral(minY, maxX, maxY, minX);
           return this;
       }
   
       // if the quadrilateral is not well formed, e.g. only 2 points, do not attempt to reduce
       if (quadrilateral.size() < 3) {
           // Point cannot yet be dismissed.
           candidates.add(point);
           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;
   }
   ```
   
   


-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
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. I am unsure why github marks the entire file still as new, but the history can be seen by `git log --follow ConvexHull2D.java`



-- 
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


[GitHub] [commons-geometry] darkma773r commented on pull request #218: Refactor hull

Posted by "darkma773r (via GitHub)" <gi...@apache.org>.
darkma773r commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1657416998

   This looks great! Thank you, @agoss94! I think the only things to address are the two points made above (toString and caching the quadrilateral) and the code coverage. There are several if statement branches in `ConvexHull2D` that are not hit by the tests, which makes me think that either the tests are not sufficiently hitting all of the edge cases or those statements are not needed.


-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269916994


##########
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()) {

Review Comment:
   The observation is correct I changed the code. Although the method becomes a bit longer this way we can save 3 of the four null checks as suggested.



-- 
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


[GitHub] [commons-geometry] codecov-commenter commented on pull request #218: Refactor hull

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1642783261

   ## [Codecov](https://app.codecov.io/gh/apache/commons-geometry/pull/218?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   > Merging [#218](https://app.codecov.io/gh/apache/commons-geometry/pull/218?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (19dc9a9) into [master](https://app.codecov.io/gh/apache/commons-geometry/commit/c3e08ed0bf2921a6967565ab433f5c9154ccd194?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (c3e08ed) will **decrease** coverage by `0.04%`.
   > The diff coverage is `92.90%`.
   
   ```diff
   @@             Coverage Diff              @@
   ##             master     #218      +/-   ##
   ============================================
   - Coverage     98.76%   98.72%   -0.04%     
   + Complexity     3748     3696      -52     
   ============================================
     Files           195      192       -3     
     Lines         10462    10454       -8     
     Branches       1544     1544              
   ============================================
   - Hits          10333    10321      -12     
   - Misses           12       15       +3     
   - Partials        117      118       +1     
   ```
   
   
   | [Files Changed](https://app.codecov.io/gh/apache/commons-geometry/pull/218?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...ons/geometry/euclidean/twod/hull/ConvexHull2D.java](https://app.codecov.io/gh/apache/commons-geometry/pull/218?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Y29tbW9ucy1nZW9tZXRyeS1ldWNsaWRlYW4vc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2NvbW1vbnMvZ2VvbWV0cnkvZXVjbGlkZWFuL3R3b2QvaHVsbC9Db252ZXhIdWxsMkQuamF2YQ==) | `92.90% <92.90%> (ø)` | |
   
   :mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   


-- 
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: notifications-unsubscribe@commons.apache.org

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


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

Posted by "darkma773r (via GitHub)" <gi...@apache.org>.
darkma773r commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1278700931


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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 quadrilateral with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of quadrilateral with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of quadrilateral with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of quadrilateral 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);

Review Comment:
   I agree with at least caching the quadrilateral. @agoss94, I'm not sure what to picture yet for your counting approach. Can you explain that a bit more?



-- 
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


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

Posted by "darkma773r (via GitHub)" <gi...@apache.org>.
darkma773r commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1278704664


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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())

Review Comment:
   That's probably a good idea. If we do this here, we should update (in another ticket) other classes that currently produce arbitrarily large `toString` representations, like `LinePath`. They should be consistent in the number of vertices printed before truncation.



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269915517


##########
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

Review Comment:
   Fixed



##########
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.

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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269919571


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);

Review Comment:
   Yes, the change could be made here. More importantly it showed that I missed an edge case when collinear points should be included. I fixed that as well.



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1270982847


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {

Review Comment:
   Changing which point to omit was no problem. 



-- 
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


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

Posted by "aherbert (via GitHub)" <gi...@apache.org>.
aherbert commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1271140701


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {
+                hullVertices.add(lowerHull.get(idx));
+            }
+            for (int idx = 0; idx < upperHull.size() - 1; idx++) {
+                hullVertices.add(upperHull.get(idx));
+            }
+
+            // special case: if the lower and upper hull may contain only 1 point if all are identical
+            if (hullVertices.isEmpty() && !lowerHull.isEmpty()) {
+                hullVertices.add(lowerHull.get(0));
+            }
+
+            return hullVertices;
+        }
+
+        /**
+         * Update the partial hull with the current point.
+         *
+         * @param point the current point
+         * @param hull the partial hull
+         */
+        private void updateHull(final Vector2D point, final List<Vector2D> hull) {
+            if (hull.size() == 1) {
+                // ensure that we do not add an identical point
+                final Vector2D p1 = hull.get(0);
+                if (p1.eq(point, precision)) {
+                    return;
+                }
+            }
+
+            while (hull.size() >= 2) {
+                final int size = hull.size();
+                final Vector2D p1 = hull.get(size - 2);
+                final Vector2D p2 = hull.get(size - 1);
+
+                final double offset = Lines.fromPoints(p1, p2, precision).offset(point);
+                if (precision.eqZero(offset)) {
+                    // the point is collinear to the line (p1, p2)
+
+                    final double distanceToCurrent = p1.distance(point);
+                    if (precision.eqZero(distanceToCurrent) || precision.eqZero(p2.distance(point))) {
+                        // the point is assumed to be identical to either p1 or p2
+                        return;
+                    }
+
+                    final double distanceToLast = p1.distance(p2);
+                    if (includeCollinearPoints) {
+                        final int index = distanceToCurrent < distanceToLast ? size - 1 : size;
+                        hull.add(index, point);
+                    } else {
+                        if (distanceToCurrent > distanceToLast) {
+                            hull.remove(size - 1);
+                            hull.add(point);
+                        }
+                    }
+                    return;
+                } else if (offset > 0) {
+                    hull.remove(size - 1);
+                } else {
+                    break;
+                }
+            }
+            hull.add(point);
+        }
+
+        /** Return true if the given vertices define a convex hull.
+         * @param vertices the hull vertices
+         * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
+         */
+        private boolean isConvex(final Collection<Vector2D> vertices) {
+            final int size = vertices.size();
+
+            if (size < 3) {
+                // 1 or 2 points always define a convex set
+                return true;
+            }
+
+            final Iterator<Vector2D> it = vertices.iterator();
+
+            Vector2D p1 = it.next();
+            Vector2D p2 = it.next();
+            Vector2D p3;
+
+            Vector2D v1;
+            Vector2D v2;
+
+            while (it.hasNext()) {
+                p3 = it.next();
+
+                v1 = p1.vectorTo(p2);

Review Comment:
   I did mean precompute v2, then inside the loop shift v2 to v1 at the start and compute v2 again.
   
   However you have precomputed v1, and shifted v2 to v1 at the end of the loop which is effectively the same (i.e. do not duplicate computing Vectors). I prefer your code as all the shifting of variables is in one place.
   
   Note that you did not drop `p1 = p2` inside the loop (where p1 is now redundant).
   
   I'll try and review fully in the next few days.



-- 
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


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

Posted by "aherbert (via GitHub)" <gi...@apache.org>.
aherbert commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1274042831


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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 quadrilateral with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of quadrilateral with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of quadrilateral with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of quadrilateral 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);

Review Comment:
   It may be useful to cache the quadrilateral. Modification of the `checkCorners` method can be done to remove the cached quadrilateral if the point supersedes one of the corners.



##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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())

Review Comment:
   I don't think the toString should be outputting the entire representation. This could result in a very big string and possible overflow. We may wish to limit this, e.g. something like:
   ```java
   List<Vector2D> v = getVertices();
   if (v != null && v.size() > 50) {
       sb.append(v.subList(0, 50)).append(", ...");
   } else {
       sb.append(v);
   }
   ```
   



##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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 quadrilateral with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of quadrilateral with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of quadrilateral with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of quadrilateral 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) {
+                minX = minY = maxX = maxY = point;
+                candidates.add(point);
+                return;
+            }
+            if (point.getX() < minX.getX()) {
+                minX = point;
+                candidates.add(point);
+            }
+            if (point.getX() > maxX.getX()) {
+                maxX = point;
+                candidates.add(point);
+            }
+            if (point.getY() < minY.getY()) {
+                minY = point;
+                candidates.add(point);
+            }
+            if (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) {
+
+            Vector2D v1 = quadrilateralPoints.get(quadrilateralPoints.size() - 1);
+            Vector2D v2 = quadrilateralPoints.get(0);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get(i);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                double signedArea = signedAreaPoints(v1, v2, point);
+                // three collinear points have an area of zero. If we include collinear points
+                // we have to consider this case.
+                if (last * signedArea < 0 || precision.eq(signedArea, 0.0) && includeCollinearPoints) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the first point of each list is omitted as it is repeated at the end of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 1; idx < lowerHull.size(); idx++) {
+                hullVertices.add(lowerHull.get(idx));
+            }
+            for (int idx = 1; idx < upperHull.size(); idx++) {
+                hullVertices.add(upperHull.get(idx));
+            }
+
+            // special case: if the lower and upper hull may contain only 1 point if all are identical
+            if (hullVertices.isEmpty() && !lowerHull.isEmpty()) {
+                hullVertices.add(lowerHull.get(0));
+            }
+
+            return hullVertices;
+        }
+
+        /**
+         * Update the partial hull with the current point.
+         *
+         * @param point the current point
+         * @param hull the partial hull
+         */
+        private void updateHull(final Vector2D point, final List<Vector2D> hull) {
+            if (hull.size() == 1) {
+                // ensure that we do not add an identical point
+                final Vector2D p1 = hull.get(0);
+                if (p1.eq(point, precision)) {
+                    return;
+                }
+            }
+
+            while (hull.size() >= 2) {
+                final int size = hull.size();
+                final Vector2D p1 = hull.get(size - 2);
+                final Vector2D p2 = hull.get(size - 1);
+
+                final double offset = Lines.fromPoints(p1, p2, precision).offset(point);
+                if (precision.eqZero(offset)) {
+                    // the point is collinear to the line (p1, p2)
+
+                    final double distanceToCurrent = p1.distance(point);
+                    if (precision.eqZero(distanceToCurrent) || precision.eqZero(p2.distance(point))) {
+                        // the point is assumed to be identical to either p1 or p2
+                        return;
+                    }
+
+                    final double distanceToLast = p1.distance(p2);
+                    if (includeCollinearPoints) {
+                        final int index = distanceToCurrent < distanceToLast ? size - 1 : size;
+                        hull.add(index, point);
+                    } else {
+                        if (distanceToCurrent > distanceToLast) {
+                            hull.remove(size - 1);
+                            hull.add(point);
+                        }
+                    }
+                    return;
+                } else if (offset > 0) {
+                    hull.remove(size - 1);
+                } else {
+                    break;
+                }
+            }
+            hull.add(point);
+        }
+
+        /** Return true if the given vertices define a convex hull.
+         * @param vertices the hull vertices
+         * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
+         */
+        private boolean isConvex(final Collection<Vector2D> vertices) {
+            final int size = vertices.size();
+
+            if (size < 3) {
+                // 1 or 2 points always define a convex set
+                return true;
+            }
+
+            final Iterator<Vector2D> it = vertices.iterator();
+
+            Vector2D first = it.next();
+            Vector2D p1 = it.next();
+            Vector2D v1 = first.vectorTo(p1);
+
+            Vector2D p2;
+            Vector2D v2;
+
+            while (it.hasNext()) {
+                p2 = it.next();
+

Review Comment:
   Remove this empty line.



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269930417


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {
+                hullVertices.add(lowerHull.get(idx));
+            }
+            for (int idx = 0; idx < upperHull.size() - 1; idx++) {
+                hullVertices.add(upperHull.get(idx));
+            }
+
+            // special case: if the lower and upper hull may contain only 1 point if all are identical
+            if (hullVertices.isEmpty() && !lowerHull.isEmpty()) {
+                hullVertices.add(lowerHull.get(0));
+            }
+
+            return hullVertices;
+        }
+
+        /**
+         * Update the partial hull with the current point.
+         *
+         * @param point the current point
+         * @param hull the partial hull
+         */
+        private void updateHull(final Vector2D point, final List<Vector2D> hull) {
+            if (hull.size() == 1) {
+                // ensure that we do not add an identical point
+                final Vector2D p1 = hull.get(0);
+                if (p1.eq(point, precision)) {
+                    return;
+                }
+            }
+
+            while (hull.size() >= 2) {
+                final int size = hull.size();
+                final Vector2D p1 = hull.get(size - 2);
+                final Vector2D p2 = hull.get(size - 1);
+
+                final double offset = Lines.fromPoints(p1, p2, precision).offset(point);
+                if (precision.eqZero(offset)) {
+                    // the point is collinear to the line (p1, p2)
+
+                    final double distanceToCurrent = p1.distance(point);
+                    if (precision.eqZero(distanceToCurrent) || precision.eqZero(p2.distance(point))) {
+                        // the point is assumed to be identical to either p1 or p2
+                        return;
+                    }
+
+                    final double distanceToLast = p1.distance(p2);
+                    if (includeCollinearPoints) {
+                        final int index = distanceToCurrent < distanceToLast ? size - 1 : size;
+                        hull.add(index, point);
+                    } else {
+                        if (distanceToCurrent > distanceToLast) {
+                            hull.remove(size - 1);
+                            hull.add(point);
+                        }
+                    }
+                    return;
+                } else if (offset > 0) {
+                    hull.remove(size - 1);
+                } else {
+                    break;
+                }
+            }
+            hull.add(point);
+        }
+
+        /** Return true if the given vertices define a convex hull.
+         * @param vertices the hull vertices
+         * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
+         */
+        private boolean isConvex(final Collection<Vector2D> vertices) {
+            final int size = vertices.size();
+
+            if (size < 3) {
+                // 1 or 2 points always define a convex set
+                return true;
+            }
+
+            final Iterator<Vector2D> it = vertices.iterator();
+
+            Vector2D p1 = it.next();
+            Vector2D p2 = it.next();
+            Vector2D p3;
+
+            Vector2D v1;
+            Vector2D v2;
+
+            while (it.hasNext()) {
+                p3 = it.next();
+
+                v1 = p1.vectorTo(p2);

Review Comment:
   I am going to check that tomorrow .



-- 
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


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

Posted by "aherbert (via GitHub)" <gi...@apache.org>.
aherbert commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1268708345


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


Review Comment:
   This is showing as a new file, but has code from the deleted file in the old hull module. A git move and then supplementing the builder code would preserve the commit history.



##########
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. */

Review Comment:
   Q. `triangle` to `quadrilateral`?



##########
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()) {

Review Comment:
   Here we only require one null check. If minX is null then the other corners are also null and all can be set to the point:
   ```Java
   if (minX == null) {
       minX = maxX = minY = maxY = point;
       candidates.add(point);
       return;
   }
   ```
   IIUC this condition will also be met if `candidate.isEmpty()`. However using the null check does indicate that for the rest of the method the corners are not null.



##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {
+                hullVertices.add(lowerHull.get(idx));
+            }
+            for (int idx = 0; idx < upperHull.size() - 1; idx++) {
+                hullVertices.add(upperHull.get(idx));
+            }
+
+            // special case: if the lower and upper hull may contain only 1 point if all are identical
+            if (hullVertices.isEmpty() && !lowerHull.isEmpty()) {
+                hullVertices.add(lowerHull.get(0));
+            }
+
+            return hullVertices;
+        }
+
+        /**
+         * Update the partial hull with the current point.
+         *
+         * @param point the current point
+         * @param hull the partial hull
+         */
+        private void updateHull(final Vector2D point, final List<Vector2D> hull) {
+            if (hull.size() == 1) {
+                // ensure that we do not add an identical point
+                final Vector2D p1 = hull.get(0);
+                if (p1.eq(point, precision)) {
+                    return;
+                }
+            }
+
+            while (hull.size() >= 2) {
+                final int size = hull.size();
+                final Vector2D p1 = hull.get(size - 2);
+                final Vector2D p2 = hull.get(size - 1);
+
+                final double offset = Lines.fromPoints(p1, p2, precision).offset(point);
+                if (precision.eqZero(offset)) {
+                    // the point is collinear to the line (p1, p2)
+
+                    final double distanceToCurrent = p1.distance(point);
+                    if (precision.eqZero(distanceToCurrent) || precision.eqZero(p2.distance(point))) {
+                        // the point is assumed to be identical to either p1 or p2
+                        return;
+                    }
+
+                    final double distanceToLast = p1.distance(p2);
+                    if (includeCollinearPoints) {
+                        final int index = distanceToCurrent < distanceToLast ? size - 1 : size;
+                        hull.add(index, point);
+                    } else {
+                        if (distanceToCurrent > distanceToLast) {
+                            hull.remove(size - 1);
+                            hull.add(point);
+                        }
+                    }
+                    return;
+                } else if (offset > 0) {
+                    hull.remove(size - 1);
+                } else {
+                    break;
+                }
+            }
+            hull.add(point);
+        }
+
+        /** Return true if the given vertices define a convex hull.
+         * @param vertices the hull vertices
+         * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
+         */
+        private boolean isConvex(final Collection<Vector2D> vertices) {
+            final int size = vertices.size();
+
+            if (size < 3) {
+                // 1 or 2 points always define a convex set
+                return true;
+            }
+
+            final Iterator<Vector2D> it = vertices.iterator();
+
+            Vector2D p1 = it.next();
+            Vector2D p2 = it.next();
+            Vector2D p3;
+
+            Vector2D v1;
+            Vector2D v2;
+
+            while (it.hasNext()) {
+                p3 = it.next();
+
+                v1 = p1.vectorTo(p2);

Review Comment:
   Precompute `v2` before the loop and then here use `v1 = v2`. By shifting the vectors you save the vector computation, and the need to track p1.



##########
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.

Review Comment:
   Whitespace after `//`. Same again on the next comment below.



##########
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:
   Indentation



##########
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

Review Comment:
   Whitespace after `/**`



##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);

Review Comment:
   Since this walks a circle around the quadrilateral we can start anywhere. IIUC this can be changed to:
   ```java
   final int size = quadrilateralPoints.size();
   Vector2D v1 = quadrilateralPoints.get(size - 1);
   Vector2D v2 = quadrilateralPoints.get(0);
   
   // ...
   
   for (int i = 1; i < size; i++) {
       v1 = v2;
       v2 = quadrilateralPoints.get(i);
   ```
   



##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {

Review Comment:
   Can these loops be changed to omit the first point of each half:
   ```java
   for (int idx = 1; idx < lowerHull.size(); idx++) {
   ```



-- 
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


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

Posted by "darkma773r (via GitHub)" <gi...@apache.org>.
darkma773r commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1283691118


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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())

Review Comment:
   That sounds good to me.



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1274404792


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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 quadrilateral with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of quadrilateral with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of quadrilateral with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of quadrilateral 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);

Review Comment:
   Or maybe get rid of the list at all. The way I see it. The purpose of the list is to count how many of the points are truely distinct and to go around all sides in an ordered manner. Since we only have 4 corners at all and only proceed if there are  at least 3 sides it would be possible to get rid of the list and just to count and then replace the loop with a method that checks either all three or four sides. 



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1271259747


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {
+                hullVertices.add(lowerHull.get(idx));
+            }
+            for (int idx = 0; idx < upperHull.size() - 1; idx++) {
+                hullVertices.add(upperHull.get(idx));
+            }
+
+            // special case: if the lower and upper hull may contain only 1 point if all are identical
+            if (hullVertices.isEmpty() && !lowerHull.isEmpty()) {
+                hullVertices.add(lowerHull.get(0));
+            }
+
+            return hullVertices;
+        }
+
+        /**
+         * Update the partial hull with the current point.
+         *
+         * @param point the current point
+         * @param hull the partial hull
+         */
+        private void updateHull(final Vector2D point, final List<Vector2D> hull) {
+            if (hull.size() == 1) {
+                // ensure that we do not add an identical point
+                final Vector2D p1 = hull.get(0);
+                if (p1.eq(point, precision)) {
+                    return;
+                }
+            }
+
+            while (hull.size() >= 2) {
+                final int size = hull.size();
+                final Vector2D p1 = hull.get(size - 2);
+                final Vector2D p2 = hull.get(size - 1);
+
+                final double offset = Lines.fromPoints(p1, p2, precision).offset(point);
+                if (precision.eqZero(offset)) {
+                    // the point is collinear to the line (p1, p2)
+
+                    final double distanceToCurrent = p1.distance(point);
+                    if (precision.eqZero(distanceToCurrent) || precision.eqZero(p2.distance(point))) {
+                        // the point is assumed to be identical to either p1 or p2
+                        return;
+                    }
+
+                    final double distanceToLast = p1.distance(p2);
+                    if (includeCollinearPoints) {
+                        final int index = distanceToCurrent < distanceToLast ? size - 1 : size;
+                        hull.add(index, point);
+                    } else {
+                        if (distanceToCurrent > distanceToLast) {
+                            hull.remove(size - 1);
+                            hull.add(point);
+                        }
+                    }
+                    return;
+                } else if (offset > 0) {
+                    hull.remove(size - 1);
+                } else {
+                    break;
+                }
+            }
+            hull.add(point);
+        }
+
+        /** Return true if the given vertices define a convex hull.
+         * @param vertices the hull vertices
+         * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
+         */
+        private boolean isConvex(final Collection<Vector2D> vertices) {
+            final int size = vertices.size();
+
+            if (size < 3) {
+                // 1 or 2 points always define a convex set
+                return true;
+            }
+
+            final Iterator<Vector2D> it = vertices.iterator();
+
+            Vector2D p1 = it.next();
+            Vector2D p2 = it.next();
+            Vector2D p3;
+
+            Vector2D v1;
+            Vector2D v2;
+
+            while (it.hasNext()) {
+                p3 = it.next();
+
+                v1 = p1.vectorTo(p2);

Review Comment:
   Ah I see. I did miss the assignment. I deleted the line and renamed the variables to better fit the now altered code. Thanks for pointing out. 



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269917506


##########
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. */

Review Comment:
   Yes, that should obviously mean quadrilateral.



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1283654884


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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 quadrilateral with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of quadrilateral with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of quadrilateral with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of quadrilateral 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);

Review Comment:
   It isn't really a new approach, but maybe more the fact, that the loop has only 3-4 iterations which could be straight forward to code. Since we only check the sides [minY, maxX], [maxX, maxY], [maxY, minX], [minX, minY]  for any given point, whereas we skip any side if the two points align, which can happen only once. I guess one could implement this logic entirely without a loop. 
   I guess the simplest way is still to just cache the list. 



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1283641369


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,475 @@
+/*
+ * 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())

Review Comment:
   I would suggest to refrain from the change for now and to open a new ticket for this one. Gilles does have a different opinion on this topic (https://issues.apache.org/jira/projects/GEOMETRY/issues/GEOMETRY-110?filter=allopenissues) and even if we implement the change all the other classes still need an update. Better to change all the classes in one ticket. 



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1676064407

   @darkma773r I added a few test and removed some if branches which seemed to be really unnecessary. Most branches were not hit because those checked for identical points, which is superfluous as the PointsSet already filtered thoses out. 
   Other if branches were not needed because of the comparator which orders the points before constructing the lower and upper hull. This allows us to make assumtions on the calculated distance to previously added points. 
   You may double check the logic in deleting these checks.


-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1270983790


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {
+                hullVertices.add(lowerHull.get(idx));
+            }
+            for (int idx = 0; idx < upperHull.size() - 1; idx++) {
+                hullVertices.add(upperHull.get(idx));
+            }
+
+            // special case: if the lower and upper hull may contain only 1 point if all are identical
+            if (hullVertices.isEmpty() && !lowerHull.isEmpty()) {
+                hullVertices.add(lowerHull.get(0));
+            }
+
+            return hullVertices;
+        }
+
+        /**
+         * Update the partial hull with the current point.
+         *
+         * @param point the current point
+         * @param hull the partial hull
+         */
+        private void updateHull(final Vector2D point, final List<Vector2D> hull) {
+            if (hull.size() == 1) {
+                // ensure that we do not add an identical point
+                final Vector2D p1 = hull.get(0);
+                if (p1.eq(point, precision)) {
+                    return;
+                }
+            }
+
+            while (hull.size() >= 2) {
+                final int size = hull.size();
+                final Vector2D p1 = hull.get(size - 2);
+                final Vector2D p2 = hull.get(size - 1);
+
+                final double offset = Lines.fromPoints(p1, p2, precision).offset(point);
+                if (precision.eqZero(offset)) {
+                    // the point is collinear to the line (p1, p2)
+
+                    final double distanceToCurrent = p1.distance(point);
+                    if (precision.eqZero(distanceToCurrent) || precision.eqZero(p2.distance(point))) {
+                        // the point is assumed to be identical to either p1 or p2
+                        return;
+                    }
+
+                    final double distanceToLast = p1.distance(p2);
+                    if (includeCollinearPoints) {
+                        final int index = distanceToCurrent < distanceToLast ? size - 1 : size;
+                        hull.add(index, point);
+                    } else {
+                        if (distanceToCurrent > distanceToLast) {
+                            hull.remove(size - 1);
+                            hull.add(point);
+                        }
+                    }
+                    return;
+                } else if (offset > 0) {
+                    hull.remove(size - 1);
+                } else {
+                    break;
+                }
+            }
+            hull.add(point);
+        }
+
+        /** Return true if the given vertices define a convex hull.
+         * @param vertices the hull vertices
+         * @return {@code true} if the vertices form a convex hull, {@code false} otherwise
+         */
+        private boolean isConvex(final Collection<Vector2D> vertices) {
+            final int size = vertices.size();
+
+            if (size < 3) {
+                // 1 or 2 points always define a convex set
+                return true;
+            }
+
+            final Iterator<Vector2D> it = vertices.iterator();
+
+            Vector2D p1 = it.next();
+            Vector2D p2 = it.next();
+            Vector2D p3;
+
+            Vector2D v1;
+            Vector2D v2;
+
+            while (it.hasNext()) {
+                p3 = it.next();
+
+                v1 = p1.vectorTo(p2);

Review Comment:
   Did you mean to precompute v1? I did that an then made the assigment at the end of the loop which worked without a problem.



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1269930240


##########
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) {
+
+            Vector2D v1 = quadrilateralPoints.get(0);
+            Vector2D v2 = quadrilateralPoints.get(1);
+
+            if (point.equals(v1) || point.equals(v2)) {
+                return true;
+            }
+
+            // get the location of the point relative to the first two vertices
+            final double last = signedAreaPoints(v1, v2, point);
+
+            // If the area is zero then this means the given point is on a boundary line.
+            // and must be included as collinear point.
+            if (precision.eq(last, 0.0) && includeCollinearPoints) {
+                return false;
+            }
+
+            final int size = quadrilateralPoints.size();
+            // loop through the rest of the vertices
+            for (int i = 1; i < size; i++) {
+                v1 = v2;
+                v2 = quadrilateralPoints.get((i + 1) == size ? 0 : i + 1);
+
+                if (point.equals(v2)) {
+                    return true;
+                }
+
+                // do side of line test: multiply the last location with this location
+                // if they are the same sign then the operation will yield a positive result
+                // -x * -y = +xy, x * y = +xy, -x * y = -xy, x * -y = -xy
+                if (last * signedAreaPoints(v1, v2, point) < 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        /** Compute the signed area of the parallelogram formed by vectors between the given points. The first
+         * vector points from {@code p0} to {@code p1} and the second from {@code p0} to {@code p3}.
+         * @param p0 first point
+         * @param p1 second point
+         * @param p2 third point
+         * @return signed area of parallelogram formed by vectors between the given points
+         */
+        private static double signedAreaPoints(final Vector2D p0, final Vector2D p1, final Vector2D p2) {
+            return p0.vectorTo(p1).signedArea(p0.vectorTo(p2));
+        }
+
+        /**
+         * Find the convex hull vertices from the set of input points.
+         * @param points the set of input points
+         * @return the convex hull vertices in CCW winding
+         */
+        private Collection<Vector2D> findHullVertices(final Collection<Vector2D> points) {
+
+            final List<Vector2D> pointsSortedByXAxis = new ArrayList<>(points);
+
+            // sort the points in increasing order on the x-axis
+            pointsSortedByXAxis.sort((o1, o2) -> {
+                // need to take the tolerance value into account, otherwise collinear points
+                // will not be handled correctly when building the upper/lower hull
+                final int cmp = precision.compare(o1.getX(), o2.getX());
+                if (cmp == 0) {
+                    return precision.compare(o1.getY(), o2.getY());
+                } else {
+                    return cmp;
+                }
+            });
+
+            // build lower hull
+            final List<Vector2D> lowerHull = new ArrayList<>();
+            for (final Vector2D p : pointsSortedByXAxis) {
+                updateHull(p, lowerHull);
+            }
+
+            // build upper hull
+            final List<Vector2D> upperHull = new ArrayList<>();
+            for (int idx = pointsSortedByXAxis.size() - 1; idx >= 0; idx--) {
+                final Vector2D p = pointsSortedByXAxis.get(idx);
+                updateHull(p, upperHull);
+            }
+
+            // concatenate the lower and upper hulls
+            // the last point of each list is omitted as it is repeated at the beginning of the other list
+            final List<Vector2D> hullVertices = new ArrayList<>(lowerHull.size() + upperHull.size() - 2);
+            for (int idx = 0; idx < lowerHull.size() - 1; idx++) {

Review Comment:
   I am going to check that tomorrow .



-- 
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


[GitHub] [commons-geometry] darkma773r commented on pull request #218: Refactor hull

Posted by "darkma773r (via GitHub)" <gi...@apache.org>.
darkma773r commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1683119565

   This looks good to me! @aherbert, any objections to merging this in (after a rebase, it looks like)?


-- 
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


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

Posted by "aherbert (via GitHub)" <gi...@apache.org>.
aherbert commented on code in PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#discussion_r1303360588


##########
commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/hull/ConvexHull2D.java:
##########
@@ -0,0 +1,461 @@
+/*
+ * 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.core.collection.PointSet;
+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 quadrilateral with minimal x coordinate. */
+        private Vector2D minX;
+
+        /** Corner of quadrilateral with maximal x coordinate. */
+        private Vector2D maxX;
+
+        /** Corner of quadrilateral with minimal y coordinate. */
+        private Vector2D minY;
+
+        /** Corner of quadrilateral with maximal y coordinate. */
+        private Vector2D maxY;
+
+        /** Collection of all remaining candidates for a convex hull. */
+        private final PointSet<Vector2D> candidates;
+
+        /** Points are tested against this quadrilateral. */
+        private final List<Vector2D> quadrilateral;
+
+        /** 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);
+            quadrilateral = new ArrayList<>();
+        }
+
+        /** 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.
+            if (checkCorners(point)) {
+                //build quadrilateral if any of the corners has changed.
+                buildQuadrilateral(minY, maxX, maxY, minX);
+                return this;
+            }
+
+            // if the quadrilateral is not well formed, e.g. only 2 points, do not attempt to reduce
+            if (quadrilateral.size() < 3) {
+                // Point cannot yet be dismissed.
+                candidates.add(point);
+                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)) {
+                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
+         */
+        private void buildQuadrilateral(final Vector2D... points) {
+            quadrilateral.clear();
+            for (final Vector2D p : points) {
+                if (!quadrilateral.contains(p)) {
+                    quadrilateral.add(p);
+                }
+            }
+        }
+
+        /**
+         * 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.
+         * @return {@code true} if any of the corners changed as a result of the check,
+         *         {@code false} otherwise.
+         */
+        boolean checkCorners(Vector2D point) {
+            boolean hasBeenModified = false;

Review Comment:
   Note: You do not require this flag until after your `min == null` check



-- 
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


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

Posted by "agoss94 (via GitHub)" <gi...@apache.org>.
agoss94 commented on PR #218:
URL: https://github.com/apache/commons-geometry/pull/218#issuecomment-1690372110

   >
   Proposition has been implemented. Thanks for the detailed review :) I learned a lot. 
   The branch has also been rebased. 


-- 
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