From bf9d98611aba67d4b4f57ec4167c7b3c59b63712 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 19 Aug 2016 14:46:02 +0200 Subject: [PATCH] implement async slicing --- example/app.js | 2 +- jspm.config.js | 248 +++++++++++++++++++++++- package.json | 30 ++- src/Slicer.js | 34 +++- src/sliceActions/{index.js => slice.js} | 0 src/sliceActions/sliceWorker.js | 25 +++ 6 files changed, 329 insertions(+), 10 deletions(-) rename src/sliceActions/{index.js => slice.js} (100%) create mode 100644 src/sliceActions/sliceWorker.js diff --git a/example/app.js b/example/app.js index 1030820..0f6c812 100644 --- a/example/app.js +++ b/example/app.js @@ -11,6 +11,6 @@ const geometry = new THREE.TorusGeometry(20, 10, 30, 30); const slicer = new SLICER.Slicer(); 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, '
'); }); diff --git a/jspm.config.js b/jspm.config.js index 7331673..f553d19 100644 --- a/jspm.config.js +++ b/jspm.config.js @@ -11,8 +11,6 @@ SystemJS.config({ "map": { "babel-runtime": "npm:babel-runtime@5.8.38", "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" }, "packages": { @@ -47,9 +45,28 @@ SystemJS.config({ "github:*/*.json" ], 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", "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: { "github:Doodle3D/clipper-js@master": { @@ -59,6 +76,231 @@ SystemJS.config({ }, "npm:clipper-lib@1.0.0": { "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" + } } } }); diff --git a/package.json b/package.json index 130684f..cebc3b6 100644 --- a/package.json +++ b/package.json @@ -8,14 +8,33 @@ "dependencies": { "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master", "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": { "babel-runtime": "npm:babel-runtime@^5.1.13", "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", - "plugin-babel": "npm:systemjs-plugin-babel@^0.0.12", - "process": "github:jspm/nodelibs-process@^0.2.0-alpha" + "http": "github:jspm/nodelibs-http@^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": { "npm:babel-runtime@5.8.38": { @@ -24,6 +43,11 @@ "optionalDependencies": { "core-js": "^1.2.0" } + }, + "npm:inherits@2.0.1": { + "ignore": [ + "test.js" + ] } } }, diff --git a/src/Slicer.js b/src/Slicer.js index 04686c2..e3d5327 100644 --- a/src/Slicer.js +++ b/src/Slicer.js @@ -1,5 +1,6 @@ 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 { setMesh(mesh) { @@ -29,7 +30,34 @@ export default class { return this; } - async slice(settings) { - return slice(this.geometry, settings); + async slice(settings, async = false) { + 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; } } diff --git a/src/sliceActions/index.js b/src/sliceActions/slice.js similarity index 100% rename from src/sliceActions/index.js rename to src/sliceActions/slice.js diff --git a/src/sliceActions/sliceWorker.js b/src/sliceActions/sliceWorker.js new file mode 100644 index 0000000..1880ad3 --- /dev/null +++ b/src/sliceActions/sliceWorker.js @@ -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);