HyperplaneSubset.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.partitioning;

  18. import java.util.List;

  19. import org.apache.commons.geometry.core.Point;
  20. import org.apache.commons.geometry.core.RegionLocation;
  21. import org.apache.commons.geometry.core.Sized;
  22. import org.apache.commons.geometry.core.Transform;

  23. /** Interface representing a subset of the points lying in a hyperplane. Examples include
  24.  * rays and line segments in Euclidean 2D space and triangular facets in Euclidean 3D space.
  25.  * Hyperplane subsets can have finite or infinite size and can represent contiguous regions
  26.  * of the hyperplane (as in the examples above); multiple, disjoint regions; or the
  27.  * {@link Hyperplane#span() entire hyperplane}.
  28.  *
  29.  * <p>This interface is very similar to the {@link org.apache.commons.geometry.core.Region Region}
  30.  * interface but has slightly different semantics. Whereas {@code Region} instances represent sets
  31.  * of points that can expand through all of the dimensions of a space, {@code HyperplaneSubset} instances
  32.  * are constrained to their containing hyperplane and are more accurately defined as {@code Region}s
  33.  * of the {@code n-1} dimension subspace defined by the hyperplane. This makes the methods of this interface
  34.  * have slightly different meanings as compared to their {@code Region} counterparts. For example, consider
  35.  * a triangular facet in Euclidean 3D space. The {@link #getSize()} method of this hyperplane subset does
  36.  * not return the <em>volume</em> of the instance (which would be {@code 0}) as a regular 3D region would, but
  37.  * rather returns the <em>area</em> of the 2D polygon defined by the facet. Similarly, the {@link #classify(Point)}
  38.  * method returns {@link RegionLocation#INSIDE} for points that lie inside of the 2D polygon defined by the
  39.  * facet, instead of the {@link RegionLocation#BOUNDARY} value that would be expected if the facet was considered
  40.  * as a true 3D region with zero thickness.
  41.  * </p>
  42.  *
  43.  * @param <P> Point implementation type
  44.  * @see Hyperplane
  45.  */
  46. public interface HyperplaneSubset<P extends Point<P>> extends Splittable<P, HyperplaneSubset<P>>, Sized {

  47.     /** Get the hyperplane containing this instance.
  48.      * @return the hyperplane containing this instance
  49.      */
  50.     Hyperplane<P> getHyperplane();

  51.     /** Return true if this instance contains all points in the
  52.      * hyperplane.
  53.      * @return true if this instance contains all points in the
  54.      *      hyperplane
  55.      */
  56.     boolean isFull();

  57.     /** Return true if this instance does not contain any points.
  58.      * @return true if this instance does not contain any points
  59.      */
  60.     boolean isEmpty();

  61.     /** Get the centroid, or geometric center, of the hyperplane subset or null
  62.      * if no centroid exists or one exists but is not unique. A centroid will not
  63.      * exist for empty or infinite subsets.
  64.      *
  65.      * <p>The centroid of a geometric object is defined as the mean position of
  66.      * all points in the object, including interior points, vertices, and other points
  67.      * lying on the boundary. If a physical object has a uniform density, then its center
  68.      * of mass is the same as its geometric centroid.
  69.      * </p>
  70.      * @return the centroid of the hyperplane subset or null if no unique centroid exists
  71.      * @see <a href="https://en.wikipedia.org/wiki/Centroid">Centroid</a>
  72.      */
  73.     P getCentroid();

  74.     /** Classify a point with respect to the subset region. The point is classified as follows:
  75.      * <ul>
  76.      *  <li>{@link RegionLocation#INSIDE INSIDE} - The point lies on the hyperplane
  77.      *      and inside of the subset region.</li>
  78.      *  <li>{@link RegionLocation#BOUNDARY BOUNDARY} - The point lies on the hyperplane
  79.      *      and is on the boundary of the subset region.</li>
  80.      *  <li>{@link RegionLocation#OUTSIDE OUTSIDE} - The point does not lie on
  81.      *      the hyperplane or it does lie on the hyperplane but is outside of the
  82.      *      subset region.</li>
  83.      * </ul>
  84.      * @param pt the point to classify
  85.      * @return classification of the point with respect to the hyperplane
  86.      *      and subspace region
  87.      */
  88.     RegionLocation classify(P pt);

  89.     /** Return true if the hyperplane subset contains the given point, meaning that the point
  90.      * lies on the hyperplane and is not on the outside of the subset region.
  91.      * @param pt the point to check
  92.      * @return true if the point is contained in the hyperplane subset
  93.      */
  94.     default boolean contains(final P pt) {
  95.         final RegionLocation loc = classify(pt);
  96.         return loc != null && loc != RegionLocation.OUTSIDE;
  97.     }

  98.     /** Return the closest point to the argument that is contained in the subset
  99.      * (ie, not classified as {@link RegionLocation#OUTSIDE outside}), or null if no
  100.      * such point exists.
  101.      * @param pt the reference point
  102.      * @return the closest point to the reference point that is contained in the subset,
  103.      *      or null if no such point exists
  104.      */
  105.     P closest(P pt);

  106.     /** Return a new hyperplane subset resulting from the application of the given transform.
  107.      * The current instance is not modified.
  108.      * @param transform the transform instance to apply
  109.      * @return new transformed hyperplane subset
  110.      */
  111.     HyperplaneSubset<P> transform(Transform<P> transform);

  112.     /** Convert this instance into a list of convex child subsets representing the same region.
  113.      * Implementations are not required to return an optimal convex subdivision of the current
  114.      * instance. They are free to return whatever subdivision is readily available.
  115.      * @return a list of hyperplane convex subsets representing the same subspace
  116.      *      region as this instance
  117.      */
  118.     List<? extends HyperplaneConvexSubset<P>> toConvex();
  119. }