Advertisement
rsidwell

TruchetFillFunc.java

Aug 11th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 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.pow;
  21. import static org.jwildfire.base.mathlib.MathLib.round;
  22. import static org.jwildfire.base.mathlib.MathLib.fabs;
  23. import static org.jwildfire.base.mathlib.MathLib.fmod;
  24. import static org.jwildfire.base.mathlib.MathLib.floor;
  25.  
  26. import org.jwildfire.create.tina.base.Layer;
  27. import org.jwildfire.create.tina.base.XForm;
  28. import org.jwildfire.create.tina.base.XYZPoint;
  29.  
  30. public class TruchetFillFunc extends VariationFunc {
  31.   private static final long serialVersionUID = 1L;
  32.  
  33.   private static final String PARAM_EXPONENT = "exponent";
  34.   private static final String PARAM_ARC_WIDTH = "arc_width";
  35.   private static final String PARAM_SEED = "seed";
  36.  
  37.   private static final String[] paramNames = { PARAM_EXPONENT, PARAM_ARC_WIDTH, PARAM_SEED };
  38.  
  39.   private double pexponent, exponent = 2.0;
  40.   private double arc_width, width = 0.5;
  41.   private double seed, seed2 = 0.0;
  42.   private double onen = 0.5;
  43.  
  44.   @Override
  45.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  46.       exponent = pexponent > 2.0 ? 2.0 : pexponent < 0.001 ? 0.001 : pexponent;
  47.       onen = 1/exponent;
  48.       width = arc_width > 1.0 ? 1.0 : arc_width < 0.001 ? 0.001 : arc_width;
  49.       seed2 = sqrt(seed*1.5)/(seed*0.5)*0.25;
  50.   }
  51.  
  52.  @Override
  53.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  54.       /* by Tatyana Zabanova, https://tatasz.deviantart.com/art/Apophysis-Plugin-Pack-1-647321921 */
  55.       double rmax = 0.5 * ( pow(2.0, onen) -1.0) * width;
  56.       double scale = 1.0 / pAmount;
  57.       double modbase=65535.0;
  58.       double multiplier=32747.0;//1103515245;
  59.       double offset=12345.0;
  60.      
  61.       double x = pAffineTP.x*scale;
  62.       double y = pAffineTP.y*scale;
  63.       double intx = round(x);
  64.       double inty = round(y);
  65.      
  66.       double r = x - intx;
  67.       x = r < 0.0 ? 1.0 + r : r;
  68.       r = y - inty;
  69.       y = r < 0.0 ? 1.0 + r : r;
  70.      
  71.       double tiletype;
  72.       if (seed == 0)
  73.           tiletype = 0.0;
  74.       else if (seed == 1.0)
  75.           tiletype = 1.0;
  76.       else {
  77.           double xrand = pAffineTP.x;
  78.           double yrand = pAffineTP.y;
  79.           xrand = round(fabs(xrand)) * seed2;
  80.           yrand = round(fabs(yrand)) * seed2;
  81.          double niter = xrand + yrand + xrand*yrand;
  82.          double randint = (seed + niter) * seed2 * 0.5;
  83.          randint = fmod((randint * multiplier + offset), modbase);
  84.          tiletype = fmod(randint, 2.0);
  85.       }
  86.      
  87.       double r0, r1;
  88.       if (tiletype < 1.0) {  //Slow drawmode
  89.           r0 = pow((pow(fabs(x), exponent) + pow(fabs(y), exponent)), onen);
  90.           r1 = pow((pow(fabs(x-1.0), exponent) + pow(fabs(y-1.0), exponent)), onen);
  91.       }
  92.       else {
  93.           r0 = pow((pow(fabs(x-1.0), exponent) + pow(fabs(y), exponent)), onen);
  94.           r1 = pow((pow(fabs(x), exponent) + pow(fabs(y-1.0), exponent)), onen);
  95.       }
  96.      
  97.       double x1, y1;
  98.       double r00 = fabs(r0 - 0.5) / rmax;
  99.       if (r00 < 1.0) {
  100.           x1 = 2.0 *(x + floor(pAffineTP.x));
  101.           y1 = 2.0 *(y + floor(pAffineTP.y));
  102.       }
  103.       else {
  104.           x1 = 0.0;
  105.           y1 = 0.0;
  106.       }
  107.      
  108.       double r11 = fabs(r1 - 0.5) / rmax;
  109.       if (r11 < 1.0) {
  110.           pVarTP.x += x1 + 2.0 * (x + floor(pAffineTP.x)) - pAffineTP.x;
  111.           pVarTP.y += y1 + 2.0 * (y + floor(pAffineTP.y)) - pAffineTP.y;
  112.       }
  113.       else
  114.       {
  115.           pVarTP.x += x1 - pAffineTP.x;
  116.           pVarTP.y += y1 - pAffineTP.y;
  117.       }
  118.      
  119.       if (pContext.isPreserveZCoordinate()) pVarTP.z += pAmount * pAffineTP.z;
  120.   }
  121.  
  122.   @Override
  123.   public String[] getParameterNames() {
  124.     return paramNames;
  125.   }
  126.  
  127.   @Override
  128.   public Object[] getParameterValues() {
  129.     return new Object[] { pexponent, arc_width, seed };
  130.   }
  131.  
  132.   @Override
  133.   public void setParameter(String pName, double pValue) {
  134.     if (PARAM_EXPONENT.equalsIgnoreCase(pName))
  135.         pexponent = pValue;
  136.     else if (PARAM_ARC_WIDTH.equalsIgnoreCase(pName))
  137.         arc_width = pValue;
  138.     else if (PARAM_SEED.equalsIgnoreCase(pName))
  139.         seed = pValue;
  140.     else
  141.       throw new IllegalArgumentException(pName);
  142.   }
  143.  
  144.   @Override
  145.   public String getName() {
  146.     return "truchet_fill";
  147.   }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement