Advertisement
rsidwell

DCDModulusFunc.java

Jan 22nd, 2018
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.45 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.  
  18. /**
  19.  * Double modulus IFS
  20.  * @author Jesus Sosa
  21.  * @date January 18, 2018
  22.  * based on modulus used as a IFS:
  23.  *
  24.  * Modifications by Brad Stefanov and Rick Sidwell
  25.  *
  26.  */
  27. package org.jwildfire.create.tina.variation;
  28.  
  29. import static org.jwildfire.base.mathlib.MathLib.fmod;
  30.  
  31.  
  32. import org.jwildfire.base.Tools;
  33. import org.jwildfire.base.mathlib.MathLib;
  34. import org.jwildfire.create.tina.base.Layer;
  35. import org.jwildfire.create.tina.base.XForm;
  36. import org.jwildfire.create.tina.base.XYZPoint;
  37.  
  38. public class DCDModulusFunc extends VariationFunc {
  39.     private static final long serialVersionUID = 1L;
  40.  
  41.     private static final String PARAM_X = "x";
  42.     private static final String PARAM_Y = "y";
  43.     private static final String PARAM_ANGLE = "angle";
  44.     private static final String PARAM_SHIFTX = "shiftx";
  45.     private static final String PARAM_SHIFTY = "shifty";
  46.     private static final String PARAM_COLOR1 = "color1";
  47.     private static final String PARAM_SPEED1 = "speed1";
  48.     private static final String PARAM_COLOR2 = "color2";
  49.     private static final String PARAM_SPEED2 = "speed2";
  50.     private static final String PARAM_SQUARE = "square";
  51.  
  52.     private static final String[] paramNames = { PARAM_X, PARAM_Y, PARAM_ANGLE,PARAM_SHIFTX, PARAM_SHIFTY,
  53.             PARAM_COLOR1, PARAM_SPEED1, PARAM_COLOR2, PARAM_SPEED2, PARAM_SQUARE };
  54.  
  55.     private double x = 2.0;
  56.     private double y = 2.0;
  57.     private double shiftx = 0.0;
  58.     private double shifty = 0.0;
  59.     private double color1 = 0;
  60.     private double color2 = 1;
  61.     private double speed1 = 0;
  62.     private double speed2 = 0;
  63.     private double angle = 45.0;
  64.     private int  square =  1;
  65.     private double coscos, sincos, sinsin, rangle;
  66.  
  67.     @Override
  68.     public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP,
  69.             double pAmount) {
  70.         double xnew, ynew, xt, yt;
  71.  
  72.         // rangle = MathLib.M_PI*angle/180.0;
  73.         // double w1,w2=Math.pow(Math.tan(rangle),2.0)*0.5;
  74.  
  75.         double p2 = coscos / (coscos + sinsin);
  76.         double p1 = sinsin / (coscos + sinsin);
  77.  
  78.         /* Modulus in the Apophysis Plugin Pack */
  79.         if (pAffineTP.x > x) {
  80.             xnew = (-x + fmod(pAffineTP.x + x, _xr));
  81.         } else if (pAffineTP.x < -x) {
  82.             xnew = (x - fmod(x - pAffineTP.x, _xr));
  83.         } else {
  84.             xnew = pAffineTP.x;
  85.         }
  86.  
  87.         if (pAffineTP.y > y) {
  88.             ynew = (-y + fmod(pAffineTP.y + y, _yr));
  89.         } else if (pAffineTP.y < -y) {
  90.             ynew = (y - fmod(y - pAffineTP.y, _yr));
  91.         } else {
  92.             ynew = pAffineTP.y;
  93.         }
  94.  
  95.         double p = pContext.random();
  96.  
  97.         if (angle <= 45.0) {
  98.             if (p < p2) {
  99.                 xt = xnew * coscos + ynew * sincos + x;
  100.                 yt = -xnew * sincos + ynew * coscos - y;
  101.                 newColor = oldColor * c11 + c12;
  102.             } else {
  103.                 xt = xnew * sincos + ynew * sinsin;
  104.                 yt = -xnew * sinsin + ynew * sincos;
  105.                 newColor = oldColor * c21 + c22;
  106.             }
  107.         } else {
  108.             if (p < p1) {
  109.                 xt = xnew * sincos + ynew * sinsin;
  110.                 yt = -xnew * sinsin + ynew * sincos;
  111.                 newColor = oldColor * c21 + c22;
  112.             } else {
  113.                 xt = xnew * coscos + ynew * sincos + x;
  114.                 yt = -xnew * sincos + ynew * coscos - y;
  115.                 newColor = oldColor * c11 + c12;
  116.             }
  117.         }
  118.        
  119.         xt += shiftx;
  120.         yt += shifty;
  121.        
  122.         if (square == 0) {
  123.             pVarTP.x += pAmount * xt;
  124.             pVarTP.y += pAmount * yt;        
  125.         }
  126.         else {
  127.         if (xt > x) {
  128.             pVarTP.x += pAmount * (-x + fmod(xt + x, _xr));
  129.           }
  130.           else if (xt < -x) {
  131.             pVarTP.x += pAmount * (x - fmod(x - xt, _xr));
  132.           }
  133.           else {
  134.             pVarTP.x += pAmount * xt;
  135.           }
  136.  
  137.           if (yt > y) {
  138.             pVarTP.y += pAmount * (-y + fmod(yt + y, _yr));
  139.           }
  140.           else if (yt < -y) {
  141.             pVarTP.y += pAmount * (y - fmod(y - yt, _yr));
  142.           }
  143.           else {
  144.             pVarTP.y += pAmount * yt;
  145.           }
  146.         }
  147.  
  148.         if (pContext.isPreserveZCoordinate()) {
  149.             pVarTP.z += pAmount * pAffineTP.z;
  150.         }
  151.  
  152.         pVarTP.color = fmod(newColor, 1);
  153.         oldColor = newColor;
  154.     }
  155.  
  156.     @Override
  157.     public String[] getParameterNames() {
  158.         return paramNames;
  159.     }
  160.  
  161.     @Override
  162.     public Object[] getParameterValues() {
  163.         return new Object[] { x, y, angle, shiftx, shifty, color1, speed1, color2, speed2, square };
  164.     }
  165.  
  166.     @Override
  167.     public void setParameter(String pName, double pValue) {
  168.         if (PARAM_X.equalsIgnoreCase(pName))
  169.             x = pValue;
  170.         else if (PARAM_Y.equalsIgnoreCase(pName))
  171.             y = pValue;
  172.         else if (PARAM_ANGLE.equalsIgnoreCase(pName)) {
  173.             angle = pValue;
  174.             rangle = MathLib.M_PI * angle / 180.0;
  175.             coscos = MathLib.cos(rangle) * MathLib.cos(rangle);
  176.             sinsin = MathLib.sin(rangle) * MathLib.sin(rangle);
  177.             sincos = MathLib.sin(rangle) * MathLib.cos(rangle);
  178.         }
  179.         else if (PARAM_SHIFTX.equalsIgnoreCase(pName))
  180.             shiftx = pValue;
  181.         else if (PARAM_SHIFTY.equalsIgnoreCase(pName))
  182.             shifty = pValue;
  183.         else if (PARAM_COLOR1.equalsIgnoreCase(pName))
  184.             color1 = pValue;
  185.         else if (PARAM_SPEED1.equalsIgnoreCase(pName))
  186.             speed1 = pValue;
  187.         else if (PARAM_COLOR2.equalsIgnoreCase(pName))
  188.             color2 = pValue;
  189.         else if (PARAM_SPEED2.equalsIgnoreCase(pName))
  190.             speed2 = pValue;
  191.         else if (PARAM_SQUARE.equalsIgnoreCase(pName))
  192.             square = limitIntVal(Tools.FTOI(pValue), 0, 1);
  193.         else
  194.             throw new IllegalArgumentException(pName);
  195.     }
  196.  
  197.     @Override
  198.     public String getName() {
  199.         return "dc_dmodulus";
  200.     }
  201.  
  202.     private double _xr, _yr, c11, c12, c21, c22, oldColor, newColor;
  203.  
  204.     @Override
  205.     public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  206.         _xr = 2.0 * x;
  207.         _yr = 2.0 * y;
  208.  
  209.         c11 = (1 + speed1) / 2;
  210.         c12 = color1 * (1 - speed1) / 2;
  211.         c21 = (1 + speed2) / 2;
  212.         c22 = color2 * (1 - speed2) / 2;
  213.        
  214.         oldColor = 0.5;
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement