Skip to content

Commit c5f46af

Browse files
committed
Use permutation table, some test cleanup
1 parent 8141fae commit c5f46af

File tree

5 files changed

+77
-67
lines changed

5 files changed

+77
-67
lines changed

.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"predef": [
3+
"define",
34
"describe",
45
"it",
56
"beforeEach"

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"author": "Jonas Wagner <[email protected]> (http://29a.ch/)",
77
"main": "./simplex-noise",
88
"devDependencies": {
9+
"alea": "0.0.9",
910
"benchmark": "~2.1.0",
1011
"chai": "^3.5.0",
1112
"mocha": "^3.2.0"
@@ -19,6 +20,7 @@
1920
"simplex",
2021
"plasma",
2122
"procedural",
23+
"generation",
2224
"gfx",
2325
"generative"
2426
],
@@ -28,6 +30,7 @@
2830
"url": "https://github.com/jwagner/simplex-noise.js.git"
2931
},
3032
"scripts": {
31-
"test": "mocha"
33+
"test": "jscs simplex-noise.js && jshint simplex-noise.js && mocha",
34+
"benchmark": "node ./perf/benchmark.js"
3235
}
3336
}

perf/benchmark.js

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
1-
var Benchmark = this.Benchmark || require('benchmark') ,
2-
SimplexNoise = this.SimplexNoise || require('../simplex-noise'),
3-
simplex = new SimplexNoise();
1+
var Benchmark = this.Benchmark || require('benchmark');
2+
var SimplexNoise = this.SimplexNoise || require('../simplex-noise');
3+
var simplex = new SimplexNoise();
44

55
var suite = new Benchmark.Suite('simplex-noise')
6-
.add('init', function() {
7-
var simplex = new SimplexNoise();
8-
})
9-
.add('noise2D', function() {
10-
for(var x = 0; x < 8; x++){
11-
for(var y = 0; y < 8; y++){
12-
for(var z = 0; z < 8; z++){
13-
simplex.noise2D(x/8, y/8);
14-
}
15-
}
6+
.add('init', function() {
7+
var simplex = new SimplexNoise();
8+
})
9+
.add('noise2D', function() {
10+
for (var x = 0; x < 8; x++) {
11+
for (var y = 0; y < 8; y++) {
12+
for (var z = 0; z < 8; z++) {
13+
simplex.noise2D(x / 8, y / 8);
1614
}
17-
})
18-
.add('noise3D', function() {
19-
for(var x = 0; x < 8; x++){
20-
for(var y = 0; y < 8; y++){
21-
for(var z = 0; z < 8; z++){
22-
simplex.noise3D(x/8, y/8, z/8);
23-
}
24-
}
15+
}
16+
}
17+
})
18+
.add('noise3D', function() {
19+
for (var x = 0; x < 8; x++) {
20+
for (var y = 0; y < 8; y++) {
21+
for (var z = 0; z < 8; z++) {
22+
simplex.noise3D(x / 8, y / 8, z / 8);
2523
}
26-
})
27-
.add('noise3D2', function() {
28-
for(var x = 0; x < 8; x++){
29-
for(var y = 0; y < 8; y++){
30-
for(var z = 0; z < 8; z++){
31-
simplex.noise3D(x/8, y/8, z/8);
32-
}
33-
}
24+
}
25+
}
26+
})
27+
.add('noise3D2', function() {
28+
for (var x = 0; x < 8; x++) {
29+
for (var y = 0; y < 8; y++) {
30+
for (var z = 0; z < 8; z++) {
31+
simplex.noise3D(x / 8, y / 8, z / 8);
3432
}
35-
})
36-
.add('noise4D', function() {
37-
for(var x = 0; x < 8; x++){
38-
for(var y = 0; y < 8; y++){
39-
for(var z = 0; z < 8; z++){
40-
simplex.noise4D(x/8, y/8, z/8, (x+y)/16);
41-
}
42-
}
33+
}
34+
}
35+
})
36+
.add('noise4D', function() {
37+
for (var x = 0; x < 8; x++) {
38+
for (var y = 0; y < 8; y++) {
39+
for (var z = 0; z < 8; z++) {
40+
simplex.noise4D(x / 8, y / 8, z / 8, (x + y) / 16);
4341
}
44-
})
45-
.on('complete', function() {
46-
this.forEach(function (bench) {
47-
console.log(bench.name + ': ' + Benchmark.formatNumber(Math.round(1.0/bench.times.period)) + ' ops/sec ±' + Math.round(bench.stats.rme) + '%');
48-
//console.log(bench.times);
49-
//console.log(bench.stats);
50-
});
51-
})
52-
.run();
42+
}
43+
}
44+
})
45+
.on('complete', function() {
46+
this.forEach(function(bench) {
47+
console.log(
48+
bench.name +
49+
': ' +
50+
Benchmark.formatNumber(Math.round(1.0 / bench.times.period)) +
51+
' ops/sec ±' +
52+
Math.round(bench.stats.rme) + '%'
53+
);
54+
});
55+
})
56+
.run();

simplex-noise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var G4 = (5.0 - Math.sqrt(5.0)) / 20.0;
4141

4242
function SimplexNoise(random) {
4343
if (!random) random = Math.random;
44-
this.p = new Uint8Array(256);
44+
this.p = buildPermutationTable(random);
4545
this.perm = new Uint8Array(512);
4646
this.permMod12 = new Uint8Array(512);
4747
for (var i = 0; i < 256; i++) {

test/simplex-noise-test.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
var SimplexNoise = require('../simplex-noise');
2-
2+
var Alea = require('alea');
33
var assert = require('chai').assert;
44

5-
describe('simplex-noise', function() {
5+
describe('SimplexNoise', function() {
66
function getRandom() {
7-
var rnd = 0;
8-
return function() {
9-
return 1.0 / (rnd++);
10-
};
7+
return new Alea('seed');
118
}
129

10+
describe('buildPermutationTable', function() {
11+
var table = SimplexNoise._buildPermutationTable(getRandom());
12+
var aTable = Array.prototype.slice.call(table);
13+
assert.lengthOf(aTable, 256);
14+
for (var i = 0; i < aTable.length; i++) {
15+
assert.include(aTable, i);
16+
}
17+
});
18+
1319
describe('constructor', function() {
1420
it('should initialize with Math.random', function() {
1521
var simplex = new SimplexNoise();
1622
assert.equal(simplex.perm.length, 512);
1723
assert.equal(simplex.permMod12.length, 512);
1824
for (var i = 0; i < 512; i++) {
19-
assert(simplex.perm[i] < 256);
20-
assert(simplex.perm[i] >= 0);
21-
assert(simplex.perm[i] >= 0);
25+
assert.isBelow(simplex.perm[i], 256);
26+
assert.isAtLeast(simplex.perm[i], 0);
2227
assert.equal(simplex.perm[i], simplex.perm[i & 255]);
2328
assert.equal(simplex.permMod12[i], simplex.perm[i] % 12);
2429
}
2530
});
2631

2732
it('should initialize with a custom random function', function() {
28-
var i = 2;
29-
var simplex = new SimplexNoise(function() {
30-
return 1.0 / i++;
31-
});
33+
var simplex = new SimplexNoise(getRandom());
3234
assert.equal(simplex.perm.length, 512);
3335
assert.equal(simplex.permMod12.length, 512);
34-
assert.equal(simplex.perm[0], 128);
35-
assert.equal(simplex.perm[1], 85);
36-
assert.equal(simplex.perm[256], 128);
37-
assert.equal(simplex.perm[257], 85);
38-
assert.equal(simplex.permMod12[0], 128 % 12);
39-
assert.equal(simplex.permMod12[1], 85 % 12);
36+
for (var i = 0; i < 512; i++) {
37+
assert.isBelow(simplex.perm[i], 256);
38+
assert.isAtLeast(simplex.perm[i], 0);
39+
assert.equal(simplex.perm[i], simplex.perm[i & 255]);
40+
assert.equal(simplex.permMod12[i], simplex.perm[i] % 12);
41+
}
4042
});
4143
});
4244

0 commit comments

Comments
 (0)