1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.euclidean;
18
19 import java.util.List;
20
21 import org.apache.commons.geometry.core.Region;
22 import org.apache.commons.geometry.core.RegionLocation;
23 import org.apache.commons.geometry.core.partitioning.HyperplaneSubset;
24 import org.apache.commons.geometry.euclidean.oned.Vector1D;
25 import org.apache.commons.geometry.euclidean.threed.Vector3D;
26 import org.apache.commons.geometry.euclidean.twod.Vector2D;
27 import org.apache.commons.numbers.core.Precision;
28 import org.junit.jupiter.api.Assertions;
29
30
31
32
33 public final class EuclideanTestUtils {
34
35
36 private EuclideanTestUtils() {}
37
38
39 @FunctionalInterface
40 public interface PermuteCallback2D {
41 void accept(double x, double y);
42 }
43
44
45 @FunctionalInterface
46 public interface PermuteCallback3D {
47 void accept(double x, double y, double z);
48 }
49
50
51
52
53
54
55
56
57
58 public static void permute(final double min, final double max, final double step, final PermuteCallback2D callback) {
59 permuteInternal(min, max, step, false, callback);
60 }
61
62
63
64
65
66
67
68
69
70 public static void permuteSkipZero(final double min, final double max, final double step, final PermuteCallback2D callback) {
71 permuteInternal(min, max, step, true, callback);
72 }
73
74
75
76
77
78
79
80
81
82
83 private static void permuteInternal(final double min, final double max, final double step, final boolean skipZero, final PermuteCallback2D callback) {
84 for (double x = min; x <= max; x += step) {
85 for (double y = min; y <= max; y += step) {
86 if (!skipZero || (x != 0.0 || y != 0.0)) {
87 callback.accept(x, y);
88 }
89 }
90 }
91 }
92
93
94
95
96
97
98
99
100
101 public static void permute(final double min, final double max, final double step, final PermuteCallback3D callback) {
102 permuteInternal(min, max, step, false, callback);
103 }
104
105
106
107
108
109
110
111
112
113 public static void permuteSkipZero(final double min, final double max, final double step, final PermuteCallback3D callback) {
114 permuteInternal(min, max, step, true, callback);
115 }
116
117
118
119
120
121
122
123
124
125
126 private static void permuteInternal(final double min, final double max, final double step, final boolean skipZero, final PermuteCallback3D callback) {
127 for (double x = min; x <= max; x += step) {
128 for (double y = min; y <= max; y += step) {
129 for (double z = min; z <= max; z += step) {
130 if (!skipZero || (x != 0.0 || y != 0.0 || z != 0.0)) {
131 callback.accept(x, y, z);
132 }
133 }
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145
146 public static void assertCoordinatesEqual(final Vector1D expected, final Vector1D actual, final double tolerance) {
147 final String msg = "Expected coordinates to equal " + expected + " but was " + actual + ";";
148 Assertions.assertEquals(expected.getX(), actual.getX(), tolerance, msg);
149 }
150
151
152
153
154
155
156
157
158
159 public static void assertCoordinatesEqual(final Vector2D expected, final Vector2D actual, final double tolerance) {
160 final String msg = "Expected coordinates to equal " + expected + " but was " + actual + ";";
161 Assertions.assertEquals(expected.getX(), actual.getX(), tolerance, msg);
162 Assertions.assertEquals(expected.getY(), actual.getY(), tolerance, msg);
163 }
164
165
166
167
168
169
170
171
172
173 public static void assertCoordinatesEqual(final Vector3D expected, final Vector3D actual, final double tolerance) {
174 final String msg = "Expected coordinates to equal " + expected + " but was " + actual + ";";
175 Assertions.assertEquals(expected.getX(), actual.getX(), tolerance, msg);
176 Assertions.assertEquals(expected.getY(), actual.getY(), tolerance, msg);
177 Assertions.assertEquals(expected.getZ(), actual.getZ(), tolerance, msg);
178 }
179
180
181
182
183
184
185 public static void assertPositiveInfinity(final double value) {
186 final String msg = "Expected value to be positive infinity but was " + value;
187 Assertions.assertTrue(Double.isInfinite(value), msg);
188 Assertions.assertTrue(value > 0, msg);
189 }
190
191
192
193
194
195
196
197
198
199 public static <V extends EuclideanVector<V>> void assertVertexLoopSequence(final List<V> expected, final List<V> actual,
200 final Precision.DoubleEquivalence precision) {
201 Assertions.assertEquals(expected.size(), actual.size(), "Vertex sequences have different sizes");
202
203 if (!expected.isEmpty()) {
204
205 int offset = -1;
206 final V start = expected.get(0);
207 for (int i = 0; i < actual.size(); ++i) {
208 if (actual.get(i).eq(start, precision)) {
209 offset = i;
210 break;
211 }
212 }
213
214 if (offset < 0) {
215 Assertions.fail("Vertex loops do not share any points: expected " + expected + " but was " + actual);
216 }
217
218 V expectedVertex;
219 V actualVertex;
220 for (int i = 0; i < expected.size(); ++i) {
221 expectedVertex = expected.get(i);
222 actualVertex = actual.get((i + offset) % actual.size());
223
224 if (!expectedVertex.eq(actualVertex, precision)) {
225 Assertions.fail("Unexpected vertex at index " + i + ": expected " + expectedVertex +
226 " but was " + actualVertex);
227 }
228 }
229 }
230 }
231
232
233
234
235
236
237 public static void assertNegativeInfinity(final double value) {
238 final String msg = "Expected value to be negative infinity but was " + value;
239 Assertions.assertTrue(Double.isInfinite(value), msg);
240 Assertions.assertTrue(value < 0, msg);
241 }
242
243
244
245
246
247
248
249 public static void assertRegionLocation(final Region<Vector1D> region, final RegionLocation loc, final Vector1D... pts) {
250 for (final Vector1D pt : pts) {
251 Assertions.assertEquals(loc, region.classify(pt), "Unexpected region location for point " + pt);
252 }
253 }
254
255
256
257
258
259
260
261 public static void assertRegionLocation(final Region<Vector2D> region, final RegionLocation loc, final Vector2D... pts) {
262 for (final Vector2D pt : pts) {
263 Assertions.assertEquals(loc, region.classify(pt), "Unexpected region location for point " + pt);
264 }
265 }
266
267
268
269
270
271
272
273 public static void assertRegionLocation(final Region<Vector3D> region, final RegionLocation loc, final Vector3D... pts) {
274 for (final Vector3D pt : pts) {
275 Assertions.assertEquals(loc, region.classify(pt), "Unexpected region location for point " + pt);
276 }
277 }
278
279
280
281
282
283
284 public static void assertRegionLocation(final HyperplaneSubset<Vector1D> sub, final RegionLocation loc, final Vector1D... pts) {
285 for (final Vector1D pt : pts) {
286 Assertions.assertEquals(loc, sub.classify(pt), "Unexpected region location for point " + pt);
287 }
288 }
289
290
291
292
293
294
295 public static void assertRegionLocation(final HyperplaneSubset<Vector2D> sub, final RegionLocation loc, final Vector2D... pts) {
296 for (final Vector2D pt : pts) {
297 Assertions.assertEquals(loc, sub.classify(pt), "Unexpected region location for point " + pt);
298 }
299 }
300
301
302
303
304
305
306 public static void assertRegionLocation(final HyperplaneSubset<Vector3D> sub, final RegionLocation loc, final Vector3D... pts) {
307 for (final Vector3D pt : pts) {
308 Assertions.assertEquals(loc, sub.classify(pt), "Unexpected region location for point " + pt);
309 }
310 }
311 }