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.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
37 final BoundarySource2D src = BoundarySource2D.from(new ArrayList<>());
38 final BoundarySourceBoundsBuilder2D builder = new BoundarySourceBoundsBuilder2D();
39
40
41 final Bounds2D b = builder.getBounds(src);
42
43
44 Assert.assertNull(b);
45 }
46
47 @Test
48 public void testGetBounds_singleFiniteBoundary() {
49
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
56 final Bounds2D b = builder.getBounds(src);
57
58
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
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
75 final Bounds2D b = builder.getBounds(src);
76
77
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
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
95 final Bounds2D b = builder.getBounds(src);
96
97
98 Assert.assertNull(b);
99 }
100
101 @Test
102 public void testGetBounds_mixedFiniteAndInfiniteBoundaries() {
103
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
117 final Bounds2D b = builder.getBounds(src);
118
119
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 }