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.enclosing;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
23  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
24  import org.apache.commons.geometry.enclosing.euclidean.threed.WelzlEncloser3D;
25  import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
26  import org.apache.commons.geometry.euclidean.threed.Vector3D;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /** This class contains code listed as examples in the user guide and other documentation.
31   * If any portion of this code changes, the corresponding examples in the documentation <em>must</em> be updated.
32   */
33  public class DocumentationExamplesTest {
34  
35      private static final double TEST_EPS = 1e-10;
36  
37      @Test
38      public void testWelzlEncloser3DExample() {
39          final DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(1e-10);
40  
41          final List<Vector3D> points = Arrays.asList(
42                      Vector3D.of(0, 0, 1),
43                      Vector3D.of(0.75, 0, 1),
44                      Vector3D.of(2, 0, 1),
45                      Vector3D.of(1, 0, 2)
46                  );
47  
48          // compute the enclosing ball
49          final WelzlEncloser3D encloser = new WelzlEncloser3D(precision);
50  
51          final EnclosingBall<Vector3D> sphere = encloser.enclose(points);
52  
53          // check the generated ball
54          final Vector3D center = sphere.getCenter(); // (1, 0, 1)
55          final double radius = sphere.getRadius(); // 1.0
56          final boolean containsCenter = sphere.contains(center); // true
57          final boolean containsOrigin = sphere.contains(Vector3D.ZERO); // false
58  
59          // ----------
60          EuclideanTestUtils.assertCoordinatesEqual(Vector3D.of(1, 0, 1), center, TEST_EPS);
61          Assert.assertEquals(1.0, radius, TEST_EPS);
62          Assert.assertTrue(containsCenter);
63          Assert.assertFalse(containsOrigin);
64      }
65  }