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;
022
023import org.apache.commons.geometry.core.partitioning.BoundarySource;
024
025/** Extension of the {@link BoundarySource} interface for Euclidean 2D space.
026 */
027public interface BoundarySource2D extends BoundarySource<LineConvexSubset>, Linecastable2D {
028
029    /** Return a BSP tree constructed from the boundaries contained in this instance. This is
030     * a convenience method for quickly constructing BSP trees and may produce unbalanced trees
031     * with unacceptable performance characteristics when used with large numbers of boundaries.
032     * In these cases, alternate tree construction approaches should be used, such as
033     * {@link RegionBSPTree2D.PartitionedRegionBuilder2D}.
034     * @return a BSP tree constructed from the boundaries in this instance
035     * @see RegionBSPTree2D#partitionedRegionBuilder()
036     */
037    default RegionBSPTree2D toTree() {
038        final RegionBSPTree2D tree = RegionBSPTree2D.empty();
039        tree.insert(this);
040
041        return tree;
042    }
043
044    /** {@inheritDoc} */
045    @Override
046    default List<LinecastPoint2D> linecast(final LineConvexSubset subset) {
047        return new BoundarySourceLinecaster2D(this).linecast(subset);
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    default LinecastPoint2D linecastFirst(final LineConvexSubset subset) {
053        return new BoundarySourceLinecaster2D(this).linecastFirst(subset);
054    }
055
056    /** Get a {@link Bounds2D} object defining the axis-aligned box containing all vertices
057     * in the boundaries for this instance. Null is returned if any boundaries are infinite
058     * or no vertices were found.
059     * @return the bounding box for this instance or null if no valid bounds could be determined
060     */
061    default Bounds2D getBounds() {
062        return new BoundarySourceBoundsBuilder2D().getBounds(this);
063    }
064
065    /** Return a {@link BoundarySource2D} instance containing the given boundaries.
066     * @param boundaries line subsets to include in the boundary source
067     * @return a boundary source containing the given boundaries
068     */
069    static BoundarySource2D from(final LineConvexSubset... boundaries) {
070        return from(Arrays.asList(boundaries));
071    }
072
073    /** Return a {@link BoundarySource2D} instance containing the given boundaries. The given
074     * collection is used directly as the source of the line subsets; no copy is made.
075     * @param boundaries line subsets to include in the boundary source
076     * @return a boundary source containing the given boundaries
077     */
078    static BoundarySource2D from(final Collection<LineConvexSubset> boundaries) {
079        return boundaries::stream;
080    }
081}