Advertisement
rsidwell

FiveFoldFunc.java

Oct 4th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.97 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.  
  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.fmod;
  23. import static org.jwildfire.base.mathlib.MathLib.M_PI;
  24. import static org.jwildfire.base.mathlib.MathLib.M_2PI;
  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. import org.jwildfire.create.tina.random.AbstractRandomGenerator;
  31.  
  32. public class FiveFoldFunc extends VariationFunc {
  33.   private static final long serialVersionUID = 1L;
  34.  
  35.   private static final String PARAM_T = "t";
  36.   private static final String PARAM_COL1 = "col1";
  37.   private static final String PARAM_COL2 = "col2";
  38.   private static final String PARAM_ON = "on";
  39.   private static final String PARAM_ITT = "itt";
  40.   private static final String PARAM_EQ = "eq";
  41.   private static final String PARAM_DEPTH = "depth";
  42.   private static final String PARAM_ON1 = "on1";
  43.   private static final String PARAM_ITT1 = "itt1";
  44.   private static final String PARAM_EQ1 = "eq1";
  45.   private static final String PARAM_DEPTH1 = "depth1";
  46.   private static final String PARAM_ON2 = "on2";
  47.   private static final String PARAM_ITT2 = "itt2";
  48.   private static final String PARAM_EQ2 = "eq2";
  49.   private static final String PARAM_DEPTH2 = "depth2";
  50.  
  51.   private static final String[] paramNames = { PARAM_T, PARAM_COL1, PARAM_COL2, PARAM_ON, PARAM_ITT, PARAM_EQ, PARAM_DEPTH,
  52.                 PARAM_ON1, PARAM_ITT1, PARAM_EQ1, PARAM_DEPTH1, PARAM_ON2, PARAM_ITT2, PARAM_EQ2, PARAM_DEPTH2 };
  53.  
  54.   private int t = 2;
  55.   private double col1 = Math.round(Math.random() * 35 + 10) / 100.0, col2 = Math.round(Math.random() * 35 + 55) / 100.0;
  56.   private double on = Math.round(Math.random()), on1 = Math.round(Math.random()), on2 = Math.round(Math.random());
  57.   private int itt = (int) (Math.random() * 5 + 1), itt1 = (int) (Math.random() * 5), itt2 = (int) (Math.random() * 5);
  58.   private int eq = (int) (Math.random() * 2 + 1), eq1 = (int) (Math.random() * 2 + 1), eq2 = (int) (Math.random() * 2 + 1);
  59.   private int depth = (int) (Math.random() * 4), depth1 = (int) (Math.random() * 4), depth2 = (int) (Math.random() * 4);
  60.  
  61.   private class triangle {
  62.       int type;
  63.       double x1, y1, x2, y2, x3, y3;
  64.       double col;
  65.      
  66.       public void assign(triangle t) {
  67.           type = t.type; col = t.col;
  68.           x1 = t.x1; y1 = t.y1;
  69.           x2 = t.x2; y2 = t.y2;
  70.           x3 = t.x3; y3 = t.y3;
  71.       }
  72.      
  73.       private void triBlur(XYZPoint p) {
  74.           double u, v, v1, v2, w, x, x0, x1, x2, x3, x4, x5, x6, y, y0, y1, y2, y3, y4, y5, y6;
  75.          
  76.           if (this.x1 <= this.x2 && this.x1 <= this.x3) {
  77.               if (this.x2 <= this.x3) {
  78.                   x1 = this.x1; y1 = this.y1;
  79.                   x2 = this.x2; y2 = this.y2;
  80.                   x3 = this.x3; y3 = this.y3;
  81.               }
  82.               else {
  83.                   x1 = this.x1; y1 = this.y1;
  84.                   x2 = this.x3; y2 = this.y3;
  85.                   x3 = this.x2; y3 = this.y2;
  86.               }      
  87.           }
  88.           else if (this.x2 <= this.x1 && this.x2 <= this.x3){
  89.               if (this.x1 <= this.x3) {
  90.                   x1 = this.x2; y1 = this.y2;
  91.                   x2 = this.x1; y2 = this.y1;
  92.                   x3 = this.x3; y3 = this.y3;
  93.               }
  94.               else {
  95.                   x1 = this.x2; y1 = this.y2;
  96.                   x2 = this.x3; y2 = this.y3;
  97.                   x3 = this.x1; y3 = this.y1;
  98.               }
  99.           }
  100.           else {
  101.               if (this.x1 <= this.x2) {
  102.                   x1 = this.x3; y1 = this.y3;
  103.                   x2 = this.x1; y2 = this.y1;
  104.                   x3 = this.x2; y3 = this.y2;
  105.               }
  106.               else {
  107.                   x1 = this.x3; y1 = this.y3;
  108.                   x2 = this.x2; y2 = this.y2;
  109.                   x3 = this.x1; y3 = this.y1;
  110.               }
  111.           }
  112.          
  113.           double r1 = genRand.random(), r2 = genRand.random();
  114.          
  115.           if (x1 == x2 && x2 == x3) {
  116.               u = x1;
  117.               if (y1 <= y2 && y1 <= y3) {
  118.                   v1 = y1;
  119.                   v2 = (y3 > y2) ? y2 : y3;
  120.               }
  121.               else if (y2 <= y1 && y2 <= y3) {
  122.                   v1 = y2;
  123.                   v2 = (y3 > y1) ? y1 : y3;
  124.               }
  125.               else {
  126.                   v1 = y3;
  127.                   v2 = (y2 > y1) ? y1 : y2;
  128.               }
  129.               v = v1 + r1 * (v2 - v1);
  130.           }
  131.           else if (x1 == x2) {
  132.               x4 = (x1 + x3) / 2; y4 = (y1 + y3) / 2;
  133.               x5 = x3 + x2 - x4; y5 = y3 + y2 - y4;
  134.               x6 = x1 + x2 - x4; y6 = y1 + y2 - y4;
  135.              
  136.               x = x3 + r1 * (x4 - x3); y = y3 + r1 * (y4 - y3);
  137.               x0 = x5 + r1 * (x2 - x5); y0 = y5 + r1 * (y2 - y5);
  138.               u = x + r2 * (x0 - x); v = y + r2 * (y0 - y);
  139.               w = y3 + (y2 - y3) / (x2 - x3) * (u - x3);
  140.                  
  141.               if (y2 > y3 + (y3 - y1) / (x3 - x1) * (x2 - x3)) {
  142.                   if (v > w) {
  143.                       x = x4 + r1 * (x1 - x4); y = y4 + r1 * (y1 - y4);
  144.                       x0 = x2 + r1 * (x6 - x2); y0 = y2 + r1 * (y6 - y2);
  145.                       u = x0 + r2 * (x - x0); v = y0 + r2 * (y - y0);
  146.                   }
  147.               }
  148.               else {
  149.                   if (v < w) {
  150.                       x = x4 + r1 * (x1 - x4); y = y4 + r1 * (y1 - y4);
  151.                       x0 = x2 + r1 * (x6 - x2); y0 = y2 + r1 * (y6 - y2);
  152.                       u = x0 + r2 * (x - x0); v = y0 + r2 * (y - y0);
  153.                   }
  154.               }
  155.           }
  156.           else {
  157.               x4 = (x1 + x3) / 2; y4 = (y1 + y3) / 2;
  158.               x5 = x1 + x2 - x4; y5 = y1 + y2 - y4;
  159.               x6 = x3 + x2 - x4; y6 = y3 + y2 - y4;
  160.              
  161.               x = x1 + r1 * (x4 - x1); y = y1 + r1 * (y4 - y1);
  162.               x0 = x5 + r1 * (x2 - x5); y0 = y5 + r1 * (y2 - y5);
  163.               u = x + r2 * (x0 - x); v = y + r2 * (y0 - y);
  164.               w = y1 + (y2 - y1) / (x2 - x1) * (u - x1);
  165.              
  166.               if (y2 > y1 + (y3 - y1) / (x3 - x1) * (x2 - x1)) {
  167.                   if (v > w) {
  168.                       x = x4 + r1 * (x3 - x4); y = y4 + r1 * (y3 - y4);
  169.                       x0 = x2 + r1 * (x6 - x2); y0 = y2 + r1 * (y6 - y2);
  170.                       u = x0 + r2 * (x - x0); v = y0 + r2 * (y - y0);
  171.                   }
  172.               }
  173.               else {
  174.                   if (v < w) {
  175.                       x = x4 + r1 * (x3 - x4); y = y4 + r1 * (y3 - y4);
  176.                       x0 = x2 + r1 * (x6 - x2); y0 = y2 + r1 * (y6 - y2);
  177.                       u = x0 + r2 * (x - x0); v = y0 + r2 * (y - y0);                
  178.                   }
  179.               }
  180.           }
  181.          
  182.           if (x1 == x2 && y1 == y2) {
  183.               u = x1 + r1 * (x3 - x1); v = y1 + r1 * (y3 - y1);      
  184.           }
  185.           if (x1 == x3 && y1 == y3) {
  186.               u = x1 + r1 * (x2 - x1); v = y1 + r1 * (y2 - y1);
  187.           }
  188.           if (x2 == x3 && y2 == y3) {
  189.               u = x1 + r1 * (x2 - x1); v = y1 + r1 * (y2 - y1);
  190.           }
  191.          
  192.           p.x = u;
  193.           p.y = v;
  194.           p.z = 0;   
  195.       }
  196.   }
  197.  
  198.   private triangle orig, t1, t2;
  199.   private XYZPoint p;
  200.   private double alfa;
  201.   AbstractRandomGenerator genRand;
  202.  
  203.   private double discretNoise(int x) {
  204.       int n = x;
  205.       n = (n << 13) ^ n;
  206.       return ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff)  / (double) 0x7fffffff;
  207.   }
  208.  
  209.   private void fiveFoldDevis(triangle in, triangle out) {
  210.       double x4, y4, x5, y5;
  211.       double w;
  212.      
  213.       switch (in.type) {
  214.       case 1:
  215.           x4 = in.x2 + alfa * (in.x3 - in.x2); y4 = in.y2 + alfa * (in.y3 - in.y2);
  216.          
  217.           w = genRand.random();
  218.          
  219.           if (w < alfa*alfa) {
  220.               out.type = 1; out.col = col1;
  221.               out.x1 = in.x3; out.y1 = in.y3;
  222.               out.x2 = in.x1; out.y2 = in.y1;
  223.               out.x3 = x4; out.y3 = y4;
  224.           }
  225.           else {
  226.               out.type = 2; out.col = col2;
  227.               out.x1 = in.x2; out.y1 = in.y2;
  228.               out.x2 = x4; out.y2 = y4;
  229.               out.x3 = in.x1; out.y3 = in.y1;
  230.           }
  231.           break;
  232.       case 2:
  233.           x4 = in.x3 + alfa * (in.x1 - in.x3); y4 = in.y3 + alfa * (in.y1 - in.y3);
  234.           x5 = in.x3 + alfa * (in.x2 - in.x3); y5 = in.y3 + alfa * (in.y2 - in.y3);
  235.          
  236.           w = genRand.random();
  237.          
  238.           if (w < alfa*alfa) {
  239.               out.type = 2; out.col = col2;
  240.               out.x1 = in.x2; out.y1 = in.y2;
  241.               out.x2 = x4; out.y2 = y4;
  242.               out.x3 = in.x1; out.y3 = in.y1;
  243.           }
  244.           else if (w < 2*alfa*alfa) {
  245.               out.type = 2; out.col = col2;
  246.               out.x1 = in.x3; out.y1 = in.y3;
  247.               out.x2 = x5; out.y2 = y5;
  248.               out.x3 = x4; out.y3 = y4;
  249.           }
  250.           else {
  251.               out.type = 1; out.col = col1;
  252.               out.x1 = in.x2; out.y1 = in.y2;
  253.               out.x2 = x4; out.y2 = y4;
  254.               out.x3 = x5; out.y3 = y5;
  255.           }
  256.           break;
  257.       }
  258.   }
  259.  
  260.  
  261.   @Override
  262.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  263.       orig = new triangle();
  264.      
  265.       switch (t) {
  266.       case 1:
  267.           orig.type = 1; orig.col = col1;
  268.           orig.x1 = 0.0; orig.y1 = 0.0;
  269.           orig.x2 = cos(M_2PI/5); orig.y2 = sin(M_2PI/5);
  270.           orig.x3 = 2 * orig.x2; orig.y3 = 0.0;
  271.           break;
  272.       case 2:
  273.           orig.type = 2; orig.col = col2;
  274.           orig.x1 = 0.0; orig.y1 = 0.0;
  275.           orig.x2 = cos(M_PI/5); orig.y2 = sin(M_PI/5);
  276.           orig.x3 = 2 * orig.x2; orig.y3 = 0.0;
  277.           break;
  278.       }
  279.      
  280.       alfa = 2*cos(M_2PI/5);
  281.      
  282.       genRand = pContext.getRandGen();
  283.       t1 = new triangle();
  284.       t2 = new triangle();
  285.       p = new XYZPoint();
  286.   }
  287.  
  288.   @Override
  289.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  290.     /* FiveFold by eralex61, https://eralex61.deviantart.com/art/Five-fold-example-of-finite-recurent-system-in-Apo-705813367 */
  291.    
  292.       t1.assign(orig);
  293.       t2.assign(orig);
  294.      
  295.       int n = itt;
  296.       int k = 1;
  297.      
  298.       while (n > 0) {
  299.           fiveFoldDevis(t1, t2);
  300.           t1.assign(t2);
  301.          
  302.           if (on < 0.5) {
  303.               if ((k == itt) && (t1.type != eq)) n = n + depth;
  304.           }
  305.           else {
  306.               if ((k >= itt) && (k < itt + depth) && (t1.type != eq)) n = n + 1;
  307.           }
  308.           n = n - 1;
  309.           k = k + 1;
  310.       }
  311.      
  312.       int m = 0;
  313.       switch (t1.type) {
  314.       case 1:
  315.           n = itt1;
  316.           k = 1;
  317.           while (n > 0) {
  318.               fiveFoldDevis(t1, t2);
  319.               t1.assign(t2);
  320.               m = 2 * m + t2.type - 1;
  321.               if (on1 < 0.5) {
  322.                   if ((k == itt1) && (t1.type != eq1)) n = n + depth1;
  323.               }
  324.               else {
  325.                   if ((k >= itt1) && (k < itt1 + depth1) && (t1.type != eq1)) n = n + 1;
  326.               }
  327.               n = n - 1;
  328.               k = k + 1;
  329.           }
  330.           t1.col = discretNoise(m+43);
  331.           break;
  332.       case 2:
  333.           n = itt2;
  334.           k = 1;
  335.           while (n > 0) {
  336.               fiveFoldDevis(t1, t2);
  337.               t1.assign(t2);
  338.               m = 2 * m + t2.type - 1;
  339.               if (on2 < 0.5) {
  340.                   if ((k == itt2) && (t1.type != eq2)) n = n + depth2;
  341.               }
  342.               else {
  343.                   if ((k >= itt2) && (k < itt2 + depth2) && (t1.type != eq2)) n = n + 1;
  344.               }
  345.               n = n - 1;
  346.               k = k + 1;
  347.           }
  348.           t1.col = discretNoise(m+31);
  349.           break;
  350.       }
  351.      
  352.       t2.triBlur(p);
  353.      
  354.       pVarTP.x += pAmount * p.x;
  355.       pVarTP.y += pAmount * p.y;
  356.       pVarTP.z += pAmount * p.z;
  357.       pVarTP.color = fmod(t1.col + t2.col, 1.0);
  358.       if (pVarTP.color < 0.0) pVarTP.color += 1.0;
  359.      
  360.   }
  361.  
  362.   @Override
  363.   public String[] getParameterNames() {
  364.     return paramNames;
  365.   }
  366.  
  367.   @Override
  368.   public Object[] getParameterValues() {
  369.     return new Object[] { t, col1, col2, on, itt, eq, depth, on1, itt1, eq1, depth1, on2, itt2, eq2, depth2 };
  370.   }
  371.  
  372.   @Override
  373.   public String[] getParameterAlternativeNames() {
  374.      return new String[] { "FiveFold_T", "FiveFold_Col1", "FiveFold_Col2", "FiveFold_On", "FiveFold_Itt", "FiveFold_Eq", "FiveFold_Depth", "FiveFold_On1", "FiveFold_Itt1", "FiveFold_Eq1", "FiveFold_Depth1", "FiveFold_On2", "FiveFold_Itt2", "FiveFold_Eq2", "FiveFold_Depth2" };
  375.    }
  376.  
  377.   @Override
  378.   public void setParameter(String pName, double pValue) {
  379.     if (PARAM_ON.equalsIgnoreCase(pName))
  380.         on = pValue;
  381.     else if (PARAM_COL1.equalsIgnoreCase(pName))
  382.         col1 = pValue;
  383.     else if (PARAM_COL2.equalsIgnoreCase(pName))
  384.         col2 = pValue;
  385.     else if (PARAM_T.equalsIgnoreCase(pName))
  386.         t = Tools.FTOI(pValue);
  387.     else if (PARAM_ITT.equalsIgnoreCase(pName))
  388.         itt = Tools.FTOI(pValue);
  389.     else if (PARAM_EQ.equalsIgnoreCase(pName))
  390.         eq = Tools.FTOI(pValue);
  391.     else if (PARAM_DEPTH.equalsIgnoreCase(pName))
  392.         depth = Tools.FTOI(pValue);
  393.     else if (PARAM_ON1.equalsIgnoreCase(pName))
  394.         on1 = pValue;
  395.     else if (PARAM_ITT1.equalsIgnoreCase(pName))
  396.         itt1 = Tools.FTOI(pValue);
  397.     else if (PARAM_EQ1.equalsIgnoreCase(pName))
  398.         eq1 = Tools.FTOI(pValue);
  399.     else if (PARAM_DEPTH1.equalsIgnoreCase(pName))
  400.         depth1 = Tools.FTOI(pValue);
  401.     else if (PARAM_ON2.equalsIgnoreCase(pName))
  402.         on2 = pValue;
  403.     else if (PARAM_ITT2.equalsIgnoreCase(pName))
  404.         itt2 = Tools.FTOI(pValue);
  405.     else if (PARAM_EQ2.equalsIgnoreCase(pName))
  406.         eq2 = Tools.FTOI(pValue);
  407.     else if (PARAM_DEPTH2.equalsIgnoreCase(pName))
  408.         depth2 = Tools.FTOI(pValue);
  409.     else
  410.       throw new IllegalArgumentException(pName);
  411.   }
  412.  
  413.   @Override
  414.   public String getName() {
  415.     return "fiveFold";
  416.   }
  417.  
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement