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