Advertisement
rsidwell

crob.java

Jul 21st, 2017
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.78 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.fabs;
  20. import static org.jwildfire.base.mathlib.MathLib.pow;
  21.  
  22. import org.jwildfire.base.Tools;
  23. import org.jwildfire.create.tina.base.Layer;
  24. import org.jwildfire.create.tina.base.XForm;
  25. import org.jwildfire.create.tina.base.XYZPoint;
  26.  
  27. public class CrobFunc extends VariationFunc {
  28.   private static final long serialVersionUID = 1L;
  29.  
  30.   private static final String PARAM_TOP = "top";
  31.   private static final String PARAM_BOTTOM = "bottom";
  32.   private static final String PARAM_LEFT = "left";
  33.   private static final String PARAM_RIGHT = "right";
  34.   private static final String PARAM_BLUR = "blur";
  35.   private static final String PARAM_RATIOBLUR = "ratioBlur";
  36.   private static final String PARAM_DIRECTBLUR = "directBlur";
  37.  
  38.   private static final String[] paramNames = { PARAM_TOP, PARAM_BOTTOM, PARAM_LEFT, PARAM_RIGHT, PARAM_BLUR, PARAM_RATIOBLUR, PARAM_DIRECTBLUR };
  39.  
  40.   private double left = -1.0;
  41.   private double top = -1.0;
  42.   private double right = 1.0;
  43.   private double bottom = 1.0;
  44.   private int blur = 1;
  45.   private double ratioblur = 0.05;
  46.   private double directblur = 2.0;
  47.  
  48.   @Override
  49.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  50.       // crob by FractalDesire, https://fractaldesire.deviantart.com/art/crob-plugin-260173724
  51.  
  52.       double gradTmp, secTmp;
  53.       double xTmp = pAffineTP.x;
  54.       double yTmp = pAffineTP.y;
  55.      
  56.       if ((pAffineTP.x < left_border) || (pAffineTP.x > right_border) || (pAffineTP.y < top_border) || (pAffineTP.y > bottom_border)) {
  57.           //***** Case: no edge *****
  58.           if (blur == 0) {
  59.               xTmp = 0;
  60.               yTmp = 0;
  61.           }
  62.           //***** Case: with edge *****
  63.           else {
  64.               secTmp = pContext.random();
  65.              
  66.               // ***** Drawing top and bottom *****
  67.               if (secTmp < setProb) {
  68.                   do {
  69.                       yTmp = top + pContext.random()*yInt_2;
  70.                       xTmp = right - pow(pContext.random(), directblur)*ratioblur*minInt_2;
  71.                       gradTmp = (yTmp - y0c)/(xTmp - x0c);
  72.                   } while (gradTmp < -1.0);
  73.                  
  74.                   if (secTmp < setProbH) xTmp = left + right - xTmp;
  75.                   if ((secTmp > setProbQ) && (secTmp < setProbTQ)) yTmp = bottom + top - yTmp;
  76.               }
  77.              
  78.               // ***** Drawing left and right *****
  79.               else {
  80.                   do {
  81.                       xTmp = right - pContext.random()*xInt_2;
  82.                       yTmp = top + pow(pContext.random(), directblur)*ratioblur*minInt_2;
  83.                       gradTmp = (yTmp - y0c)/(xTmp - x0c);
  84.                   } while ((gradTmp <= 0.0) && (gradTmp > -1.0));
  85.                  
  86.                   if (secTmp > setCompProbH) yTmp = bottom + top - yTmp;
  87.                   if ((secTmp > setCompProbQ) && (secTmp < setCompProbTQ)) xTmp = left + right - xTmp;
  88.               }
  89.           }
  90.       }
  91.      
  92.       pVarTP.x += xTmp;
  93.       pVarTP.y += yTmp;
  94.       if (pContext.isPreserveZCoordinate()) {
  95.           pVarTP.z += pAmount * pAffineTP.z;
  96.     }
  97.   }
  98.  
  99.   @Override
  100.   public String[] getParameterNames() {
  101.     return paramNames;
  102.   }
  103.  
  104.   @Override
  105.   public Object[] getParameterValues() {
  106.     return new Object[] { top, bottom, left, right, blur, ratioblur, directblur };
  107.   }
  108.  
  109.   @Override
  110.   public void setParameter(String pName, double pValue) {
  111.     if (PARAM_LEFT.equalsIgnoreCase(pName))
  112.       left = pValue;
  113.     else if (PARAM_RIGHT.equalsIgnoreCase(pName))
  114.       right = pValue;
  115.     else if (PARAM_TOP.equalsIgnoreCase(pName))
  116.       top = pValue;
  117.     else if (PARAM_BOTTOM.equalsIgnoreCase(pName))
  118.       bottom = pValue;
  119.     else if (PARAM_BLUR.equalsIgnoreCase(pName))
  120.       blur = Tools.FTOI(pValue);
  121.     else if (PARAM_RATIOBLUR.equalsIgnoreCase(pName))
  122.       ratioblur = pValue;
  123.     else if (PARAM_DIRECTBLUR.equalsIgnoreCase(pName))
  124.       directblur = pValue;
  125.     else
  126.       throw new IllegalArgumentException(pName);
  127.   }
  128.  
  129.   @Override
  130.   public String getName() {
  131.     return "crob";
  132.   }
  133.  
  134.   private double top_border, bottom_border, left_border, right_border;
  135.   private double xInterval, yInterval, xInt_2, yInt_2, minInt_2;
  136.   private double x0, y0, x0c, y0c;
  137.   private double setProb, setProbH, setProbQ, setProbTQ, setCompProb, setCompProbH, setCompProbQ, setCompProbTQ;
  138.  
  139.   @Override
  140.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  141.       double tmp;
  142.  
  143.       // ***** Preventing bad inputs *****
  144.       if (top > bottom) {
  145.           tmp = top;
  146.           top = bottom;
  147.           bottom = tmp;
  148.       }
  149.       if (top == bottom) {
  150.           top = -1.0;
  151.           bottom = 1.0;
  152.       }
  153.       if (left > right) {
  154.           tmp = left;
  155.           left = right;
  156.           right = tmp;
  157.       }
  158.       if (left == right) {
  159.           left = -1.0;
  160.           right = 1.0;
  161.       }
  162.       if (directblur < 0.0) directblur = 0.0;
  163.      
  164.       // ***** Initializing intervals *****
  165.       if (blur != 0) blur = 1;
  166.       xInterval = fabs(right - left);
  167.       yInterval = fabs(bottom - top);
  168.       xInt_2 = xInterval/2.0;
  169.       yInt_2 = yInterval/2.0;
  170.       minInt_2 = (xInt_2 > yInt_2) ? yInt_2 : xInt_2;
  171.       x0 = right - xInt_2;
  172.       y0 = top + yInt_2;
  173.      
  174.       // ***** Initializing reference point *****
  175.       if (xInt_2 > yInt_2) {
  176.           x0c = right - minInt_2;
  177.           y0c = y0;
  178.       }
  179.       else if (xInt_2 < yInt_2) {
  180.           x0c = x0;
  181.           y0c = top + minInt_2;
  182.       }
  183.       else {
  184.           x0c = x0;
  185.           y0c = y0;
  186.       }
  187.      
  188.       // ***** Initializing probabilities *****
  189.       setProb = yInterval / (xInterval + yInterval);
  190.       setProbQ = 0.25 * setProb;
  191.       setProbH = 0.50 * setProb;
  192.       setProbTQ = 0.75 * setProb;
  193.       setCompProb = 1.0 - setProb;
  194.       setCompProbQ = setProb + 0.25 * setCompProb;
  195.       setCompProbH = setProb + 0.50 * setCompProb;
  196.       setCompProbTQ = setProb + 0.75 * setCompProb;
  197.      
  198.       // ***** Defining inner area *****
  199.       if (blur == 0) {
  200.           top_border = top;
  201.           bottom_border = bottom;
  202.           left_border = left;
  203.           right_border = right;
  204.       }
  205.       else {
  206.           top_border = top + minInt_2 * ratioblur;
  207.           bottom_border = bottom - minInt_2 * ratioblur;
  208.           left_border = left + minInt_2 * ratioblur;
  209.           right_border = right - minInt_2 * ratioblur;
  210.       }
  211.   }
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement