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.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.core.precision.DoublePrecisionContext;
25  import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
26  import org.apache.commons.geometry.enclosing.EnclosingBall;
27  import org.apache.commons.geometry.euclidean.threed.Vector3D;
28  import org.apache.commons.rng.UniformRandomProvider;
29  import org.apache.commons.rng.sampling.UnitSphereSampler;
30  import org.apache.commons.rng.simple.RandomSource;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  public class WelzlEncloser3DTest {
35  
36      private static final double TEST_EPS = 1e-10;
37  
38      private static final DoublePrecisionContext TEST_PRECISION =
39              new EpsilonDoublePrecisionContext(TEST_EPS);
40  
41      private final WelzlEncloser3D encloser = new WelzlEncloser3D(TEST_PRECISION);
42  
43      @Test
44      public void testNoPoints() {
45          // arrange
46          final String msg = "Unable to generate enclosing ball: no points given";
47  
48          // act/assert
49          GeometryTestUtils.assertThrows(() -> {
50              encloser.enclose(null);
51          }, IllegalArgumentException.class, msg);
52  
53          GeometryTestUtils.assertThrows(() -> {
54              encloser.enclose(new ArrayList<Vector3D>());
55          }, IllegalArgumentException.class, msg);
56      }
57  
58      @Test
59      public void testReducingBall() {
60          // arrange
61          final List<Vector3D> list =
62                  Arrays.asList(Vector3D.of(-7.140397329568118, -16.571661242582177, 11.714458961735405),
63                                Vector3D.of(-7.137986707455888, -16.570767323375720, 11.708602108715928),
64                                Vector3D.of(-7.139185068549035, -16.570891204702250, 11.715554057357394),
65                                Vector3D.of(-7.142682716997507, -16.571609818234290, 11.710787934580328),
66                                Vector3D.of(-7.139018392423351, -16.574405614157020, 11.710518716711425),
67                                Vector3D.of(-7.140870659936730, -16.567993074240455, 11.710914678204503),
68                                Vector3D.of(-7.136350173659562, -16.570498228820930, 11.713965225900928),
69                                Vector3D.of(-7.141675762759172, -16.572852471407028, 11.714033471449508),
70                                Vector3D.of(-7.140453077221105, -16.570212820780647, 11.708624578004980),
71                                Vector3D.of(-7.140322188726825, -16.574152894557717, 11.710305611121410),
72                                Vector3D.of(-7.141116131477088, -16.574061164624560, 11.712938509321699));
73  
74          // act
75          final EnclosingBall<Vector3D> ball = encloser.enclose(list);
76  
77          // assert
78          Assert.assertTrue(ball.getRadius() > 0);
79      }
80  
81      @Test
82      public void testInfiniteLoop() {
83          // arrange
84          // this test used to generate an infinite loop
85          final List<Vector3D> list =
86                  Arrays.asList(Vector3D.of(-0.89227075512164380, -2.89317694645713900, 14.84572323743355500),
87                                Vector3D.of(-0.92099498940693580, -2.31086108263908940, 12.92071026467688300),
88                                Vector3D.of(-0.85227999411005200, -3.06314731441320730, 15.40163831651287000),
89                                Vector3D.of(-1.77399413020785970, -3.65630391378114260, 14.13190097751873400),
90                                Vector3D.of(0.33157833272465354, -2.22813591757792160, 14.21225234159008200),
91                                Vector3D.of(-1.53065579165484400, -1.65692084770139570, 14.61483055714788500),
92                                Vector3D.of(-1.08457093941217140, -1.96100325935602980, 13.09265170575555000),
93                                Vector3D.of(0.30029469589708850, -3.05470831395667370, 14.56352400426342600),
94                                Vector3D.of(-0.95007443938638460, -1.86810946486118360, 15.14491234340057000),
95                                Vector3D.of(-1.89661503804130830, -2.17004080885185860, 14.81235128513927000),
96                                Vector3D.of(-0.72193328761607530, -1.44513142833618270, 14.52355724218561800),
97                                Vector3D.of(-0.26895980939606550, -3.69512371522084140, 14.72272846327652000),
98                                Vector3D.of(-1.53501693431786170, -3.25055166611021900, 15.15509062584274800),
99                                Vector3D.of(-0.71727553535519410, -3.62284279460799100, 13.26256700929380700),
100                               Vector3D.of(-0.30220950676137365, -3.25410412500779070, 13.13682612771606000),
101                               Vector3D.of(-0.04543996608267075, -1.93081853923797750, 14.79497997883171400),
102                               Vector3D.of(-1.53348892951571640, -3.66688919703524900, 14.73095600812074200),
103                               Vector3D.of(-0.98034899533935820, -3.34004481162763960, 13.03245014017556800));
104 
105         // act
106         final EnclosingBall<Vector3D> ball = encloser.enclose(list);
107 
108         // assert
109         Assert.assertTrue(ball.getRadius() > 0);
110     }
111 
112     @Test
113     public void testLargeSamples() {
114         // arrange
115         final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A,
116                                                                  0x35ddecfc78131e1dL);
117         final UnitSphereSampler sr = new UnitSphereSampler(3, random);
118         for (int k = 0; k < 50; ++k) {
119 
120             // define the reference sphere we want to compute
121             final double d = 25 * random.nextDouble();
122             final double refRadius = 10 * random.nextDouble();
123             final Vector3D refCenter = Vector3D.linearCombination(d, Vector3D.of(sr.nextVector()));
124             // set up a large sample inside the reference sphere
125             final int nbPoints = random.nextInt(1000);
126 
127             final List<Vector3D> points = new ArrayList<>();
128             for (int i = 0; i < nbPoints; ++i) {
129                 final double r = refRadius * random.nextDouble();
130                 points.add(Vector3D.linearCombination(1.0, refCenter, r, Vector3D.of(sr.nextVector())));
131             }
132 
133             // act/assert
134             // test we find a sphere at most as large as the one used for random drawings
135             checkSphere(points, refRadius);
136         }
137     }
138 
139     private void checkSphere(final List<Vector3D> points, final double refRadius) {
140 
141         final EnclosingBall<Vector3D> sphere = checkSphere(points);
142 
143         // compare computed sphere with bounding sphere
144         Assert.assertTrue(sphere.getRadius() <= refRadius);
145 
146         // check removing any point of the support Sphere fails to enclose the point
147         for (int i = 0; i < sphere.getSupportSize(); ++i) {
148             final List<Vector3D> reducedSupport = new ArrayList<>();
149             int count = 0;
150             for (final Vector3D s : sphere.getSupport()) {
151                 if (count++ != i) {
152                     reducedSupport.add(s);
153                 }
154             }
155             final EnclosingBall<Vector3D> reducedSphere = new SphereGenerator(TEST_PRECISION)
156                     .ballOnSupport(reducedSupport);
157             boolean foundOutside = false;
158             for (int j = 0; j < points.size() && !foundOutside; ++j) {
159                 if (!reducedSphere.contains(points.get(j), TEST_PRECISION)) {
160                     foundOutside = true;
161                 }
162             }
163             Assert.assertTrue(foundOutside);
164         }
165     }
166 
167     private EnclosingBall<Vector3D> checkSphere(final List<Vector3D> points) {
168 
169         final EnclosingBall<Vector3D> sphere = encloser.enclose(points);
170 
171         // all points are enclosed
172         for (final Vector3D v : points) {
173             Assert.assertTrue(sphere.contains(v, TEST_PRECISION));
174         }
175 
176         // all support points are on the boundary
177         final Vector3D center = sphere.getCenter();
178         final double radius = sphere.getRadius();
179 
180         for (final Vector3D s : sphere.getSupport()) {
181             Assert.assertTrue(TEST_PRECISION.eqZero(center.distance(s) - radius));
182         }
183 
184         return sphere;
185     }
186 }