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    *      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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.enclosing;
23  
24  import java.io.Serializable;
25  
26  import org.hipparchus.geometry.Point;
27  import org.hipparchus.geometry.Space;
28  
29  /** This class represents a ball enclosing some points.
30   * @param <S> Space type.
31   * @param <P> Point type.
32   * @see Space
33   * @see Point
34   * @see Encloser
35   */
36  public class EnclosingBall<S extends Space, P extends Point<S>> implements Serializable {
37  
38      /** Serializable UID. */
39      private static final long serialVersionUID = 20140126L;
40  
41      /** Center of the ball. */
42      private final P center;
43  
44      /** Radius of the ball. */
45      private final double radius;
46  
47      /** Support points used to define the ball. */
48      private final P[] support;
49  
50      /** Simple constructor.
51       * @param center center of the ball
52       * @param radius radius of the ball
53       * @param support support points used to define the ball
54       */
55      @SafeVarargs
56      public EnclosingBall(final P center, final double radius, final P ... support) {
57          this.center  = center;
58          this.radius  = radius;
59          this.support = support.clone();
60      }
61  
62      /** Get the center of the ball.
63       * @return center of the ball
64       */
65      public P getCenter() {
66          return center;
67      }
68  
69      /** Get the radius of the ball.
70       * @return radius of the ball (can be negative if the ball is empty)
71       */
72      public double getRadius() {
73          return radius;
74      }
75  
76      /** Get the support points used to define the ball.
77       * @return support points used to define the ball
78       */
79      public P[] getSupport() {
80          return support.clone();
81      }
82  
83      /** Get the number of support points used to define the ball.
84       * @return number of support points used to define the ball
85       */
86      public int getSupportSize() {
87          return support.length;
88      }
89  
90      /** Check if a point is within the ball or at boundary.
91       * @param point point to test
92       * @return true if the point is within the ball or at boundary
93       */
94      public boolean contains(final P point) {
95          return point.distance(center) <= radius;
96      }
97  
98      /** Check if a point is within an enlarged ball or at boundary.
99       * @param point point to test
100      * @param margin margin to consider
101      * @return true if the point is within the ball enlarged
102      * by the margin or at boundary
103      */
104     public boolean contains(final P point, final double margin) {
105         return point.distance(center) <= radius + margin;
106     }
107 
108 }