1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.io.euclidean.threed;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.apache.commons.geometry.core.GeometryTestUtils;
24 import org.apache.commons.geometry.euclidean.threed.Vector3D;
25 import org.junit.jupiter.api.Assertions;
26 import org.junit.jupiter.api.Test;
27
28 class SimpleFacetDefinitionTest {
29
30 private static final List<Vector3D> FACET_PTS = Arrays.asList(
31 Vector3D.ZERO, Vector3D.of(1, 0, 0), Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0));
32
33 @Test
34 void testProperties_verticesOnly() {
35
36 final SimpleFacetDefinition f = new SimpleFacetDefinition(new ArrayList<>(FACET_PTS));
37
38
39 Assertions.assertEquals(FACET_PTS, f.getVertices());
40 Assertions.assertNotSame(FACET_PTS, f.getVertices());
41
42 final List<Vector3D> vertices = f.getVertices();
43 final Vector3D toAdd = FACET_PTS.get(0);
44 Assertions.assertThrows(UnsupportedOperationException.class,
45 () -> vertices.add(toAdd));
46
47 Assertions.assertNull(f.getNormal());
48 }
49
50 @Test
51 void testProperties_verticesAndNormal() {
52
53 final Vector3D normal = Vector3D.ZERO;
54
55
56 final SimpleFacetDefinition f = new SimpleFacetDefinition(new ArrayList<>(FACET_PTS), normal);
57
58
59 Assertions.assertEquals(FACET_PTS, f.getVertices());
60 Assertions.assertNotSame(FACET_PTS, f.getVertices());
61
62 final List<Vector3D> vertices = f.getVertices();
63 final Vector3D toAdd = FACET_PTS.get(0);
64 Assertions.assertThrows(UnsupportedOperationException.class,
65 () -> vertices.add(toAdd));
66
67 Assertions.assertSame(normal, f.getNormal());
68 }
69
70 @Test
71 void testCtor_invalidArgs() {
72
73 final Vector3D normal = Vector3D.ZERO;
74 final List<Vector3D> invalid = Arrays.asList(Vector3D.ZERO, Vector3D.Unit.PLUS_X);
75
76 final String verticesNull = "Facet vertex list cannot be null";
77 final String vertexCountMsg = "Facet vertex list must contain at least 3 points; found 2";
78
79
80 GeometryTestUtils.assertThrowsWithMessage(
81 () -> new SimpleFacetDefinition(null),
82 NullPointerException.class, verticesNull);
83
84 GeometryTestUtils.assertThrowsWithMessage(
85 () -> new SimpleFacetDefinition(null, normal),
86 NullPointerException.class, verticesNull);
87
88 GeometryTestUtils.assertThrowsWithMessage(
89 () -> new SimpleFacetDefinition(invalid),
90 IllegalArgumentException.class, vertexCountMsg);
91
92 GeometryTestUtils.assertThrowsWithMessage(
93 () -> new SimpleFacetDefinition(invalid, normal),
94 IllegalArgumentException.class, vertexCountMsg);
95 }
96
97 @Test
98 void testToString() {
99
100 final SimpleFacetDefinition f = new SimpleFacetDefinition(FACET_PTS, Vector3D.Unit.PLUS_Z);
101
102
103 final String str = f.toString();
104
105
106 GeometryTestUtils.assertContains("SimpleFacetDefinition[vertices= [(0", str);
107 GeometryTestUtils.assertContains(", normal= (0", str);
108 }
109 }