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.math4.legacy.analysis.interpolation;
018
019import org.apache.commons.math4.core.jdkmath.JdkMath;
020
021/**
022 * Utility class for the {@link MicrosphereProjectionInterpolator} algorithm.
023 * For 2D interpolation, this class constructs the microsphere as a series of
024 * evenly spaced facets (rather than generating random normals as in the
025 * base implementation).
026 *
027 * @since 4.0
028 */
029public class InterpolatingMicrosphere2D extends InterpolatingMicrosphere {
030    /** Space dimension. */
031    private static final int DIMENSION = 2;
032
033    /**
034     * Create a sphere from vectors regularly sampled around a circle.
035     *
036     * @param size Number of surface elements of the sphere.
037     * @param maxDarkFraction Maximum fraction of the facets that can be dark.
038     * If the fraction of "non-illuminated" facets is larger, no estimation
039     * of the value will be performed, and the {@code background} value will
040     * be returned instead.
041     * @param darkThreshold Value of the illumination below which a facet is
042     * considered dark.
043     * @param background Value returned when the {@code maxDarkFraction}
044     * threshold is exceeded.
045     * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException
046     * if {@code size <= 0}.
047     * @throws org.apache.commons.math4.legacy.exception.NotPositiveException if
048     * {@code darkThreshold < 0}.
049     * @throws org.apache.commons.math4.legacy.exception.OutOfRangeException if
050     * {@code maxDarkFraction} does not belong to the interval {@code [0, 1]}.
051     */
052    public InterpolatingMicrosphere2D(int size,
053                                      double maxDarkFraction,
054                                      double darkThreshold,
055                                      double background) {
056        super(DIMENSION, size, maxDarkFraction, darkThreshold, background);
057
058        // Generate the microsphere normals.
059        final double twopi = 2 * Math.PI;
060        for (int i = 0; i < size; i++) {
061            final double angle = i * twopi / size;
062
063            add(new double[] { JdkMath.cos(angle),
064                               JdkMath.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}