View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // act
36          final SimpleFacetDefinition f = new SimpleFacetDefinition(new ArrayList<>(FACET_PTS));
37  
38          // assert
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          // arrange
53          final Vector3D normal = Vector3D.ZERO; // invalid normal is accepted
54  
55          // act
56          final SimpleFacetDefinition f = new SimpleFacetDefinition(new ArrayList<>(FACET_PTS), normal);
57  
58          // assert
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          // arrange
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          // act/assert
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          // arrange
100         final SimpleFacetDefinition f = new SimpleFacetDefinition(FACET_PTS, Vector3D.Unit.PLUS_Z);
101 
102         // act
103         final String str = f.toString();
104 
105         // assert
106         GeometryTestUtils.assertContains("SimpleFacetDefinition[vertices= [(0", str);
107         GeometryTestUtils.assertContains(", normal= (0", str);
108     }
109 }