summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math3/geometry/enclosing/EnclosingBall.java
blob: eedbd46c33b9e8cf8232c14322fa8bdb3b10e37f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * 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.math3.geometry.enclosing;

import java.io.Serializable;

import org.apache.commons.math3.geometry.Point;
import org.apache.commons.math3.geometry.Space;

/** This class represents a ball enclosing some points.
 * @param <S> Space type.
 * @param <P> Point type.
 * @see Space
 * @see Point
 * @see Encloser
 * @since 3.3
 */
public class EnclosingBall<S extends Space, P extends Point<S>> implements Serializable {

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

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

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

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

    /** Simple constructor.
     * @param center center of the ball
     * @param radius radius of the ball
     * @param support support points used to define the ball
     */
    public EnclosingBall(final P center, final double radius, final P ... support) {
        this.center  = center;
        this.radius  = radius;
        this.support = support.clone();
    }

    /** Get the center of the ball.
     * @return center of the ball
     */
    public P getCenter() {
        return center;
    }

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

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

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

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

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

}