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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.geometry.partitioning;

  22. import org.hipparchus.geometry.Point;
  23. import org.hipparchus.geometry.Space;

  24. /** This interface represents a region of a space as a partition.

  25.  * <p>Region are subsets of a space, they can be infinite (whole
  26.  * space, half space, infinite stripe ...) or finite (polygons in 2D,
  27.  * polyhedrons in 3D ...). Their main characteristic is to separate
  28.  * points that are considered to be <em>inside</em> the region from
  29.  * points considered to be <em>outside</em> of it. In between, there
  30.  * may be points on the <em>boundary</em> of the region.</p>

  31.  * <p>This implementation is limited to regions for which the boundary
  32.  * is composed of several {@link SubHyperplane sub-hyperplanes},
  33.  * including regions with no boundary at all: the whole space and the
  34.  * empty region. They are not necessarily finite and not necessarily
  35.  * path-connected. They can contain holes.</p>

  36.  * <p>Regions can be combined using the traditional sets operations :
  37.  * union, intersection, difference and symetric difference (exclusive
  38.  * or) for the binary operations, complement for the unary
  39.  * operation.</p>

  40.  * <p>
  41.  * Note that this interface is <em>not</em> intended to be implemented
  42.  * by Hipparchus users, it is only intended to be implemented
  43.  * within the library itself. New methods may be added even for minor
  44.  * versions, which breaks compatibility for external implementations.
  45.  * </p>

  46.  * @param <S> Type of the space.
  47.  * @param <P> Type of the points in the space.
  48.  * @param <H> Type of the hyperplane.
  49.  * @param <I> Type of the sub-hyperplane.

  50.  */
  51. public interface Region<S extends Space, P extends Point<S, P>, H extends Hyperplane<S, P, H, I>, I extends SubHyperplane<S, P, H, I>> {

  52.     /** Enumerate for the location of a point with respect to the region. */
  53.     enum Location {
  54.         /** Code for points inside the partition. */
  55.         INSIDE,

  56.         /** Code for points outside of the partition. */
  57.         OUTSIDE,

  58.         /** Code for points on the partition boundary. */
  59.         BOUNDARY
  60.     }

  61.     /** Build a region using the instance as a prototype.
  62.      * <p>This method allow to create new instances without knowing
  63.      * exactly the type of the region. It is an application of the
  64.      * prototype design pattern.</p>
  65.      * <p>The leaf nodes of the BSP tree <em>must</em> have a
  66.      * {@code Boolean} attribute representing the inside status of
  67.      * the corresponding cell (true for inside cells, false for outside
  68.      * cells). In order to avoid building too many small objects, it is
  69.      * recommended to use the predefined constants
  70.      * {@code Boolean.TRUE} and {@code Boolean.FALSE}. The
  71.      * tree also <em>must</em> have either null internal nodes or
  72.      * internal nodes representing the boundary as specified in the
  73.      * {@link #getTree getTree} method).</p>
  74.      * @param newTree inside/outside BSP tree representing the new region
  75.      * @return the built region
  76.      */
  77.     Region<S, P, H, I> buildNew(BSPTree<S, P, H, I> newTree);

  78.     /** Copy the instance.
  79.      * <p>The instance created is completely independant of the original
  80.      * one. A deep copy is used, none of the underlying objects are
  81.      * shared (except for the underlying tree {@code Boolean}
  82.      * attributes and immutable objects).</p>
  83.      * @return a new region, copy of the instance
  84.      */
  85.     Region<S, P, H, I> copySelf();

  86.     /** Check if the instance is empty.
  87.      * @return true if the instance is empty
  88.      */
  89.     boolean isEmpty();

  90.     /** Check if the sub-tree starting at a given node is empty.
  91.      * @param node root node of the sub-tree (<em>must</em> have {@link
  92.      * Region Region} tree semantics, i.e. the leaf nodes must have
  93.      * {@code Boolean} attributes representing an inside/outside
  94.      * property)
  95.      * @return true if the sub-tree starting at the given node is empty
  96.      */
  97.     boolean isEmpty(BSPTree<S, P, H, I> node);

  98.     /** Check if the instance covers the full space.
  99.      * @return true if the instance covers the full space
  100.      */
  101.     boolean isFull();

  102.     /** Check if the sub-tree starting at a given node covers the full space.
  103.      * @param node root node of the sub-tree (<em>must</em> have {@link
  104.      * Region Region} tree semantics, i.e. the leaf nodes must have
  105.      * {@code Boolean} attributes representing an inside/outside
  106.      * property)
  107.      * @return true if the sub-tree starting at the given node covers the full space
  108.      */
  109.     boolean isFull(BSPTree<S, P, H, I> node);

  110.     /** Check if the instance entirely contains another region.
  111.      * @param region region to check against the instance
  112.      * @return true if the instance contains the specified tree
  113.      */
  114.     boolean contains(Region<S, P, H, I> region);

  115.     /** Check a point with respect to the region.
  116.      * @param point point to check
  117.      * @return a code representing the point status: either {@link
  118.      * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY}
  119.      */
  120.     Location checkPoint(P point);

  121.     /** Project a point on the boundary of the region.
  122.      * @param point point to check
  123.      * @return projection of the point on the boundary
  124.      */
  125.     BoundaryProjection<S, P> projectToBoundary(P point);

  126.     /** Get the underlying BSP tree.

  127.      * <p>Regions are represented by an underlying inside/outside BSP
  128.      * tree whose leaf attributes are {@code Boolean} instances
  129.      * representing inside leaf cells if the attribute value is
  130.      * {@code true} and outside leaf cells if the attribute is
  131.      * {@code false}. These leaf attributes are always present and
  132.      * guaranteed to be non null.</p>

  133.      * <p>In addition to the leaf attributes, the internal nodes which
  134.      * correspond to cells split by cut sub-hyperplanes may contain
  135.      * {@link BoundaryAttribute BoundaryAttribute} objects representing
  136.      * the parts of the corresponding cut sub-hyperplane that belong to
  137.      * the boundary. When the boundary attributes have been computed,
  138.      * all internal nodes are guaranteed to have non-null
  139.      * attributes, however some {@link BoundaryAttribute
  140.      * BoundaryAttribute} instances may have their {@link
  141.      * BoundaryAttribute#getPlusInside() getPlusInside} and {@link
  142.      * BoundaryAttribute#getPlusOutside() getPlusOutside} methods both
  143.      * returning null if the corresponding cut sub-hyperplane does not
  144.      * have any parts belonging to the boundary.</p>

  145.      * <p>Since computing the boundary is not always required and can be
  146.      * time-consuming for large trees, these internal nodes attributes
  147.      * are computed using lazy evaluation only when required by setting
  148.      * the {@code includeBoundaryAttributes} argument to
  149.      * {@code true}. Once computed, these attributes remain in the
  150.      * tree, which implies that in this case, further calls to the
  151.      * method for the same region will always include these attributes
  152.      * regardless of the value of the
  153.      * {@code includeBoundaryAttributes} argument.</p>

  154.      * @param includeBoundaryAttributes if true, the boundary attributes
  155.      * at internal nodes are guaranteed to be included (they may be
  156.      * included even if the argument is false, if they have already been
  157.      * computed due to a previous call)
  158.      * @return underlying BSP tree
  159.      * @see BoundaryAttribute
  160.      */
  161.     BSPTree<S, P, H, I> getTree(boolean includeBoundaryAttributes);

  162.     /** Get the size of the boundary.
  163.      * @return the size of the boundary (this is 0 in 1D, a length in
  164.      * 2D, an area in 3D ...)
  165.      */
  166.     double getBoundarySize();

  167.     /** Get the size of the instance.
  168.      * @return the size of the instance (this is a length in 1D, an area
  169.      * in 2D, a volume in 3D ...)
  170.      */
  171.     double getSize();

  172.     /** Get the barycenter of the instance.
  173.      * @return an object representing the barycenter
  174.      */
  175.     P getBarycenter();

  176.     /** Get an interior point.
  177.      * @return an arbitrary interior point, or null if region is empty
  178.      * @since 4.0
  179.      */
  180.     P getInteriorPoint();

  181.     /** Get the parts of a sub-hyperplane that are contained in the region.
  182.      * <p>The parts of the sub-hyperplane that belong to the boundary are
  183.      * <em>not</em> included in the resulting parts.</p>
  184.      * @param sub sub-hyperplane traversing the region
  185.      * @return filtered sub-hyperplane
  186.      */
  187.     I intersection(I sub);

  188. }