Advertisement
rsidwell

Interference2Func.java

Jan 9th, 2021
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. /*
  2. JWildfire - an image and animation processor written in Java
  3. Copyright (C) 1995-2011 Andreas Maschke
  4. This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
  5. General Public License as published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7.  
  8. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  9. even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along with this software;
  12. if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  13. 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  14. */
  15. package org.jwildfire.create.tina.variation;
  16.  
  17. import org.jwildfire.create.tina.base.XForm;
  18. import org.jwildfire.create.tina.base.XYZPoint;
  19.  
  20. import static org.jwildfire.base.mathlib.MathLib.cos;
  21. import static org.jwildfire.base.mathlib.MathLib.sin;
  22. import static org.jwildfire.base.mathlib.MathLib.asin;
  23. import static org.jwildfire.base.mathlib.MathLib.pow;
  24.  
  25. import org.jwildfire.base.Tools;
  26.  
  27. import static org.jwildfire.base.mathlib.MathLib.M_PI;
  28. import static org.jwildfire.base.mathlib.MathLib.M_PI_2;
  29.  
  30. public class Interference2Func extends VariationFunc {
  31.     private static final long serialVersionUID = 1L;
  32.  
  33.     private static final String PARAM_A1 = "a1";
  34.     private static final String PARAM_B1 = "b1";
  35.     private static final String PARAM_C1 = "c1";
  36.     private static final String PARAM_P1 = "p1";
  37.     private static final String PARAM_T1 = "t1";
  38.     private static final String PARAM_A2 = "a2";
  39.     private static final String PARAM_B2 = "b2";
  40.     private static final String PARAM_C2 = "c2";
  41.     private static final String PARAM_P2 = "p2";
  42.     private static final String PARAM_T2 = "t2";
  43.  
  44.     private static final String[] paramNames = { PARAM_A1, PARAM_B1, PARAM_C1, PARAM_P1, PARAM_T1, PARAM_A2, PARAM_B2, PARAM_C2, PARAM_P2, PARAM_T2 };
  45.    
  46.     private double a1 = 1.0;
  47.     private double b1 = 1.0;
  48.     private double c1 = 0.0;
  49.     private double p1 = 1.0;
  50.     private int t1 = 0;
  51.     private double a2 = 1.0;
  52.     private double b2 = 1.0;
  53.     private double c2 = 0.0;
  54.     private double p2 = 1.0;
  55.     private int t2 = 0;
  56.    
  57.     private double sine(double a, double b, double c, double p, double x)
  58.     {
  59.         return a * pow(sin(b * x + c), p);
  60.     }
  61.  
  62.     private double tri(double a, double b, double c, double p, double x)
  63.     {
  64.         return a * 2 * pow(asin(cos(b * x + c - M_PI_2)) / M_PI, p);
  65.     }
  66.  
  67.     private double squ(double a, double b, double c, double p, double x)
  68.     {
  69.         return a * pow(sin(b * x + c) < 0 ? -1 : 1, p);
  70.     }
  71.  
  72.  
  73.     @Override
  74.     public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP,
  75.             double pAmount) {
  76.       // interference2 by Brandon Brown (zephyrtronium)
  77.       // https://www.deviantart.com/zephyrtronium/art/Interference2-Apophysis-Plugin-151168529
  78.      
  79.       double x1, y1, x2, y2;
  80.       switch(t1) {
  81.       case 1:
  82.         x1 = tri(a1, b1, c1, p1, pAffineTP.x);
  83.         y1 = tri(a1, b1, c1, p1, pAffineTP.y);
  84.         break;
  85.       case 2:
  86.         x1 = squ(a1, b1, c1, p1, pAffineTP.x);
  87.         y1 = squ(a1, b1, c1, p1, pAffineTP.y);
  88.         break;
  89.       default:
  90.         x1 = sine(a1, b1, c1, p1, pAffineTP.x);
  91.         y1 = sine(a1, b1, c1, p1, pAffineTP.y);
  92.         break;
  93.       }
  94.       switch(t2) {
  95.       case 1:
  96.         x2 = tri(a2, b2, c2, p2, pAffineTP.x);
  97.         y2 = tri(a2, b2, c2, p2, pAffineTP.y);
  98.         break;
  99.       case 2:
  100.         x2 = squ(a2, b2, c2, p2, pAffineTP.x);
  101.         y2 = squ(a2, b2, c2, p2, pAffineTP.y);
  102.         break;
  103.       default:
  104.         x2 = sine(a2, b2, c2, p2, pAffineTP.x);
  105.         y2 = sine(a2, b2, c2, p2, pAffineTP.y);
  106.         break;
  107.       }
  108.      
  109.       pVarTP.x += pAmount * (x1 + x2);
  110.       pVarTP.y += pAmount * (y1 + y2);
  111.       if (pContext.isPreserveZCoordinate()) {
  112.         pVarTP.z += pAmount * pAffineTP.z;
  113.       }
  114.     }
  115.  
  116.     @Override
  117.     public String[] getParameterNames() {
  118.         return paramNames;
  119.     }
  120.  
  121.     @Override
  122.     public Object[] getParameterValues() {
  123.         return new Object[] { a1, b1, c1, p1, t1, a2, b2, c2, p2, t2 };
  124.     }
  125.  
  126.     @Override
  127.     public String[] getParameterAlternativeNames() {
  128.       return new String[]{"intrfr2_a1", "intrfr2_b1", "intrfr2_c1", "intrfr2_p1", "intrfr2_t1", "intrfr2_a2", "intrfr2_b2", "intrfr2_c2", "intrfr2_p2", "intrfr2_t2"};
  129.     }
  130.  
  131.    @Override
  132.     public void setParameter(String pName, double pValue) {
  133.         if (PARAM_A1.equalsIgnoreCase(pName)) {
  134.             a1 = pValue;
  135.         } else if (PARAM_B1.equalsIgnoreCase(pName)) {
  136.             b1 = pValue;
  137.         } else if (PARAM_C1.equalsIgnoreCase(pName)) {
  138.             c1 = pValue;
  139.         } else if (PARAM_P1.equalsIgnoreCase(pName)) {
  140.             p1 = pValue;
  141.         } else if (PARAM_T1.equalsIgnoreCase(pName)) {
  142.             t1 = (int) Tools.limitValue(pValue, 0, 2);
  143.         } else if (PARAM_A2.equalsIgnoreCase(pName)) {
  144.             a2 = pValue;
  145.         } else if (PARAM_B2.equalsIgnoreCase(pName)) {
  146.             b2 = pValue;
  147.         } else if (PARAM_C2.equalsIgnoreCase(pName)) {
  148.             c2 = pValue;
  149.         } else if (PARAM_P2.equalsIgnoreCase(pName)) {
  150.             p2 = pValue;
  151.         } else if (PARAM_T2.equalsIgnoreCase(pName)) {
  152.             t2 = (int) Tools.limitValue(pValue, 0, 2);
  153.         } else {
  154.             System.out.println("pName not recognized: " + pName);
  155.             throw new IllegalArgumentException(pName);
  156.         }
  157.     }
  158.  
  159.     @Override
  160.     public String getName() {
  161.         return "interference2";
  162.     }
  163.  
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement