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.geometry.core;
018
019/** Interface representing a region in a space. A region partitions a space
020 * into sets of points lying on the inside, outside, and boundary.
021 * @param <P> Point implementation type
022 */
023public interface Region<P extends Point<P>> extends Sized {
024
025    /** Return true if the region spans the entire space. In other words,
026     * a region is full if no points in the space are classified as
027     * {@link RegionLocation#OUTSIDE outside}.
028     * @return true if the region spans the entire space
029     */
030    boolean isFull();
031
032    /** Return true if the region is completely empty, ie all points in
033     * the space are classified as {@link RegionLocation#OUTSIDE outside}.
034     * @return true if the region is empty
035     */
036    boolean isEmpty();
037
038    /** Get the size of the boundary of the region. The size is a value in
039     * the {@code d-1} dimension space. For example, in Euclidean space,
040     * this will be a length in 2D and an area in 3D.
041     * @return the size of the boundary of the region
042     */
043    double getBoundarySize();
044
045    /** Get the centroid, or geometric center, of the region or null if no centroid
046     * exists or one exists but is not unique. A centroid will not exist for empty or
047     * infinite regions.
048     *
049     * <p>The centroid of a geometric object is defined as the mean position of
050     * all points in the object, including interior points, vertices, and other points
051     * lying on the boundary. If a physical object has a uniform density, then its center
052     * of mass is the same as its geometric centroid.
053     * </p>
054     * @return the centroid of the region or null if no unique centroid exists
055     * @see <a href="https://en.wikipedia.org/wiki/Centroid">Centroid</a>
056     */
057    P getCentroid();
058
059    /** Classify the given point with respect to the region.
060     * @param pt the point to classify
061     * @return the location of the point with respect to the region
062     */
063    RegionLocation classify(P pt);
064
065    /** Return true if the given point is on the inside or boundary
066     * of the region.
067     * @param pt the point to test
068     * @return true if the point is on the inside or boundary of the region
069     */
070    default boolean contains(final P pt) {
071        final RegionLocation location = classify(pt);
072        return location != null && location != RegionLocation.OUTSIDE;
073    }
074
075    /** Project a point onto the boundary of the region. Null is returned if
076     * the region contains no boundaries (ie, is either {@link #isFull() full}
077     * or {@link #isEmpty() empty}).
078     * @param pt pt to project
079     * @return projection of the point on the boundary of the region or null
080     *      if the region does not contain any boundaries
081     */
082    P project(P pt);
083}