Advertisement
rsidwell

cpow2

Aug 27th, 2017
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 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.sin;
  21. import static org.jwildfire.base.mathlib.MathLib.cos;
  22. import static org.jwildfire.base.mathlib.MathLib.exp;
  23. import static org.jwildfire.base.mathlib.MathLib.log;
  24. import static org.jwildfire.base.mathlib.MathLib.floor;
  25.  
  26. import org.jwildfire.base.Tools;
  27. import org.jwildfire.create.tina.base.Layer;
  28. import org.jwildfire.create.tina.base.XForm;
  29. import org.jwildfire.create.tina.base.XYZPoint;
  30.  
  31. public class CPow2Func extends VariationFunc {
  32.   private static final long serialVersionUID = 1L;
  33.  
  34.   private static final String PARAM_R = "r";
  35.   private static final String PARAM_A = "a";
  36.   private static final String PARAM_DIVISOR = "divisor";
  37.   private static final String PARAM_RANGE = "range";
  38.  
  39.   private static final String[] paramNames = { PARAM_R, PARAM_A, PARAM_DIVISOR, PARAM_RANGE };
  40.  
  41.   private double p_r = 1.0;
  42.   private double p_a = 0.0;
  43.   private double divisor = 1;
  44.   private int range = 1;
  45.  
  46.   double ang, c, half_c, d, half_d, inv_range, full_range;
  47.  
  48.   @Override
  49.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  50.       ang = 2.0 * M_PI / divisor;
  51.       c = p_r * cos(M_PI / 2.0 * p_a) / divisor;
  52.       d = p_r * sin(M_PI / 2.0 * p_a) / divisor;
  53.       half_c = c / 2.0;
  54.       half_d = d / 2.0;
  55.       inv_range = 0.5 / range;
  56.       full_range = 2 * M_PI * range;
  57.   }
  58.      
  59.   @Override
  60.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  61.     /* cpow2 by Peter Sdobnov (Zueuk) */
  62.    
  63.     double a = pAffineTP.getPrecalcAtanYX();
  64.    
  65.     int n = pContext.random(range);
  66.     if (a < 0) n++;
  67.     a += 2 * M_PI * n;
  68.     if (cos(a * inv_range) < pContext.random() * 2.0 - 1.0)
  69.         a -= full_range;
  70.    
  71.     double lnr2 = log(pAffineTP.getPrecalcSumsq());  // logarithm * 2
  72.    
  73.     double r = pAmount * exp(half_c * lnr2 - d * a);
  74.     double th = c * a + half_d * lnr2 + ang * floor(divisor * pContext.random());
  75.    
  76.     pVarTP.x += r * cos(th);
  77.     pVarTP.y += r * sin(th);
  78.    
  79.     if (pContext.isPreserveZCoordinate()) pVarTP.z += pAmount * pAffineTP.z;
  80.  
  81.   }
  82.  
  83.   @Override
  84.   public String[] getParameterNames() {
  85.     return paramNames;
  86.   }
  87.  
  88.   @Override
  89.   public Object[] getParameterValues() {
  90.     return new Object[] { p_r, p_a, divisor, range };
  91.   }
  92.  
  93.   @Override
  94.   public void setParameter(String pName, double pValue) {
  95.     if (PARAM_R.equalsIgnoreCase(pName))
  96.       p_r = pValue;
  97.     else if (PARAM_A.equalsIgnoreCase(pName))
  98.       p_a = pValue;
  99.     else if (PARAM_DIVISOR.equalsIgnoreCase(pName))
  100.       divisor = (pValue == 0) ? 1 : pValue;
  101.     else if (PARAM_RANGE.equalsIgnoreCase(pName))
  102.       range = (pValue < 1)? 1 : Tools.FTOI(pValue);
  103.     else
  104.       throw new IllegalArgumentException(pName);
  105.   }
  106.  
  107.   @Override
  108.   public String[] getParameterAlternativeNames() {
  109.     return new String[] { "cpow_r", "cpow_a", "cpow_divisor", "cpow_spread" };
  110.   }
  111.  
  112.   @Override
  113.   public String getName() {
  114.     return "cpow2";
  115.   }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement