Advertisement
snicker02

Strangescope variation

Jan 28th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 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.  
  20. import static org.jwildfire.base.mathlib.MathLib.M_PI;
  21. import static org.jwildfire.base.mathlib.MathLib.atan2;
  22. import static org.jwildfire.base.mathlib.MathLib.cos;
  23. import static org.jwildfire.base.mathlib.MathLib.iabs;
  24. import static org.jwildfire.base.mathlib.MathLib.pow;
  25. import static org.jwildfire.base.mathlib.MathLib.sin;
  26. import static org.jwildfire.base.mathlib.MathLib.sqr;
  27. import static org.jwildfire.base.mathlib.MathLib.sqrt;
  28.  
  29. import org.jwildfire.base.Tools;
  30. import org.jwildfire.create.tina.base.Layer;
  31. import org.jwildfire.create.tina.base.XForm;
  32. import org.jwildfire.create.tina.base.XYZPoint;
  33.  
  34. public class StrangeScopeFunc extends VariationFunc {
  35. private static final long serialVersionUID = 1L;
  36.  
  37. private static final String PARAM_POWER = "power";
  38. private static final String PARAM_DIST = "dist";
  39. private static final String PARAM_A = "a";
  40. private static final String PARAM_B = "b";
  41. private static final String PARAM_C = "c";
  42. private static final String PARAM_D = "d";
  43. private static final String PARAM_E = "e";
  44. private static final String PARAM_F = "f";
  45. private static final String PARAM_WAVEX = "wavex";
  46. private static final String PARAM_WAVEY = "wavey";
  47. private static final String PARAM_CA = "ca";
  48. private static final String PARAM_C1 = "c1";
  49. private static final String PARAM_C2 = "c2";
  50. private static final String[] paramNames = { PARAM_POWER, PARAM_DIST, PARAM_A, PARAM_B, PARAM_C, PARAM_D, PARAM_E, PARAM_F, PARAM_WAVEX, PARAM_WAVEY, PARAM_CA, PARAM_C1, PARAM_C2 };
  51.  
  52. private int power = genRandomPower();
  53. private double dist = 1.0;
  54. private double a = 1.0;
  55. private double b = 0.0;
  56. private double c = 0.0;
  57. private double d = 1.0;
  58. private double e = 0.0;
  59. private double f = 0.0;
  60. private double wavex = 1.0;
  61. private double wavey = 1.0;
  62. private double ca = 0.5;
  63. private double c1 = 0.0;
  64. private double c2 = 0.0;
  65.  
  66. @Override
  67. public void transform(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  68. // if (power == 2)
  69. // transformPower2(pContext, pXForm, pAffineTP, pVarTP, pAmount);
  70. // else if (power == -2)
  71. // transformPowerMinus2(pContext, pXForm, pAffineTP, pVarTP, pAmount);
  72. // else if (power == 1)
  73. // transformPower1(pContext, pXForm, pAffineTP, pVarTP, pAmount);
  74. // else if (power == -1)
  75. // transformPowerMinus1(pContext, pXForm, pAffineTP, pVarTP, pAmount);
  76. // else
  77. transformFunction(pContext, pXForm, pAffineTP, pVarTP, pAmount);
  78. }
  79.  
  80. public void transformPower2(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  81.  
  82. double a2;
  83. double x = a * pAffineTP.x + b * pAffineTP.y + e;
  84. double y = c * pAffineTP.x + d * pAffineTP.y + f;
  85. if (pContext.random(2) == 0) {
  86. a2 = atan2(pAffineTP.y, pAffineTP.x) / 2.0;
  87. }
  88. else {
  89. a2 = M_PI - atan2(pAffineTP.y, pAffineTP.x) / 2.0;
  90. }
  91. double sina = sin(a2*wavex);
  92. double cosa = cos(a2*wavey);
  93. double r = pAmount * sqrt(sqrt(sqr(x) + sqr(y)));
  94.  
  95. pVarTP.x = pVarTP.x + r * cosa;
  96. pVarTP.y = pVarTP.y + r * sina;
  97. if (pContext.isPreserveZCoordinate()) {
  98. pVarTP.z += pAmount * pAffineTP.z;
  99. }
  100. }
  101.  
  102. public void transformPowerMinus2(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  103. double a2;
  104. double x = a * pAffineTP.x + b * pAffineTP.y + e;
  105. double y = c * pAffineTP.x + d * pAffineTP.y + f;
  106. if (pContext.random(2) == 0)
  107. a2 = atan2(pAffineTP.y, pAffineTP.x) / 2.0;
  108. else
  109. a2 = M_PI - atan2(pAffineTP.y, pAffineTP.x) / 2.0;
  110. double sina = sin(a2*wavex);
  111. double cosa = cos(a2*wavey);
  112. double r = pAmount / sqrt(sqrt(sqr(x) + sqr(y)));
  113. pVarTP.x = pVarTP.x + r * cosa;
  114. pVarTP.y = pVarTP.y - r * sina;
  115. if (pContext.isPreserveZCoordinate()) {
  116. pVarTP.z += pAmount * pAffineTP.z;
  117. }
  118. }
  119.  
  120. public void transformPower1(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  121. pVarTP.x = pVarTP.x + pAmount * pAffineTP.x;
  122. pVarTP.y = pVarTP.y + pAmount * pAffineTP.y;
  123. if (pContext.isPreserveZCoordinate()) {
  124. pVarTP.z += pAmount * pAffineTP.z;
  125. }
  126. }
  127.  
  128. public void transformPowerMinus1(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  129. double x = a * pAffineTP.x + b * pAffineTP.y + e;
  130. double y = c * pAffineTP.x + d * pAffineTP.y + f;
  131. double r = pAmount / (sqr(x) + sqr(y));
  132. pVarTP.x = pVarTP.x + r * pAffineTP.x;
  133. pVarTP.y = pVarTP.y + r * pAffineTP.y;
  134. if (pContext.isPreserveZCoordinate()) {
  135. pVarTP.z += pAmount * pAffineTP.z;
  136. }
  137. }
  138.  
  139. public void transformFunction(FlameTransformationContext pContext, XForm pXForm, XYZPoint pAffineTP, XYZPoint pVarTP, double pAmount) {
  140. int rnd = pContext.random(absPower);
  141. double a2;
  142. double x = a * pAffineTP.x + b * pAffineTP.y + e;
  143. double y = c * pAffineTP.x + d * pAffineTP.y + f;
  144. if ((rnd & 1) == 0)
  145. a2 = (2 * M_PI * rnd + atan2(pAffineTP.y, pAffineTP.x)) / power;
  146. else
  147. a2 = (2 * M_PI * rnd - atan2(pAffineTP.y, pAffineTP.x)) / power;
  148. double sina = sin(a2*wavex);
  149. double cosa = cos(a2*wavey);
  150.  
  151. double re = 1 + c1 * pAffineTP.x + c2 * (sqr(pAffineTP.x) - sqr(pAffineTP.y));
  152. double im = c1 * pAffineTP.y + c2 * 2 * pAffineTP.x * pAffineTP.y;
  153.  
  154. double r = pAmount * pow(sqr(x) + sqr(y) , cPower);
  155. double r2 = ca / (sqr(re) + sqr(im));
  156.  
  157. pVarTP.x = pVarTP.x + r * cosa + (pAffineTP.x * re + pAffineTP.y * im) * r2;
  158. pVarTP.y = pVarTP.y + r * sina + (pAffineTP.y * re - pAffineTP.x * im) * r2;;
  159. if (pContext.isPreserveZCoordinate()) {
  160. pVarTP.z += pAmount * pAffineTP.z;
  161. }
  162. }
  163.  
  164. @Override
  165. public String[] getParameterNames() {
  166. return paramNames;
  167. }
  168.  
  169. @Override
  170. public Object[] getParameterValues() {
  171. return new Object[] { power, dist, a, b, c, d, e, f, wavex, wavey, ca, c1, c2 };
  172. }
  173.  
  174. @Override
  175. public void setParameter(String pName, double pValue) {
  176. if (PARAM_POWER.equalsIgnoreCase(pName))
  177. power = Tools.FTOI(pValue);
  178. else if (PARAM_DIST.equalsIgnoreCase(pName))
  179. dist = pValue;
  180. else if (PARAM_A.equalsIgnoreCase(pName))
  181. a = pValue;
  182. else if (PARAM_B.equalsIgnoreCase(pName))
  183. b = pValue;
  184. else if (PARAM_C.equalsIgnoreCase(pName))
  185. c = pValue;
  186. else if (PARAM_D.equalsIgnoreCase(pName))
  187. d = pValue;
  188. else if (PARAM_E.equalsIgnoreCase(pName))
  189. e = pValue;
  190. else if (PARAM_F.equalsIgnoreCase(pName))
  191. f = pValue;
  192. else if (PARAM_WAVEX.equalsIgnoreCase(pName))
  193. wavex = pValue;
  194. else if (PARAM_WAVEY.equalsIgnoreCase(pName))
  195. wavey = pValue;
  196. else if (PARAM_CA.equalsIgnoreCase(pName))
  197. ca = pValue;
  198. else if (PARAM_C1.equalsIgnoreCase(pName))
  199. c1 = pValue;
  200. else if (PARAM_C2.equalsIgnoreCase(pName))
  201. c2 = pValue;
  202.  
  203. else
  204. throw new IllegalArgumentException(pName);
  205. }
  206.  
  207. @Override
  208. public String getName() {
  209. return "strangescope";
  210. }
  211.  
  212. private int genRandomPower() {
  213. int res = (int) (Math.random() * 5.0 + 2.5);
  214. return Math.random() < 0.5 ? res : -res;
  215. }
  216.  
  217. private int absPower;
  218. private double cPower;
  219.  
  220. @Override
  221. public void init(FlameTransformationContext pContext, Layer pLayer, XForm pXForm, double pAmount) {
  222. absPower = iabs(Tools.FTOI(power));
  223. cPower = dist / power * 0.5;
  224.  
  225. }
  226.  
  227.  
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement