Skip to content

Commit 9545d45

Browse files
committed
Update noise
1 parent 5922305 commit 9545d45

File tree

5 files changed

+1916
-2022
lines changed

5 files changed

+1916
-2022
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/*
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-
*/
2+
* Copyright (c) 2020-2022 Martin Prout
3+
*/
64
package monkstone;
75

8-
import monkstone.noise.OpenSimplex2F;
6+
import monkstone.noise.OpenSimplex2;
97
import org.jruby.Ruby;
108
import org.jruby.RubyFixnum;
119
import org.jruby.RubyFloat;
@@ -22,8 +20,8 @@
2220
@JRubyModule(name = "FastNoise")
2321
public class FastNoiseModuleJava {
2422

25-
static OpenSimplex2F ng = new OpenSimplex2F(System.currentTimeMillis());
26-
23+
static OpenSimplex2 ng = new OpenSimplex2();
24+
static long seed = System.currentTimeMillis();
2725
/**
2826
*
2927
* @param runtime Ruby
@@ -32,6 +30,23 @@ public static void createNoiseModule(Ruby runtime) {
3230
RubyModule noiseModule = runtime.defineModule("FastNoise");
3331
noiseModule.defineAnnotatedMethods(FastNoiseModuleJava.class);
3432
}
33+
34+
35+
/**
36+
* Utility method
37+
*
38+
* @param obj
39+
* @return parse float value of object or zero
40+
*/
41+
private static double jvalue(IRubyObject obj) {
42+
if (obj instanceof RubyFloat rubyFloat) {
43+
return rubyFloat.getValue();
44+
}
45+
if (obj instanceof RubyFixnum rubyFixnum) {
46+
return rubyFixnum.getDoubleValue();
47+
}
48+
return 0;
49+
}
3550

3651
/**
3752
*
@@ -42,34 +57,37 @@ public static void createNoiseModule(Ruby runtime) {
4257
*/
4358
@JRubyMethod(name = "tnoise", rest = true, module = true)
4459
public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
45-
double result = 0;
4660
double one;
4761
double two;
4862
double three;
4963
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");
64+
double result = switch (args.length) {
65+
case 2 -> {
66+
two = jvalue(args[1]);
67+
one = jvalue(args[0]);
68+
//yield ng.noise2_XBeforeY(one, two);
69+
yield ng.noise2(seed, one, two);
70+
}
71+
case 3 -> {
72+
three = jvalue(args[2]);
73+
two = jvalue(args[1]);
74+
one = jvalue(args[0]);
75+
yield ng.noise3_ImproveXY(seed, one, two, three);
76+
}
77+
case 4 -> {
78+
four = jvalue(args[3]);
79+
three = jvalue(args[2]);
80+
two = jvalue(args[1]);
81+
one = jvalue(args[0]);
82+
yield ng.noise4_ImproveXYZ_ImproveXY(seed, one, two, three, four);
83+
}
84+
default -> { yield 2; } // yield an invalid value for noise
85+
};
86+
if (result != 2) {
87+
return RubyFloat.newFloat(context.runtime, result);
88+
} else {
89+
throw new RuntimeException("Min 2D Max 4D Noise");
7190
}
72-
return RubyFloat.newFloat(context.runtime, result);
7391
}
7492

7593
/**
@@ -81,47 +99,51 @@ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject re
8199
*/
82100
@JRubyMethod(name = "noise", rest = true, module = true)
83101
public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
84-
double result = 0;
85102
double one;
86103
double two;
87104
double three;
88105
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");
106+
double result = switch (args.length) {
107+
case 1 -> {
108+
one = jvalue(args[0]);
109+
yield ng.noise2(seed, one, 0);
110+
}
111+
case 2 -> {
112+
two = jvalue(args[1]);
113+
one = jvalue(args[0]);
114+
yield ng.noise2(seed, one, two);
115+
}
116+
case 3 -> {
117+
three = jvalue(args[2]);
118+
two = jvalue(args[1]);
119+
one = jvalue(args[0]);
120+
yield ng.noise3_ImproveXY(seed, one, two, three);
121+
}
122+
case 4 -> {
123+
four = jvalue(args[3]);
124+
three = jvalue(args[2]);
125+
two = jvalue(args[1]);
126+
one = jvalue(args[0]);
127+
yield ng.noise4_ImproveXY_ImproveZW(seed, one, two, three, four);
128+
}
129+
default -> { yield 2; } // yield an invalid value for noise
130+
};
131+
if (result != 2) {
132+
return RubyFloat.newFloat(context.runtime, result);
133+
} else {
134+
throw new RuntimeException("Min 2D Max 4D Noise");
114135
}
115-
return RubyFloat.newFloat(context.runtime, result);
116136
}
137+
}
138+
117139
// @JRubyMethod(name = "noise_seed", rest = true, module = true)
118140
// public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {
119141
// long seed;
120142
// if (arg instanceof RubyNumeric) {
121143
// seed = ((RubyNumeric) arg).getLongValue();
122-
// ng = new OpenSimplex2F(seed);
144+
// ng = new OpenSimplex2(seed);
123145
// return RubyBoolean.newBoolean(context.runtime, true);
124146
// }
125147
// return RubyBoolean.newBoolean(context.runtime, false);
126148
// }
127-
}
149+
//}

src/main/java/monkstone/SmoothNoiseModuleJava.java

+78-55
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/*
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-
*/
2+
* Copyright (c) 2020-2022 Martin Prout
3+
*/
64
package monkstone;
75

86
import monkstone.noise.OpenSimplex2S;
@@ -22,7 +20,7 @@
2220
@JRubyModule(name = "SmoothNoise")
2321
public class SmoothNoiseModuleJava {
2422

25-
static OpenSimplex2S ng = new OpenSimplex2S(System.currentTimeMillis());
23+
static OpenSimplex2S ng = new OpenSimplex2S();
2624

2725
/**
2826
*
@@ -32,6 +30,22 @@ public static void createNoiseModule(Ruby runtime) {
3230
RubyModule noiseModule = runtime.defineModule("SmoothNoise");
3331
noiseModule.defineAnnotatedMethods(SmoothNoiseModuleJava.class);
3432
}
33+
34+
/**
35+
* Utility method
36+
*
37+
* @param obj
38+
* @return parse float value of object or zero
39+
*/
40+
private static double jvalue(IRubyObject obj) {
41+
if (obj instanceof RubyFloat rubyFloat) {
42+
return rubyFloat.getValue();
43+
}
44+
if (obj instanceof RubyFixnum rubyFixnum) {
45+
return rubyFixnum.getDoubleValue();
46+
}
47+
return 0;
48+
}
3549

3650
/**
3751
*
@@ -42,34 +56,39 @@ public static void createNoiseModule(Ruby runtime) {
4256
*/
4357
@JRubyMethod(name = "tnoise", rest = true, module = true)
4458
public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
45-
double result = 0;
4659
double one;
4760
double two;
4861
double three;
4962
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");
63+
double result = switch (args.length) {
64+
case 2 -> {
65+
two = jvalue(args[1]);
66+
one = jvalue(args[0]);
67+
yield ng.noise2(System.currentTimeMillis(), one, two);
68+
}
69+
case 3 -> {
70+
three = jvalue(args[2]);
71+
two = jvalue(args[1]);
72+
one = jvalue(args[0]);
73+
yield ng.noise3_ImproveXY(System.currentTimeMillis(), one, two, three);
74+
}
75+
case 4 -> {
76+
four = jvalue(args[3]);
77+
three = jvalue(args[2]);
78+
two = jvalue(args[1]);
79+
one = jvalue(args[0]);
80+
yield ng.noise4_ImproveXYZ_ImproveXY(System.currentTimeMillis(), one, two, three, four);
81+
}
82+
default -> {
83+
yield 2;
84+
} // yield an invalid value for noise
85+
};
86+
if (result != 2) {
87+
return RubyFloat.newFloat(context.runtime, result);
88+
} else {
89+
throw new RuntimeException("Min 2D Max 4D Noise");
7190
}
72-
return RubyFloat.newFloat(context.runtime, result);
91+
7392
}
7493

7594
/**
@@ -81,38 +100,42 @@ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject re
81100
*/
82101
@JRubyMethod(name = "noise", rest = true, module = true)
83102
public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
84-
double result = 0;
85103
double one;
86104
double two;
87105
double three;
88106
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");
107+
double result = switch (args.length) {
108+
case 1 -> {
109+
one = jvalue(args[0]);
110+
yield ng.noise2(System.currentTimeMillis(), one, 0);
111+
}
112+
case 2 -> {
113+
two = jvalue(args[1]);
114+
one = jvalue(args[0]);
115+
yield ng.noise2(System.currentTimeMillis(), one, two);
116+
}
117+
case 3 -> {
118+
three = jvalue(args[2]);
119+
two = jvalue(args[1]);
120+
one = jvalue(args[0]);
121+
yield ng.noise3_ImproveXY(System.currentTimeMillis(), one, two, three);
122+
}
123+
case 4 -> {
124+
four = jvalue(args[3]);
125+
three = jvalue(args[2]);
126+
two = jvalue(args[1]);
127+
one = jvalue(args[0]);
128+
yield ng.noise4_ImproveXYZ(System.currentTimeMillis(), one, two, three, four);
129+
}
130+
default -> {
131+
yield 2;
132+
} // yield an invalid value for noise
133+
};
134+
if (result != 2) {
135+
return RubyFloat.newFloat(context.runtime, result);
136+
} else {
137+
throw new RuntimeException("Min 2D Max 4D Noise");
114138
}
115-
return RubyFloat.newFloat(context.runtime, result);
116139
}
117140
// @JRubyMethod(name = "noise_seed", rest = true, module = true)
118141
// public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {

0 commit comments

Comments
 (0)