Advertisement
rsidwell

spherecrop

Nov 21st, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 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.atan2;
  20. import static org.jwildfire.base.mathlib.MathLib.cos;
  21. import static org.jwildfire.base.mathlib.MathLib.acos;
  22. import static org.jwildfire.base.mathlib.MathLib.max;
  23. import static org.jwildfire.base.mathlib.MathLib.min;
  24. import static org.jwildfire.base.mathlib.MathLib.sin;
  25. import static org.jwildfire.base.mathlib.MathLib.sqrt;
  26.  
  27. import org.jwildfire.base.Tools;
  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 SphereCropFunc extends VariationFunc {
  33.   private static final long serialVersionUID = 1L;
  34.  
  35.   private static final String PARAM_RADIUS = "radius";
  36.   private static final String PARAM_X = "x";
  37.   private static final String PARAM_Y = "y";
  38.   private static final String PARAM_Z = "z";
  39.   private static final String PARAM_SCATTER_AREA = "scatter_area";
  40.   private static final String PARAM_ZERO = "zero";
  41.  
  42.   private static final String[] paramNames = { PARAM_RADIUS, PARAM_X, PARAM_Y, PARAM_Z, PARAM_SCATTER_AREA, PARAM_ZERO };
  43.  
  44.   private double radius = 1.0;
  45.   private double x = 0.0;
  46.   private double y = 0.0;
  47.   private double z = 0.0;
  48.   private double scatter_area = 0.0;
  49.   private int zero = 1;
  50.  
  51.   @Override
  52.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  53.     // 3D version of circlecrop by Xyrus02, http://xyrus02.deviantart.com/art/CircleCrop-Plugins-185353309
  54.     double x0 = x;
  55.     double y0 = y;
  56.     double z0 = z;
  57.     double cr = radius;
  58.     double ca = cA;
  59.     double vv = pAmount;
  60.  
  61.     pAffineTP.x -= x0;
  62.     pAffineTP.y -= y0;
  63.     pAffineTP.z -= z0;
  64.  
  65.     double rad = sqrt(pAffineTP.x * pAffineTP.x + pAffineTP.y * pAffineTP.y + pAffineTP.z * pAffineTP.z);
  66.     double ang = atan2(pAffineTP.y, pAffineTP.x);
  67.     double az = acos(pAffineTP.z / rad);
  68.     double rdc = cr + (pContext.random() * 0.5 * ca);
  69.  
  70.     boolean esc = rad > cr;
  71.     boolean cr0 = zero == 1;
  72.  
  73.     double s = sin(ang);
  74.     double c = cos(ang);
  75.     double saz = sin(az);
  76.     double caz = cos(az);
  77.  
  78.     pVarTP.doHide = false;
  79.     if (cr0 && esc) {
  80.       pVarTP.x = pVarTP.y = pVarTP.z = 0;
  81.       pVarTP.doHide = true;
  82.     }
  83.     else if (cr0 && !esc) {
  84.       pVarTP.x += vv * pAffineTP.x + x0;
  85.       pVarTP.y += vv * pAffineTP.y + y0;
  86.       pVarTP.z += vv * pAffineTP.z + z0;
  87.     }
  88.     else if (!cr0 && esc) {
  89.       pVarTP.x += vv * rdc * c * saz + x0;
  90.       pVarTP.y += vv * rdc * s * saz + y0;
  91.       pVarTP.z += vv * rdc * caz + z0;
  92.     }
  93.     else if (!cr0 && !esc) {
  94.       pVarTP.x += vv * pAffineTP.x + x0;
  95.       pVarTP.y += vv * pAffineTP.y + y0;
  96.       pVarTP.z += vv * pAffineTP.z + z0;
  97.     }
  98.  
  99.   }
  100.  
  101.   @Override
  102.   public String[] getParameterNames() {
  103.     return paramNames;
  104.   }
  105.  
  106.   @Override
  107.   public Object[] getParameterValues() {
  108.     return new Object[] { radius, x, y, z, scatter_area, zero };
  109.   }
  110.  
  111.   @Override
  112.   public void setParameter(String pName, double pValue) {
  113.     if (PARAM_RADIUS.equalsIgnoreCase(pName))
  114.       radius = pValue;
  115.     else if (PARAM_X.equalsIgnoreCase(pName))
  116.       x = pValue;
  117.     else if (PARAM_Y.equalsIgnoreCase(pName))
  118.       y = pValue;
  119.     else if (PARAM_Z.equalsIgnoreCase(pName))
  120.       z = pValue;
  121.     else if (PARAM_SCATTER_AREA.equalsIgnoreCase(pName))
  122.       scatter_area = pValue;
  123.     else if (PARAM_ZERO.equalsIgnoreCase(pName))
  124.       zero = Tools.FTOI(pValue);
  125.     else
  126.       throw new IllegalArgumentException(pName);
  127.   }
  128.  
  129.   @Override
  130.   public String getName() {
  131.     return "spherecrop";
  132.   }
  133.  
  134.   private double cA;
  135.  
  136.   @Override
  137.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  138.     cA = max(-1.0, min(scatter_area, 1.0));
  139.   }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement