Advertisement
rsidwell

PrePostCirclizeFunc.java

Feb 17th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 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.M_PI;
  20. import static org.jwildfire.base.mathlib.MathLib.M_2PI;
  21. import static org.jwildfire.base.mathlib.MathLib.M_PI_4;
  22. import static org.jwildfire.base.mathlib.MathLib.sin;
  23. import static org.jwildfire.base.mathlib.MathLib.cos;
  24. import static org.jwildfire.base.mathlib.MathLib.floor;
  25.  
  26. import org.jwildfire.base.Tools;
  27.  
  28. import org.jwildfire.create.tina.base.Layer;
  29. import org.jwildfire.create.tina.base.XForm;
  30. import org.jwildfire.create.tina.base.XYZPoint;
  31.  
  32. public class PrePostCirclizeFunc extends VariationFunc {
  33.   private static final long serialVersionUID = 1L;
  34.  
  35.   private static final String PARAM_N = "n";
  36.   private static final String PARAM_ROTATION = "rotation";
  37.   private static final String PARAM_REVERSE = "reverse";
  38.  
  39.   private static final String[] paramNames = { PARAM_N, PARAM_ROTATION, PARAM_REVERSE };
  40.  
  41.   private int n = 4;
  42.   private double rotation = 45;
  43.   private double rot_rad = M_PI_4;
  44.   private int reverse = 0;
  45.  
  46.   private double pi_n, cospi_n;
  47.  
  48.   private void circlize(XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  49.       double theta = pAffineTP.getPrecalcAtanYX();
  50.       double r = pAffineTP.getPrecalcSqrt();
  51.       double factor = cospi_n / cos((theta - rot_rad) - pi_n * (2 * floor((n * (theta - rot_rad)) / M_2PI) + 1));
  52.      
  53.       pVarTP.x = lerp(r, r / factor, pAmount) * cos(theta);
  54.       pVarTP.y = lerp(r, r / factor, pAmount) * sin(theta);  
  55.   }
  56.  
  57.   private void uncirclize(XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  58.       double theta = pAffineTP.getPrecalcAtanYX();
  59.       double r = pAffineTP.getPrecalcSqrt();
  60.       double factor = cospi_n / cos((theta - rot_rad) - pi_n * (2 * floor((n * (theta - rot_rad)) / M_2PI) + 1));
  61.      
  62.       pVarTP.x = lerp(r, r * factor, pAmount) * cos(theta);
  63.       pVarTP.y = lerp(r, r * factor, pAmount) * sin(theta);  
  64.   }
  65.  
  66.   @Override
  67.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  68.       pi_n = M_PI / n;
  69.       cospi_n = cos(pi_n);
  70.   }
  71.  
  72.   @Override
  73.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  74.       if (reverse == 0) {
  75.           uncirclize(pVarTP, pVarTP, pAmount);
  76.       }
  77.       else {
  78.           circlize(pVarTP, pVarTP, pAmount);
  79.       }
  80.   }
  81.  
  82.   @Override
  83.   public void invtransform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  84.       if (reverse == 0) {
  85.           circlize(pAffineTP, pAffineTP, pAmount);
  86.       }
  87.       else {
  88.           uncirclize(pAffineTP, pAffineTP, pAmount);
  89.       }
  90.   }
  91.  
  92.   @Override
  93.   public String[] getParameterNames() {
  94.     return paramNames;
  95.   }
  96.  
  97.   @Override
  98.   public Object[] getParameterValues() {
  99.     return new Object[] { n, rotation, reverse };
  100.   }
  101.  
  102.   @Override
  103.   public void setParameter(String pName, double pValue) {
  104.     if (PARAM_N.equalsIgnoreCase(pName))
  105.       n = limitIntVal(Tools.FTOI(pValue), 3, 50);
  106.     else if (PARAM_ROTATION.equalsIgnoreCase(pName)) {
  107.       rotation = pValue;
  108.       rot_rad = rotation * M_PI / 180;
  109.     }
  110.     else if (PARAM_REVERSE.equalsIgnoreCase(pName))
  111.       reverse = limitIntVal(Tools.FTOI(pValue), 0, 1);
  112.     else
  113.       throw new IllegalArgumentException(pName);
  114.   }
  115.  
  116.   @Override
  117.   public String getName() {
  118.     return "prepost_circlize";
  119.   }
  120.  
  121.   @Override
  122.   public int getPriority() {
  123.     return 2;
  124.   }
  125.  
  126.   private double lerp(double v0, double v1, double t) {
  127.       return (1-t) * v0 + t * v1;
  128.   }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement