propane 3.7.1-java → 3.11.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -1
  3. data/.mvn/wrapper/maven-wrapper.properties +1 -1
  4. data/.travis.yml +1 -1
  5. data/CHANGELOG.md +7 -1
  6. data/README.md +8 -15
  7. data/Rakefile +6 -5
  8. data/lib/propane/app.rb +11 -15
  9. data/lib/propane/helper_methods.rb +6 -6
  10. data/lib/propane/version.rb +1 -1
  11. data/lib/{propane-3.7.1.jar → propane-3.11.0.jar} +0 -0
  12. data/library/pdf/itextpdf-5.5.13.2.jar +0 -0
  13. data/library/pdf/pdf.rb +7 -0
  14. data/library/svg/batik-all-1.14.jar +0 -0
  15. data/library/svg/svg.rb +7 -0
  16. data/mvnw +3 -3
  17. data/mvnw.cmd +2 -2
  18. data/pom.rb +32 -5
  19. data/pom.xml +58 -7
  20. data/propane.gemspec +9 -5
  21. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  22. data/src/main/java/monkstone/MathToolModule.java +30 -30
  23. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  24. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  25. data/src/main/java/monkstone/fastmath/DegLutTables.java +111 -0
  26. data/src/main/java/monkstone/fastmath/Deglut.java +6 -56
  27. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  28. data/src/main/java/monkstone/noise/OpenSimplex2F.java +914 -0
  29. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -0
  30. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  31. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  32. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +27 -31
  33. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +26 -40
  34. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  35. data/src/main/java/processing/awt/PImageAWT.java +6 -4
  36. data/src/main/java/processing/core/PApplet.java +13259 -13379
  37. data/src/main/java/processing/core/PConstants.java +155 -163
  38. data/src/main/java/processing/core/PGraphics.java +118 -111
  39. data/src/main/java/processing/core/PImage.java +14 -14
  40. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13 -13
  41. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  42. data/src/main/java/processing/opengl/PShader.java +1 -6
  43. data/src/main/java/processing/opengl/PSurfaceJOGL.java +6 -6
  44. data/src/main/java/processing/pdf/PGraphicsPDF.java +581 -0
  45. data/src/main/java/processing/svg/PGraphicsSVG.java +378 -0
  46. data/src/main/{java/processing/opengl → resources}/cursors/arrow.png +0 -0
  47. data/src/main/{java/processing/opengl → resources}/cursors/cross.png +0 -0
  48. data/src/main/{java/processing/opengl → resources}/cursors/hand.png +0 -0
  49. data/src/main/{java/processing/opengl → resources}/cursors/license.txt +0 -0
  50. data/src/main/{java/processing/opengl → resources}/cursors/move.png +0 -0
  51. data/src/main/{java/processing/opengl → resources}/cursors/text.png +0 -0
  52. data/src/main/{java/processing/opengl → resources}/cursors/wait.png +0 -0
  53. data/src/main/{java/processing/opengl → resources}/shaders/ColorFrag.glsl +0 -0
  54. data/src/main/{java/processing/opengl → resources}/shaders/ColorVert.glsl +0 -0
  55. data/src/main/{java/processing/opengl → resources}/shaders/LightFrag.glsl +0 -0
  56. data/src/main/{java/processing/opengl → resources}/shaders/LightVert.glsl +0 -0
  57. data/src/main/{java/processing/opengl → resources}/shaders/LineFrag.glsl +0 -0
  58. data/src/main/{java/processing/opengl → resources}/shaders/LineVert.glsl +0 -0
  59. data/src/main/{java/processing/opengl → resources}/shaders/MaskFrag.glsl +0 -0
  60. data/src/main/{java/processing/opengl → resources}/shaders/PointFrag.glsl +0 -0
  61. data/src/main/{java/processing/opengl → resources}/shaders/PointVert.glsl +0 -0
  62. data/src/main/{java/processing/opengl → resources}/shaders/TexFrag.glsl +0 -0
  63. data/src/main/{java/processing/opengl → resources}/shaders/TexLightFrag.glsl +0 -0
  64. data/src/main/{java/processing/opengl → resources}/shaders/TexLightVert.glsl +0 -0
  65. data/src/main/{java/processing/opengl → resources}/shaders/TexVert.glsl +0 -0
  66. data/test/deglut_spec_test.rb +2 -2
  67. data/test/test_helper.rb +1 -0
  68. data/vendors/Rakefile +1 -1
  69. metadata +49 -46
  70. data/library/simplex_noise/simplex_noise.rb +0 -5
  71. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -436
@@ -0,0 +1,127 @@
1
+ /*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+ package monkstone;
7
+
8
+ import monkstone.noise.OpenSimplex2F;
9
+ import org.jruby.Ruby;
10
+ import org.jruby.RubyFixnum;
11
+ import org.jruby.RubyFloat;
12
+ import org.jruby.RubyModule;
13
+ import org.jruby.anno.JRubyMethod;
14
+ import org.jruby.anno.JRubyModule;
15
+ import org.jruby.runtime.ThreadContext;
16
+ import org.jruby.runtime.builtin.IRubyObject;
17
+
18
+ /**
19
+ *
20
+ * @author Martin Prout
21
+ */
22
+ @JRubyModule(name = "FastNoise")
23
+ public class FastNoiseModuleJava {
24
+
25
+ static OpenSimplex2F ng = new OpenSimplex2F(System.currentTimeMillis());
26
+
27
+ /**
28
+ *
29
+ * @param runtime Ruby
30
+ */
31
+ public static void createNoiseModule(Ruby runtime) {
32
+ RubyModule noiseModule = runtime.defineModule("FastNoise");
33
+ noiseModule.defineAnnotatedMethods(FastNoiseModuleJava.class);
34
+ }
35
+
36
+ /**
37
+ *
38
+ * @param context ThreadContext
39
+ * @param recv IRubyObject
40
+ * @param args array of numeric values
41
+ * @return mapped value RubyFloat
42
+ */
43
+ @JRubyMethod(name = "tnoise", rest = true, module = true)
44
+ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
45
+ double result = 0;
46
+ double one;
47
+ double two;
48
+ double three;
49
+ double four;
50
+ switch (args.length) {
51
+ case 2:
52
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
53
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
54
+ result = ng.noise2_XBeforeY(one, two);
55
+ break;
56
+ case 3:
57
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
58
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
59
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
60
+ result = ng.noise3_XYBeforeZ(one, two, three);
61
+ break;
62
+ case 4:
63
+ four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
64
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
65
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
66
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
67
+ result = ng.noise4_XYBeforeZW(one, two, three, four);
68
+ break;
69
+ default:
70
+ throw new RuntimeException("Min 2D Max 4D Noise");
71
+ }
72
+ return RubyFloat.newFloat(context.runtime, result);
73
+ }
74
+
75
+ /**
76
+ *
77
+ * @param context ThreadContext
78
+ * @param recv IRubyObject
79
+ * @param args array of numeric values
80
+ * @return mapped value RubyFloat
81
+ */
82
+ @JRubyMethod(name = "noise", rest = true, module = true)
83
+ public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
84
+ double result = 0;
85
+ double one;
86
+ double two;
87
+ double three;
88
+ double four;
89
+ switch (args.length) {
90
+ case 1:
91
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
92
+ result = ng.noise2(one, 0);
93
+ break;
94
+ case 2:
95
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
96
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
97
+ result = ng.noise2(one, two);
98
+ break;
99
+ case 3:
100
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
101
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
102
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
103
+ result = ng.noise3_Classic(one, two, three);
104
+ break;
105
+ case 4:
106
+ four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
107
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
108
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
109
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
110
+ result = ng.noise4_Classic(one, two, three, four);
111
+ break;
112
+ default:
113
+ throw new RuntimeException("Maximum of 4D Noise");
114
+ }
115
+ return RubyFloat.newFloat(context.runtime, result);
116
+ }
117
+ // @JRubyMethod(name = "noise_seed", rest = true, module = true)
118
+ // public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {
119
+ // long seed;
120
+ // if (arg instanceof RubyNumeric) {
121
+ // seed = ((RubyNumeric) arg).getLongValue();
122
+ // ng = new OpenSimplex2F(seed);
123
+ // return RubyBoolean.newBoolean(context.runtime, true);
124
+ // }
125
+ // return RubyBoolean.newBoolean(context.runtime, false);
126
+ // }
127
+ }
@@ -47,17 +47,17 @@ public class MathToolModule {
47
47
  */
48
48
  @JRubyMethod(name = "map1d", rest = true, module = true)
49
49
  public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
50
- double value = (args[0] instanceof RubyFloat)
50
+ double value = args[0] instanceof RubyFloat
51
51
  ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
52
52
  RubyRange r1 = (RubyRange) args[1];
53
53
  RubyRange r2 = (RubyRange) args[2];
54
- double first1 = (r1.first(context) instanceof RubyFloat)
54
+ double first1 = r1.first(context) instanceof RubyFloat
55
55
  ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
56
- double first2 = (r2.first(context) instanceof RubyFloat)
56
+ double first2 = r2.first(context) instanceof RubyFloat
57
57
  ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
58
- double last1 = (r1.last(context) instanceof RubyFloat)
58
+ double last1 = r1.last(context) instanceof RubyFloat
59
59
  ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
60
- double last2 = (r2.last(context) instanceof RubyFloat)
60
+ double last2 = r2.last(context) instanceof RubyFloat
61
61
  ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
62
62
  return mapMt(context, value, first1, last1, first2, last2);
63
63
  }
@@ -71,16 +71,16 @@ public class MathToolModule {
71
71
  */
72
72
  @JRubyMethod(name = "constrained_map", rest = true, module = true)
73
73
  public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
74
- double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
74
+ double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
75
75
  RubyRange r1 = (RubyRange) args[1];
76
76
  RubyRange r2 = (RubyRange) args[2];
77
- double first1 = (r1.first(context) instanceof RubyFloat)
77
+ double first1 = r1.first(context) instanceof RubyFloat
78
78
  ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
79
- double first2 = (r2.first(context) instanceof RubyFloat)
79
+ double first2 = r2.first(context) instanceof RubyFloat
80
80
  ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
81
- double last1 = (r1.last(context) instanceof RubyFloat)
81
+ double last1 = r1.last(context) instanceof RubyFloat
82
82
  ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
83
- double last2 = (r2.last(context) instanceof RubyFloat)
83
+ double last2 = r2.last(context) instanceof RubyFloat
84
84
  ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
85
85
  double max = Math.max(first1, last1);
86
86
  double min = Math.min(first1, last1);
@@ -102,11 +102,11 @@ public class MathToolModule {
102
102
  */
103
103
  @JRubyMethod(name = "p5map", rest = true, module = true)
104
104
  public static IRubyObject mapProcessing(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
105
- double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
106
- double first1 = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
107
- double first2 = (args[3] instanceof RubyFloat) ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
108
- double last1 = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
109
- double last2 = (args[4] instanceof RubyFloat) ? ((RubyFloat) args[4]).getValue() : ((RubyFixnum) args[4]).getDoubleValue();
105
+ double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
106
+ double first1 = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
107
+ double first2 = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
108
+ double last1 = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
109
+ double last2 = args[4] instanceof RubyFloat ? ((RubyFloat) args[4]).getValue() : ((RubyFixnum) args[4]).getDoubleValue();
110
110
  return mapMt(context, value, first1, last1, first2, last2);
111
111
  }
112
112
 
@@ -121,16 +121,16 @@ public class MathToolModule {
121
121
  */
122
122
  @JRubyMethod(name = "lerp", rest = true, module = true)
123
123
  public static IRubyObject lerpP(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
124
- double start = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
125
- double stop = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
126
- double amount = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
124
+ double start = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
125
+ double stop = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
126
+ double amount = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
127
127
  if (amount <= 0) {
128
128
  return args[0];
129
129
  }
130
130
  if (amount >= 1.0) {
131
131
  return args[1];
132
132
  }
133
- return context.runtime.newFloat((1 - amount) * start + (stop * amount));
133
+ return context.runtime.newFloat((1 - amount) * start + stop * amount);
134
134
  }
135
135
 
136
136
  /**
@@ -145,9 +145,9 @@ public class MathToolModule {
145
145
  */
146
146
  @JRubyMethod(name = "norm", rest = true, module = true)
147
147
  public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
148
- double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
149
- double start = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
150
- double stop = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
148
+ double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
149
+ double start = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
150
+ double stop = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
151
151
  return mapMt(context, value, start, stop, 0, 1.0);
152
152
  }
153
153
 
@@ -163,9 +163,9 @@ public class MathToolModule {
163
163
  @JRubyMethod(name = "norm_strict", rest = true, module = true)
164
164
  public static IRubyObject norm_strict(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
165
165
 
166
- double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
167
- double start = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
168
- double stop = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
166
+ double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
167
+ double start = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
168
+ double stop = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
169
169
  double max = Math.max(start, stop);
170
170
  double min = Math.min(start, stop);
171
171
  if (value < min) {
@@ -266,14 +266,14 @@ public class MathToolModule {
266
266
  int xDim = (int) args[0].toJava(Integer.class);
267
267
  int yDim = (int) args[1].toJava(Integer.class);
268
268
  int zDim = (int) args[2].toJava(Integer.class);
269
- int xStep = (args.length > 3) ? (int) args[3].toJava(Integer.class) : 1;
270
- int yStep = (args.length > 4) ? (int) args[4].toJava(Integer.class) : 1;
271
- int zStep = (args.length == 6) ? (int) args[5].toJava(Integer.class) : 1;
272
- int dimX = xDim / xStep;
269
+ int xStep = args.length > 3 ? (int) args[3].toJava(Integer.class) : 1;
270
+ int yStep = args.length > 4 ? (int) args[4].toJava(Integer.class) : 1;
271
+ int zStep = args.length == 6 ? (int) args[5].toJava(Integer.class) : 1;
272
+ // int dimX = xDim / xStep;
273
273
  int dimY = yDim / yStep;
274
274
  int dimZ = zDim / zStep;
275
275
  if (block.isGiven()) {
276
- int count = dimX * dimY * dimZ;
276
+ // int count = dimX * dimY * dimZ;
277
277
  for (int x = 0; x < xDim; x += xStep){
278
278
  for (int j = 0; j < (dimZ * dimY); j++){
279
279
  int z = j % dimZ;
@@ -28,6 +28,8 @@ public class PropaneLibrary implements Library {
28
28
  */
29
29
  public static void load(final Ruby runtime) {
30
30
  MathToolModule.createMathToolModule(runtime);
31
+ FastNoiseModuleJava.createNoiseModule(runtime);
32
+ SmoothNoiseModuleJava.createNoiseModule(runtime);
31
33
  Deglut.createDeglut(runtime);
32
34
  Vec2.createVec2(runtime);
33
35
  Vec3.createVec3(runtime);
@@ -0,0 +1,127 @@
1
+ /*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+ package monkstone;
7
+
8
+ import monkstone.noise.OpenSimplex2S;
9
+ import org.jruby.Ruby;
10
+ import org.jruby.RubyFixnum;
11
+ import org.jruby.RubyFloat;
12
+ import org.jruby.RubyModule;
13
+ import org.jruby.anno.JRubyMethod;
14
+ import org.jruby.anno.JRubyModule;
15
+ import org.jruby.runtime.ThreadContext;
16
+ import org.jruby.runtime.builtin.IRubyObject;
17
+
18
+ /**
19
+ *
20
+ * @author Martin Prout
21
+ */
22
+ @JRubyModule(name = "SmoothNoise")
23
+ public class SmoothNoiseModuleJava {
24
+
25
+ static OpenSimplex2S ng = new OpenSimplex2S(System.currentTimeMillis());
26
+
27
+ /**
28
+ *
29
+ * @param runtime Ruby
30
+ */
31
+ public static void createNoiseModule(Ruby runtime) {
32
+ RubyModule noiseModule = runtime.defineModule("SmoothNoise");
33
+ noiseModule.defineAnnotatedMethods(SmoothNoiseModuleJava.class);
34
+ }
35
+
36
+ /**
37
+ *
38
+ * @param context ThreadContext
39
+ * @param recv IRubyObject
40
+ * @param args array of numeric values
41
+ * @return mapped value RubyFloat
42
+ */
43
+ @JRubyMethod(name = "tnoise", rest = true, module = true)
44
+ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
45
+ double result = 0;
46
+ double one;
47
+ double two;
48
+ double three;
49
+ double four;
50
+ switch (args.length) {
51
+ case 2:
52
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
53
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
54
+ result = ng.noise2_XBeforeY(one, two);
55
+ break;
56
+ case 3:
57
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
58
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
59
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
60
+ result = ng.noise3_XYBeforeZ(one, two, three);
61
+ break;
62
+ case 4:
63
+ four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
64
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
65
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
66
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
67
+ result = ng.noise4_XYBeforeZW(one, two, three, four);
68
+ break;
69
+ default:
70
+ throw new RuntimeException("Min 2D Max 4D Noise");
71
+ }
72
+ return RubyFloat.newFloat(context.runtime, result);
73
+ }
74
+
75
+ /**
76
+ *
77
+ * @param context ThreadContext
78
+ * @param recv IRubyObject
79
+ * @param args array of numeric values
80
+ * @return mapped value RubyFloat
81
+ */
82
+ @JRubyMethod(name = "noise", rest = true, module = true)
83
+ public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
84
+ double result = 0;
85
+ double one;
86
+ double two;
87
+ double three;
88
+ double four;
89
+ switch (args.length) {
90
+ case 1:
91
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
92
+ result = ng.noise2(one, 0);
93
+ break;
94
+ case 2:
95
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
96
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
97
+ result = ng.noise2(one, two);
98
+ break;
99
+ case 3:
100
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
101
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
102
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
103
+ result = ng.noise3_Classic(one, two, three);
104
+ break;
105
+ case 4:
106
+ four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
107
+ three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
108
+ two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
109
+ one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
110
+ result = ng.noise4_Classic(one, two, three, four);
111
+ break;
112
+ default:
113
+ throw new RuntimeException("Maximum of 4D Noise");
114
+ }
115
+ return RubyFloat.newFloat(context.runtime, result);
116
+ }
117
+ // @JRubyMethod(name = "noise_seed", rest = true, module = true)
118
+ // public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {
119
+ // long seed;
120
+ // if (arg instanceof RubyNumeric) {
121
+ // seed = ((RubyNumeric) arg).getLongValue();
122
+ // ng = new OpenSimplex2S(seed);
123
+ // return RubyBoolean.newBoolean(context.runtime, true);
124
+ // }
125
+ // return RubyBoolean.newBoolean(context.runtime, false);
126
+ // }
127
+ }
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Copyright (c) 2021 Martin Prout
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * http://creativecommons.org/licenses/LGPL/2.1/
10
+ *
11
+ * This library is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with this library; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+ package monkstone.fastmath;
21
+
22
+ public final class DegLutTables {
23
+
24
+ /**
25
+ *
26
+ */
27
+ static public float PI = 3.1415927f;
28
+
29
+ /**
30
+ *
31
+ */
32
+ public static final float PI2 = PI * 2;
33
+ static private final int SIN_BITS = 15; // 16KB. Adjust for accuracy.
34
+ static private final int SIN_MASK = ~(-1 << SIN_BITS);
35
+ static private final int SIN_COUNT = SIN_MASK + 1;
36
+
37
+ private static final float RAD_FULL = PI * 2;
38
+ private static final float DEG_FULL = 360;
39
+ private static final float RAD_TO_INDEX = SIN_COUNT / RAD_FULL;
40
+ private static final float DEG_TO_INDEX = SIN_COUNT / DEG_FULL;
41
+
42
+ /**
43
+ * multiply by this to convert from radians to degrees
44
+ */
45
+ public static final float RADIANS_TO_DEGREES = 180f / PI;
46
+
47
+ /**
48
+ *
49
+ */
50
+ public static final float RAD_DEG = RADIANS_TO_DEGREES;
51
+ /**
52
+ * multiply by this to convert from degrees to radians
53
+ */
54
+ public static final float DEGREES_TO_RADIANS = PI / 180;
55
+
56
+ /**
57
+ *
58
+ */
59
+ public static final float DEG_RAD = DEGREES_TO_RADIANS;
60
+
61
+ static private class Sin {
62
+
63
+ static float[] table = new float[SIN_COUNT];
64
+
65
+ static {
66
+ for (int i = 0; i < SIN_COUNT; i++) {
67
+ table[i] = (float) Math.sin((i + 0.5f) / SIN_COUNT * RAD_FULL);
68
+ }
69
+ for (int i = 0; i < 360; i += 90) {
70
+ table[(int) (i * DEG_TO_INDEX) & SIN_MASK] = (float) Math.sin(i * DEGREES_TO_RADIANS);
71
+ }
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Returns the sine in radians from a lookup table.
77
+ * @param radians
78
+ * @return
79
+ */
80
+ static public float sin(float radians) {
81
+ return Sin.table[(int) (radians * RAD_TO_INDEX) & SIN_MASK];
82
+ }
83
+
84
+ /**
85
+ * Returns the cosine in radians from a lookup table.
86
+ * @param radians
87
+ * @return
88
+ */
89
+ static public float cos(float radians) {
90
+ return Sin.table[(int) ((radians + PI / 2) * RAD_TO_INDEX) & SIN_MASK];
91
+ }
92
+
93
+ /**
94
+ * Returns the sine in radians from a lookup table.
95
+ * @param degrees
96
+ * @return
97
+ */
98
+ static public float sinDeg(float degrees) {
99
+ return Sin.table[(int) (degrees * DEG_TO_INDEX) & SIN_MASK];
100
+ }
101
+
102
+ /**
103
+ * Returns the cosine in radians from a lookup table.
104
+ * @param degrees
105
+ * @return
106
+ */
107
+ static public float cosDeg(float degrees) {
108
+ return Sin.table[(int) ((degrees + 90) * DEG_TO_INDEX) & SIN_MASK];
109
+ }
110
+ }
111
+
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2015-20 Martin Prout
2
+ * Copyright (c) 2015-21 Martin Prout
3
3
  *
4
4
  * This library is free software; you can redistribute it and/or
5
5
  * modify it under the terms of the GNU Lesser General Public
@@ -20,7 +20,7 @@
20
20
  package monkstone.fastmath;
21
21
 
22
22
  import org.jruby.Ruby;
23
- import org.jruby.RubyInteger;
23
+ import org.jruby.RubyNumeric;
24
24
  import org.jruby.RubyModule;
25
25
  import org.jruby.anno.JRubyModule;
26
26
  import org.jruby.anno.JRubyMethod;
@@ -34,38 +34,6 @@ import org.jruby.runtime.builtin.IRubyObject;
34
34
  @JRubyModule(name = "DegLut")
35
35
  public class Deglut {
36
36
 
37
- /**
38
- * Lookup table for degree cosine/sine, has a fixed precision 1.0 degrees
39
- * Quite accurate but imprecise
40
- *
41
- * @author Martin Prout <martin_p@lineone.net>
42
- */
43
- static final double[] SIN_DEG_LUT = new double[91];
44
- /**
45
- *
46
- */
47
- public static final double TO_RADIANS = Math.PI / 180;
48
- /**
49
- *
50
- */
51
- private static boolean initialized = false;
52
-
53
- private final static int NINETY = 90;
54
- private final static int FULL = 360;
55
- private static final long serialVersionUID = -1466528933765940101L;
56
-
57
- /**
58
- * Initialize sin table with values (first quadrant only)
59
- */
60
- public static final void initTable() {
61
- if (initialized == false) {
62
- for (int i = 0; i <= NINETY; i++) {
63
- SIN_DEG_LUT[i] = Math.sin(TO_RADIANS * i);
64
- }
65
- initialized = true;
66
- }
67
- }
68
-
69
37
  /**
70
38
  *
71
39
  * @param runtime Ruby
@@ -73,7 +41,6 @@ public class Deglut {
73
41
  public static void createDeglut(final Ruby runtime) {
74
42
  RubyModule deglutModule = runtime.defineModule("DegLut");
75
43
  deglutModule.defineAnnotatedMethods(Deglut.class);
76
- Deglut.initTable();
77
44
  }
78
45
 
79
46
  /**
@@ -84,18 +51,9 @@ public class Deglut {
84
51
  * @return sin IRubyObject
85
52
  */
86
53
  @JRubyMethod(name = "sin", module = true)
87
-
88
54
  public static IRubyObject sin(ThreadContext context, IRubyObject recv, IRubyObject other) {
89
- int thet = (int) ((RubyInteger) other).getLongValue();
90
- while (thet < 0) {
91
- thet += FULL; // Needed because negative modulus plays badly in java
92
- }
93
- int theta = thet % FULL;
94
- int y = theta % NINETY;
95
- double result = (theta < NINETY) ? SIN_DEG_LUT[y] : (theta < 180)
96
- ? SIN_DEG_LUT[NINETY - y] : (theta < 270)
97
- ? -SIN_DEG_LUT[y] : -SIN_DEG_LUT[NINETY - y];
98
- return context.runtime.newFloat(result);
55
+ float thet = (float) ((RubyNumeric) other).getLongValue();
56
+ return context.runtime.newFloat(DegLutTables.sinDeg(thet));
99
57
  }
100
58
 
101
59
  /**
@@ -107,15 +65,7 @@ public class Deglut {
107
65
  */
108
66
  @JRubyMethod(name = "cos", module = true)
109
67
  public static IRubyObject cos(ThreadContext context, IRubyObject recv, IRubyObject other) {
110
- int thet = (int) ((RubyInteger) other).getLongValue();
111
- while (thet < 0) {
112
- thet += FULL; // Needed because negative modulus plays badly in java
113
- }
114
- int theta = thet % FULL;
115
- int y = theta % NINETY;
116
- double result = (theta < NINETY) ? SIN_DEG_LUT[NINETY - y] : (theta < 180)
117
- ? -SIN_DEG_LUT[y] : (theta < 270)
118
- ? -SIN_DEG_LUT[NINETY - y] : SIN_DEG_LUT[y];
119
- return context.runtime.newFloat(result);
68
+ float thet = (float) ((RubyNumeric) other).getLongValue();
69
+ return context.runtime.newFloat(DegLutTables.cosDeg(thet));
120
70
  }
121
71
  }
@@ -35,5 +35,5 @@ import java.io.File;
35
35
  @FunctionalInterface
36
36
  public interface Chooser {
37
37
 
38
- public void file_selected(File selection);
38
+ void file_selected(File selection);
39
39
  }