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 import java.util.List;
21 import java.util.stream.Collectors;
22
23 import org.apache.commons.numbers.core.Precision;
24 import org.junit.jupiter.api.Assertions;
25 import org.junit.jupiter.api.Test;
26
27 class BoundarySource2DTest {
28
29 private static final double TEST_EPS = 1e-10;
30
31 private static final Precision.DoubleEquivalence TEST_PRECISION =
32 Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
33
34 @Test
35 void testToList() {
36
37 final BoundarySource2D src = BoundarySource2D.of(
38 Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.of(1, 0), TEST_PRECISION),
39 Lines.segmentFromPoints(Vector2D.of(1, 0), Vector2D.of(1, 1), TEST_PRECISION)
40 );
41
42
43 final BoundaryList2D list = src.toList();
44
45
46 Assertions.assertEquals(2, list.count());
47 }
48
49 @Test
50 void testToList_noBoundaries() {
51
52 final BoundarySource2D src = BoundarySource2D.of();
53
54
55 final BoundaryList2D list = src.toList();
56
57
58 Assertions.assertEquals(0, list.count());
59 }
60
61 @Test
62 void testToTree() {
63
64 final BoundarySource2D src = BoundarySource2D.of(
65 Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.of(1, 0), TEST_PRECISION),
66 Lines.segmentFromPoints(Vector2D.of(1, 0), Vector2D.of(1, 1), TEST_PRECISION)
67 );
68
69
70 final RegionBSPTree2D tree = src.toTree();
71
72
73 Assertions.assertEquals(5, tree.count());
74 Assertions.assertFalse(tree.isFull());
75 Assertions.assertFalse(tree.isEmpty());
76 }
77
78 @Test
79 void testToTree_noBoundaries() {
80
81 final BoundarySource2D src = BoundarySource2D.of();
82
83
84 final RegionBSPTree2D tree = src.toTree();
85
86
87 Assertions.assertEquals(1, tree.count());
88 Assertions.assertFalse(tree.isFull());
89 Assertions.assertTrue(tree.isEmpty());
90 }
91
92 @Test
93 void testOf_varargs_empty() {
94
95 final BoundarySource2D src = BoundarySource2D.of();
96
97
98 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
99 Assertions.assertEquals(0, segments.size());
100 }
101
102 @Test
103 void testOf_varargs() {
104
105 final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
106 final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
107
108 final BoundarySource2D src = BoundarySource2D.of(a, b);
109
110
111 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
112 Assertions.assertEquals(2, segments.size());
113
114 Assertions.assertSame(a, segments.get(0));
115 Assertions.assertSame(b, segments.get(1));
116 }
117
118 @Test
119 void testOf_list_empty() {
120
121 final List<LineConvexSubset> input = new ArrayList<>();
122
123
124 final BoundarySource2D src = BoundarySource2D.of(input);
125
126
127 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
128 Assertions.assertEquals(0, segments.size());
129 }
130
131 @Test
132 void testOf_list() {
133
134 final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
135 final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
136
137 final List<LineConvexSubset> input = new ArrayList<>();
138 input.add(a);
139 input.add(b);
140
141 final BoundarySource2D src = BoundarySource2D.of(input);
142
143
144 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
145 Assertions.assertEquals(2, segments.size());
146
147 Assertions.assertSame(a, segments.get(0));
148 Assertions.assertSame(b, segments.get(1));
149 }
150 }