MicrosphereProjectionInterpolator.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.analysis.interpolation;

  22. import org.hipparchus.analysis.MultivariateFunction;
  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.exception.NullArgumentException;
  26. import org.hipparchus.random.UnitSphereRandomVectorGenerator;

  27. /**
  28.  * Interpolator that implements the algorithm described in
  29.  * <em>William Dudziak</em>'s
  30.  * <a href="http://www.dudziak.com/microsphere.pdf">MS thesis</a>.
  31.  *
  32.  */
  33. public class MicrosphereProjectionInterpolator
  34.     implements MultivariateInterpolator {
  35.     /** Brightness exponent. */
  36.     private final double exponent;
  37.     /** Microsphere. */
  38.     private final InterpolatingMicrosphere microsphere;
  39.     /** Whether to share the sphere. */
  40.     private final boolean sharedSphere;
  41.     /** Tolerance value below which no interpolation is necessary. */
  42.     private final double noInterpolationTolerance;

  43.     /**
  44.      * Create a microsphere interpolator.
  45.      *
  46.      * @param dimension Space dimension.
  47.      * @param elements Number of surface elements of the microsphere.
  48.      * @param exponent Exponent used in the power law that computes the
  49.      * @param maxDarkFraction Maximum fraction of the facets that can be dark.
  50.      * If the fraction of "non-illuminated" facets is larger, no estimation
  51.      * of the value will be performed, and the {@code background} value will
  52.      * be returned instead.
  53.      * @param darkThreshold Value of the illumination below which a facet is
  54.      * considered dark.
  55.      * @param background Value returned when the {@code maxDarkFraction}
  56.      * threshold is exceeded.
  57.      * @param sharedSphere Whether the sphere can be shared among the
  58.      * interpolating function instances.  If {@code true}, the instances
  59.      * will share the same data, and thus will <em>not</em> be thread-safe.
  60.      * @param noInterpolationTolerance When the distance between an
  61.      * interpolated point and one of the sample points is less than this
  62.      * value, no interpolation will be performed (the value of the sample
  63.      * will be returned).
  64.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  65.      * if {@code dimension <= 0} or {@code elements <= 0}.
  66.      * @throws MathIllegalArgumentException if {@code exponent < 0}.
  67.      * @throws MathIllegalArgumentException if {@code darkThreshold < 0}.
  68.      * @throws org.hipparchus.exception.MathIllegalArgumentException if
  69.      * {@code maxDarkFraction} does not belong to the interval {@code [0, 1]}.
  70.      */
  71.     public MicrosphereProjectionInterpolator(int dimension,
  72.                                              int elements,
  73.                                              double maxDarkFraction,
  74.                                              double darkThreshold,
  75.                                              double background,
  76.                                              double exponent,
  77.                                              boolean sharedSphere,
  78.                                              double noInterpolationTolerance) {
  79.         this(new InterpolatingMicrosphere(dimension,
  80.                                           elements,
  81.                                           maxDarkFraction,
  82.                                           darkThreshold,
  83.                                           background,
  84.                                           new UnitSphereRandomVectorGenerator(dimension)),
  85.              exponent,
  86.              sharedSphere,
  87.              noInterpolationTolerance);
  88.     }

  89.     /**
  90.      * Create a microsphere interpolator.
  91.      *
  92.      * @param microsphere Microsphere.
  93.      * @param exponent Exponent used in the power law that computes the
  94.      * weights (distance dimming factor) of the sample data.
  95.      * @param sharedSphere Whether the sphere can be shared among the
  96.      * interpolating function instances.  If {@code true}, the instances
  97.      * will share the same data, and thus will <em>not</em> be thread-safe.
  98.      * @param noInterpolationTolerance When the distance between an
  99.      * interpolated point and one of the sample points is less than this
  100.      * value, no interpolation will be performed (the value of the sample
  101.      * will be returned).
  102.      * @throws MathIllegalArgumentException if {@code exponent < 0}.
  103.      */
  104.     public MicrosphereProjectionInterpolator(InterpolatingMicrosphere microsphere,
  105.                                              double exponent,
  106.                                              boolean sharedSphere,
  107.                                              double noInterpolationTolerance)
  108.         throws MathIllegalArgumentException {
  109.         if (exponent < 0) {
  110.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, exponent, 0);
  111.         }

  112.         this.microsphere = microsphere;
  113.         this.exponent = exponent;
  114.         this.sharedSphere = sharedSphere;
  115.         this.noInterpolationTolerance = noInterpolationTolerance;
  116.     }

  117.     /**
  118.      * {@inheritDoc}
  119.      *
  120.      * @throws MathIllegalArgumentException if the space dimension of the
  121.      * given samples does not match the space dimension of the microsphere.
  122.      */
  123.     @Override
  124.     public MultivariateFunction interpolate(final double[][] xval,
  125.                                             final double[] yval)
  126.         throws MathIllegalArgumentException, NullArgumentException {
  127.         if (xval == null ||
  128.             yval == null) {
  129.             throw new NullArgumentException();
  130.         }
  131.         if (xval.length == 0) {
  132.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA);
  133.         }
  134.         if (xval.length != yval.length) {
  135.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  136.                                                    xval.length, yval.length);
  137.         }
  138.         if (xval[0] == null) {
  139.             throw new NullArgumentException();
  140.         }
  141.         final int dimension = microsphere.getDimension();
  142.         if (dimension != xval[0].length) {
  143.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  144.                                                    xval[0].length, dimension);
  145.         }

  146.         // Microsphere copy.
  147.         final InterpolatingMicrosphere m = sharedSphere ? microsphere : microsphere.copy();

  148.         return new MultivariateFunction() {
  149.             /** {inheritDoc} */
  150.             @Override
  151.             public double value(double[] point) {
  152.                 return m.value(point,
  153.                                xval,
  154.                                yval,
  155.                                exponent,
  156.                                noInterpolationTolerance);
  157.             }
  158.         };
  159.     }
  160. }