001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.geometry.euclidean.twod;
018
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.List;
022import java.util.stream.Collectors;
023
024import org.apache.commons.geometry.core.partitioning.BoundarySource;
025
026/** Extension of the {@link BoundarySource} interface for Euclidean 2D space.
027 */
028public interface BoundarySource2D extends BoundarySource<LineConvexSubset>, Linecastable2D {
029
030    /** Return a {@link BoundaryList2D} containing the boundaries in this instance.
031     * @return a {@link BoundaryList2D} containing the boundaries in this instance
032     */
033    default BoundaryList2D toList() {
034        final List<LineConvexSubset> boundaries = boundaryStream()
035                .collect(Collectors.toList());
036
037        return new BoundaryList2D(boundaries);
038    }
039
040    /** Return a BSP tree constructed from the boundaries contained in this instance. This is
041     * a convenience method for quickly constructing BSP trees and may produce unbalanced trees
042     * with unacceptable performance characteristics when used with large numbers of boundaries.
043     * In these cases, alternate tree construction approaches should be used, such as
044     * {@link RegionBSPTree2D.PartitionedRegionBuilder2D}.
045     * @return a BSP tree constructed from the boundaries in this instance
046     * @see RegionBSPTree2D#partitionedRegionBuilder()
047     */
048    default RegionBSPTree2D toTree() {
049        final RegionBSPTree2D tree = RegionBSPTree2D.empty();
050        tree.insert(this);
051
052        return tree;
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    default List<LinecastPoint2D> linecast(final LineConvexSubset subset) {
058        return new BoundarySourceLinecaster2D(this).linecast(subset);
059    }
060
061    /** {@inheritDoc} */
062    @Override
063    default LinecastPoint2D linecastFirst(final LineConvexSubset subset) {
064        return new BoundarySourceLinecaster2D(this).linecastFirst(subset);
065    }
066
067    /** Get a {@link Bounds2D} object defining the axis-aligned box containing all vertices
068     * in the boundaries for this instance. Null is returned if any boundaries are infinite
069     * or no vertices were found.
070     * @return the bounding box for this instance or null if no valid bounds could be determined
071     */
072    default Bounds2D getBounds() {
073        return new BoundarySourceBoundsBuilder2D().getBounds(this);
074    }
075
076    /** Return a {@link BoundarySource2D} instance containing the given boundaries.
077     * @param boundaries line subsets to include in the boundary source
078     * @return a boundary source containing the given boundaries
079     */
080    static BoundarySource2D of(final LineConvexSubset... boundaries) {
081        return of(Arrays.asList(boundaries));
082    }
083
084    /** Return a {@link BoundarySource2D} instance containing the given boundaries. The given
085     * collection is used directly as the source of the line subsets; no copy is made.
086     * @param boundaries line subsets to include in the boundary source
087     * @return a boundary source containing the given boundaries
088     */
089    static BoundarySource2D of(final Collection<LineConvexSubset> boundaries) {
090        return boundaries::stream;
091    }
092}