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.twod;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.stream.Collectors;
22  
23  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
24  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  public class BoundarySource2DTest {
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 testToTree() {
37          // act
38          final BoundarySource2D src = BoundarySource2D.from(
39              Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.of(1, 0), TEST_PRECISION),
40              Lines.segmentFromPoints(Vector2D.of(1, 0), Vector2D.of(1, 1), TEST_PRECISION)
41          );
42  
43          // act
44          final RegionBSPTree2D tree = src.toTree();
45  
46          // assert
47          Assert.assertEquals(5, tree.count());
48          Assert.assertFalse(tree.isFull());
49          Assert.assertFalse(tree.isEmpty());
50      }
51  
52      @Test
53      public void testToTree_noBoundaries() {
54          // act
55          final BoundarySource2D src = BoundarySource2D.from();
56  
57          // act
58          final RegionBSPTree2D tree = src.toTree();
59  
60          // assert
61          Assert.assertEquals(1, tree.count());
62          Assert.assertFalse(tree.isFull());
63          Assert.assertTrue(tree.isEmpty());
64      }
65  
66      @Test
67      public void testFrom_varargs_empty() {
68          // act
69          final BoundarySource2D src = BoundarySource2D.from();
70  
71          // assert
72          final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
73          Assert.assertEquals(0, segments.size());
74      }
75  
76      @Test
77      public void testFrom_varargs() {
78          // act
79          final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
80          final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
81  
82          final BoundarySource2D src = BoundarySource2D.from(a, b);
83  
84          // assert
85          final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
86          Assert.assertEquals(2, segments.size());
87  
88          Assert.assertSame(a, segments.get(0));
89          Assert.assertSame(b, segments.get(1));
90      }
91  
92      @Test
93      public void testFrom_list_empty() {
94          // arrange
95          final List<LineConvexSubset> input = new ArrayList<>();
96  
97          // act
98          final BoundarySource2D src = BoundarySource2D.from(input);
99  
100         // assert
101         final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
102         Assert.assertEquals(0, segments.size());
103     }
104 
105     @Test
106     public void testFrom_list() {
107         // act
108         final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
109         final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
110 
111         final List<LineConvexSubset> input = new ArrayList<>();
112         input.add(a);
113         input.add(b);
114 
115         final BoundarySource2D src = BoundarySource2D.from(input);
116 
117         // assert
118         final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
119         Assert.assertEquals(2, segments.size());
120 
121         Assert.assertSame(a, segments.get(0));
122         Assert.assertSame(b, segments.get(1));
123     }
124 }