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.math4.legacy.analysis.interpolation;
18  
19  import org.apache.commons.math4.core.jdkmath.JdkMath;
20  
21  /**
22   * Utility class for the {@link MicrosphereProjectionInterpolator} algorithm.
23   * For 2D interpolation, this class constructs the microsphere as a series of
24   * evenly spaced facets (rather than generating random normals as in the
25   * base implementation).
26   *
27   * @since 4.0
28   */
29  public class InterpolatingMicrosphere2D extends InterpolatingMicrosphere {
30      /** Space dimension. */
31      private static final int DIMENSION = 2;
32  
33      /**
34       * Create a sphere from vectors regularly sampled around a circle.
35       *
36       * @param size Number of surface elements of the sphere.
37       * @param maxDarkFraction Maximum fraction of the facets that can be dark.
38       * If the fraction of "non-illuminated" facets is larger, no estimation
39       * of the value will be performed, and the {@code background} value will
40       * be returned instead.
41       * @param darkThreshold Value of the illumination below which a facet is
42       * considered dark.
43       * @param background Value returned when the {@code maxDarkFraction}
44       * threshold is exceeded.
45       * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException
46       * if {@code size <= 0}.
47       * @throws org.apache.commons.math4.legacy.exception.NotPositiveException if
48       * {@code darkThreshold < 0}.
49       * @throws org.apache.commons.math4.legacy.exception.OutOfRangeException if
50       * {@code maxDarkFraction} does not belong to the interval {@code [0, 1]}.
51       */
52      public InterpolatingMicrosphere2D(int size,
53                                        double maxDarkFraction,
54                                        double darkThreshold,
55                                        double background) {
56          super(DIMENSION, size, maxDarkFraction, darkThreshold, background);
57  
58          // Generate the microsphere normals.
59          final double twopi = 2 * Math.PI;
60          for (int i = 0; i < size; i++) {
61              final double angle = i * twopi / size;
62  
63              add(new double[] { JdkMath.cos(angle),
64                                 JdkMath.sin(angle) },
65                  false);
66          }
67      }
68  
69      /**
70       * Copy constructor.
71       *
72       * @param other Instance to copy.
73       */
74      protected InterpolatingMicrosphere2D(InterpolatingMicrosphere2D other) {
75          super(other);
76      }
77  
78      /**
79       * Perform a copy.
80       *
81       * @return a copy of this instance.
82       */
83      @Override
84      public InterpolatingMicrosphere2D copy() {
85          return new InterpolatingMicrosphere2D(this);
86      }
87  }