Advertisement
rsidwell

EllipticAltFunc.java

Nov 1st, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. /*
  2.   JWildfire - an image and animation processor written in Java
  3.   Copyright (C) 1995-2011 Andreas Maschke
  4.  
  5.   This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
  6.   General Public License as published by the Free Software Foundation; either version 2.1 of the
  7.   License, or (at your option) any later version.
  8.  
  9.   This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  10.   even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11.   Lesser General Public License for more details.
  12.  
  13.   You should have received a copy of the GNU Lesser General Public License along with this software;
  14.   if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  15.   02110-1301 USA, or see the FSF site: http://www.fsf.org.
  16. */
  17. package org.jwildfire.create.tina.variation;
  18.  
  19. import static org.jwildfire.base.mathlib.MathLib.sqrt;
  20. import static org.jwildfire.base.mathlib.MathLib.min;
  21. import static org.jwildfire.base.mathlib.MathLib.max;
  22. import static org.jwildfire.base.mathlib.MathLib.asin;
  23.  
  24. import org.jwildfire.create.tina.base.XForm;
  25. import org.jwildfire.create.tina.base.XYZPoint;
  26.  
  27. public class EllipticAltFunc extends SimpleVariationFunc {
  28.   private static final long serialVersionUID = 1L;
  29.  
  30.   private double sqrt1pm1(double x) {
  31.     if (-0.0625 < x && x < 0.0625)
  32.     {
  33.       // [4,4] Pade approximant to degree 8 truncated Taylor series of sqrt(x+1)-1 about 0
  34.       // computed with a Wolfram Alpha Open Code notebook
  35.       // accurate to machine precision within this range?
  36.       double num = 0;
  37.       double den = 0;
  38.       num += 1.0 / 32.0;
  39.       den += 1.0 / 256.0;
  40.       num *= x;
  41.       den *= x;
  42.       num += 5.0 / 16.0;
  43.       den += 5.0 / 32.0;
  44.       num *= x;
  45.       den *= x;
  46.       num += 3.0 / 4.0;
  47.       den += 15.0 / 16.0;
  48.       num *= x;
  49.       den *= x;
  50.       num += 1.0 / 2.0;
  51.       den += 7.0 / 4.0;
  52.       num *= x;
  53.       den *= x;
  54.       den += 1.0;
  55.       return num / den;
  56.     }
  57.     return sqrt(1 + x) - 1;
  58.   }
  59.  
  60.   @Override
  61.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  62.       /* Alternate elliptic implementation by claude on discord fractal chat.
  63.        * It has better precision when x is near ±1 and y is near 0.
  64.        */
  65.       double x2 = 2.0 * pAffineTP.x;
  66.       double sq = pAffineTP.x * pAffineTP.x + pAffineTP.y * pAffineTP.y;
  67.       double u = sq + x2;
  68.       double v = sq - x2;
  69.       double xmaxm1 = 0.5 * (sqrt1pm1(u) + sqrt1pm1(v));
  70.       double a = pAffineTP.x / (1 + xmaxm1);
  71.       double ssx = xmaxm1;
  72.       double w = pAmount / 1.5707963267948966;
  73.       if (ssx<0) ssx = 0; else ssx = sqrt(ssx);
  74.       pVarTP.x = w * asin(max(-1, min(1, a))); //asin(clamp(a, -1, 1));
  75.       if (pAffineTP.y > 0)
  76.         pVarTP.y =  w * Math.log1p(xmaxm1 + ssx);
  77.       else
  78.         pVarTP.y = -w * Math.log1p(xmaxm1 + ssx);
  79.      
  80.     if (pContext.isPreserveZCoordinate()) pVarTP.z += pAmount * pAffineTP.z;
  81. }
  82.  
  83.   @Override
  84.   public String getName() {
  85.     return "ellipticAlt";
  86.   }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement