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  
23  package org.hipparchus.geometry.spherical.twod;
24  
25  import java.io.Serializable;
26  
27  import org.hipparchus.exception.MathIllegalArgumentException;
28  import org.hipparchus.geometry.LocalizedGeometryFormats;
29  import org.hipparchus.geometry.Space;
30  import org.hipparchus.geometry.spherical.oned.Sphere1D;
31  import org.hipparchus.util.FastMath;
32  
33  /**
34   * This class implements a two-dimensional sphere (i.e. the regular sphere).
35   * <p>
36   * We use here the topologists definition of the 2-sphere (see
37   * <a href="http://mathworld.wolfram.com/Sphere.html">Sphere</a> on
38   * MathWorld), i.e. the 2-sphere is the two-dimensional surface
39   * defined in 3D as x<sup>2</sup>+y<sup>2</sup>+z<sup>2</sup>=1.
40   * </p>
41   */
42  public class Sphere2D implements Serializable, Space {
43  
44      /** Smallest tolerance that can be managed.
45       * <p>
46       * Tolerances smaller than this value will generate exceptions.
47       * </p>
48       * @since 1.4
49       */
50      public static final double SMALLEST_TOLERANCE = FastMath.ulp(2 * FastMath.PI);
51  
52      /** Serializable version identifier. */
53      private static final long serialVersionUID = 20131218L;
54  
55      /** Private constructor for the singleton.
56       */
57      private Sphere2D() {
58      }
59  
60      /** Get the unique instance.
61       * @return the unique instance
62       */
63      public static Sphere2D getInstance() {
64          return LazyHolder.INSTANCE;
65      }
66  
67      /** Check tolerance against {@link #SMALLEST_TOLERANCE}.
68       * @param tolerance tolerance to check
69       * @exception MathIllegalArgumentException if tolerance is smaller
70       * than {@link #SMALLEST_TOLERANCE}
71       */
72      public static void checkTolerance(final double tolerance)
73          throws MathIllegalArgumentException {
74          if (tolerance < Sphere1D.SMALLEST_TOLERANCE) {
75              throw new MathIllegalArgumentException(LocalizedGeometryFormats.TOO_SMALL_TOLERANCE,
76                                                     tolerance, "Sphere2D.SMALLEST_TOLERANCE", SMALLEST_TOLERANCE);
77          }
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public int getDimension() {
83          return 2;
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public Sphere1D getSubSpace() {
89          return Sphere1D.getInstance();
90      }
91  
92      // CHECKSTYLE: stop HideUtilityClassConstructor
93      /** Holder for the instance.
94       * <p>We use here the Initialization On Demand Holder Idiom.</p>
95       */
96      private static class LazyHolder {
97          /** Cached field instance. */
98          private static final Sphere2D INSTANCE = new Sphere2D();
99      }
100     // CHECKSTYLE: resume HideUtilityClassConstructor
101 
102     /** Handle deserialization of the singleton.
103      * @return the singleton instance
104      */
105     private Object readResolve() {
106         // return the singleton instance
107         return LazyHolder.INSTANCE;
108     }
109 
110 }