View Javadoc

    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    *      http://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.apache.commons.geometry.enclosing;
    18
    19  import java.util.ArrayList;
    20  import java.util.Collection;
    21  import java.util.Collections;
    22  import java.util.List;
    23
    24  import org.apache.commons.geometry.core.Point;
    25  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
    26
    27  /** This class represents a ball enclosing some points.
    28   * @param <P> Point type.
    29   * @see Point
    30   * @see Encloser
    31   */
    32  public class EnclosingBall<P extends Point<P>> {
    33      /** Center of the ball. */
    34      private final P center;
    35
    36      /** Radius of the ball. */
    37      private final double radius;
    38
    39      /** Support points used to define the ball. */
    40      private final List<P> support;
    41
    42      /** Construct an enclosing ball defined by a collection of support points. Callers are responsible
    43       * for ensuring that the given points lie inside the ball. No validation is performed.
    44       * @param center center of the ball
    45       * @param radius radius of the ball
    46       * @param support support points used to define the ball
    47       */
    48      public EnclosingBall(final P center, final double radius, final Collection<P> support) {
    49          this.center  = center;
    50          this.radius  = radius;
    51          this.support = Collections.unmodifiableList(new ArrayList<>(support));
    52      }
    53
    54      /** Get the center of the ball.
    55       * @return center of the ball
    56       */
    57      public P getCenter() {
    58          return center;
    59      }
    60
    61      /** Get the radius of the ball.
    62       * @return radius of the ball (can be negative if the ball is empty)
    63       */
    64      public double getRadius() {
    65          return radius;
    66      }
    67
    68      /** Get the support points used to define the ball.
    69       * @return support points used to define the ball
    70       */
    71      public List<P> getSupport() {
    72          return support;
    73      }
    74
    75      /** Get the number of support points used to define the ball.
    76       * @return number of support points used to define the ball
    77       */
    78      public int getSupportSize() {
    79          return support.size();
    80      }
    81
    82      /** Check if a point is within the ball or on the boundary. True is returned if the
    83       * distance from the center of the ball to the given point is strictly less than
    84       * or equal to the ball radius.
    85       * @param point point to test
    86       * @return true if the point is within the ball or on the boundary
    87       */
    88      public boolean contains(final P point) {
    89          return point.distance(center) <= radius;
    90      }
    91
    92      /** Check if a point is within the ball or on the boundary, using the given precision
    93       * context for floating point comparison. True is returned if the distance from the
    94       * center of the ball to the given point is less than or equal to the ball radius
    95       * as evaluated by the precision context.
    96       * @param point point to test
    97       * @param precision precision context to use for floating point comparisons
    98       * @return true if the point is within the ball or on the boundary as evaluated by
    99       *      the precision context
    100      */
    101     public boolean contains(final P point, final DoublePrecisionContext precision) {
    102         return precision.lte(point.distance(center), radius);
    103     }
    104
    105     /** {@inheritDoc} */
    106     @Override
    107     public String toString() {
    108         final StringBuilder sb = new StringBuilder();
    109         sb.append(getClass().getSimpleName())
    110             .append("[center= ")
    111             .append(getCenter())
    112             .append(", radius= ")
    113             .append(getRadius())
    114             .append(']');
    115
    116         return sb.toString();
    117     }
    118 }