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.threed;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  
22  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
23  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
24  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  public class BoundarySourceBoundsBuilder3DTest {
29  
30      private static final double TEST_EPS = 1e-10;
31  
32      private static final DoublePrecisionContext TEST_PRECISION =
33              new EpsilonDoublePrecisionContext(TEST_EPS);
34  
35      @Test
36      public void testGetBounds_noBoundaries() {
37          // arrange
38          final BoundarySource3D src = BoundarySource3D.from(new ArrayList<>());
39          final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
40  
41          // act
42          final Bounds3D b = builder.getBounds(src);
43  
44          // assert
45          Assert.assertNull(b);
46      }
47  
48      @Test
49      public void testGetBounds_singleFiniteBoundary() {
50          // arrange
51          final ConvexPolygon3D poly = Planes.convexPolygonFromVertices(Arrays.asList(
52                  Vector3D.of(1, 1, 1),
53                  Vector3D.of(1, 0, 2),
54                  Vector3D.of(3, 4, 5)), TEST_PRECISION);
55  
56          final BoundarySource3D src = BoundarySource3D.from(poly);
57          final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
58  
59          // act
60          final Bounds3D b = builder.getBounds(src);
61  
62          // assert
63          checkBounds(b, Vector3D.of(1, 0, 1), Vector3D.of(3, 4, 5));
64          for (final Vector3D pt : poly.getVertices()) {
65              Assert.assertTrue(b.contains(pt));
66          }
67      }
68  
69      @Test
70      public void testGetBounds_multipleFiniteBoundaries() {
71          // arrange
72          final ConvexPolygon3D poly1 = Planes.convexPolygonFromVertices(Arrays.asList(
73                  Vector3D.of(1, 1, 1),
74                  Vector3D.of(1, 0, 2),
75                  Vector3D.of(3, 4, 5)), TEST_PRECISION);
76  
77          final ConvexPolygon3D poly2 = Planes.convexPolygonFromVertices(Arrays.asList(
78                  Vector3D.of(-1, 1, 1),
79                  Vector3D.of(1, 4, 4),
80                  Vector3D.of(7, 4, 5)), TEST_PRECISION);
81  
82          final ConvexPolygon3D poly3 = Planes.convexPolygonFromVertices(Arrays.asList(
83                  Vector3D.of(-2, 1, 1),
84                  Vector3D.of(1, 7, 2),
85                  Vector3D.of(5, 4, 10)), TEST_PRECISION);
86  
87          final BoundarySource3D src = BoundarySource3D.from(poly1, poly2, poly3);
88          final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
89  
90          // act
91          final Bounds3D b = builder.getBounds(src);
92  
93          // assert
94          checkBounds(b, Vector3D.of(-2, 0, 1), Vector3D.of(7, 7, 10));
95  
96          src.boundaryStream().forEach(boundary -> {
97              for (final Vector3D pt : boundary.getVertices()) {
98                  Assert.assertTrue(b.contains(pt));
99              }
100         });
101     }
102 
103     @Test
104     public void testGetBounds_singleInfiniteBoundary() {
105         // arrange
106         final PlaneConvexSubset boundary = Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Z, TEST_PRECISION)
107                 .span();
108         final BoundarySource3D src = BoundarySource3D.from(boundary);
109         final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
110 
111         // act
112         final Bounds3D b = builder.getBounds(src);
113 
114         // assert
115         Assert.assertNull(b);
116     }
117 
118     @Test
119     public void testGetBounds_mixedFiniteAndInfiniteBoundaries() {
120         // arrange
121         final PlaneConvexSubset inf = Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Z, TEST_PRECISION)
122                 .span()
123                 .split(Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Y, TEST_PRECISION))
124                 .getMinus();
125 
126         final ConvexPolygon3D poly1 = Planes.convexPolygonFromVertices(Arrays.asList(
127                 Vector3D.of(1, 1, 1),
128                 Vector3D.of(1, 0, 2),
129                 Vector3D.of(3, 4, 5)), TEST_PRECISION);
130 
131         final ConvexPolygon3D poly2 = Planes.convexPolygonFromVertices(Arrays.asList(
132                 Vector3D.of(-1, 1, 1),
133                 Vector3D.of(1, 4, 4),
134                 Vector3D.of(7, 4, 5)), TEST_PRECISION);
135 
136         final ConvexPolygon3D poly3 = Planes.convexPolygonFromVertices(Arrays.asList(
137                 Vector3D.of(-2, 1, 1),
138                 Vector3D.of(1, 7, 2),
139                 Vector3D.of(5, 4, 10)), TEST_PRECISION);
140 
141         final BoundarySource3D src = BoundarySource3D.from(poly1, poly2, inf, poly3);
142         final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
143 
144         // act
145         final Bounds3D b = builder.getBounds(src);
146 
147         // assert
148         Assert.assertNull(b);
149     }
150 
151     private static void checkBounds(final Bounds3D b, final Vector3D min, final Vector3D max) {
152         EuclideanTestUtils.assertCoordinatesEqual(min, b.getMin(), TEST_EPS);
153         EuclideanTestUtils.assertCoordinatesEqual(max, b.getMax(), TEST_EPS);
154     }
155 }