001/*
002 * MIT License
003 *
004 * Copyright (c) 2017 Justin Kunimune
005 *
006 * Permission is hereby granted, free of charge, to any person obtaining a copy
007 * of this software and associated documentation files (the "Software"), to deal
008 * in the Software without restriction, including without limitation the rights
009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 * copies of the Software, and to permit persons to whom the Software is
011 * furnished to do so, subject to the following conditions:
012 *
013 * The above copyright notice and this permission notice shall be included in all
014 * copies or substantial portions of the Software.
015 *
016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022 * SOFTWARE.
023 */
024
025package squidpony.squidgrid.mapping;
026
027/**
028 * Added to SquidLib by Tommy Ettinger on 7/4/2018, using MIT-licensed work by Justin Kunimune from
029 * <a href="https://github.com/jkunimune15/Map-Projections/blob/9f820aba788ba0b37a1c67128a4c861d243b4a46/src/utils/NumericalAnalysis.java">his Map-Projections repo</a>.
030 * @author jkunimune
031 * @author Tommy Ettinger
032 */
033public class ProjectionTools {
034    /**
035     * Performs a definite integral using Simpson's rule and a constant step size; hard-coded to integrate a
036     * hyperellipse function.
037     * @param a The start of the integration region
038     * @param b The end of the integration region (must be greater than a)
039     * @param h The step size (must be positive)
040     * @param kappa the kappa value of the hyperellipse
041     * @return some magic stuff needed for Tobler Hyperelliptical maps
042     */
043    public static double simpsonIntegrateHyperellipse(double a, double b, double h, double kappa) {
044        double sum = 0, ik = 1/kappa;
045        for (double x = a; x < b; x += h) {
046            if (x+h > b) h = b-x;
047            sum += h/6*(Math.pow(1 - Math.pow(Math.abs(x), kappa), ik) 
048                    + 4*Math.pow(1 - Math.pow(Math.abs(x + h * 0.5), kappa), ik) 
049                    + Math.pow(1 - Math.pow(Math.abs(x + h), kappa), ik));
050        }
051        return sum;
052    }
053
054    /**
055     * Solves a simple ODE using Simpson's rule and a constant step size; hard-coded to solve a hyperelliptical map
056     * projection task.
057     * @param T The maximum time value at which to sample (must be positive)
058     * @param y the double array to fill with samples; must not be null and must have length 1 or greater
059     * @param h The internal step size (must be positive)
060     * @param alpha part of the hyperelliptical projection's parameters 
061     * @param kappa part of the hyperelliptical projection's parameters
062     * @param epsilon calculated beforehand using {@link #simpsonIntegrateHyperellipse(double, double, double, double)}
063     * @return y, after modifications
064     */
065    public static double[] simpsonODESolveHyperellipse(final double T, final double[] y, final double h, final double alpha, final double kappa, final double epsilon)
066    {
067        final int m = y.length - 1, n = m + 1;
068        double t = 0;
069        double sum = 0;
070        for (int i = 0; i <= m; i++) {
071            while (t < i * T / n) {
072                final double tph = Math.min(t + h, i * T / n);
073                sum += (tph - t) / 6 * (Math.abs((alpha + (1-alpha)*Math.pow(1 - Math.pow(Math.abs(t), kappa), 1.0/kappa)) / (alpha + (1-alpha)*epsilon))
074                        + 4 * Math.abs((alpha + (1-alpha)*Math.pow(1 - Math.pow(Math.abs((t + tph) * 0.5), kappa), 1.0/kappa)) / (alpha + (1-alpha)*epsilon))
075                        + Math.abs((alpha + (1-alpha)*Math.pow(1 - Math.pow(Math.abs(tph), kappa), 1.0/kappa)) / (alpha + (1-alpha)*epsilon)));
076                t = tph;
077            }
078            y[i] = sum;
079        }
080        return y;
081    }
082
083    /**
084     * Part of computing a hyperellipse; takes only a y parameter corresponding to the y on a map and a kappa parameter
085     * used by Tobler's hyperelliptical projection to determine shape.
086     * @param y y on a map, usually -1.0 to 1.0
087     * @param kappa one of the Tobler parameters
088     * @return I'm guessing the actual y used after hyperelliptical distortion; not sure
089     */
090    public static double hyperellipse(double y, double kappa) {
091        return Math.pow(1 - Math.pow(Math.abs(y),kappa), 1/kappa);
092    }
093
094}