Skip to content

Commit edf3a81

Browse files
committed
Change to use module for noise
1 parent 38ec373 commit edf3a81

11 files changed

Lines changed: 2147 additions & 4097 deletions

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
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)