Skip to content

Commit 2330368

Browse files
authored
Merge pull request #14 from ruby-processing/OpenSimplexModule
Open simplex module
2 parents 38ec373 + a8752b8 commit 2330368

File tree

18 files changed

+2154
-4194
lines changed

18 files changed

+2154
-4194
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
v2.4.0 Refactor noise to two modules, FastNoise (default) & SmoothNoise, with terrain noise `:tnoise` option.
2+
13
v2.3.0 Abandon Processing Value Noise in favour of OpenSimplex2, with choosable options. Added pdf library using iText5.
24

35
v2.2.0 Bump to latest JOGL-rc January 2021

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Requires java to build (and [jogl-2.4.0-rc jars][jogl_jars]), but uses a maven w
1919
```bash
2020
cd PiCrate # or whatever you call it
2121
rake # assumes an installed version of vanilla processing
22-
jgem install picrate-2.2.0-java.gem
22+
jgem install picrate-2.4.0-java.gem
2323

2424
```
2525
To create a template sketch:-

lib/picrate/app.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ module Render
1616
java_import 'monkstone.vecmath.ShapeRender'
1717
end
1818

19-
module NoiseModule
20-
java_import 'monkstone.noise.NoiseMode'
21-
end
22-
2319
# This class is the base class the user should inherit from when making
2420
# their own sketch.
2521
#
@@ -59,7 +55,7 @@ class App < PApplet
5955
include HelperMethods
6056
include MathTool
6157
include Math
62-
include NoiseModule
58+
include FastNoise
6359
# Alias some methods for familiarity for Shoes coders.
6460
alias oval ellipse
6561
alias stroke_width stroke_weight

lib/picrate/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module PiCrate
4-
VERSION = '2.3.0'
4+
VERSION = '2.4.0'
55
end

picrate.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Gem::Specification.new do |gem|
3131
gem.files << 'library/svg/batik-all-1.14.jar'
3232
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
3333
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
34-
gem.add_development_dependency 'minitest', '~> 5.10'
34+
gem.add_development_dependency 'minitest', '~> 5.14'
3535
gem.add_runtime_dependency 'rake', '~> 13.0'
3636
gem.add_runtime_dependency 'arcball', '~> 1.0', '>= 1.0.1'
3737
gem.require_paths = ['lib']
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/main/java/monkstone/PicrateLibrary.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class PicrateLibrary implements Library{
3030
*/
3131
public static void load(final Ruby runtime) {
3232
MathToolModule.createMathToolModule(runtime);
33+
FastNoiseModuleJava.createNoiseModule(runtime);
34+
SmoothNoiseModuleJava.createNoiseModule(runtime);
3335
Deglut.createDeglut(runtime);
3436
Vec2.createVec2(runtime);
3537
Vec3.createVec3(runtime);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)