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  import java.util.List;
22  import java.util.stream.Collectors;
23  
24  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
25  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  public class BoundarySource3DTest {
30  
31      private static final double TEST_EPS = 1e-10;
32  
33      private static final DoublePrecisionContext TEST_PRECISION =
34              new EpsilonDoublePrecisionContext(TEST_EPS);
35  
36      @Test
37      public void testToTree() {
38          // act
39          final PlaneConvexSubset a = Planes.convexPolygonFromVertices(
40                  Arrays.asList(Vector3D.ZERO, Vector3D.Unit.PLUS_X, Vector3D.Unit.PLUS_Y), TEST_PRECISION);
41          final PlaneConvexSubset b = Planes.convexPolygonFromVertices(
42                  Arrays.asList(Vector3D.ZERO, Vector3D.Unit.PLUS_Y, Vector3D.Unit.MINUS_Z), TEST_PRECISION);
43  
44          final BoundarySource3D src = BoundarySource3D.from(a, b);
45  
46          // act
47          final RegionBSPTree3D tree = src.toTree();
48  
49          // assert
50          Assert.assertEquals(5, tree.count());
51          Assert.assertFalse(tree.isFull());
52          Assert.assertFalse(tree.isEmpty());
53      }
54  
55      @Test
56      public void testToTree_noBoundaries() {
57          // act
58          final BoundarySource3D src = BoundarySource3D.from();
59  
60          // act
61          final RegionBSPTree3D tree = src.toTree();
62  
63          // assert
64          Assert.assertEquals(1, tree.count());
65          Assert.assertFalse(tree.isFull());
66          Assert.assertTrue(tree.isEmpty());
67      }
68  
69      @Test
70      public void testFrom_varargs_empty() {
71          // act
72          final BoundarySource3D src = BoundarySource3D.from();
73  
74          // assert
75          final List<PlaneConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
76          Assert.assertEquals(0, segments.size());
77      }
78  
79      @Test
80      public void testFrom_varargs() {
81          // act
82          final PlaneConvexSubset a = Planes.convexPolygonFromVertices(
83                  Arrays.asList(Vector3D.ZERO, Vector3D.Unit.PLUS_X, Vector3D.Unit.PLUS_Y), TEST_PRECISION);
84          final PlaneConvexSubset b = Planes.convexPolygonFromVertices(
85                  Arrays.asList(Vector3D.ZERO, Vector3D.Unit.PLUS_Y, Vector3D.Unit.MINUS_Z), TEST_PRECISION);
86  
87          final BoundarySource3D src = BoundarySource3D.from(a, b);
88  
89          // assert
90          final List<PlaneConvexSubset> boundaries = src.boundaryStream().collect(Collectors.toList());
91          Assert.assertEquals(2, boundaries.size());
92  
93          Assert.assertSame(a, boundaries.get(0));
94          Assert.assertSame(b, boundaries.get(1));
95      }
96  
97      @Test
98      public void testFrom_list_empty() {
99          // arrange
100         final List<PlaneConvexSubset> input = new ArrayList<>();
101 
102         // act
103         final BoundarySource3D src = BoundarySource3D.from(input);
104 
105         // assert
106         final List<PlaneConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
107         Assert.assertEquals(0, segments.size());
108     }
109 
110     @Test
111     public void testFrom_list() {
112         // act
113         final PlaneConvexSubset a = Planes.convexPolygonFromVertices(
114                 Arrays.asList(Vector3D.ZERO, Vector3D.Unit.PLUS_X, Vector3D.Unit.PLUS_Y), TEST_PRECISION);
115         final PlaneConvexSubset b = Planes.convexPolygonFromVertices(
116                 Arrays.asList(Vector3D.ZERO, Vector3D.Unit.PLUS_Y, Vector3D.Unit.MINUS_Z), TEST_PRECISION);
117 
118         final List<PlaneConvexSubset> input = new ArrayList<>();
119         input.add(a);
120         input.add(b);
121 
122         final BoundarySource3D src = BoundarySource3D.from(input);
123 
124         // assert
125         final List<PlaneConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
126         Assert.assertEquals(2, segments.size());
127 
128         Assert.assertSame(a, segments.get(0));
129         Assert.assertSame(b, segments.get(1));
130     }
131 }