Advertisement
rsidwell

PostPointSymmetryWFFunc

Jan 10th, 2023
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.25 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.base.Tools;
  20. import org.jwildfire.create.tina.base.Layer;
  21. import org.jwildfire.create.tina.base.XForm;
  22. import org.jwildfire.create.tina.base.XYZPoint;
  23.  
  24. import static org.jwildfire.base.mathlib.MathLib.*;
  25.  
  26. public class PostPointSymmetryWFFunc extends VariationFunc implements SupportsGPU {
  27.   private static final long serialVersionUID = 1L;
  28.  
  29.   public static final String PARAM_CENTRE_X = "centre_x";
  30.   public static final String PARAM_CENTRE_Y = "centre_y";
  31.   public static final String PARAM_CENTRE_Z = "centre_z";
  32.   public static final String PARAM_PLANE = "plane";
  33.   public static final String PARAM_ORDER = "order";
  34.   private static final String PARAM_COLORSHIFT = "colorshift";
  35.   private static final String[] paramNames = {PARAM_CENTRE_X, PARAM_CENTRE_Y, PARAM_CENTRE_Z, PARAM_PLANE, PARAM_ORDER, PARAM_COLORSHIFT};
  36.  
  37.   private double centre_x = 0.25;
  38.   private double centre_y = 0.5;
  39.   private double centre_z = 0.0;
  40.   private int plane = 0;
  41.   private int order = 3;
  42.   private double colorshift = 0;
  43.  
  44.   @Override
  45.   public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP,
  46.                         double pAmount) {
  47.     double dx = (pVarTP.x - centre_x) * pAmount;
  48.     double dy = (pVarTP.y - centre_y) * pAmount;
  49.     double dz = (pVarTP.z - centre_z) * pAmount;
  50.     int idx = pContext.random(order);
  51.     if (plane == 0) {
  52.       pVarTP.x = centre_x + dx * _cosa[idx] + dy * _sina[idx];
  53.       pVarTP.y = centre_y + dy * _cosa[idx] - dx * _sina[idx];
  54.     } else if (plane == 1) {
  55.       pVarTP.y = centre_y + dy * _cosa[idx] + dz * _sina[idx];
  56.       pVarTP.z = centre_z + dz * _cosa[idx] - dy * _sina[idx];
  57.     } else if (plane == 2) {
  58.       pVarTP.z = centre_z + dz * _cosa[idx] + dx * _sina[idx];
  59.       pVarTP.x = centre_x + dx * _cosa[idx] - dz * _sina[idx];
  60.     }
  61.     pVarTP.color = fmod(pVarTP.color + idx * colorshift, 1.0);
  62.   }
  63.  
  64.   @Override
  65.   public String[] getParameterNames() {
  66.     return paramNames;
  67.   }
  68.  
  69.   @Override
  70.   public Object[] getParameterValues() {
  71.     return new Object[]{centre_x, centre_y, centre_z, plane, order, colorshift};
  72.   }
  73.  
  74.   @Override
  75.   public void setParameter(String pName, double pValue) {
  76.     if (PARAM_CENTRE_X.equalsIgnoreCase(pName))
  77.       centre_x = pValue;
  78.     else if (PARAM_CENTRE_Y.equalsIgnoreCase(pName))
  79.       centre_y = pValue;
  80.     else if (PARAM_CENTRE_Z.equalsIgnoreCase(pName))
  81.       centre_z = pValue;
  82.     else if (PARAM_PLANE.equalsIgnoreCase(pName))
  83.       plane = limitIntVal(Tools.FTOI(pValue), 0, 2);
  84.     else if (PARAM_ORDER.equalsIgnoreCase(pName))
  85.       order = limitIntVal(Tools.FTOI(pValue), 1, Integer.MAX_VALUE);
  86.     else if (PARAM_COLORSHIFT.equalsIgnoreCase(pName))
  87.       colorshift = pValue;
  88.     else
  89.       throw new IllegalArgumentException(pName);
  90.   }
  91.  
  92.   @Override
  93.   public String getName() {
  94.     return "post_point_symmetry_wf";
  95.   }
  96.  
  97.   @Override
  98.   public int getPriority() {
  99.     return 1;
  100.   }
  101.  
  102.   private double _sina[], _cosa[];
  103.  
  104.   @Override
  105.   public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  106.     _sina = new double[order];
  107.     _cosa = new double[order];
  108.     double da = M_2PI / (double) order;
  109.     double angle = 0.0;
  110.     for (int i = 0; i < order; i++) {
  111.       _sina[i] = sin(angle);
  112.       _cosa[i] = cos(angle);
  113.       angle += da;
  114.     }
  115.   }
  116.  
  117.   @Override
  118.   public VariationFuncType[] getVariationTypes() {
  119.     return new VariationFuncType[]{VariationFuncType.VARTYPE_2D, VariationFuncType.VARTYPE_DC, VariationFuncType.VARTYPE_POST, VariationFuncType.VARTYPE_SUPPORTS_GPU};
  120.   }
  121.  
  122.   @Override
  123.   public String getGPUCode(FlameTransformationContext context) {
  124.     return "int order = lroundf(__post_point_symmetry_wf_order);\n"
  125.         + "if(order>36) order=36;\n"
  126.         +"float _sina[36];\n"
  127.         + "float _cosa[36];\n"
  128.         + "    float da = (2.0f*PI) / (float) order;\n"
  129.         + "    float angle = 0.0;\n"
  130.         + "    for (int i = 0; i < order; i++) {\n"
  131.         + "      _sina[i] = sinf(angle);\n"
  132.         + "      _cosa[i] = cosf(angle);\n"
  133.         + "      angle += da;\n"
  134.         + "    }\n"
  135.         + "    float dx = (__px - __post_point_symmetry_wf_centre_x) * __post_point_symmetry_wf;\n"
  136.         + "    float dy = (__py - __post_point_symmetry_wf_centre_y) * __post_point_symmetry_wf;\n"
  137.         + "    float dz = (__pz - __post_point_symmetry_wf_centre_z) * __post_point_symmetry_wf;\n"
  138.         + "    int idx = lroundf(RANDFLOAT() * (order-1));\n"
  139.         + "    if (__post_point_symmetry_wf_plane == 0) {\n"
  140.         + "      __px = __post_point_symmetry_wf_centre_x + dx * _cosa[idx] + dy * _sina[idx];\n"
  141.         + "      __py = __post_point_symmetry_wf_centre_y + dy * _cosa[idx] - dx * _sina[idx];\n"
  142.         + "    } else if (__post_point_symmetry_wf_plane == 0) {\n"
  143.         + "      __py = __post_point_symmetry_wf_centre_y + dy * _cosa[idx] + dz * _sina[idx];\n"
  144.         + "      __pz = __post_point_symmetry_wf_centre_z + dz * _cosa[idx] - dy * _sina[idx];\n"
  145.         + "    } else if (__post_point_symmetry_wf_plane == 0) {\n"
  146.         + "      __pz = __post_point_symmetry_wf_centre_z + dz * _cosa[idx] + dx * _sina[idx];\n"
  147.         + "      __px = __post_point_symmetry_wf_centre_x + dx * _cosa[idx] - dz * _sina[idx];\n"
  148.         + "    }\n"
  149.         + "    __pal = fmodf(__pal + idx * __post_point_symmetry_wf_colorshift, 1.0);\n";
  150.   }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement