Advertisement
rsidwell

crop3D

Nov 21st, 2017
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 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.max;
  20. import static org.jwildfire.base.mathlib.MathLib.min;
  21.  
  22. import org.jwildfire.base.Tools;
  23. import org.jwildfire.create.tina.base.Layer;
  24. import org.jwildfire.create.tina.base.XForm;
  25. import org.jwildfire.create.tina.base.XYZPoint;
  26.  
  27. public class Crop3DFunc extends VariationFunc {
  28.   private static final long serialVersionUID = 1L;
  29.  
  30.   private static final String PARAM_LEFT = "left";
  31.   private static final String PARAM_RIGHT = "right";
  32.   private static final String PARAM_TOP = "top";
  33.   private static final String PARAM_CEILING = "ceiling";
  34.   private static final String PARAM_BOTTOM = "bottom";
  35.   private static final String PARAM_FLOOR = "floor";
  36.   private static final String PARAM_SCATTER_AREA = "scatter_area";
  37.   private static final String PARAM_ZERO = "zero";
  38.  
  39.   private static final String[] paramNames = { PARAM_LEFT, PARAM_RIGHT, PARAM_TOP, PARAM_BOTTOM, PARAM_FLOOR, PARAM_CEILING, PARAM_SCATTER_AREA, PARAM_ZERO };
  40.  
  41.   private double left = -1.0;
  42.   private double top = -1.0;
  43.   private double floor = -1.0;
  44.   private double right = 1.0;
  45.   private double bottom = 1.0;
  46.   private double ceiling = 1.0;
  47.   private double scatter_area = 0.0;
  48.   private int zero = 0;
  49.  
  50.   @Override
  51.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  52.     // 3D version of crop by Xyrus02, http://xyrus02.deviantart.com/art/Crop-Plugin-Updated-169958881
  53.     double x = pAffineTP.x;
  54.     double y = pAffineTP.y;
  55.     double z = pAffineTP.z;
  56.     if (((x < xmin) || (x > xmax) || (y < ymin) || (y > ymax) || (z < zmin) || (z > zmax)) && (zero != 0)) {
  57.       pVarTP.x = pVarTP.y = pVarTP.z = 0;
  58.       pVarTP.doHide = true;
  59.       return;
  60.     }
  61.     else {
  62.       pVarTP.doHide = false;
  63.       if (x < xmin)
  64.         x = xmin + pContext.random() * w;
  65.       else if (x > xmax)
  66.         x = xmax - pContext.random() * w;
  67.       if (y < ymin)
  68.         y = ymin + pContext.random() * h;
  69.       else if (y > ymax)
  70.         y = ymax - pContext.random() * h;
  71.       if (z < zmin)
  72.         z = zmin + pContext.random() * l;
  73.       else if (z > zmax)
  74.         z = zmax - pContext.random() * l;
  75.     }
  76.     pVarTP.x = pAmount * x;
  77.     pVarTP.y = pAmount * y;
  78.     pVarTP.z = pAmount * z;
  79.   }
  80.  
  81.   @Override
  82.   public String[] getParameterNames() {
  83.     return paramNames;
  84.   }
  85.  
  86.   @Override
  87.   public Object[] getParameterValues() {
  88.     return new Object[] { left, right, top, bottom, floor, ceiling, scatter_area, zero };
  89.   }
  90.  
  91.   @Override
  92.   public void setParameter(String pName, double pValue) {
  93.     if (PARAM_LEFT.equalsIgnoreCase(pName))
  94.       left = pValue;
  95.     else if (PARAM_RIGHT.equalsIgnoreCase(pName))
  96.       right = pValue;
  97.     else if (PARAM_TOP.equalsIgnoreCase(pName))
  98.       top = pValue;
  99.     else if (PARAM_BOTTOM.equalsIgnoreCase(pName))
  100.       bottom = pValue;
  101.     else if (PARAM_CEILING.equalsIgnoreCase(pName))
  102.       ceiling = pValue;
  103.     else if (PARAM_FLOOR.equalsIgnoreCase(pName))
  104.       floor = pValue;
  105.     else if (PARAM_SCATTER_AREA.equalsIgnoreCase(pName))
  106.       scatter_area = limitVal(pValue, -1.0, 1.0);
  107.     else if (PARAM_ZERO.equalsIgnoreCase(pName))
  108.       zero = limitIntVal(Tools.FTOI(pValue), 0, 1);
  109.     else
  110.       throw new IllegalArgumentException(pName);
  111.   }
  112.  
  113.   @Override
  114.   public String getName() {
  115.     return "crop3D";
  116.   }
  117.  
  118.   private double xmin, xmax, ymin, ymax, zmin, zmax, w, h, l;
  119.  
  120.   @Override
  121.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  122.     xmin = min(left, right);
  123.     ymin = min(top, bottom);
  124.     zmin = min(floor, ceiling);
  125.     xmax = max(left, right);
  126.     ymax = max(top, bottom);
  127.     zmax = max(floor, ceiling);
  128.     w = (xmax - xmin) * 0.5 * scatter_area;
  129.     h = (ymax - ymin) * 0.5 * scatter_area;
  130.     l = (zmax - zmin) * 0.5 * scatter_area;
  131.   }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement