rsidwell icon

Interference2Func.java

rsidwell | PRO | 01/09/21 05:41:28 PM UTC | 0 ⭐ | 1094 👁️ | Never ⏰ | []
Java |

5.81 KB

|

None

|

0 👍

/

0 👎

/*
JWildfire - an image and animation processor written in Java 
Copyright (C) 1995-2011 Andreas Maschke
This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
General Public License as published by the Free Software Foundation; either version 2.1 of the 
License, or (at your option) any later version.
 
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this software; 
if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jwildfire.create.tina.variation;
 
import org.jwildfire.create.tina.base.XForm;
import org.jwildfire.create.tina.base.XYZPoint;
 
import static org.jwildfire.base.mathlib.MathLib.cos;
import static org.jwildfire.base.mathlib.MathLib.sin;
import static org.jwildfire.base.mathlib.MathLib.asin;
import static org.jwildfire.base.mathlib.MathLib.pow;
 
import org.jwildfire.base.Tools;
 
import static org.jwildfire.base.mathlib.MathLib.M_PI;
import static org.jwildfire.base.mathlib.MathLib.M_PI_2;
 
public class Interference2Func extends VariationFunc {
    private static final long serialVersionUID = 1L;
 
    private static final String PARAM_A1 = "a1";
    private static final String PARAM_B1 = "b1";
    private static final String PARAM_C1 = "c1";
    private static final String PARAM_P1 = "p1";
    private static final String PARAM_T1 = "t1";
    private static final String PARAM_A2 = "a2";
    private static final String PARAM_B2 = "b2";
    private static final String PARAM_C2 = "c2";
    private static final String PARAM_P2 = "p2";
    private static final String PARAM_T2 = "t2";
 
    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 };
    
    private double a1 = 1.0;
    private double b1 = 1.0;
    private double c1 = 0.0;
    private double p1 = 1.0;
    private int t1 = 0;
    private double a2 = 1.0;
    private double b2 = 1.0;
    private double c2 = 0.0;
    private double p2 = 1.0;
    private int t2 = 0;
    
    private double sine(double a, double b, double c, double p, double x)
    {
        return a * pow(sin(b * x + c), p);
    }
 
    private double tri(double a, double b, double c, double p, double x)
    {
        return a * 2 * pow(asin(cos(b * x + c - M_PI_2)) / M_PI, p);
    }
 
    private double squ(double a, double b, double c, double p, double x)
    {
        return a * pow(sin(b * x + c) < 0 ? -1 : 1, p);
    }
 
 
    @Override
    public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP,
            double pAmount) {
      // interference2 by Brandon Brown (zephyrtronium)
      // https://www.deviantart.com/zephyrtronium/art/Interference2-Apophysis-Plugin-151168529
      
      double x1, y1, x2, y2;
      switch(t1) {
      case 1:
        x1 = tri(a1, b1, c1, p1, pAffineTP.x);
        y1 = tri(a1, b1, c1, p1, pAffineTP.y);
        break;
      case 2:
        x1 = squ(a1, b1, c1, p1, pAffineTP.x);
        y1 = squ(a1, b1, c1, p1, pAffineTP.y);
        break;
      default:
        x1 = sine(a1, b1, c1, p1, pAffineTP.x);
        y1 = sine(a1, b1, c1, p1, pAffineTP.y);
        break;
      }
      switch(t2) {
      case 1:
        x2 = tri(a2, b2, c2, p2, pAffineTP.x);
        y2 = tri(a2, b2, c2, p2, pAffineTP.y);
        break;
      case 2:
        x2 = squ(a2, b2, c2, p2, pAffineTP.x);
        y2 = squ(a2, b2, c2, p2, pAffineTP.y);
        break;
      default:
        x2 = sine(a2, b2, c2, p2, pAffineTP.x);
        y2 = sine(a2, b2, c2, p2, pAffineTP.y);
        break;
      }
      
      pVarTP.x += pAmount * (x1 + x2);
      pVarTP.y += pAmount * (y1 + y2);
      if (pContext.isPreserveZCoordinate()) {
        pVarTP.z += pAmount * pAffineTP.z;
      }
    }
 
    @Override
    public String[] getParameterNames() {
        return paramNames;
    }
 
    @Override
    public Object[] getParameterValues() {
        return new Object[] { a1, b1, c1, p1, t1, a2, b2, c2, p2, t2 };
    }
 
    @Override
    public String[] getParameterAlternativeNames() {
      return new String[]{"intrfr2_a1", "intrfr2_b1", "intrfr2_c1", "intrfr2_p1", "intrfr2_t1", "intrfr2_a2", "intrfr2_b2", "intrfr2_c2", "intrfr2_p2", "intrfr2_t2"};
    }
 
   @Override
    public void setParameter(String pName, double pValue) {
        if (PARAM_A1.equalsIgnoreCase(pName)) {
            a1 = pValue;
        } else if (PARAM_B1.equalsIgnoreCase(pName)) {
            b1 = pValue;
        } else if (PARAM_C1.equalsIgnoreCase(pName)) {
            c1 = pValue;
        } else if (PARAM_P1.equalsIgnoreCase(pName)) {
            p1 = pValue;
        } else if (PARAM_T1.equalsIgnoreCase(pName)) {
            t1 = (int) Tools.limitValue(pValue, 0, 2);
        } else if (PARAM_A2.equalsIgnoreCase(pName)) {
            a2 = pValue;
        } else if (PARAM_B2.equalsIgnoreCase(pName)) {
            b2 = pValue;
        } else if (PARAM_C2.equalsIgnoreCase(pName)) {
            c2 = pValue;
        } else if (PARAM_P2.equalsIgnoreCase(pName)) {
            p2 = pValue;
        } else if (PARAM_T2.equalsIgnoreCase(pName)) {
            t2 = (int) Tools.limitValue(pValue, 0, 2);
        } else {
            System.out.println("pName not recognized: " + pName);
            throw new IllegalArgumentException(pName);
        }
    }
 
    @Override
    public String getName() {
        return "interference2";
    }
 
}
 

Comments