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.geometry.core.precision.DoublePrecisionContext;
24 import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
25 import org.junit.Assert;
26 import org.junit.Test;
27
28 public class BoundarySource2DTest {
29
30 private static final double TEST_EPS = 1e-10;
31
32 private static final DoublePrecisionContext TEST_PRECISION =
33 new EpsilonDoublePrecisionContext(TEST_EPS);
34
35 @Test
36 public void testToTree() {
37
38 final BoundarySource2D src = BoundarySource2D.from(
39 Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.of(1, 0), TEST_PRECISION),
40 Lines.segmentFromPoints(Vector2D.of(1, 0), Vector2D.of(1, 1), TEST_PRECISION)
41 );
42
43
44 final RegionBSPTree2D tree = src.toTree();
45
46
47 Assert.assertEquals(5, tree.count());
48 Assert.assertFalse(tree.isFull());
49 Assert.assertFalse(tree.isEmpty());
50 }
51
52 @Test
53 public void testToTree_noBoundaries() {
54
55 final BoundarySource2D src = BoundarySource2D.from();
56
57
58 final RegionBSPTree2D tree = src.toTree();
59
60
61 Assert.assertEquals(1, tree.count());
62 Assert.assertFalse(tree.isFull());
63 Assert.assertTrue(tree.isEmpty());
64 }
65
66 @Test
67 public void testFrom_varargs_empty() {
68
69 final BoundarySource2D src = BoundarySource2D.from();
70
71
72 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
73 Assert.assertEquals(0, segments.size());
74 }
75
76 @Test
77 public void testFrom_varargs() {
78
79 final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
80 final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
81
82 final BoundarySource2D src = BoundarySource2D.from(a, b);
83
84
85 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
86 Assert.assertEquals(2, segments.size());
87
88 Assert.assertSame(a, segments.get(0));
89 Assert.assertSame(b, segments.get(1));
90 }
91
92 @Test
93 public void testFrom_list_empty() {
94
95 final List<LineConvexSubset> input = new ArrayList<>();
96
97
98 final BoundarySource2D src = BoundarySource2D.from(input);
99
100
101 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
102 Assert.assertEquals(0, segments.size());
103 }
104
105 @Test
106 public void testFrom_list() {
107
108 final Segment a = Lines.segmentFromPoints(Vector2D.ZERO, Vector2D.Unit.PLUS_X, TEST_PRECISION);
109 final Segment b = Lines.segmentFromPoints(Vector2D.Unit.PLUS_X, Vector2D.of(1, 1), TEST_PRECISION);
110
111 final List<LineConvexSubset> input = new ArrayList<>();
112 input.add(a);
113 input.add(b);
114
115 final BoundarySource2D src = BoundarySource2D.from(input);
116
117
118 final List<LineConvexSubset> segments = src.boundaryStream().collect(Collectors.toList());
119 Assert.assertEquals(2, segments.size());
120
121 Assert.assertSame(a, segments.get(0));
122 Assert.assertSame(b, segments.get(1));
123 }
124 }