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.Assert;
28  import org.junit.Test;
29  
30  public class OutlineExtractorTest {
31  
32      @Test
33      public void testBox() {
34  
35          PolyhedronsSet tree = new PolyhedronsSet(0, 1, 0, 1, 0, 1, 1.0e-10);
36          Assert.assertEquals(1.0, tree.getSize(), 1.0e-10);
37          Assert.assertEquals(6.0, tree.getBoundarySize(), 1.0e-10);
38          Vector2D[][] outline = new OutlineExtractor(Vector3D.PLUS_I, Vector3D.PLUS_J).getOutline(tree);
39          Assert.assertEquals(1, outline.length);
40          Assert.assertEquals(4, outline[0].length);
41  
42          Vector2D[] expected = new Vector2D[] {
43              new Vector2D(0.0, 0.0),
44              new Vector2D(0.0, 1.0),
45              new Vector2D(1.0, 1.0),
46              new Vector2D(1.0, 0.0)
47          };
48          for (final Vector2D vertex : outline[0]) {
49              for (int j = 0; j < expected.length; ++j) {
50                  if (expected[j] != null && Vector2D.distance(vertex, expected[j]) < 1.0e-10) {
51                      expected[j] = null;
52                  }
53              }
54          }
55          for (final Vector2D e : expected) {
56              Assert.assertNull(e);
57          }
58  
59          // observed from a diagonal, the cube appears as an hexagon
60          Rotation r = new Rotation(Vector3D.PLUS_I, Vector3D.PLUS_J,
61                                    new Vector3D(1, 1, 0), new Vector3D(0, 1, 1));
62          Vector2D[][] outlineSkew = new OutlineExtractor(r.applyTo(Vector3D.PLUS_I),
63                                                          r.applyTo(Vector3D.PLUS_J)).
64                                     getOutline(tree);
65          Assert.assertEquals(1, outlineSkew.length);
66          int n = outlineSkew[0].length;
67          Assert.assertEquals(6, n);
68          for (int i = 0; i < n; ++i) {
69              Vector2D v1 = outlineSkew[0][i];
70              Vector2D v2 = outlineSkew[0][(i + n - 1) % n];
71              Assert.assertEquals(FastMath.sqrt(2.0 / 3.0), Vector2D.distance(v1, v2), 1.0e-10);
72          }
73  
74      }
75  
76      @Test
77      public void testHolesInFacet() {
78          double tolerance = 1.0e-10;
79          PolyhedronsSet cube       = new PolyhedronsSet(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, tolerance);
80          PolyhedronsSet tubeAlongX = new PolyhedronsSet(-2.0, 2.0, -0.5, 0.5, -0.5, 0.5, tolerance);
81          PolyhedronsSet tubeAlongY = new PolyhedronsSet(-0.5, 0.5, -2.0, 2.0, -0.5, 0.5, tolerance);
82          PolyhedronsSet tubeAlongZ = new PolyhedronsSet(-0.5, 0.5, -0.5, 0.5, -2.0, 2.0, tolerance);
83          RegionFactory<Euclidean3D> factory = new RegionFactory<>();
84          PolyhedronsSet cubeWithHoles = (PolyhedronsSet) factory.difference(cube,
85                                                                             factory.union(tubeAlongX,
86                                                                                           factory.union(tubeAlongY, tubeAlongZ)));
87          Assert.assertEquals(4.0, cubeWithHoles.getSize(), 1.0e-10);
88          Vector2D[][] outline = new OutlineExtractor(Vector3D.PLUS_I, Vector3D.PLUS_J).getOutline(cubeWithHoles);
89          Assert.assertEquals(2, outline.length);
90          Assert.assertEquals(4, outline[0].length);
91          Assert.assertEquals(4, outline[1].length);
92      }
93  
94  }