Skip to content

Commit

Permalink
Merge pull request #14 from ruby-processing/OpenSimplexModule
Browse files Browse the repository at this point in the history
Open simplex module
  • Loading branch information
monkstone authored Apr 6, 2021
2 parents 38ec373 + a8752b8 commit 2330368
Show file tree
Hide file tree
Showing 18 changed files with 2,154 additions and 4,194 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
v2.4.0 Refactor noise to two modules, FastNoise (default) & SmoothNoise, with terrain noise `:tnoise` option.

v2.3.0 Abandon Processing Value Noise in favour of OpenSimplex2, with choosable options. Added pdf library using iText5.

v2.2.0 Bump to latest JOGL-rc January 2021
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Requires java to build (and [jogl-2.4.0-rc jars][jogl_jars]), but uses a maven w
```bash
cd PiCrate # or whatever you call it
rake # assumes an installed version of vanilla processing
jgem install picrate-2.2.0-java.gem
jgem install picrate-2.4.0-java.gem

```
To create a template sketch:-
Expand Down
6 changes: 1 addition & 5 deletions lib/picrate/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ module Render
java_import 'monkstone.vecmath.ShapeRender'
end

module NoiseModule
java_import 'monkstone.noise.NoiseMode'
end

# This class is the base class the user should inherit from when making
# their own sketch.
#
Expand Down Expand Up @@ -59,7 +55,7 @@ class App < PApplet
include HelperMethods
include MathTool
include Math
include NoiseModule
include FastNoise
# Alias some methods for familiarity for Shoes coders.
alias oval ellipse
alias stroke_width stroke_weight
Expand Down
2 changes: 1 addition & 1 deletion lib/picrate/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module PiCrate
VERSION = '2.3.0'
VERSION = '2.4.0'
end
2 changes: 1 addition & 1 deletion picrate.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gem::Specification.new do |gem|
gem.files << 'library/svg/batik-all-1.14.jar'
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.add_development_dependency 'minitest', '~> 5.10'
gem.add_development_dependency 'minitest', '~> 5.14'
gem.add_runtime_dependency 'rake', '~> 13.0'
gem.add_runtime_dependency 'arcball', '~> 1.0', '>= 1.0.1'
gem.require_paths = ['lib']
Expand Down
127 changes: 127 additions & 0 deletions src/main/java/monkstone/FastNoiseModuleJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package monkstone;

import monkstone.noise.OpenSimplex2F;
import org.jruby.Ruby;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyModule;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
* @author Martin Prout
*/
@JRubyModule(name = "FastNoise")
public class FastNoiseModuleJava {

static OpenSimplex2F ng = new OpenSimplex2F(System.currentTimeMillis());

/**
*
* @param runtime Ruby
*/
public static void createNoiseModule(Ruby runtime) {
RubyModule noiseModule = runtime.defineModule("FastNoise");
noiseModule.defineAnnotatedMethods(FastNoiseModuleJava.class);
}

/**
*
* @param context ThreadContext
* @param recv IRubyObject
* @param args array of numeric values
* @return mapped value RubyFloat
*/
@JRubyMethod(name = "tnoise", rest = true, module = true)
public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
double result = 0;
double one;
double two;
double three;
double four;
switch (args.length) {
case 2:
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise2_XBeforeY(one, two);
break;
case 3:
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise3_XYBeforeZ(one, two, three);
break;
case 4:
four = (args[3] instanceof RubyFloat) ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise4_XYBeforeZW(one, two, three, four);
break;
default:
throw new RuntimeException("Min 2D Max 4D Noise");
}
return RubyFloat.newFloat(context.runtime, result);
}

/**
*
* @param context ThreadContext
* @param recv IRubyObject
* @param args array of numeric values
* @return mapped value RubyFloat
*/
@JRubyMethod(name = "noise", rest = true, module = true)
public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
double result = 0;
double one;
double two;
double three;
double four;
switch (args.length) {
case 1:
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise2(one, 0);
break;
case 2:
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise2(one, two);
break;
case 3:
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise3_Classic(one, two, three);
break;
case 4:
four = (args[3] instanceof RubyFloat) ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise4_Classic(one, two, three, four);
break;
default:
throw new RuntimeException("Maximum of 4D Noise");
}
return RubyFloat.newFloat(context.runtime, result);
}
// @JRubyMethod(name = "noise_seed", rest = true, module = true)
// public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {
// long seed;
// if (arg instanceof RubyNumeric) {
// seed = ((RubyNumeric) arg).getLongValue();
// ng = new OpenSimplex2F(seed);
// return RubyBoolean.newBoolean(context.runtime, true);
// }
// return RubyBoolean.newBoolean(context.runtime, false);
// }
}
2 changes: 2 additions & 0 deletions src/main/java/monkstone/PicrateLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class PicrateLibrary implements Library{
*/
public static void load(final Ruby runtime) {
MathToolModule.createMathToolModule(runtime);
FastNoiseModuleJava.createNoiseModule(runtime);
SmoothNoiseModuleJava.createNoiseModule(runtime);
Deglut.createDeglut(runtime);
Vec2.createVec2(runtime);
Vec3.createVec3(runtime);
Expand Down
127 changes: 127 additions & 0 deletions src/main/java/monkstone/SmoothNoiseModuleJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package monkstone;

import monkstone.noise.OpenSimplex2S;
import org.jruby.Ruby;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyModule;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
* @author Martin Prout
*/
@JRubyModule(name = "SmoothNoise")
public class SmoothNoiseModuleJava {

static OpenSimplex2S ng = new OpenSimplex2S(System.currentTimeMillis());

/**
*
* @param runtime Ruby
*/
public static void createNoiseModule(Ruby runtime) {
RubyModule noiseModule = runtime.defineModule("SmoothNoise");
noiseModule.defineAnnotatedMethods(SmoothNoiseModuleJava.class);
}

/**
*
* @param context ThreadContext
* @param recv IRubyObject
* @param args array of numeric values
* @return mapped value RubyFloat
*/
@JRubyMethod(name = "tnoise", rest = true, module = true)
public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
double result = 0;
double one;
double two;
double three;
double four;
switch (args.length) {
case 2:
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise2_XBeforeY(one, two);
break;
case 3:
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise3_XYBeforeZ(one, two, three);
break;
case 4:
four = (args[3] instanceof RubyFloat) ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise4_XYBeforeZW(one, two, three, four);
break;
default:
throw new RuntimeException("Min 2D Max 4D Noise");
}
return RubyFloat.newFloat(context.runtime, result);
}

/**
*
* @param context ThreadContext
* @param recv IRubyObject
* @param args array of numeric values
* @return mapped value RubyFloat
*/
@JRubyMethod(name = "noise", rest = true, module = true)
public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
double result = 0;
double one;
double two;
double three;
double four;
switch (args.length) {
case 1:
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise2(one, 0);
break;
case 2:
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise2(one, two);
break;
case 3:
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise3_Classic(one, two, three);
break;
case 4:
four = (args[3] instanceof RubyFloat) ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
three = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
two = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
one = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
result = ng.noise4_Classic(one, two, three, four);
break;
default:
throw new RuntimeException("Maximum of 4D Noise");
}
return RubyFloat.newFloat(context.runtime, result);
}
// @JRubyMethod(name = "noise_seed", rest = true, module = true)
// public static IRubyObject noiseSeedImpl(ThreadContext context, IRubyObject recv, IRubyObject arg) {
// long seed;
// if (arg instanceof RubyNumeric) {
// seed = ((RubyNumeric) arg).getLongValue();
// ng = new OpenSimplex2S(seed);
// return RubyBoolean.newBoolean(context.runtime, true);
// }
// return RubyBoolean.newBoolean(context.runtime, false);
// }
}
Loading

0 comments on commit 2330368

Please sign in to comment.