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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.euclidean.threed;
23  
24  import org.hipparchus.geometry.euclidean.twod.Vector2D;
25  import org.hipparchus.geometry.partitioning.RegionFactory;
26  import org.hipparchus.util.FastMath;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertNull;
31  
32  class OutlineExtractorTest {
33  
34      @Test
35      void testBox() {
36  
37          PolyhedronsSet tree = new PolyhedronsSet(0, 1, 0, 1, 0, 1, 1.0e-10);
38          assertEquals(1.0, tree.getSize(), 1.0e-10);
39          assertEquals(6.0, tree.getBoundarySize(), 1.0e-10);
40          Vector2D[][] outline = new OutlineExtractor(Vector3D.PLUS_I, Vector3D.PLUS_J).getOutline(tree);
41          assertEquals(1, outline.length);
42          assertEquals(4, outline[0].length);
43  
44          Vector2D[] expected = new Vector2D[] {
45              new Vector2D(0.0, 0.0),
46              new Vector2D(0.0, 1.0),
47              new Vector2D(1.0, 1.0),
48              new Vector2D(1.0, 0.0)
49          };
50          for (final Vector2D vertex : outline[0]) {
51              for (int j = 0; j < expected.length; ++j) {
52                  if (expected[j] != null && Vector2D.distance(vertex, expected[j]) < 1.0e-10) {
53                      expected[j] = null;
54                  }
55              }
56          }
57          for (final Vector2D e : expected) {
58              assertNull(e);
59          }
60  
61          // observed from a diagonal, the cube appears as an hexagon
62          Rotation r = new Rotation(Vector3D.PLUS_I, Vector3D.PLUS_J,
63                                    new Vector3D(1, 1, 0), new Vector3D(0, 1, 1));
64          Vector2D[][] outlineSkew = new OutlineExtractor(r.applyTo(Vector3D.PLUS_I),
65                                                          r.applyTo(Vector3D.PLUS_J)).
66                                     getOutline(tree);
67          assertEquals(1, outlineSkew.length);
68          int n = outlineSkew[0].length;
69          assertEquals(6, n);
70          for (int i = 0; i < n; ++i) {
71              Vector2D v1 = outlineSkew[0][i];
72              Vector2D v2 = outlineSkew[0][(i + n - 1) % n];
73              assertEquals(FastMath.sqrt(2.0 / 3.0), Vector2D.distance(v1, v2), 1.0e-10);
74          }
75  
76      }
77  
78      @Test
79      void testHolesInFacet() {
80          double tolerance = 1.0e-10;
81          PolyhedronsSet cube       = new PolyhedronsSet(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, tolerance);
82          PolyhedronsSet tubeAlongX = new PolyhedronsSet(-2.0, 2.0, -0.5, 0.5, -0.5, 0.5, tolerance);
83          PolyhedronsSet tubeAlongY = new PolyhedronsSet(-0.5, 0.5, -2.0, 2.0, -0.5, 0.5, tolerance);
84          PolyhedronsSet tubeAlongZ = new PolyhedronsSet(-0.5, 0.5, -0.5, 0.5, -2.0, 2.0, tolerance);
85          RegionFactory<Euclidean3D, Vector3D, Plane, SubPlane> factory = new RegionFactory<>();
86          PolyhedronsSet cubeWithHoles = (PolyhedronsSet) factory.difference(cube,
87                                                                             factory.union(tubeAlongX,
88                                                                                           factory.union(tubeAlongY, tubeAlongZ)));
89          assertEquals(4.0, cubeWithHoles.getSize(), 1.0e-10);
90          Vector2D[][] outline = new OutlineExtractor(Vector3D.PLUS_I, Vector3D.PLUS_J).getOutline(cubeWithHoles);
91          assertEquals(2, outline.length);
92          assertEquals(4, outline[0].length);
93          assertEquals(4, outline[1].length);
94      }
95  
96  }