Sphere2D.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.geometry.spherical.twod;

  22. import java.io.Serializable;

  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.geometry.LocalizedGeometryFormats;
  25. import org.hipparchus.geometry.Space;
  26. import org.hipparchus.geometry.spherical.oned.Sphere1D;
  27. import org.hipparchus.util.FastMath;

  28. /**
  29.  * This class implements a two-dimensional sphere (i.e. the regular sphere).
  30.  * <p>
  31.  * We use here the topologists definition of the 2-sphere (see
  32.  * <a href="http://mathworld.wolfram.com/Sphere.html">Sphere</a> on
  33.  * MathWorld), i.e. the 2-sphere is the two-dimensional surface
  34.  * defined in 3D as x<sup>2</sup>+y<sup>2</sup>+z<sup>2</sup>=1.
  35.  * </p>
  36.  */
  37. public class Sphere2D implements Serializable, Space {

  38.     /** Smallest tolerance that can be managed.
  39.      * <p>
  40.      * Tolerances smaller than this value will generate exceptions.
  41.      * </p>
  42.      * @since 1.4
  43.      */
  44.     public static final double SMALLEST_TOLERANCE = FastMath.ulp(2 * FastMath.PI);

  45.     /** Serializable version identifier. */
  46.     private static final long serialVersionUID = 20131218L;

  47.     /** Private constructor for the singleton.
  48.      */
  49.     private Sphere2D() {
  50.     }

  51.     /** Get the unique instance.
  52.      * @return the unique instance
  53.      */
  54.     public static Sphere2D getInstance() {
  55.         return LazyHolder.INSTANCE;
  56.     }

  57.     /** Check tolerance against {@link #SMALLEST_TOLERANCE}.
  58.      * @param tolerance tolerance to check
  59.      * @exception MathIllegalArgumentException if tolerance is smaller
  60.      * than {@link #SMALLEST_TOLERANCE}
  61.      */
  62.     public static void checkTolerance(final double tolerance)
  63.         throws MathIllegalArgumentException {
  64.         if (tolerance < SMALLEST_TOLERANCE) {
  65.             throw new MathIllegalArgumentException(LocalizedGeometryFormats.TOO_SMALL_TOLERANCE,
  66.                                                    tolerance, "Sphere2D.SMALLEST_TOLERANCE", SMALLEST_TOLERANCE);
  67.         }
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public int getDimension() {
  72.         return 2;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public Sphere1D getSubSpace() {
  77.         return Sphere1D.getInstance();
  78.     }

  79.     // CHECKSTYLE: stop HideUtilityClassConstructor
  80.     /** Holder for the instance.
  81.      * <p>We use here the Initialization On Demand Holder Idiom.</p>
  82.      */
  83.     private static class LazyHolder {
  84.         /** Cached field instance. */
  85.         private static final Sphere2D INSTANCE = new Sphere2D();
  86.     }
  87.     // CHECKSTYLE: resume HideUtilityClassConstructor

  88.     /** Handle deserialization of the singleton.
  89.      * @return the singleton instance
  90.      */
  91.     private Object readResolve() {
  92.         // return the singleton instance
  93.         return LazyHolder.INSTANCE;
  94.     }

  95. }