Region.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.  *      http://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. package org.apache.commons.geometry.core;

  18. /** Interface representing a region in a space. A region partitions a space
  19.  * into sets of points lying on the inside, outside, and boundary.
  20.  * @param <P> Point implementation type
  21.  */
  22. public interface Region<P extends Point<P>> extends Sized {

  23.     /** Return true if the region spans the entire space. In other words,
  24.      * a region is full if no points in the space are classified as
  25.      * {@link RegionLocation#OUTSIDE outside}.
  26.      * @return true if the region spans the entire space
  27.      */
  28.     boolean isFull();

  29.     /** Return true if the region is completely empty, ie all points in
  30.      * the space are classified as {@link RegionLocation#OUTSIDE outside}.
  31.      * @return true if the region is empty
  32.      */
  33.     boolean isEmpty();

  34.     /** Get the size of the boundary of the region. The size is a value in
  35.      * the {@code d-1} dimension space. For example, in Euclidean space,
  36.      * this will be a length in 2D and an area in 3D.
  37.      * @return the size of the boundary of the region
  38.      */
  39.     double getBoundarySize();

  40.     /** Get the centroid, or geometric center, of the region or null if no centroid
  41.      * exists or one exists but is not unique. A centroid will not exist for empty or
  42.      * infinite regions.
  43.      *
  44.      * <p>The centroid of a geometric object is defined as the mean position of
  45.      * all points in the object, including interior points, vertices, and other points
  46.      * lying on the boundary. If a physical object has a uniform density, then its center
  47.      * of mass is the same as its geometric centroid.
  48.      * </p>
  49.      * @return the centroid of the region or null if no unique centroid exists
  50.      * @see <a href="https://en.wikipedia.org/wiki/Centroid">Centroid</a>
  51.      */
  52.     P getCentroid();

  53.     /** Classify the given point with respect to the region.
  54.      * @param pt the point to classify
  55.      * @return the location of the point with respect to the region
  56.      */
  57.     RegionLocation classify(P pt);

  58.     /** Return true if the given point is on the inside or boundary
  59.      * of the region.
  60.      * @param pt the point to test
  61.      * @return true if the point is on the inside or boundary of the region
  62.      */
  63.     default boolean contains(final P pt) {
  64.         final RegionLocation location = classify(pt);
  65.         return location != null && location != RegionLocation.OUTSIDE;
  66.     }

  67.     /** Project a point onto the boundary of the region. Null is returned if
  68.      * the region contains no boundaries (ie, is either {@link #isFull() full}
  69.      * or {@link #isEmpty() empty}).
  70.      * @param pt pt to project
  71.      * @return projection of the point on the boundary of the region or null
  72.      *      if the region does not contain any boundaries
  73.      */
  74.     P project(P pt);
  75. }