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