Advertisement
rsidwell

DCGnarlyFunc.java

Feb 5th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.73 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.M_PI;
  20. import static org.jwildfire.base.mathlib.MathLib.fabs;
  21. import static org.jwildfire.base.mathlib.MathLib.fmod;
  22. import static org.jwildfire.base.mathlib.MathLib.sin;
  23. import static org.jwildfire.base.mathlib.MathLib.cos;
  24. import static org.jwildfire.base.mathlib.MathLib.tan;
  25. import static org.jwildfire.base.mathlib.MathLib.sqr;
  26. import static org.jwildfire.base.mathlib.MathLib.sqrt;
  27.  
  28. import org.jwildfire.base.Tools;
  29.  
  30. import static org.jwildfire.base.mathlib.MathLib.log;
  31.  
  32. import org.jwildfire.create.tina.base.Layer;
  33. import org.jwildfire.create.tina.base.XForm;
  34. import org.jwildfire.create.tina.base.XYZPoint;
  35.  
  36. public class DCGnarlyFunc extends VariationFunc {
  37.     private static final long serialVersionUID = 1L;
  38.  
  39.     private static final String PARAM_MODE = "mode";
  40.     private static final String PARAM_SCALEX = "scalex";
  41.     private static final String PARAM_SCALEY = "scaley";
  42.     private static final String PARAM_FREQX1 = "freqx1";
  43.     private static final String PARAM_FREQY1 = "freqy1";
  44.     private static final String PARAM_FREQX2 = "freqx2";
  45.     private static final String PARAM_FREQY2 = "freqy2";
  46.     private static final String PARAM_FREQX3 = "freqx3";
  47.     private static final String PARAM_FREQY3 = "freqy3";
  48.     private static final String PARAM_DC = "dc";
  49.     private static final String PARAM_COLOR1 = "color1";
  50.     private static final String PARAM_COLOR2 = "color2";
  51.     private static final String PARAM_FMAG = "fmag";
  52.     private static final String PARAM_DISTORT = "distort";
  53.     private static final String PARAM_BLUR = "blur";
  54.       private static final String PARAM_SCALEZ = "scale_z";
  55.       private static final String PARAM_OFFSETZ = "offset_z";
  56.       private static final String PARAM_RESETZ = "reset_z";
  57.    
  58.     private static final String[] paramNames = { PARAM_MODE, PARAM_SCALEX, PARAM_SCALEY, PARAM_FREQX1, PARAM_FREQY1,
  59.             PARAM_FREQX2, PARAM_FREQY2, PARAM_FREQX3, PARAM_FREQY3, PARAM_DC, PARAM_COLOR1, PARAM_COLOR2,
  60.             PARAM_FMAG, PARAM_DISTORT, PARAM_BLUR, PARAM_SCALEZ, PARAM_OFFSETZ, PARAM_RESETZ };
  61.  
  62.     private int mode = 1;
  63.     private double scalex = 0.05;
  64.     private double scaley = 0.05;
  65.     private double freqx1 = 3.5;
  66.     private double freqy1 = 3.5;
  67.     private double freqx2 = 2;
  68.     private double freqy2 = 2;
  69.     private double freqx3 = 5;
  70.     private double freqy3 = 5;
  71.     private int dc = 1;
  72.     private double color1 = 5;
  73.     private double color2 = 5;
  74.     private double fmag = 1;
  75.     private double distort = 0.0;
  76.     private double blur = 5.0;
  77.     private double scale_z = 0.0;
  78.     private double offset_z = 0.0;
  79.     private int reset_z = 0;
  80.    
  81.     private final double gauss_rnd[] = new double[6];
  82.     private int gauss_N;
  83.  
  84.     @Override
  85.     public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  86.         // dc_gnarly by Brad Stefanov and Rick Sidwell
  87.         // inspired by techniques and formulas by Tatyana Zabanova, Georg Kiehne, Clifford Pickover, and Mark Townsend
  88.  
  89.         double x0 = pAffineTP.x;
  90.         double y0 = pAffineTP.y;
  91.  
  92.         if (blur > 0) {
  93.           double r = pContext.random() * 2 * M_PI;
  94.           double sina = sin(r);
  95.           double cosa = cos(r);
  96.           r = blur * (gauss_rnd[0] + gauss_rnd[1] + gauss_rnd[2] + gauss_rnd[3] + gauss_rnd[4] + gauss_rnd[5] - 3);
  97.           gauss_rnd[gauss_N] = pContext.random();
  98.           gauss_N = (gauss_N + 1) & 5;
  99.  
  100.           x0 += r * cosa;
  101.           y0 += r * sina;
  102.         }
  103.         else if (blur < 0) {
  104.           x0 += pContext.random() * blur * 2 - blur;
  105.           y0 += pContext.random() * blur * 2 - blur;
  106.         }
  107.  
  108.         double x1, y1;
  109.         switch (mode) {
  110.         case 1:
  111.             x1 = cos(freqx1 * y0 + sin(freqx2 * (y0 + sin(freqx3 * y0)))) * scalex;
  112.             y1 = cos(freqy1 * x0 + sin(freqy2 * (x0 + sin(freqy3 * x0)))) * scaley;
  113.             break;
  114.         case 2:
  115.             x1 = sin(freqx1 * y0 + sin(freqx2 * (y0 + cos(freqx3 * y0)))) * scalex;
  116.             y1 = sin(freqy1 * x0 + sin(freqy2 * (x0 + cos(freqy3 * x0)))) * scaley;
  117.             break;
  118.         case 3:
  119.             x1 = cos(freqx1 * y0 + sin(freqx2 * (x0 + sin(freqx3 * y0)))) * scalex;
  120.             y1 = cos(freqy1 * x0 + sin(freqy2 * (y0 + sin(freqy3 * x0)))) * scaley;
  121.             break;
  122.         case 4:
  123.             x1 = sin(freqx1 * y0 + sin(sqrt(fabs(freqx2 * (y0 + cos(freqx3 * y0)))))) * scalex;
  124.             y1 = sin(freqy1 * x0 + sin(sqrt(fabs(freqy2 * (x0 + cos(freqy3 * x0)))))) * scaley;
  125.             break;
  126.         case 5:
  127.             x1 = cos(freqx1 * y0 + asinh(freqx2 * (y0 + sin(freqx3 * y0)))) * scalex;
  128.             y1 = cos(freqy1 * x0 + asinh(freqy2 * (x0 + sin(freqy3 * x0)))) * scaley;
  129.             break;
  130.         case 6:
  131.             x1 = cos(freqx1 * y0 + tan(freqx2 * (y0 + sin(freqx3 * y0)))) * scalex;
  132.             y1 = cos(freqy1 * x0 + tan(freqy2 * (x0 + sin(freqy3 * x0)))) * scaley;
  133.             break;
  134.         case 7:
  135.             x1 = sin(freqx1 * y0 + sin(sqr(freqx2 * (y0 + cos(freqx3 * y0))))) * scalex;
  136.             y1 = sin(freqy1 * x0 + sin(sqr(freqy2 * (x0 + cos(freqy3 * x0))))) * scaley;
  137.             break;
  138.         case 8:
  139.             x1 = sin(freqx1 * y0 + sin(sqr(freqx2 * (x0 + cos(freqx3 * y0))))) * scalex;
  140.             y1 = sin(freqy1 * x0 + sin(sqr(freqy2 * (y0 + cos(freqy3 * x0))))) * scaley;
  141.             break;
  142.         default:
  143.             x1 = sin(freqx1 * y0) * scalex;
  144.             y1 = sin(freqy1 * x0) * scaley;
  145.             break;
  146.         }
  147.        
  148.         double s1 = log(sqrt(sqr(x1) + sqr(y1))) * distort;
  149.  
  150.         pVarTP.x += pAmount * (x0 + x1) / 2;
  151.         pVarTP.y += pAmount * (y0 + y1) / 2;
  152.        
  153.         if (dc != 0) {
  154.             pVarTP.color = fmod(fabs(cos(x0 + x1 * color1 + s1) + sin(y0 + y1 * color2 + s1)), fmag);
  155.         }
  156.        
  157.         double dz = pVarTP.color * scale_z + offset_z;
  158.         if (reset_z > 0) {
  159.           pVarTP.z = dz;
  160.         }
  161.         else {
  162.           pVarTP.z += dz;
  163.         }
  164.     }
  165.  
  166.     @Override
  167.     public String[] getParameterNames() {
  168.         return paramNames;
  169.     }
  170.  
  171.     @Override
  172.     public Object[] getParameterValues() {
  173.         return new Object[] { mode, scalex, scaley, freqx1, freqy1, freqx2, freqy2, freqx3, freqy3,
  174.                 dc, color1, color2, fmag, distort, blur, scale_z, offset_z, reset_z };
  175.     }
  176.  
  177.     @Override
  178.     public void setParameter(String pName, double pValue) {
  179.         if (PARAM_MODE.equalsIgnoreCase(pName))
  180.             mode = Tools.limitValue(Tools.FTOI(pValue), 0, 8);
  181.         else if (PARAM_SCALEX.equalsIgnoreCase(pName))
  182.             scalex = pValue;
  183.         else if (PARAM_SCALEY.equalsIgnoreCase(pName))
  184.             scaley = pValue;
  185.         else if (PARAM_FREQX1.equalsIgnoreCase(pName))
  186.             freqx1 = pValue;
  187.         else if (PARAM_FREQY1.equalsIgnoreCase(pName))
  188.             freqy1 = pValue;
  189.         else if (PARAM_FREQX2.equalsIgnoreCase(pName))
  190.             freqx2 = pValue;
  191.         else if (PARAM_FREQY2.equalsIgnoreCase(pName))
  192.             freqy2 = pValue;
  193.         else if (PARAM_FREQX3.equalsIgnoreCase(pName))
  194.             freqx3 = pValue;
  195.         else if (PARAM_FREQY3.equalsIgnoreCase(pName))
  196.             freqy3 = pValue;
  197.         else if (PARAM_DC.equalsIgnoreCase(pName))
  198.             dc = Tools.limitValue(Tools.FTOI(pValue), 0, 1);
  199.         else if (PARAM_COLOR1.equalsIgnoreCase(pName))
  200.             color1 = pValue;
  201.         else if (PARAM_COLOR2.equalsIgnoreCase(pName))
  202.             color2 = pValue;
  203.         else if (PARAM_FMAG.equalsIgnoreCase(pName))
  204.             fmag = Tools.limitValue(pValue, 0.05, 1);
  205.         else if (PARAM_DISTORT.equalsIgnoreCase(pName))
  206.             distort = pValue;
  207.         else if (PARAM_BLUR.equalsIgnoreCase(pName))
  208.             blur = pValue;
  209.         else if (PARAM_SCALEZ.equalsIgnoreCase(pName))
  210.             scale_z = pValue;
  211.           else if (PARAM_OFFSETZ.equalsIgnoreCase(pName))
  212.             offset_z = pValue;
  213.           else if (PARAM_RESETZ.equalsIgnoreCase(pName))
  214.             reset_z = Tools.limitValue(Tools.FTOI(pValue), 0, 1);
  215.         else
  216.             throw new IllegalArgumentException(pName);
  217.     }
  218.  
  219.     @Override
  220.     public String getName() {
  221.         return "dc_gnarly";
  222.     }
  223.  
  224.     @Override
  225.     public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  226.  
  227.  
  228.         gauss_rnd[0] = pContext.random();
  229.         gauss_rnd[1] = pContext.random();
  230.         gauss_rnd[2] = pContext.random();
  231.         gauss_rnd[3] = pContext.random();
  232.         gauss_rnd[4] = pContext.random();
  233.         gauss_rnd[5] = pContext.random();
  234.         gauss_N = 0;
  235.     }
  236.    
  237.     public static double asinh(double x) {
  238.         return log(x + sqrt(x * x + 1.0));
  239.     }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement