BoundarySource2D.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.euclidean.twod;

  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import java.util.stream.Collectors;

  22. import org.apache.commons.geometry.core.partitioning.BoundarySource;

  23. /** Extension of the {@link BoundarySource} interface for Euclidean 2D space.
  24.  */
  25. public interface BoundarySource2D extends BoundarySource<LineConvexSubset>, Linecastable2D {

  26.     /** Return a {@link BoundaryList2D} containing the boundaries in this instance.
  27.      * @return a {@link BoundaryList2D} containing the boundaries in this instance
  28.      */
  29.     default BoundaryList2D toList() {
  30.         final List<LineConvexSubset> boundaries = boundaryStream()
  31.                 .collect(Collectors.toList());

  32.         return new BoundaryList2D(boundaries);
  33.     }

  34.     /** Return a BSP tree constructed from the boundaries contained in this instance. This is
  35.      * a convenience method for quickly constructing BSP trees and may produce unbalanced trees
  36.      * with unacceptable performance characteristics when used with large numbers of boundaries.
  37.      * In these cases, alternate tree construction approaches should be used, such as
  38.      * {@link RegionBSPTree2D.PartitionedRegionBuilder2D}.
  39.      * @return a BSP tree constructed from the boundaries in this instance
  40.      * @see RegionBSPTree2D#partitionedRegionBuilder()
  41.      */
  42.     default RegionBSPTree2D toTree() {
  43.         final RegionBSPTree2D tree = RegionBSPTree2D.empty();
  44.         tree.insert(this);

  45.         return tree;
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     default List<LinecastPoint2D> linecast(final LineConvexSubset subset) {
  50.         return new BoundarySourceLinecaster2D(this).linecast(subset);
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     default LinecastPoint2D linecastFirst(final LineConvexSubset subset) {
  55.         return new BoundarySourceLinecaster2D(this).linecastFirst(subset);
  56.     }

  57.     /** Get a {@link Bounds2D} object defining the axis-aligned box containing all vertices
  58.      * in the boundaries for this instance. Null is returned if any boundaries are infinite
  59.      * or no vertices were found.
  60.      * @return the bounding box for this instance or null if no valid bounds could be determined
  61.      */
  62.     default Bounds2D getBounds() {
  63.         return new BoundarySourceBoundsBuilder2D().getBounds(this);
  64.     }

  65.     /** Return a {@link BoundarySource2D} instance containing the given boundaries.
  66.      * @param boundaries line subsets to include in the boundary source
  67.      * @return a boundary source containing the given boundaries
  68.      */
  69.     static BoundarySource2D of(final LineConvexSubset... boundaries) {
  70.         return of(Arrays.asList(boundaries));
  71.     }

  72.     /** Return a {@link BoundarySource2D} instance containing the given boundaries. The given
  73.      * collection is used directly as the source of the line subsets; no copy is made.
  74.      * @param boundaries line subsets to include in the boundary source
  75.      * @return a boundary source containing the given boundaries
  76.      */
  77.     static BoundarySource2D of(final Collection<LineConvexSubset> boundaries) {
  78.         return boundaries::stream;
  79.     }
  80. }