Skip to content

Commit 60022f4

Browse files
authored
Merge pull request #38 from ruby-processing/delegate-noise
Delegate noise
2 parents 9b766fa + 08fc05d commit 60022f4

File tree

18 files changed

+13882
-13536
lines changed

18 files changed

+13882
-13536
lines changed

.mvn/extensions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<extension>
44
<groupId>io.takari.polyglot</groupId>
55
<artifactId>polyglot-ruby</artifactId>
6-
<version>0.4.5</version>
6+
<version>0.4.6</version>
77
</extension>
88
</extensions>

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
**v3.8.0** Refactor noise to delegate pattern, improve default implementation and add simplex noise option
2+
13
**v3.7.1** Recommend JRuby-9.2.15.0 use
24

35
**v3.5.0** Rebase processing core code around Sam Pottingers latest fixes for JOGL an image save, does mean breaking some of Diwis and Joshua Davis examples, but one hopes codeanticode knows what he's doing.

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ adjust above for your OS/distro setup.
88

99
## Requirements
1010

11-
There may be a linker issue with P2D or P3D sketches with stock openjdk with some linux distros, in which case you might find [Adopt open jdk binaries][adopt] will work for you.
11+
Previously there has been linker issue with P2D or P3D sketches with stock openjdk with some linux distros, but that seems to have gone away. [Adopt open jdk binaries][adopt] have always worked.
1212

1313
- `jdk-11.0.7+`
14-
- `jruby-9.2.14.0`
15-
16-
Currently you can ignore `illegal reflective access` warnings, see [here how to suppress them][warning].
14+
- `jruby-9.2.16.0`
1715

1816
## Building and testing
1917

@@ -27,10 +25,10 @@ rake javadoc
2725

2826
```bash
2927
jgem install propane # from rubygems
30-
jgem install propane-3.6.0-java.gem # local install
28+
jgem install propane-3.8.0-java.gem # local install
3129
# Alternative
3230
jruby -S gem install propane # from rubygems
33-
jruby -S gem install propane-3.6.0-java.gem # local install
31+
jruby -S gem install propane-3.8.0-java.gem # local install
3432
```
3533

3634
## Check Install
@@ -39,11 +37,6 @@ To check version and confirm gem bin files are on your path (also checks JDK ver
3937
```bash
4038
propane --version
4139
```
42-
43-
## Suppressing Reflective Access warnings
44-
45-
Since propane-3.5.0 it is possible to suppress reflective access warnings by setting JAVA_HOME environmental variable jruby does the rest.
46-
4740
## Usage
4841

4942
A propane sketch:-
@@ -100,7 +93,7 @@ See [gh-pages][gh-pages] for more detailed instructions and much more.
10093
```bash
10194
propane --install samples
10295
```
103-
please move existing `propane_samples` if you wish to keep them. The current release features several PixelFlow glsl library examples, including a few shadertoy demos as sketches.
96+
please move existing `propane_samples` if you wish to keep them. The current release features some noise sketches that exploit the new choice of noise implementation.
10497

10598
[adopt]: https://adoptopenjdk.net/
10699
[building]:http://ruby-processing.github.io/building/building/

lib/propane/app.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ module Render
1616
java_import 'monkstone.vecmath.ShapeRender'
1717
end
1818

19+
PROCESSING = Java::MonkstoneNoise::NoiseMode::PERLIN
20+
SIMPLEX = Java::MonkstoneNoise::NoiseMode::SIMPLEX
21+
1922
# This class is the base class the user should inherit from when making
2023
# their own sketch.
2124
#

lib/propane/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Propane
4-
VERSION = '3.7.1'
4+
VERSION = '3.8.0'
55
end

pom.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
project 'propane', 'https://github.com/monkstone/propane' do
44
model_version '4.0.0'
5-
id 'propane:propane:3.7.1'
5+
id 'propane:propane:3.8.0'
66
packaging 'jar'
77

88
description 'An integrated processing-core (somewhat hacked), with additional java code for a jruby version of processing.'
@@ -46,7 +46,7 @@
4646
'jogl.version' => '2.3.2', # for compiling actual included 2.4.0-rc
4747
'jruby.api' => 'http://jruby.org/apidocs/')
4848

49-
pom 'org.jruby:jruby:9.2.15.0'
49+
pom 'org.jruby:jruby:9.2.16.0'
5050
jar 'org.processing:video:3.3.7' # only for compiling
5151
jar 'org.jogamp.jogl:jogl-all:${jogl.version}'
5252
jar 'org.jogamp.gluegen:gluegen-rt-main:${jogl.version}'

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!--
33
44
5-
DO NOT MODIFIY - GENERATED CODE
5+
DO NOT MODIFY - GENERATED CODE
66
77
88
-->
@@ -11,7 +11,7 @@ DO NOT MODIFIY - GENERATED CODE
1111
<modelVersion>4.0.0</modelVersion>
1212
<groupId>propane</groupId>
1313
<artifactId>propane</artifactId>
14-
<version>3.7.1</version>
14+
<version>3.8.0</version>
1515
<name>propane</name>
1616
<description>An integrated processing-core (somewhat hacked), with additional java code for a jruby version of processing.</description>
1717
<url>https://github.com/monkstone/propane</url>
@@ -74,7 +74,7 @@ DO NOT MODIFIY - GENERATED CODE
7474
<dependency>
7575
<groupId>org.jruby</groupId>
7676
<artifactId>jruby</artifactId>
77-
<version>9.2.15.0</version>
77+
<version>9.2.16.0</version>
7878
<type>pom</type>
7979
</dependency>
8080
<dependency>

propane.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Gem::Specification.new do |gem|
2121
gem.files << 'lib/jogl-all.jar'
2222
gem.files << 'lib/gluegen-rt-natives-linux-amd64.jar'
2323
gem.files << 'lib/gluegen-rt-natives-macosx-universal.jar'
24-
gem.files << 'lib/gluegen-rt-natives-ios-arm64.jar'
24+
# gem.files << 'lib/gluegen-rt-natives-ios-arm64.jar'
2525
gem.files << 'lib/gluegen-rt-natives-windows-amd64.jar'
2626
gem.files << 'lib/jogl-all-natives-linux-amd64.jar'
2727
gem.files << 'lib/jogl-all-natives-macosx-universal.jar'
28-
gem.files << 'lib/jogl-all-natives-ios-arm64.jar'
28+
# gem.files << 'lib/jogl-all-natives-ios-arm64.jar'
2929
gem.files << 'lib/jogl-all-natives-windows-amd64.jar'
3030
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
3131
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2021 Martin Prout
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* http://creativecommons.org/licenses/LGPL/2.1/
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
package monkstone.fastmath;
21+
22+
public final class DegLutTables {
23+
24+
/**
25+
*
26+
*/
27+
static public final float PI = 3.1415927f;
28+
29+
/**
30+
*
31+
*/
32+
static public final float PI2 = PI * 2;
33+
static private final int SIN_BITS = 15; // 16KB. Adjust for accuracy.
34+
static private final int SIN_MASK = ~(-1 << SIN_BITS);
35+
static private final int SIN_COUNT = SIN_MASK + 1;
36+
37+
static private final float RAD_FULL = PI * 2;
38+
static private final float DEG_FULL = 360;
39+
static private final float RAD_TO_INDEX = SIN_COUNT / RAD_FULL;
40+
static private final float DEG_TO_INDEX = SIN_COUNT / DEG_FULL;
41+
42+
/**
43+
* multiply by this to convert from radians to degrees
44+
*/
45+
static public final float RADIANS_TO_DEGREES = 180f / PI;
46+
47+
/**
48+
*
49+
*/
50+
static public final float RAD_DEG = RADIANS_TO_DEGREES;
51+
/**
52+
* multiply by this to convert from degrees to radians
53+
*/
54+
static public final float DEGREES_TO_RADIANS = PI / 180;
55+
56+
/**
57+
*
58+
*/
59+
static public final float DEG_RAD = DEGREES_TO_RADIANS;
60+
61+
static private class Sin {
62+
63+
static final float[] table = new float[SIN_COUNT];
64+
65+
static {
66+
for (int i = 0; i < SIN_COUNT; i++) {
67+
table[i] = (float) Math.sin((i + 0.5f) / SIN_COUNT * RAD_FULL);
68+
}
69+
for (int i = 0; i < 360; i += 90) {
70+
table[(int) (i * DEG_TO_INDEX) & SIN_MASK] = (float) Math.sin(i * DEGREES_TO_RADIANS);
71+
}
72+
}
73+
}
74+
75+
/**
76+
* Returns the sine in radians from a lookup table.
77+
* @param radians
78+
* @return
79+
*/
80+
static public final float sin(float radians) {
81+
return Sin.table[(int) (radians * RAD_TO_INDEX) & SIN_MASK];
82+
}
83+
84+
/**
85+
* Returns the cosine in radians from a lookup table.
86+
* @param radians
87+
* @return
88+
*/
89+
static public final float cos(float radians) {
90+
return Sin.table[(int) ((radians + PI / 2) * RAD_TO_INDEX) & SIN_MASK];
91+
}
92+
93+
/**
94+
* Returns the sine in radians from a lookup table.
95+
* @param degrees
96+
* @return
97+
*/
98+
static public final float sinDeg(float degrees) {
99+
return Sin.table[(int) (degrees * DEG_TO_INDEX) & SIN_MASK];
100+
}
101+
102+
/**
103+
* Returns the cosine in radians from a lookup table.
104+
* @param degrees
105+
* @return
106+
*/
107+
static public final float cosDeg(float degrees) {
108+
return Sin.table[(int) ((degrees + 90) * DEG_TO_INDEX) & SIN_MASK];
109+
}
110+
}
111+
Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-20 Martin Prout
2+
* Copyright (c) 2015-21 Martin Prout
33
*
44
* This library is free software; you can redistribute it and/or
55
* modify it under the terms of the GNU Lesser General Public
@@ -20,7 +20,7 @@
2020
package monkstone.fastmath;
2121

2222
import org.jruby.Ruby;
23-
import org.jruby.RubyInteger;
23+
import org.jruby.RubyNumeric;
2424
import org.jruby.RubyModule;
2525
import org.jruby.anno.JRubyModule;
2626
import org.jruby.anno.JRubyMethod;
@@ -34,46 +34,13 @@
3434
@JRubyModule(name = "DegLut")
3535
public class Deglut {
3636

37-
/**
38-
* Lookup table for degree cosine/sine, has a fixed precision 1.0 degrees
39-
* Quite accurate but imprecise
40-
*
41-
* @author Martin Prout <[email protected]>
42-
*/
43-
static final double[] SIN_DEG_LUT = new double[91];
44-
/**
45-
*
46-
*/
47-
public static final double TO_RADIANS = Math.PI / 180;
48-
/**
49-
*
50-
*/
51-
private static boolean initialized = false;
52-
53-
private final static int NINETY = 90;
54-
private final static int FULL = 360;
55-
private static final long serialVersionUID = -1466528933765940101L;
56-
57-
/**
58-
* Initialize sin table with values (first quadrant only)
59-
*/
60-
public static final void initTable() {
61-
if (initialized == false) {
62-
for (int i = 0; i <= NINETY; i++) {
63-
SIN_DEG_LUT[i] = Math.sin(TO_RADIANS * i);
64-
}
65-
initialized = true;
66-
}
67-
}
68-
6937
/**
7038
*
7139
* @param runtime Ruby
7240
*/
7341
public static void createDeglut(final Ruby runtime) {
7442
RubyModule deglutModule = runtime.defineModule("DegLut");
7543
deglutModule.defineAnnotatedMethods(Deglut.class);
76-
Deglut.initTable();
7744
}
7845

7946
/**
@@ -84,18 +51,9 @@ public static void createDeglut(final Ruby runtime) {
8451
* @return sin IRubyObject
8552
*/
8653
@JRubyMethod(name = "sin", module = true)
87-
8854
public static IRubyObject sin(ThreadContext context, IRubyObject recv, IRubyObject other) {
89-
int thet = (int) ((RubyInteger) other).getLongValue();
90-
while (thet < 0) {
91-
thet += FULL; // Needed because negative modulus plays badly in java
92-
}
93-
int theta = thet % FULL;
94-
int y = theta % NINETY;
95-
double result = (theta < NINETY) ? SIN_DEG_LUT[y] : (theta < 180)
96-
? SIN_DEG_LUT[NINETY - y] : (theta < 270)
97-
? -SIN_DEG_LUT[y] : -SIN_DEG_LUT[NINETY - y];
98-
return context.runtime.newFloat(result);
55+
float thet = (float) ((RubyNumeric) other).getLongValue();
56+
return context.runtime.newFloat(DegLutTables.sinDeg(thet));
9957
}
10058

10159
/**
@@ -107,15 +65,7 @@ public static IRubyObject sin(ThreadContext context, IRubyObject recv, IRubyObje
10765
*/
10866
@JRubyMethod(name = "cos", module = true)
10967
public static IRubyObject cos(ThreadContext context, IRubyObject recv, IRubyObject other) {
110-
int thet = (int) ((RubyInteger) other).getLongValue();
111-
while (thet < 0) {
112-
thet += FULL; // Needed because negative modulus plays badly in java
113-
}
114-
int theta = thet % FULL;
115-
int y = theta % NINETY;
116-
double result = (theta < NINETY) ? SIN_DEG_LUT[NINETY - y] : (theta < 180)
117-
? -SIN_DEG_LUT[y] : (theta < 270)
118-
? -SIN_DEG_LUT[NINETY - y] : SIN_DEG_LUT[y];
119-
return context.runtime.newFloat(result);
68+
float thet = (float) ((RubyNumeric) other).getLongValue();
69+
return context.runtime.newFloat(DegLutTables.cosDeg(thet));
12070
}
12171
}

0 commit comments

Comments
 (0)