AbstractConvexHullGenerator2D.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.hipparchus.geometry.euclidean.twod.hull;

  18. import java.util.Collection;

  19. import org.hipparchus.exception.LocalizedCoreFormats;
  20. import org.hipparchus.exception.MathIllegalArgumentException;
  21. import org.hipparchus.exception.MathIllegalStateException;
  22. import org.hipparchus.geometry.euclidean.twod.Vector2D;
  23. import org.hipparchus.util.MathUtils;

  24. /**
  25.  * Abstract base class for convex hull generators in the two-dimensional euclidean space.
  26.  *
  27.  */
  28. abstract class AbstractConvexHullGenerator2D implements ConvexHullGenerator2D {

  29.     /** Default value for tolerance. */
  30.     private static final double DEFAULT_TOLERANCE = 1e-10;

  31.     /** Tolerance below which points are considered identical. */
  32.     private final double tolerance;

  33.     /**
  34.      * Indicates if collinear points on the hull shall be present in the output.
  35.      * If {@code false}, only the extreme points are added to the hull.
  36.      */
  37.     private final boolean includeCollinearPoints;

  38.     /**
  39.      * Simple constructor.
  40.      * <p>
  41.      * The default tolerance (1e-10) will be used to determine identical points.
  42.      *
  43.      * @param includeCollinearPoints indicates if collinear points on the hull shall be
  44.      * added as hull vertices
  45.      */
  46.     protected AbstractConvexHullGenerator2D(final boolean includeCollinearPoints) {
  47.         this(includeCollinearPoints, DEFAULT_TOLERANCE);
  48.     }

  49.     /**
  50.      * Simple constructor.
  51.      *
  52.      * @param includeCollinearPoints indicates if collinear points on the hull shall be
  53.      * added as hull vertices
  54.      * @param tolerance tolerance below which points are considered identical
  55.      */
  56.     protected AbstractConvexHullGenerator2D(final boolean includeCollinearPoints, final double tolerance) {
  57.         this.includeCollinearPoints = includeCollinearPoints;
  58.         this.tolerance = tolerance;
  59.     }

  60.     /**
  61.      * Get the tolerance below which points are considered identical.
  62.      * @return the tolerance below which points are considered identical
  63.      */
  64.     public double getTolerance() {
  65.         return tolerance;
  66.     }

  67.     /**
  68.      * Returns if collinear points on the hull will be added as hull vertices.
  69.      * @return {@code true} if collinear points are added as hull vertices, or {@code false}
  70.      * if only extreme points are present.
  71.      */
  72.     public boolean isIncludeCollinearPoints() {
  73.         return includeCollinearPoints;
  74.     }

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public ConvexHull2D generate(final Collection<Vector2D> points)
  78.             throws MathIllegalStateException {
  79.         // check for null points
  80.         MathUtils.checkNotNull(points);

  81.         final Collection<Vector2D> hullVertices;
  82.         if (points.size() < 2) {
  83.             hullVertices = points;
  84.         } else {
  85.             hullVertices = findHullVertices(points);
  86.         }

  87.         try {
  88.             return new ConvexHull2D(hullVertices.toArray(new Vector2D[0]), tolerance);
  89.         } catch (MathIllegalArgumentException e) {
  90.             // the hull vertices may not form a convex hull if the tolerance value is to large
  91.             throw new MathIllegalStateException(e, LocalizedCoreFormats.CONVERGENCE_FAILED);
  92.         }
  93.     }

  94.     /**
  95.      * Find the convex hull vertices from the set of input points.
  96.      * @param points the set of input points
  97.      * @return the convex hull vertices in CCW winding
  98.      */
  99.     protected abstract Collection<Vector2D> findHullVertices(Collection<Vector2D> points);

  100. }