    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
    006 *
    007 * http://www.object-refinery.com/orsoncharts/index.html
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * Orson Charts home page:
    028 *
    029 * http://www.object-refinery.com/orsoncharts/index.html
    030 *
    031 */
    032
    033package com.orsoncharts.data.function;
    034
    035import com.orsoncharts.Range;
    036import com.orsoncharts.util.ArgChecks;
    037
    038/**
    039 * Utility methods related to {@link Function3D}.
    040 *
    041 * @since 1.1
    042 */
    043public class Function3DUtils {
    044
    045    private Function3DUtils() {
    046        // no need to instantiate this
    047    }
    048
    049    /**
    050     * Returns the range of y-values in the function by sampling.
    051     *
    052     * @param f  the function ({@code null} not permitted).
    053     * @param xRange  the x-range to sample ({@code null} not permitted).
    054     * @param zRange  the z-range to sample ({@code null} not permitted).
    055     * @param xSamples  the number of x-samples (must be at least 2).
    056     * @param zSamples  the number of z-samples (must be at least 2).
    057     * @param ignoreNaN  if {@code true}, any {@code NaN} values will
    058     *     be ignored.
    059     *
    060     * @return The range ({@code null} in the case that the function
    061     *     returns no valid values).
    062     */
    063    public static Range findYRange(Function3D f, Range xRange, Range zRange,
    064            int xSamples, int zSamples, boolean ignoreNaN) {
    065        ArgChecks.nullNotPermitted(f, "f");
    066        ArgChecks.nullNotPermitted(xRange, "xRange");
    067        ArgChecks.nullNotPermitted(zRange, "zRange");
    068        double min = Double.POSITIVE_INFINITY;
    069        double max = Double.NEGATIVE_INFINITY;
    070        for (int xIndex = 0; xIndex <= xSamples - 1; xIndex++) {
    071            double fracX = xIndex / (xSamples - 1.0);
    072            double x = xRange.value(fracX);
    073            for (int zIndex = 0; zIndex <= zSamples - 1; zIndex++) {
    074                double fracZ = zIndex / (zSamples - 1.0);
    075                double z = zRange.value(fracZ);
    076                double y = f.getValue(x, z);
    077                if (Double.isNaN(y) && ignoreNaN) {
    078                    continue;
    079                }
    080                min = Math.min(y, min);
    081                max = Math.max(y, max);
    082            }
    083        }
    084        if (min <= max) {
    085            return new Range(min, max);
    086        }
    087        return null;
    088    }
    089}