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

  18. import org.apache.commons.math3.analysis.MultivariateFunction;
  19. import org.apache.commons.math3.exception.NotPositiveException;
  20. import org.apache.commons.math3.exception.NotStrictlyPositiveException;
  21. import org.apache.commons.math3.exception.NoDataException;
  22. import org.apache.commons.math3.exception.DimensionMismatchException;
  23. import org.apache.commons.math3.exception.NullArgumentException;
  24. import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator;

  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 2.1
  31.  */
  32. public class MicrosphereInterpolator
  33.     implements MultivariateInterpolator {
  34.     /**
  35.      * Default number of surface elements that composes the microsphere.
  36.      */
  37.     public static final int DEFAULT_MICROSPHERE_ELEMENTS = 2000;
  38.     /**
  39.      * Default exponent used the weights calculation.
  40.      */
  41.     public static final int DEFAULT_BRIGHTNESS_EXPONENT = 2;
  42.     /**
  43.      * Number of surface elements of the microsphere.
  44.      */
  45.     private final int microsphereElements;
  46.     /**
  47.      * Exponent used in the power law that computes the weights of the
  48.      * sample data.
  49.      */
  50.     private final int brightnessExponent;

  51.     /**
  52.      * Create a microsphere interpolator with default settings.
  53.      * Calling this constructor is equivalent to call {@link
  54.      * #MicrosphereInterpolator(int, int)
  55.      * MicrosphereInterpolator(MicrosphereInterpolator.DEFAULT_MICROSPHERE_ELEMENTS,
  56.      * MicrosphereInterpolator.DEFAULT_BRIGHTNESS_EXPONENT)}.
  57.      */
  58.     public MicrosphereInterpolator() {
  59.         this(DEFAULT_MICROSPHERE_ELEMENTS, DEFAULT_BRIGHTNESS_EXPONENT);
  60.     }

  61.     /** Create a microsphere interpolator.
  62.      * @param elements Number of surface elements of the microsphere.
  63.      * @param exponent Exponent used in the power law that computes the
  64.      * weights (distance dimming factor) of the sample data.
  65.      * @throws NotPositiveException if {@code exponent < 0}.
  66.      * @throws NotStrictlyPositiveException if {@code elements <= 0}.
  67.      */
  68.     public MicrosphereInterpolator(final int elements,
  69.                                    final int exponent)
  70.         throws NotPositiveException,
  71.                NotStrictlyPositiveException {
  72.         if (exponent < 0) {
  73.             throw new NotPositiveException(exponent);
  74.         }
  75.         if (elements <= 0) {
  76.             throw new NotStrictlyPositiveException(elements);
  77.         }

  78.         microsphereElements = elements;
  79.         brightnessExponent = exponent;
  80.     }

  81.     /**
  82.      * {@inheritDoc}
  83.      */
  84.     public MultivariateFunction interpolate(final double[][] xval,
  85.                                             final double[] yval)
  86.         throws DimensionMismatchException,
  87.                NoDataException,
  88.                NullArgumentException {
  89.         final UnitSphereRandomVectorGenerator rand
  90.             = new UnitSphereRandomVectorGenerator(xval[0].length);
  91.         return new MicrosphereInterpolatingFunction(xval, yval,
  92.                                                     brightnessExponent,
  93.                                                     microsphereElements,
  94.                                                     rand);
  95.     }
  96. }