Advertisement
rsidwell

ringer

Jan 8th, 2025
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.38 KB | None | 0 0
  1. /*
  2.   JWildfire - an image and animation processor written in Java
  3.   Copyright (C) 1995-2021 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 org.jwildfire.create.tina.base.Layer;
  20. import org.jwildfire.create.tina.base.XForm;
  21. import org.jwildfire.create.tina.base.XYZPoint;
  22.  
  23. import java.io.Serializable;
  24.  
  25. import static org.jwildfire.base.mathlib.MathLib.*;
  26.  
  27. public class RingerFunc extends VariationFunc {
  28.   private static final long serialVersionUID = 1L;
  29.  
  30.   private static final String PARAM_HIDE = "hide_inner";
  31.   private static final String PARAM_RADIUS = "innerradius";
  32.   private static final String PARAM_CROPRADIUS = "outerradius";
  33.   private static final String PARAM_THICKNESS = "thickness";
  34.   private static final String PARAM_CONTRAST = "contrast";
  35.   private static final String PARAM_POW = "pow";
  36.   private static final String PARAM_SCATTER_AREA = "scatter_area";
  37.   private static final String PARAM_ZERO = "hide_outer";
  38.  
  39.   private static final String[] paramNames = {PARAM_HIDE, PARAM_RADIUS, PARAM_CROPRADIUS, PARAM_THICKNESS,
  40.           PARAM_CONTRAST, PARAM_POW, PARAM_SCATTER_AREA, PARAM_ZERO};
  41.  
  42.  
  43.   private int hide = 0;
  44.   private double oldx = 0, oldy = 0;
  45.  
  46.   private double radius = 0.5;
  47.   private double thickness = 0.0;
  48.   private double contrast = 0;
  49.   private double pow = 1.0;
  50.  
  51.   private double cropradius = 1.0;
  52.   private double scatter_area = 0.0;
  53.   private int zero = 1;
  54.   private double cA;
  55.  
  56.  
  57.   private static class Point implements Serializable {
  58.     private static final long serialVersionUID = 1L;
  59.  
  60.     private double x, y;
  61.   }
  62.  
  63.   private void circle2(FlameTransformationContext pContext, Point p) {
  64.     //    double r = this.radius + this.thickness - this.Gamma * pContext.random();
  65.     double phi = 2.0 * M_PI * pContext.random();
  66.     double sinPhi = sin(phi);
  67.     double cosPhi = cos(phi);
  68.     double r;
  69.     if (pContext.random() < this._gamma) {
  70.       r = this._radius1;
  71.     } else {
  72.       r = this._radius2;
  73.     }
  74.     p.x = r * cosPhi;
  75.     p.y = r * sinPhi;
  76.   }
  77.  
  78.   @Override
  79.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  80.  
  81.     double r, Alpha;
  82.     r = sqrt(pAffineTP.x * pAffineTP.x + pAffineTP.y * pAffineTP.y);
  83.     Alpha = this.radius / r;
  84.  
  85.     if (r < this._radius1) {
  86.       if (hide == 0) {
  87.         circle2(pContext, toolPoint); // Draw/Hide the circle
  88.         pVarTP.x += pAmount * toolPoint.x;
  89.         pVarTP.y += pAmount * toolPoint.y;
  90.       } else {
  91.         pVarTP.x = oldx;
  92.         pVarTP.y = oldy;
  93.       }
  94.     } else {
  95.       if (pContext.random() > this.contrast * pow(Alpha, this._absPow)) {
  96.         pVarTP.x += pAmount * pAffineTP.x;
  97.         pVarTP.y += pAmount * pAffineTP.y;
  98.       } else {
  99.         pVarTP.x += pAmount * Alpha * Alpha * pAffineTP.x;
  100.         pVarTP.y += pAmount * Alpha * Alpha * pAffineTP.y;
  101.       }
  102.     }
  103.     oldx = pVarTP.x;
  104.     oldy = pVarTP.y;
  105.  
  106.     double x0 = 0.0;
  107.     double y0 = 0.0;
  108.     double cr = cropradius;
  109.     double ca = cA;
  110.     double vv = pAmount;
  111.  
  112.     pVarTP.x -= x0;
  113.     pVarTP.y -= y0;
  114.  
  115.     double rad = sqrt(pVarTP.x * pVarTP.x + pVarTP.y * pVarTP.y);
  116.     double ang = atan2(pVarTP.y, pVarTP.x);
  117.     double rdc = cr + (pContext.random() * 0.5 * ca);
  118.  
  119.     boolean esc = rad > cr;
  120.     boolean cr0 = zero == 1;
  121.  
  122.     double s = sin(ang);
  123.     double c = cos(ang);
  124.  
  125.     pVarTP.doHide = false;
  126.     if (cr0 && esc) {
  127.       pVarTP.x = pVarTP.y = 0;
  128.       pVarTP.doHide = true;
  129.     } else if (cr0 && !esc) {
  130.       pVarTP.x = vv * pVarTP.x + x0;
  131.       pVarTP.y = vv * pVarTP.y + y0;
  132.     } else if (!cr0 && esc) {
  133.       pVarTP.x = vv * rdc * c + x0;
  134.       pVarTP.y = vv * rdc * s + y0;
  135.     } else if (!cr0 && !esc) {
  136.       pVarTP.x = vv * pVarTP.x + x0;
  137.       pVarTP.y = vv * pVarTP.y + y0;
  138.     }
  139.  
  140.     // setColor(pVarTP);
  141.   }
  142.  
  143.  
  144.   @Override
  145.   public String[] getParameterNames() {
  146.     return paramNames;
  147.   }
  148.  
  149.   @Override
  150.   public Object[] getParameterValues() {
  151.     return new Object[]{hide, radius, cropradius, thickness, contrast, pow, scatter_area, zero};
  152.   }
  153.  
  154.   @Override
  155.   public void setParameter(String pName, double pValue) {
  156.     if (PARAM_HIDE.equalsIgnoreCase(pName))
  157.       hide = (int) limitVal(pValue, 0, 1);
  158.     else if (PARAM_RADIUS.equalsIgnoreCase(pName))
  159.       radius = pValue;
  160.     else if (PARAM_CROPRADIUS.equalsIgnoreCase(pName))
  161.       cropradius = pValue;
  162.     else if (PARAM_THICKNESS.equalsIgnoreCase(pName))
  163.       thickness = limitVal(pValue, 0.0, 1.0);
  164.     else if (PARAM_CONTRAST.equalsIgnoreCase(pName))
  165.       contrast = limitVal(pValue, 0.0, 1.0);
  166.     else if (PARAM_POW.equalsIgnoreCase(pName))
  167.       pow = pValue;
  168.     else if (PARAM_SCATTER_AREA.equalsIgnoreCase(pName))
  169.       scatter_area = pValue;
  170.     else if (PARAM_ZERO.equalsIgnoreCase(pName))
  171.       zero = (int) limitVal(pValue, 0, 1);
  172.     else
  173.       throw new IllegalArgumentException(pName);
  174.   }
  175.  
  176.   @Override
  177.   public String getName() {
  178.     return "ringer";
  179.   }
  180.  
  181.   private Point toolPoint = new Point();
  182.   private double _radius1, _radius2, _gamma, _absPow;
  183.  
  184.   @Override
  185.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  186.     super.init(pContext, pLayer, pXForm, pAmount);
  187.     this._radius1 = this.radius + this.thickness;
  188.     this._radius2 = sqr(this.radius) / this._radius1;
  189.     this._gamma = this._radius1 / (this._radius1 + this._radius2);
  190.     this._absPow = fabs(this.pow);
  191.  
  192.     cA = max(-1.0, min(scatter_area, 1.0));
  193.   }
  194.  
  195.   @Override
  196.   public VariationFuncType[] getVariationTypes() {
  197.     return new VariationFuncType[]{VariationFuncType.VARTYPE_2D};
  198.   }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement