001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math3.geometry.enclosing; 018 019import java.io.Serializable; 020 021import org.apache.commons.math3.geometry.Point; 022import org.apache.commons.math3.geometry.Space; 023 024/** This class represents a ball enclosing some points. 025 * @param <S> Space type. 026 * @param <P> Point type. 027 * @see Space 028 * @see Point 029 * @see Encloser 030 * @since 3.3 031 */ 032public class EnclosingBall<S extends Space, P extends Point<S>> implements Serializable { 033 034 /** Serializable UID. */ 035 private static final long serialVersionUID = 20140126L; 036 037 /** Center of the ball. */ 038 private final P center; 039 040 /** Radius of the ball. */ 041 private final double radius; 042 043 /** Support points used to define the ball. */ 044 private final P[] support; 045 046 /** Simple constructor. 047 * @param center center of the ball 048 * @param radius radius of the ball 049 * @param support support points used to define the ball 050 */ 051 public EnclosingBall(final P center, final double radius, final P ... support) { 052 this.center = center; 053 this.radius = radius; 054 this.support = support.clone(); 055 } 056 057 /** Get the center of the ball. 058 * @return center of the ball 059 */ 060 public P getCenter() { 061 return center; 062 } 063 064 /** Get the radius of the ball. 065 * @return radius of the ball (can be negative if the ball is empty) 066 */ 067 public double getRadius() { 068 return radius; 069 } 070 071 /** Get the support points used to define the ball. 072 * @return support points used to define the ball 073 */ 074 public P[] getSupport() { 075 return support.clone(); 076 } 077 078 /** Get the number of support points used to define the ball. 079 * @return number of support points used to define the ball 080 */ 081 public int getSupportSize() { 082 return support.length; 083 } 084 085 /** Check if a point is within the ball or at boundary. 086 * @param point point to test 087 * @return true if the point is within the ball or at boundary 088 */ 089 public boolean contains(final P point) { 090 return point.distance(center) <= radius; 091 } 092 093 /** Check if a point is within an enlarged ball or at boundary. 094 * @param point point to test 095 * @param margin margin to consider 096 * @return true if the point is within the ball enlarged 097 * by the margin or at boundary 098 */ 099 public boolean contains(final P point, final double margin) { 100 return point.distance(center) <= radius + margin; 101 } 102 103}