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.  *      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. import org.apache.commons.rng.simple.RandomSource;
  19. import org.apache.commons.rng.sampling.UnitSphereSampler;
  20. import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
  21. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  22. import org.apache.commons.math4.legacy.exception.NoDataException;
  23. import org.apache.commons.math4.legacy.exception.NotPositiveException;
  24. import org.apache.commons.math4.legacy.exception.NullArgumentException;

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

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

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

  117.     /**
  118.      * {@inheritDoc}
  119.      *
  120.      * @throws DimensionMismatchException 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 DimensionMismatchException,
  127.                NoDataException,
  128.                NullArgumentException {
  129.         if (xval == null ||
  130.             yval == null) {
  131.             throw new NullArgumentException();
  132.         }
  133.         if (xval.length == 0) {
  134.             throw new NoDataException();
  135.         }
  136.         if (xval.length != yval.length) {
  137.             throw new DimensionMismatchException(xval.length, yval.length);
  138.         }
  139.         if (xval[0] == null) {
  140.             throw new NullArgumentException();
  141.         }
  142.         final int dimension = microsphere.getDimension();
  143.         if (dimension != xval[0].length) {
  144.             throw new DimensionMismatchException(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. }