View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.geometry;
18  
19  import org.hipparchus.exception.LocalizedCoreFormats;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.hipparchus.geometry.euclidean.threed.Vector3D;
22  import org.hipparchus.geometry.euclidean.twod.Vector2D;
23  import org.hipparchus.geometry.spherical.twod.S2Point;
24  import org.hipparchus.util.CombinatoricsUtils;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.Test;
27  
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.List;
31  
32  public class GeometryTest {
33  
34      @Test
35      public void testBarycenterException() {
36          try {
37              final List<S2Point> points = Collections.emptyList();
38              Geometry.barycenter(points);
39              Assertions.fail("Should have thrown an exception");
40          } catch (MathIllegalArgumentException miae) {
41              Assertions.assertEquals(LocalizedCoreFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, miae.getSpecifier());
42              Assertions.assertEquals(0, miae.getParts()[0]);
43          }
44      }
45  
46      @Test
47      public void testBarycenterIdentity() {
48          final Vector3D p = new Vector3D(1.0, 2.0, 3.0);
49          Assertions.assertSame(p, Geometry.barycenter(Collections.singletonList(p)));
50      }
51  
52      @Test
53      void testBarycenter2D() {
54          final List<Vector2D> points = Arrays.asList(new Vector2D(740.0, 1070.0),
55                                                      new Vector2D(1010.0, 1330.0),
56                                                      new Vector2D(1240.0, 1160.0),
57                                                      new Vector2D(1250.0,  800.0),
58                                                      new Vector2D( 890.0,  720.0));
59          final Vector2D naive = barycenterNaive2D(points);
60          CombinatoricsUtils.
61                  permutations(points).
62                  map(Geometry::barycenter).
63                  forEach(barycenter -> Assertions.assertEquals(0.0, naive.distance(barycenter), 2.0e-13));
64  
65      }
66  
67      @Test
68      void testBarycenter3D() {
69          final List<Vector3D> points = Arrays.asList(new Vector3D( 740.0, 1070.0, -12.0),
70                                                      new Vector3D(1010.0, 1330.0,   0.0),
71                                                      new Vector3D(1240.0, 1160.0,   6.0),
72                                                      new Vector3D(1250.0,  800.0,   6.0));
73          final Vector3D naive = barycenterNaive3D(points);
74          CombinatoricsUtils.
75                  permutations(points).
76                  map(Geometry::barycenter).
77                  forEach(barycenter -> Assertions.assertEquals(0.0, naive.distance(barycenter), 1.0e-15));
78  
79      }
80  
81      private Vector2D barycenterNaive2D(final List<Vector2D> points) {
82          double sumX = 0;
83          double sumY = 0;
84          for (final Vector2D point : points) {
85              sumX += point.getX();
86              sumY += point.getY();
87          }
88          return new Vector2D(sumX / points.size(), sumY / points.size());
89      }
90  
91      private Vector3D barycenterNaive3D(final List<Vector3D> points) {
92          double sumX = 0;
93          double sumY = 0;
94          double sumZ = 0;
95          for (final Vector3D point : points) {
96              sumX += point.getX();
97              sumY += point.getY();
98              sumZ += point.getZ();
99          }
100         return new Vector3D(sumX / points.size(), sumY / points.size(), sumZ / points.size());
101     }
102 
103 }