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  
21  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
22  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
23  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  public class BoundarySourceBoundsBuilder2DTest {
28  
29      private static final double TEST_EPS = 1e-10;
30  
31      private static final DoublePrecisionContext TEST_PRECISION =
32              new EpsilonDoublePrecisionContext(TEST_EPS);
33  
34      @Test
35      public void testGetBounds_noBoundaries() {
36          // arrange
37          final BoundarySource2D src = BoundarySource2D.from(new ArrayList<>());
38          final BoundarySourceBoundsBuilder2D builder = new BoundarySourceBoundsBuilder2D();
39  
40          // act
41          final Bounds2D b = builder.getBounds(src);
42  
43          // assert
44          Assert.assertNull(b);
45      }
46  
47      @Test
48      public void testGetBounds_singleFiniteBoundary() {
49          // arrange
50          final Segment seg = Lines.segmentFromPoints(Vector2D.of(1, -2), Vector2D.of(-3, 4), TEST_PRECISION);
51  
52          final BoundarySource2D src = BoundarySource2D.from(seg);
53          final BoundarySourceBoundsBuilder2D builder = new BoundarySourceBoundsBuilder2D();
54  
55          // act
56          final Bounds2D b = builder.getBounds(src);
57  
58          // assert
59          checkBounds(b, Vector2D.of(-3, -2), Vector2D.of(1, 4));
60          Assert.assertTrue(b.contains(seg.getStartPoint()));
61          Assert.assertTrue(b.contains(seg.getEndPoint()));
62      }
63  
64      @Test
65      public void testGetBounds_multipleFiniteBoundaries() {
66          // arrange
67          final Segment seg1 = Lines.segmentFromPoints(Vector2D.of(1, -2), Vector2D.of(-3, 4), TEST_PRECISION);
68          final Segment seg2 = Lines.segmentFromPoints(Vector2D.of(0, 1), Vector2D.of(7, 0), TEST_PRECISION);
69          final Segment seg3 = Lines.segmentFromPoints(Vector2D.of(4, 6), Vector2D.of(-3, 9), TEST_PRECISION);
70  
71          final BoundarySource2D src = BoundarySource2D.from(seg1, seg2, seg3);
72          final BoundarySourceBoundsBuilder2D builder = new BoundarySourceBoundsBuilder2D();
73  
74          // act
75          final Bounds2D b = builder.getBounds(src);
76  
77          // assert
78          checkBounds(b, Vector2D.of(-3, -2), Vector2D.of(7, 9));
79  
80          src.boundaryStream().forEach(boundary -> {
81              Assert.assertTrue(b.contains(boundary.getStartPoint()));
82              Assert.assertTrue(b.contains(boundary.getEndPoint()));
83          });
84      }
85  
86      @Test
87      public void testGetBounds_singleInfiniteBoundary() {
88          // arrange
89          final LineConvexSubset boundary = Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION)
90                  .span();
91          final BoundarySource2D src = BoundarySource2D.from(boundary);
92          final BoundarySourceBoundsBuilder2D builder = new BoundarySourceBoundsBuilder2D();
93  
94          // act
95          final Bounds2D b = builder.getBounds(src);
96  
97          // assert
98          Assert.assertNull(b);
99      }
100 
101     @Test
102     public void testGetBounds_mixedFiniteAndInfiniteBoundaries() {
103         // arrange
104         final LineConvexSubset inf = Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION)
105                 .span()
106                 .split(Lines.fromPointAndDirection(Vector2D.ZERO, Vector2D.Unit.PLUS_Y, TEST_PRECISION))
107                 .getMinus();
108 
109         final Segment seg1 = Lines.segmentFromPoints(Vector2D.of(1, -2), Vector2D.of(-3, 4), TEST_PRECISION);
110         final Segment seg2 = Lines.segmentFromPoints(Vector2D.of(0, 1), Vector2D.of(7, 0), TEST_PRECISION);
111         final Segment seg3 = Lines.segmentFromPoints(Vector2D.of(4, 6), Vector2D.of(-3, 9), TEST_PRECISION);
112 
113         final BoundarySource2D src = BoundarySource2D.from(seg1, seg2, inf, seg3);
114         final BoundarySourceBoundsBuilder2D builder = new BoundarySourceBoundsBuilder2D();
115 
116         // act
117         final Bounds2D b = builder.getBounds(src);
118 
119         // assert
120         Assert.assertNull(b);
121     }
122 
123     private static void checkBounds(final Bounds2D b, final Vector2D min, final Vector2D max) {
124         EuclideanTestUtils.assertCoordinatesEqual(min, b.getMin(), TEST_EPS);
125         EuclideanTestUtils.assertCoordinatesEqual(max, b.getMax(), TEST_EPS);
126     }
127 }