001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.analysis.interpolation;
018
019import org.apache.commons.math3.util.FastMath;
020import org.apache.commons.math3.util.MathUtils;
021
022/**
023 * Utility class for the {@link MicrosphereProjectionInterpolator} algorithm.
024 * For 2D interpolation, this class constructs the microsphere as a series of
025 * evenly spaced facets (rather than generating random normals as in the
026 * base implementation).
027 *
028 * @since 3.6
029 */
030public class InterpolatingMicrosphere2D extends InterpolatingMicrosphere {
031    /** Space dimension. */
032    private static final int DIMENSION = 2;
033
034    /**
035     * Create a sphere from vectors regularly sampled around a circle.
036     *
037     * @param size Number of surface elements of the sphere.
038     * @param maxDarkFraction Maximum fraction of the facets that can be dark.
039     * If the fraction of "non-illuminated" facets is larger, no estimation
040     * of the value will be performed, and the {@code background} value will
041     * be returned instead.
042     * @param darkThreshold Value of the illumination below which a facet is
043     * considered dark.
044     * @param background Value returned when the {@code maxDarkFraction}
045     * threshold is exceeded.
046     * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
047     * if {@code size <= 0}.
048     * @throws org.apache.commons.math3.exception.NotPositiveException if
049     * {@code darkThreshold < 0}.
050     * @throws org.apache.commons.math3.exception.OutOfRangeException if
051     * {@code maxDarkFraction} does not belong to the interval {@code [0, 1]}.
052     */
053    public InterpolatingMicrosphere2D(int size,
054                                      double maxDarkFraction,
055                                      double darkThreshold,
056                                      double background) {
057        super(DIMENSION, size, maxDarkFraction, darkThreshold, background);
058
059        // Generate the microsphere normals.
060        for (int i = 0; i < size; i++) {
061            final double angle = i * MathUtils.TWO_PI / size;
062
063            add(new double[] { FastMath.cos(angle),
064                               FastMath.sin(angle) },
065                false);
066        }
067    }
068
069    /**
070     * Copy constructor.
071     *
072     * @param other Instance to copy.
073     */
074    protected InterpolatingMicrosphere2D(InterpolatingMicrosphere2D other) {
075        super(other);
076    }
077
078    /**
079     * Perform a copy.
080     *
081     * @return a copy of this instance.
082     */
083    @Override
084    public InterpolatingMicrosphere2D copy() {
085        return new InterpolatingMicrosphere2D(this);
086    }
087}