1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.euclidean.threed;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21
22 import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
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 BoundarySourceBoundsBuilder3DTest {
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 testGetBounds_noBoundaries() {
36
37 final BoundarySource3D src = BoundarySource3D.of(new ArrayList<>());
38 final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
39
40
41 final Bounds3D b = builder.getBounds(src);
42
43
44 Assertions.assertNull(b);
45 }
46
47 @Test
48 void testGetBounds_singleFiniteBoundary() {
49
50 final ConvexPolygon3D poly = Planes.convexPolygonFromVertices(Arrays.asList(
51 Vector3D.of(1, 1, 1),
52 Vector3D.of(1, 0, 2),
53 Vector3D.of(3, 4, 5)), TEST_PRECISION);
54
55 final BoundarySource3D src = BoundarySource3D.of(poly);
56 final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
57
58
59 final Bounds3D b = builder.getBounds(src);
60
61
62 checkBounds(b, Vector3D.of(1, 0, 1), Vector3D.of(3, 4, 5));
63 for (final Vector3D pt : poly.getVertices()) {
64 Assertions.assertTrue(b.contains(pt));
65 }
66 }
67
68 @Test
69 void testGetBounds_multipleFiniteBoundaries() {
70
71 final ConvexPolygon3D poly1 = Planes.convexPolygonFromVertices(Arrays.asList(
72 Vector3D.of(1, 1, 1),
73 Vector3D.of(1, 0, 2),
74 Vector3D.of(3, 4, 5)), TEST_PRECISION);
75
76 final ConvexPolygon3D poly2 = Planes.convexPolygonFromVertices(Arrays.asList(
77 Vector3D.of(-1, 1, 1),
78 Vector3D.of(1, 4, 4),
79 Vector3D.of(7, 4, 5)), TEST_PRECISION);
80
81 final ConvexPolygon3D poly3 = Planes.convexPolygonFromVertices(Arrays.asList(
82 Vector3D.of(-2, 1, 1),
83 Vector3D.of(1, 7, 2),
84 Vector3D.of(5, 4, 10)), TEST_PRECISION);
85
86 final BoundarySource3D src = BoundarySource3D.of(poly1, poly2, poly3);
87 final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
88
89
90 final Bounds3D b = builder.getBounds(src);
91
92
93 checkBounds(b, Vector3D.of(-2, 0, 1), Vector3D.of(7, 7, 10));
94
95 src.boundaryStream().forEach(boundary -> {
96 for (final Vector3D pt : boundary.getVertices()) {
97 Assertions.assertTrue(b.contains(pt));
98 }
99 });
100 }
101
102 @Test
103 void testGetBounds_singleInfiniteBoundary() {
104
105 final PlaneConvexSubset boundary = Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Z, TEST_PRECISION)
106 .span();
107 final BoundarySource3D src = BoundarySource3D.of(boundary);
108 final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
109
110
111 final Bounds3D b = builder.getBounds(src);
112
113
114 Assertions.assertNull(b);
115 }
116
117 @Test
118 void testGetBounds_mixedFiniteAndInfiniteBoundaries() {
119
120 final PlaneConvexSubset inf = Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Z, TEST_PRECISION)
121 .span()
122 .split(Planes.fromPointAndNormal(Vector3D.ZERO, Vector3D.Unit.PLUS_Y, TEST_PRECISION))
123 .getMinus();
124
125 final ConvexPolygon3D poly1 = Planes.convexPolygonFromVertices(Arrays.asList(
126 Vector3D.of(1, 1, 1),
127 Vector3D.of(1, 0, 2),
128 Vector3D.of(3, 4, 5)), TEST_PRECISION);
129
130 final ConvexPolygon3D poly2 = Planes.convexPolygonFromVertices(Arrays.asList(
131 Vector3D.of(-1, 1, 1),
132 Vector3D.of(1, 4, 4),
133 Vector3D.of(7, 4, 5)), TEST_PRECISION);
134
135 final ConvexPolygon3D poly3 = Planes.convexPolygonFromVertices(Arrays.asList(
136 Vector3D.of(-2, 1, 1),
137 Vector3D.of(1, 7, 2),
138 Vector3D.of(5, 4, 10)), TEST_PRECISION);
139
140 final BoundarySource3D src = BoundarySource3D.of(poly1, poly2, inf, poly3);
141 final BoundarySourceBoundsBuilder3D builder = new BoundarySourceBoundsBuilder3D();
142
143
144 final Bounds3D b = builder.getBounds(src);
145
146
147 Assertions.assertNull(b);
148 }
149
150 private static void checkBounds(final Bounds3D b, final Vector3D min, final Vector3D max) {
151 EuclideanTestUtils.assertCoordinatesEqual(min, b.getMin(), TEST_EPS);
152 EuclideanTestUtils.assertCoordinatesEqual(max, b.getMax(), TEST_EPS);
153 }
154 }