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.euclidean.twod;
    18
    19  import java.util.Arrays;
    20  import java.util.Collection;
    21  import java.util.List;
    22  import java.util.stream.Collectors;
    23
    24  import org.apache.commons.geometry.core.partitioning.BoundarySource;
    25
    26  /** Extension of the {@link BoundarySource} interface for Euclidean 2D space.
    27   */
    28  public interface BoundarySource2D extends BoundarySource<LineConvexSubset>, Linecastable2D {
    29
    30      /** Return a {@link BoundaryList2D} containing the boundaries in this instance.
    31       * @return a {@link BoundaryList2D} containing the boundaries in this instance
    32       */
    33      default BoundaryList2D toList() {
    34          final List<LineConvexSubset> boundaries = boundaryStream()
    35                  .collect(Collectors.toList());
    36
    37          return new BoundaryList2D(boundaries);
    38      }
    39
    40      /** Return a BSP tree constructed from the boundaries contained in this instance. This is
    41       * a convenience method for quickly constructing BSP trees and may produce unbalanced trees
    42       * with unacceptable performance characteristics when used with large numbers of boundaries.
    43       * In these cases, alternate tree construction approaches should be used, such as
    44       * {@link RegionBSPTree2D.PartitionedRegionBuilder2D}.
    45       * @return a BSP tree constructed from the boundaries in this instance
    46       * @see RegionBSPTree2D#partitionedRegionBuilder()
    47       */
    48      default RegionBSPTree2D toTree() {
    49          final RegionBSPTree2D tree = RegionBSPTree2D.empty();
    50          tree.insert(this);
    51
    52          return tree;
    53      }
    54
    55      /** {@inheritDoc} */
    56      @Override
    57      default List<LinecastPoint2D> linecast(final LineConvexSubset subset) {
    58          return new BoundarySourceLinecaster2D(this).linecast(subset);
    59      }
    60
    61      /** {@inheritDoc} */
    62      @Override
    63      default LinecastPoint2D linecastFirst(final LineConvexSubset subset) {
    64          return new BoundarySourceLinecaster2D(this).linecastFirst(subset);
    65      }
    66
    67      /** Get a {@link Bounds2D} object defining the axis-aligned box containing all vertices
    68       * in the boundaries for this instance. Null is returned if any boundaries are infinite
    69       * or no vertices were found.
    70       * @return the bounding box for this instance or null if no valid bounds could be determined
    71       */
    72      default Bounds2D getBounds() {
    73          return new BoundarySourceBoundsBuilder2D().getBounds(this);
    74      }
    75
    76      /** Return a {@link BoundarySource2D} instance containing the given boundaries.
    77       * @param boundaries line subsets to include in the boundary source
    78       * @return a boundary source containing the given boundaries
    79       */
    80      static BoundarySource2D of(final LineConvexSubset... boundaries) {
    81          return of(Arrays.asList(boundaries));
    82      }
    83
    84      /** Return a {@link BoundarySource2D} instance containing the given boundaries. The given
    85       * collection is used directly as the source of the line subsets; no copy is made.
    86       * @param boundaries line subsets to include in the boundary source
    87       * @return a boundary source containing the given boundaries
    88       */
    89      static BoundarySource2D of(final Collection<LineConvexSubset> boundaries) {
    90          return boundaries::stream;
    91      }
    92  }