Advertisement
rsidwell

PreStabilizeFunc.java

Dec 13th, 2017
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 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 java.util.Random;
  20.  
  21. import org.jwildfire.base.Tools;
  22. import org.jwildfire.create.tina.base.Layer;
  23. import org.jwildfire.create.tina.base.XForm;
  24. import org.jwildfire.create.tina.base.XYZPoint;
  25.  
  26. public class PreStabilizeFunc extends VariationFunc {
  27.   private static final long serialVersionUID = 1L;
  28.  
  29.   private static final String PARAM_N = "n";
  30.   private static final String PARAM_SEED = "seed";
  31.   private static final String PARAM_P = "p";
  32.   private static final String PARAM_DC = "dc";
  33.   private static final String[] paramNames = { PARAM_N, PARAM_SEED, PARAM_P, PARAM_DC };
  34.  
  35.   private int n = 4;
  36.   private int seed = (int) (Math.random() * 100000);
  37.   private double p = 0.1;
  38.   private int dc = 0;
  39.   private Random rand = new Random();
  40.   private double[] x, y, c;
  41.   private boolean start = true;
  42.   private double nextColor, colorDelta;
  43.  
  44.   @Override
  45.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  46.     // pre_stabilize by Rick Sidwell, works as glitch repellent
  47.  
  48.     if (start || pContext.random() < p / 1000) {
  49.       start = false;
  50.       int i = pContext.random(n);
  51.       pAffineTP.x = x[i];
  52.       pAffineTP.y = y[i];
  53.       pAffineTP.z = 0;
  54.       if (dc != 0) pAffineTP.color = c[i];
  55.     }
  56.   }
  57.  
  58.   @Override
  59.   public String[] getParameterNames() {
  60.     return paramNames;
  61.   }
  62.  
  63.   @Override
  64.   public Object[] getParameterValues() {
  65.     return new Object[] { n, seed, p, dc };
  66.   }
  67.  
  68.   @Override
  69.   public void setParameter(String pName, double pValue) {
  70.     if (PARAM_N.equalsIgnoreCase(pName))
  71.       n = Tools.FTOI(pValue);
  72.     else if (PARAM_SEED.equalsIgnoreCase(pName))
  73.       seed = Tools.FTOI(pValue);
  74.     else if (PARAM_P.equalsIgnoreCase(pName))
  75.       p = pValue;
  76.     else if (PARAM_DC.equalsIgnoreCase(pName))
  77.       dc = limitIntVal(Tools.FTOI(pValue), 0, 1);
  78.     else
  79.       throw new IllegalArgumentException(pName);
  80.   }
  81.  
  82.   @Override
  83.   public String getName() {
  84.     return "pre_stabilize";
  85.   }
  86.  
  87.   @Override
  88.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  89.     if (n < 1)
  90.       n = 1;
  91.  
  92.     x = new double[n];
  93.     y = new double[n];
  94.     c = new double[n];
  95.  
  96.     rand.setSeed(seed);
  97.     nextColor = 0.5;
  98.     colorDelta = 1.0;
  99.  
  100.     for (int i = 0; i < n; i++) {
  101.       x[i] = rand.nextDouble() * 2 - 1;
  102.       y[i] = rand.nextDouble() * 2 - 1;
  103.       c[i] = nextColor;
  104.       nextColor += colorDelta;
  105.       if (nextColor >= 1) {
  106.           colorDelta /= 2;
  107.           nextColor = colorDelta / 2;
  108.       }
  109.     }
  110.  
  111.     start = true;
  112.  
  113.   }
  114.  
  115.   @Override
  116.   public int getPriority() {
  117.     return -1;
  118.   }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement