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    *      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  
19  /** Interface representing a region in a space. A region partitions a space
20   * into sets of points lying on the inside, outside, and boundary.
21   * @param <P> Point implementation type
22   */
23  public interface Region<P extends Point<P>> extends Sized {
24  
25      /** Return true if the region spans the entire space. In other words,
26       * a region is full if no points in the space are classified as
27       * {@link RegionLocation#OUTSIDE outside}.
28       * @return true if the region spans the entire space
29       */
30      boolean isFull();
31  
32      /** Return true if the region is completely empty, ie all points in
33       * the space are classified as {@link RegionLocation#OUTSIDE outside}.
34       * @return true if the region is empty
35       */
36      boolean isEmpty();
37  
38      /** Get the size of the boundary of the region. The size is a value in
39       * the {@code d-1} dimension space. For example, in Euclidean space,
40       * this will be a length in 2D and an area in 3D.
41       * @return the size of the boundary of the region
42       */
43      double getBoundarySize();
44  
45      /** Get the centroid, or geometric center, of the region or null if no centroid
46       * exists or one exists but is not unique. A centroid will not exist for empty or
47       * infinite regions.
48       *
49       * <p>The centroid of a geometric object is defined as the mean position of
50       * all points in the object, including interior points, vertices, and other points
51       * lying on the boundary. If a physical object has a uniform density, then its center
52       * of mass is the same as its geometric centroid.
53       * </p>
54       * @return the centroid of the region or null if no unique centroid exists
55       * @see <a href="https://en.wikipedia.org/wiki/Centroid">Centroid</a>
56       */
57      P getCentroid();
58  
59      /** Classify the given point with respect to the region.
60       * @param pt the point to classify
61       * @return the location of the point with respect to the region
62       */
63      RegionLocation classify(P pt);
64  
65      /** Return true if the given point is on the inside or boundary
66       * of the region.
67       * @param pt the point to test
68       * @return true if the point is on the inside or boundary of the region
69       */
70      default boolean contains(final P pt) {
71          final RegionLocation location = classify(pt);
72          return location != null && location != RegionLocation.OUTSIDE;
73      }
74  
75      /** Project a point onto the boundary of the region. Null is returned if
76       * the region contains no boundaries (ie, is either {@link #isFull() full}
77       * or {@link #isEmpty() empty}).
78       * @param pt pt to project
79       * @return projection of the point on the boundary of the region or null
80       *      if the region does not contain any boundaries
81       */
82      P project(P pt);
83  }