EnclosingBall.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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.geometry.enclosing;

  22. import java.io.Serializable;

  23. import org.hipparchus.geometry.Point;
  24. import org.hipparchus.geometry.Space;

  25. /** This class represents a ball enclosing some points.
  26.  * @param <S> Space type.
  27.  * @param <P> Point type.
  28.  * @see Space
  29.  * @see Point
  30.  * @see Encloser
  31.  */
  32. public class EnclosingBall<S extends Space, P extends Point<S, P>> implements Serializable {

  33.     /** Serializable UID. */
  34.     private static final long serialVersionUID = 20140126L;

  35.     /** Center of the ball. */
  36.     private final P center;

  37.     /** Radius of the ball. */
  38.     private final double radius;

  39.     /** Support points used to define the ball. */
  40.     private final P[] support;

  41.     /** Simple constructor.
  42.      * @param center center of the ball
  43.      * @param radius radius of the ball
  44.      * @param support support points used to define the ball
  45.      */
  46.     @SafeVarargs
  47.     public EnclosingBall(final P center, final double radius, final P ... support) {
  48.         this.center  = center;
  49.         this.radius  = radius;
  50.         this.support = support.clone();
  51.     }

  52.     /** Get the center of the ball.
  53.      * @return center of the ball
  54.      */
  55.     public P getCenter() {
  56.         return center;
  57.     }

  58.     /** Get the radius of the ball.
  59.      * @return radius of the ball (can be negative if the ball is empty)
  60.      */
  61.     public double getRadius() {
  62.         return radius;
  63.     }

  64.     /** Get the support points used to define the ball.
  65.      * @return support points used to define the ball
  66.      */
  67.     public P[] getSupport() {
  68.         return support.clone();
  69.     }

  70.     /** Get the number of support points used to define the ball.
  71.      * @return number of support points used to define the ball
  72.      */
  73.     public int getSupportSize() {
  74.         return support.length;
  75.     }

  76.     /** Check if a point is within the ball or at boundary.
  77.      * @param point point to test
  78.      * @return true if the point is within the ball or at boundary
  79.      */
  80.     public boolean contains(final P point) {
  81.         return point.distance(center) <= radius;
  82.     }

  83.     /** Check if a point is within an enlarged ball or at boundary.
  84.      * @param point point to test
  85.      * @param margin margin to consider
  86.      * @return true if the point is within the ball enlarged
  87.      * by the margin or at boundary
  88.      */
  89.     public boolean contains(final P point, final double margin) {
  90.         return point.distance(center) <= radius + margin;
  91.     }

  92. }