Skip to content

Commit e5d04a3

Browse files
committed
OpenSimplexNoiseModules
1 parent c9fa9f3 commit e5d04a3

42 files changed

Lines changed: 2793 additions & 4667 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Rakefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ task :init do
1616
opengl.each do |gl|
1717
FileUtils.cp(File.join(jogl24, gl), File.join('.', 'lib'))
1818
end
19-
2019
end
2120

2221
desc 'Build gem'
@@ -31,6 +30,11 @@ task :compile do
3130
system "#{MVN} dependency:copy"
3231
end
3332

33+
desc 'pmd'
34+
task :pmd do
35+
sh './mvnw pmd:pmd'
36+
end
37+
3438
desc 'Test'
3539
task :test do
3640
system 'jruby --dev test/deglut_spec_test.rb'

lib/jruby_art/app.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class App < PApplet
4343
include Math
4444
include MathTool
4545
include Render
46+
include FastNoise
4647
# Alias some methods for familiarity for Shoes coders.
4748
# surface replaces :frame
4849
alias oval ellipse

pom.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
'itextpdf.version' => '5.5.13.2',
3434
'jruby.api' => 'http://jruby.org/apidocs/')
3535

36-
pom 'org.jruby:jruby:9.2.16.0'
36+
pom 'org.jruby:jruby:9.2.17.0'
3737
jar 'org.jogamp.jogl:jogl-all:${jogl.version}'
3838
jar 'org.jogamp.gluegen:gluegen-rt-main:${jogl.version}'
3939
jar 'org.processing:video:3.0.2'
@@ -72,6 +72,7 @@
7272
'Automatic-Module-Name' => 'processing.core'
7373
}
7474
})
75+
plugin('org.apache.maven.plugins:maven-pmd-plugin:3.14.0')
7576
plugin :jdeps, '3.1.2' do
7677
execute_goals 'jdkinternals', 'test-jdkinternals'
7778
end
@@ -90,4 +91,7 @@
9091
excludes
9192
end
9293
end
94+
reporting do
95+
plugin('org.apache.maven.plugins:mavan-jxr-plugin:2.3')
96+
end
9397
end

pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ DO NOT MODIFY - GENERATED CODE
6969
<dependency>
7070
<groupId>org.jruby</groupId>
7171
<artifactId>jruby</artifactId>
72-
<version>9.2.16.0</version>
72+
<version>9.2.17.0</version>
7373
<type>pom</type>
7474
</dependency>
7575
<dependency>
@@ -180,6 +180,10 @@ DO NOT MODIFY - GENERATED CODE
180180
</archive>
181181
</configuration>
182182
</plugin>
183+
<plugin>
184+
<artifactId>maven-pmd-plugin</artifactId>
185+
<version>3.14.0</version>
186+
</plugin>
183187
<plugin>
184188
<artifactId>maven-jdeps-plugin</artifactId>
185189
<version>3.1.2</version>
@@ -195,4 +199,12 @@ DO NOT MODIFY - GENERATED CODE
195199
</plugins>
196200
</pluginManagement>
197201
</build>
202+
<reporting>
203+
<plugins>
204+
<plugin>
205+
<artifactId>mavan-jxr-plugin</artifactId>
206+
<version>2.3</version>
207+
</plugin>
208+
</plugins>
209+
</reporting>
198210
</project>

src/main/java/monkstone/ColorUtil.java

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* This utility allows JRubyArt users to use the processing.org color method
3-
* in their sketches. Includes a method to efficiently convert an cols of web
4-
* strings to an cols of color int, and another to convert an cols of p5 color
5-
* (int) to a string that can be used in ruby code (to generate web color cols).
3+
* in their sketches. Includes a method to efficiently convert an array of web
4+
* strings to an array of color int, and another to convert an array of color
5+
* int to a string that can be used in ruby code (to generate web color array).
66
* Copyright (c) 2015-20 Martin Prout.
77
* This utility is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU Lesser General Public License as published by
@@ -13,8 +13,6 @@
1313
*/
1414
package monkstone;
1515

16-
import java.util.ArrayList;
17-
import java.util.List;
1816
import java.util.Random;
1917

2018
/**
@@ -23,8 +21,6 @@
2321
*/
2422
public class ColorUtil {
2523

26-
static final String TOO_BIG = "produces a line too long a line for code";
27-
2824
/**
2925
* Returns hex long as a positive int unless greater than Integer.MAX_VALUE
3026
* else return the complement as a negative integer or something like that
@@ -34,11 +30,7 @@ public class ColorUtil {
3430
*/
3531
static final int hexLong(long hexlong) {
3632
long SPLIT = Integer.MAX_VALUE + 1;
37-
if (hexlong < SPLIT) {
38-
return (int) hexlong;
39-
} else {
40-
return (int) (hexlong - SPLIT * 2L);
41-
}
33+
return (hexlong < SPLIT) ? (int) hexlong : (int) (hexlong - SPLIT * 2L);
4234
}
4335

4436
/**
@@ -52,7 +44,7 @@ static public int colorString(String hexstring) {
5244
/**
5345
*
5446
* @param web Array of web (hex) String
55-
* @return cols of p5 color (int)
47+
* @return array of color int according to java
5648
*/
5749
static public int[] webArray(String[] web) {
5850
int[] result = new int[web.length];
@@ -63,41 +55,22 @@ static public int[] webArray(String[] web) {
6355
}
6456

6557
/**
66-
* Return a ruby string of the form "%w[a b c]" where a, b, c are raw web
58+
* Return a ruby string of the form "%w(a b c)" where a, b, c are raw web
6759
* strings. This string can be used in ruby code.
6860
*
69-
* @param p5colors cols of p5 colors (int)
61+
* @param hex int array
7062
* @return String for use in ruby
7163
*/
72-
static public String rubyString(int[] p5colors) {
73-
if (p5colors.length > 8){ return TOO_BIG;}
74-
StringBuilder sb = new StringBuilder("%w[");
75-
for (int p5color : p5colors) {
76-
sb.append(String.format("#%06X", (0xFFFFFF & p5color)));
77-
sb.append(' ');
64+
static public String rubyString(int[] hex) {
65+
StringBuilder result = new StringBuilder("%w(");
66+
for (int i = 0; i < hex.length; i++) {
67+
result.append(String.format("#%06X", 0xFFFFFF & hex[i]));
68+
if (i < hex.length - 1) {
69+
result.append(' ');
70+
}
7871
}
79-
sb.deleteCharAt(sb.length() - 1);
80-
sb.append("]\n");
81-
return sb.toString();
82-
}
83-
84-
static public String[] p5ToWeb(int[] p5colors) {
85-
List<String> list = new ArrayList<>();
86-
for (int p5color : p5colors) {
87-
list.add(String.format("#%06X", (0xFFFFFF & p5color)));
88-
}
89-
return list.toArray(new String[0]);
90-
}
91-
92-
static public int[] shuffle(int[] cols) {
93-
Random rgen = new Random(); // Random number generator
94-
for (int i = 0; i < cols.length; i++) {
95-
int randomPosition = rgen.nextInt(cols.length);
96-
int temp = cols[i];
97-
cols[i] = cols[randomPosition];
98-
cols[randomPosition] = temp;
99-
}
100-
return cols;
72+
result.append(")\n");
73+
return result.toString();
10174
}
10275

10376
/**
@@ -127,6 +100,17 @@ static public float colorDouble(double hex) {
127100
return (float) hex;
128101
}
129102

103+
static public int[] shuffle(int[] cols) {
104+
Random rgen = new Random(); // Random number generator
105+
for (int i = 0; i < cols.length; i++) {
106+
int randomPosition = rgen.nextInt(cols.length);
107+
int temp = cols[i];
108+
cols[i] = cols[randomPosition];
109+
cols[randomPosition] = temp;
110+
}
111+
return cols;
112+
}
113+
130114
/**
131115
*
132116
* @param hue
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/JRLibrary.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class JRLibrary implements Library{
2929
* @param runtime Ruby
3030
*/
3131
public static void load(final Ruby runtime) {
32+
FastNoiseModuleJava.createNoiseModule(runtime);
33+
SmoothNoiseModuleJava.createNoiseModule(runtime);
3234
MathToolModule.createMathToolModule(runtime);
3335
Deglut.createDeglut(runtime);
3436
Rarcball.createArcBall(runtime);

0 commit comments

Comments
 (0)