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.twod;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.hipparchus.geometry.enclosing.EnclosingBall;
29  import org.hipparchus.random.RandomGenerator;
30  import org.hipparchus.random.UnitSphereRandomVectorGenerator;
31  import org.hipparchus.random.Well1024a;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  
36  public class DiskGeneratorTest {
37  
38      @Test
39      public void testSupport0Point() {
40          List<Vector2D> support = Arrays.asList(new Vector2D[0]);
41          EnclosingBall<Euclidean2D, Vector2D> disk = new DiskGenerator().ballOnSupport(support);
42          Assert.assertTrue(disk.getRadius() < 0);
43          Assert.assertEquals(0, disk.getSupportSize());
44          Assert.assertEquals(0, disk.getSupport().length);
45      }
46  
47      @Test
48      public void testSupport1Point() {
49          List<Vector2D> support = Arrays.asList(new Vector2D(1, 2));
50          EnclosingBall<Euclidean2D, Vector2D> disk = new DiskGenerator().ballOnSupport(support);
51          Assert.assertEquals(0.0, disk.getRadius(), 1.0e-10);
52          Assert.assertTrue(disk.contains(support.get(0)));
53          Assert.assertTrue(disk.contains(support.get(0), 0.5));
54          Assert.assertFalse(disk.contains(new Vector2D(support.get(0).getX() + 0.1,
55                                                        support.get(0).getY() - 0.1),
56                                           0.001));
57          Assert.assertTrue(disk.contains(new Vector2D(support.get(0).getX() + 0.1,
58                                                       support.get(0).getY() - 0.1),
59                                          0.5));
60          Assert.assertEquals(0, support.get(0).distance(disk.getCenter()), 1.0e-10);
61          Assert.assertEquals(1, disk.getSupportSize());
62          Assert.assertTrue(support.get(0) == disk.getSupport()[0]);
63      }
64  
65      @Test
66      public void testSupport2Points() {
67          List<Vector2D> support = Arrays.asList(new Vector2D(1, 0),
68                                                 new Vector2D(3, 0));
69          EnclosingBall<Euclidean2D, Vector2D> disk = new DiskGenerator().ballOnSupport(support);
70          Assert.assertEquals(1.0, disk.getRadius(), 1.0e-10);
71          int i = 0;
72          for (Vector2D v : support) {
73              Assert.assertTrue(disk.contains(v));
74              Assert.assertEquals(1.0, v.distance(disk.getCenter()), 1.0e-10);
75              Assert.assertTrue(v == disk.getSupport()[i++]);
76          }
77          Assert.assertTrue(disk.contains(new Vector2D(2, 0.9)));
78          Assert.assertFalse(disk.contains(Vector2D.ZERO));
79          Assert.assertEquals(0.0, new Vector2D(2, 0).distance(disk.getCenter()), 1.0e-10);
80          Assert.assertEquals(2, disk.getSupportSize());
81      }
82  
83      @Test
84      public void testSupport3Points() {
85          List<Vector2D> support = Arrays.asList(new Vector2D(1, 0),
86                                                 new Vector2D(3, 0),
87                                                 new Vector2D(2, 2));
88          EnclosingBall<Euclidean2D, Vector2D> disk = new DiskGenerator().ballOnSupport(support);
89          Assert.assertEquals(5.0 / 4.0, disk.getRadius(), 1.0e-10);
90          int i = 0;
91          for (Vector2D v : support) {
92              Assert.assertTrue(disk.contains(v));
93              Assert.assertEquals(5.0 / 4.0, v.distance(disk.getCenter()), 1.0e-10);
94              Assert.assertTrue(v == disk.getSupport()[i++]);
95          }
96          Assert.assertTrue(disk.contains(new Vector2D(2, 0.9)));
97          Assert.assertFalse(disk.contains(new Vector2D(0.9,  0)));
98          Assert.assertFalse(disk.contains(new Vector2D(3.1,  0)));
99          Assert.assertTrue(disk.contains(new Vector2D(2.0, -0.499)));
100         Assert.assertFalse(disk.contains(new Vector2D(2.0, -0.501)));
101         Assert.assertEquals(0.0, new Vector2D(2.0, 3.0 / 4.0).distance(disk.getCenter()), 1.0e-10);
102         Assert.assertEquals(3, disk.getSupportSize());
103     }
104 
105     @Test
106     public void testRandom() {
107         final RandomGenerator random = new Well1024a(0x12faa818373ffe90l);
108         final UnitSphereRandomVectorGenerator sr = new UnitSphereRandomVectorGenerator(2, random);
109         for (int i = 0; i < 500; ++i) {
110             double d = 25 * random.nextDouble();
111             double refRadius = 10 * random.nextDouble();
112             Vector2D refCenter = new Vector2D(d, new Vector2D(sr.nextVector()));
113             List<Vector2D> support = new ArrayList<Vector2D>();
114             for (int j = 0; j < 3; ++j) {
115                 support.add(new Vector2D(1.0, refCenter, refRadius, new Vector2D(sr.nextVector())));
116             }
117             EnclosingBall<Euclidean2D, Vector2D> disk = new DiskGenerator().ballOnSupport(support);
118             Assert.assertEquals(0.0, refCenter.distance(disk.getCenter()), 3e-9 * refRadius);
119             Assert.assertEquals(refRadius, disk.getRadius(), 7e-10 * refRadius);
120         }
121 
122     }
123 }