BoundarySource3D.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.threed;

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

  23. import org.apache.commons.geometry.core.partitioning.BoundarySource;
  24. import org.apache.commons.geometry.euclidean.threed.line.LineConvexSubset3D;
  25. import org.apache.commons.geometry.euclidean.threed.line.LinecastPoint3D;
  26. import org.apache.commons.geometry.euclidean.threed.line.Linecastable3D;
  27. import org.apache.commons.geometry.euclidean.threed.mesh.SimpleTriangleMesh;
  28. import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh;
  29. import org.apache.commons.numbers.core.Precision;

  30. /** Extension of the {@link BoundarySource} interface for Euclidean 3D space.
  31.  */
  32. public interface BoundarySource3D extends BoundarySource<PlaneConvexSubset>, Linecastable3D {

  33.     /** Return a {@link BoundaryList3D} containing the boundaries in this instance.
  34.      * @return a {@link BoundaryList3D} containing the boundaries in this instance
  35.      */
  36.     default BoundaryList3D toList() {
  37.         final List<PlaneConvexSubset> boundaries = boundaryStream()
  38.                 .collect(Collectors.toList());

  39.         return new BoundaryList3D(boundaries);
  40.     }

  41.     /** Return a BSP tree constructed from the boundaries contained in this instance. This is
  42.      * a convenience method for quickly constructing BSP trees and may produce unbalanced trees
  43.      * with unacceptable performance characteristics when used with large numbers of boundaries.
  44.      * In these cases, alternate tree construction approaches should be used, such as
  45.      * {@link RegionBSPTree3D.PartitionedRegionBuilder3D}.
  46.      * @return a BSP tree constructed from the boundaries in this instance
  47.      * @see RegionBSPTree3D#partitionedRegionBuilder()
  48.      */
  49.     default RegionBSPTree3D toTree() {
  50.         final RegionBSPTree3D tree = RegionBSPTree3D.empty();
  51.         tree.insert(this);

  52.         return tree;
  53.     }

  54.     /** Construct a triangle mesh from the boundaries in this instance.
  55.      * @param precision precision context used in boundaries generated by the resulting mesh
  56.      * @return a triangle mesh representing the boundaries in this instance
  57.      * @throws IllegalStateException if any boundary in this boundary source is infinite
  58.      */
  59.     default TriangleMesh toTriangleMesh(final Precision.DoubleEquivalence precision) {
  60.         return SimpleTriangleMesh.from(this, precision);
  61.     }

  62.     /** Return the boundaries of this instance as a stream of {@link Triangle3D}
  63.      * instances. An {@link IllegalStateException} exception is thrown while reading
  64.      * from the stream if any boundary cannot be converted to a triangle (i.e. if it
  65.      * has infinite size).
  66.      * @return a stream of triangles representing the instance boundaries
  67.      * @see org.apache.commons.geometry.euclidean.threed.PlaneSubset#toTriangles()
  68.      */
  69.     default Stream<Triangle3D> triangleStream() {
  70.         return boundaryStream().flatMap(b -> b.toTriangles().stream());
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     default List<LinecastPoint3D> linecast(final LineConvexSubset3D subset) {
  75.         return new BoundarySourceLinecaster3D(this).linecast(subset);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     default LinecastPoint3D linecastFirst(final LineConvexSubset3D subset) {
  80.         return new BoundarySourceLinecaster3D(this).linecastFirst(subset);
  81.     }

  82.     /** Get a {@link Bounds3D} object defining the axis-aligned box containing all vertices
  83.      * in the boundaries for this instance. Null is returned if any boundary is infinite
  84.      * or no vertices are found.
  85.      * @return the bounding box for this instance or null if no valid bounds could be determined
  86.      */
  87.     default Bounds3D getBounds() {
  88.         return new BoundarySourceBoundsBuilder3D().getBounds(this);
  89.     }

  90.     /** Return a {@link BoundarySource3D} instance containing the given boundaries.
  91.      * @param boundaries boundaries to include in the boundary source
  92.      * @return a boundary source containing the given boundaries
  93.      */
  94.     static BoundarySource3D of(final PlaneConvexSubset... boundaries) {
  95.         return of(Arrays.asList(boundaries));
  96.     }

  97.     /** Return a {@link BoundarySource3D} instance containing the given boundaries. The given
  98.      * collection is used directly as the source of the boundaries; no copy is made.
  99.      * @param boundaries boundaries to include in the boundary source
  100.      * @return a boundary source containing the given boundaries
  101.      */
  102.     static BoundarySource3D of(final Collection<PlaneConvexSubset> boundaries) {
  103.         return boundaries::stream;
  104.     }
  105. }