1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36 final BoundarySource2D src = BoundarySource2D.of(new ArrayList<>());
37 final BoundarySourceBoundsBuilder2D builder = new BoundarySourceBoundsBuilder2D();
38
39
40 final Bounds2D b = builder.getBounds(src);
41
42
43 Assertions.assertNull(b);
44 }
45
46 @Test
47 void testGetBounds_singleFiniteBoundary() {
48
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
55 final Bounds2D b = builder.getBounds(src);
56
57
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
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
74 final Bounds2D b = builder.getBounds(src);
75
76
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
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
94 final Bounds2D b = builder.getBounds(src);
95
96
97 Assertions.assertNull(b);
98 }
99
100 @Test
101 void testGetBounds_mixedFiniteAndInfiniteBoundaries() {
102
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
116 final Bounds2D b = builder.getBounds(src);
117
118
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 }