implement async slicing

This commit is contained in:
casperlamboo 2016-08-19 14:46:02 +02:00 committed by Simon Voordouw
parent c6a9d5adb8
commit e457994496
6 changed files with 329 additions and 10 deletions

View File

@ -11,6 +11,6 @@ const geometry = new THREE.TorusGeometry(20, 10, 30, 30);
const slicer = new SLICER.Slicer(); const slicer = new SLICER.Slicer();
slicer.setGeometry(geometry); slicer.setGeometry(geometry);
const gcode = slicer.slice(settings).then(gcode => { slicer.slice(settings, false).then(gcode => {
document.getElementById('gcode').innerHTML = gcode.replace(/(?:\r\n|\r|\n)/g, '<br />'); document.getElementById('gcode').innerHTML = gcode.replace(/(?:\r\n|\r|\n)/g, '<br />');
}); });

View File

@ -11,8 +11,6 @@ SystemJS.config({
"map": { "map": {
"babel-runtime": "npm:babel-runtime@5.8.38", "babel-runtime": "npm:babel-runtime@5.8.38",
"core-js": "npm:core-js@0.9.18", "core-js": "npm:core-js@0.9.18",
"process": "github:jspm/nodelibs-process@0.2.0-alpha",
"fs": "github:jspm/nodelibs-fs@0.2.0-alpha",
"plugin-babel": "npm:systemjs-plugin-babel@0.0.12" "plugin-babel": "npm:systemjs-plugin-babel@0.0.12"
}, },
"packages": { "packages": {
@ -47,9 +45,28 @@ SystemJS.config({
"github:*/*.json" "github:*/*.json"
], ],
map: { map: {
"assert": "github:jspm/nodelibs-assert@0.2.0-alpha",
"buffer": "github:jspm/nodelibs-buffer@0.2.0-alpha",
"child_process": "github:jspm/nodelibs-child_process@0.2.0-alpha",
"constants": "github:jspm/nodelibs-constants@0.2.0-alpha",
"crypto": "github:jspm/nodelibs-crypto@0.2.0-alpha",
"events": "github:jspm/nodelibs-events@0.2.0-alpha",
"fs": "github:jspm/nodelibs-fs@0.2.0-alpha",
"http": "github:jspm/nodelibs-http@0.2.0-alpha",
"json": "github:systemjs/plugin-json@0.1.2", "json": "github:systemjs/plugin-json@0.1.2",
"Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master", "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master",
"three.js": "github:mrdoob/three.js@r72" "module": "github:jspm/nodelibs-module@0.2.0-alpha",
"os": "github:jspm/nodelibs-os@0.2.0-alpha",
"path": "github:jspm/nodelibs-path@0.2.0-alpha",
"process": "github:jspm/nodelibs-process@0.2.0-alpha",
"stream": "github:jspm/nodelibs-stream@0.2.0-alpha",
"string_decoder": "github:jspm/nodelibs-string_decoder@0.2.0-alpha",
"three.js": "github:mrdoob/three.js@r75",
"tty": "github:jspm/nodelibs-tty@0.2.0-alpha",
"url": "github:jspm/nodelibs-url@0.2.0-alpha",
"util": "github:jspm/nodelibs-util@0.2.0-alpha",
"vm": "github:jspm/nodelibs-vm@0.2.0-alpha",
"worker": "github:casperlamboo/plugin-worker@master"
}, },
packages: { packages: {
"github:Doodle3D/clipper-js@master": { "github:Doodle3D/clipper-js@master": {
@ -59,6 +76,231 @@ SystemJS.config({
}, },
"npm:clipper-lib@1.0.0": { "npm:clipper-lib@1.0.0": {
"map": {} "map": {}
},
"github:jspm/nodelibs-stream@0.2.0-alpha": {
"map": {
"stream-browserify": "npm:stream-browserify@2.0.1"
}
},
"github:jspm/nodelibs-buffer@0.2.0-alpha": {
"map": {
"buffer-browserify": "npm:buffer@4.9.1"
}
},
"github:jspm/nodelibs-string_decoder@0.2.0-alpha": {
"map": {
"string_decoder-browserify": "npm:string_decoder@0.10.31"
}
},
"github:jspm/nodelibs-url@0.2.0-alpha": {
"map": {
"url-browserify": "npm:url@0.11.0"
}
},
"github:jspm/nodelibs-http@0.2.0-alpha": {
"map": {
"http-browserify": "npm:stream-http@2.3.1"
}
},
"npm:stream-browserify@2.0.1": {
"map": {
"inherits": "npm:inherits@2.0.1",
"readable-stream": "npm:readable-stream@2.1.5"
}
},
"github:jspm/nodelibs-crypto@0.2.0-alpha": {
"map": {
"crypto-browserify": "npm:crypto-browserify@3.11.0"
}
},
"npm:buffer@4.9.1": {
"map": {
"base64-js": "npm:base64-js@1.1.2",
"isarray": "npm:isarray@1.0.0",
"ieee754": "npm:ieee754@1.1.6"
}
},
"npm:stream-http@2.3.1": {
"map": {
"inherits": "npm:inherits@2.0.1",
"readable-stream": "npm:readable-stream@2.1.5",
"builtin-status-codes": "npm:builtin-status-codes@2.0.0",
"to-arraybuffer": "npm:to-arraybuffer@1.0.1",
"xtend": "npm:xtend@4.0.1"
}
},
"npm:url@0.11.0": {
"map": {
"querystring": "npm:querystring@0.2.0",
"punycode": "npm:punycode@1.3.2"
}
},
"npm:readable-stream@2.1.5": {
"map": {
"string_decoder": "npm:string_decoder@0.10.31",
"inherits": "npm:inherits@2.0.1",
"isarray": "npm:isarray@1.0.0",
"buffer-shims": "npm:buffer-shims@1.0.0",
"core-util-is": "npm:core-util-is@1.0.2",
"process-nextick-args": "npm:process-nextick-args@1.0.7",
"util-deprecate": "npm:util-deprecate@1.0.2"
}
},
"npm:crypto-browserify@3.11.0": {
"map": {
"inherits": "npm:inherits@2.0.1",
"browserify-cipher": "npm:browserify-cipher@1.0.0",
"create-hash": "npm:create-hash@1.1.2",
"browserify-sign": "npm:browserify-sign@4.0.0",
"pbkdf2": "npm:pbkdf2@3.0.4",
"public-encrypt": "npm:public-encrypt@4.0.0",
"randombytes": "npm:randombytes@2.0.3",
"diffie-hellman": "npm:diffie-hellman@5.0.2",
"create-ecdh": "npm:create-ecdh@4.0.0",
"create-hmac": "npm:create-hmac@1.1.4"
}
},
"npm:browserify-sign@4.0.0": {
"map": {
"create-hash": "npm:create-hash@1.1.2",
"inherits": "npm:inherits@2.0.1",
"create-hmac": "npm:create-hmac@1.1.4",
"parse-asn1": "npm:parse-asn1@5.0.0",
"elliptic": "npm:elliptic@6.3.1",
"bn.js": "npm:bn.js@4.11.6",
"browserify-rsa": "npm:browserify-rsa@4.0.1"
}
},
"npm:create-hash@1.1.2": {
"map": {
"inherits": "npm:inherits@2.0.1",
"ripemd160": "npm:ripemd160@1.0.1",
"cipher-base": "npm:cipher-base@1.0.2",
"sha.js": "npm:sha.js@2.4.5"
}
},
"npm:public-encrypt@4.0.0": {
"map": {
"randombytes": "npm:randombytes@2.0.3",
"create-hash": "npm:create-hash@1.1.2",
"parse-asn1": "npm:parse-asn1@5.0.0",
"bn.js": "npm:bn.js@4.11.6",
"browserify-rsa": "npm:browserify-rsa@4.0.1"
}
},
"npm:pbkdf2@3.0.4": {
"map": {
"create-hmac": "npm:create-hmac@1.1.4"
}
},
"npm:diffie-hellman@5.0.2": {
"map": {
"randombytes": "npm:randombytes@2.0.3",
"bn.js": "npm:bn.js@4.11.6",
"miller-rabin": "npm:miller-rabin@4.0.0"
}
},
"npm:create-hmac@1.1.4": {
"map": {
"create-hash": "npm:create-hash@1.1.2",
"inherits": "npm:inherits@2.0.1"
}
},
"npm:browserify-cipher@1.0.0": {
"map": {
"browserify-des": "npm:browserify-des@1.0.0",
"evp_bytestokey": "npm:evp_bytestokey@1.0.0",
"browserify-aes": "npm:browserify-aes@1.0.6"
}
},
"npm:create-ecdh@4.0.0": {
"map": {
"elliptic": "npm:elliptic@6.3.1",
"bn.js": "npm:bn.js@4.11.6"
}
},
"npm:parse-asn1@5.0.0": {
"map": {
"create-hash": "npm:create-hash@1.1.2",
"pbkdf2": "npm:pbkdf2@3.0.4",
"browserify-aes": "npm:browserify-aes@1.0.6",
"evp_bytestokey": "npm:evp_bytestokey@1.0.0",
"asn1.js": "npm:asn1.js@4.8.0"
}
},
"npm:browserify-des@1.0.0": {
"map": {
"inherits": "npm:inherits@2.0.1",
"cipher-base": "npm:cipher-base@1.0.2",
"des.js": "npm:des.js@1.0.0"
}
},
"npm:evp_bytestokey@1.0.0": {
"map": {
"create-hash": "npm:create-hash@1.1.2"
}
},
"npm:browserify-aes@1.0.6": {
"map": {
"create-hash": "npm:create-hash@1.1.2",
"evp_bytestokey": "npm:evp_bytestokey@1.0.0",
"inherits": "npm:inherits@2.0.1",
"cipher-base": "npm:cipher-base@1.0.2",
"buffer-xor": "npm:buffer-xor@1.0.3"
}
},
"npm:elliptic@6.3.1": {
"map": {
"bn.js": "npm:bn.js@4.11.6",
"inherits": "npm:inherits@2.0.1",
"hash.js": "npm:hash.js@1.0.3",
"brorand": "npm:brorand@1.0.5"
}
},
"npm:cipher-base@1.0.2": {
"map": {
"inherits": "npm:inherits@2.0.1"
}
},
"npm:sha.js@2.4.5": {
"map": {
"inherits": "npm:inherits@2.0.1"
}
},
"npm:browserify-rsa@4.0.1": {
"map": {
"bn.js": "npm:bn.js@4.11.6",
"randombytes": "npm:randombytes@2.0.3"
}
},
"npm:miller-rabin@4.0.0": {
"map": {
"bn.js": "npm:bn.js@4.11.6",
"brorand": "npm:brorand@1.0.5"
}
},
"npm:des.js@1.0.0": {
"map": {
"inherits": "npm:inherits@2.0.1",
"minimalistic-assert": "npm:minimalistic-assert@1.0.0"
}
},
"npm:asn1.js@4.8.0": {
"map": {
"bn.js": "npm:bn.js@4.11.6",
"inherits": "npm:inherits@2.0.1",
"minimalistic-assert": "npm:minimalistic-assert@1.0.0"
}
},
"npm:hash.js@1.0.3": {
"map": {
"inherits": "npm:inherits@2.0.1"
}
},
"github:jspm/nodelibs-os@0.2.0-alpha": {
"map": {
"os-browserify": "npm:os-browserify@0.2.1"
}
} }
} }
}); });

View File

@ -8,14 +8,33 @@
"dependencies": { "dependencies": {
"Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master", "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master",
"json": "github:systemjs/plugin-json@^0.1.2", "json": "github:systemjs/plugin-json@^0.1.2",
"three.js": "github:mrdoob/three.js@r72" "three.js": "github:mrdoob/three.js@r75",
"worker": "github:casperlamboo/plugin-worker@master"
}, },
"devDependencies": { "devDependencies": {
"babel-runtime": "npm:babel-runtime@^5.1.13", "babel-runtime": "npm:babel-runtime@^5.1.13",
"core-js": "npm:core-js@^0.9.4", "core-js": "npm:core-js@^0.9.4",
"plugin-babel": "npm:systemjs-plugin-babel@^0.0.12"
},
"peerDependencies": {
"assert": "github:jspm/nodelibs-assert@^0.2.0-alpha",
"buffer": "github:jspm/nodelibs-buffer@^0.2.0-alpha",
"child_process": "github:jspm/nodelibs-child_process@^0.2.0-alpha",
"constants": "github:jspm/nodelibs-constants@^0.2.0-alpha",
"crypto": "github:jspm/nodelibs-crypto@^0.2.0-alpha",
"events": "github:jspm/nodelibs-events@^0.2.0-alpha",
"fs": "github:jspm/nodelibs-fs@^0.2.0-alpha", "fs": "github:jspm/nodelibs-fs@^0.2.0-alpha",
"plugin-babel": "npm:systemjs-plugin-babel@^0.0.12", "http": "github:jspm/nodelibs-http@^0.2.0-alpha",
"process": "github:jspm/nodelibs-process@^0.2.0-alpha" "module": "github:jspm/nodelibs-module@^0.2.0-alpha",
"os": "github:jspm/nodelibs-os@^0.2.0-alpha",
"path": "github:jspm/nodelibs-path@^0.2.0-alpha",
"process": "github:jspm/nodelibs-process@^0.2.0-alpha",
"stream": "github:jspm/nodelibs-stream@^0.2.0-alpha",
"string_decoder": "github:jspm/nodelibs-string_decoder@^0.2.0-alpha",
"tty": "github:jspm/nodelibs-tty@^0.2.0-alpha",
"url": "github:jspm/nodelibs-url@^0.2.0-alpha",
"util": "github:jspm/nodelibs-util@^0.2.0-alpha",
"vm": "github:jspm/nodelibs-vm@^0.2.0-alpha"
}, },
"overrides": { "overrides": {
"npm:babel-runtime@5.8.38": { "npm:babel-runtime@5.8.38": {
@ -24,6 +43,11 @@
"optionalDependencies": { "optionalDependencies": {
"core-js": "^1.2.0" "core-js": "^1.2.0"
} }
},
"npm:inherits@2.0.1": {
"ignore": [
"test.js"
]
} }
} }
}, },

View File

@ -1,5 +1,6 @@
import THREE from 'three.js'; import THREE from 'three.js';
import slice from './sliceActions/index.js'; import slice from './sliceActions/slice.js';
import SliceWorker from './sliceActions/sliceWorker.js!worker';
export default class { export default class {
setMesh(mesh) { setMesh(mesh) {
@ -29,7 +30,34 @@ export default class {
return this; return this;
} }
async slice(settings) { async slice(settings, async = false) {
return slice(this.geometry, settings); let gcode;
if (async) {
const sliceWorker = new SliceWorker();
const geometry = this.geometry.toJSON();
const { config } = settings;
gcode = await new Promise((resolve) => {
sliceWorker.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
case 'SLICE': {
sliceWorker.terminate();
resolve(data.gcode);
}
}
});
sliceWorker.postMessage({
message: 'SLICE',
data: { geometry, config }
});
});
} else {
gcode = slice(this.geometry, settings);
}
return gcode;
} }
} }

View File

@ -0,0 +1,25 @@
import Settings from '../Settings.js';
import slice from './slice.js';
import THREE from 'three.js';
const loader = new THREE.JSONLoader();
self.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
case 'SLICE': {
const { geometry: JSONGeometry, config } = data;
const { geometry } = new loader.parse(JSONGeometry.data);
const settings = new Settings(config);
const gcode = slice(geometry, settings);
self.postMessage({
message: 'SLICE',
data: { gcode }
});
break;
}
}
}, false);