From 45b0f541b136328d153838b8afcb964a9fb36cdc Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 12 Feb 2018 10:28:42 +0100 Subject: [PATCH] send typed array instead of string across worker --- src/sliceActions/helpers/binary.js | 15 +++++++++++++++ src/slicer.js | 6 +++++- src/slicer.worker.js | 5 ++++- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/sliceActions/helpers/binary.js diff --git a/src/sliceActions/helpers/binary.js b/src/sliceActions/helpers/binary.js new file mode 100644 index 0000000..7e6b33a --- /dev/null +++ b/src/sliceActions/helpers/binary.js @@ -0,0 +1,15 @@ +export function stringToTypedArray(string) { + const array = new Uint8Array(string.length); + for (let i = 0; i < string.length; i ++) { + array[i] = string.charCodeAt(i); + } + return array; +} + +export function typedArrayToString(array) { + let string = ''; + for (let i = 0; i < array.length; i ++) { + string += String.fromCharCode(array[i]); + } + return string; +} diff --git a/src/slicer.js b/src/slicer.js index 79590cb..2ebba5c 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -1,6 +1,7 @@ import * as THREE from 'three'; import slice from './sliceActions/slice.js'; import SlicerWorker from './slicer.worker.js'; +import { typedArrayToString } from './sliceActions/helpers/binary.js'; export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) { if (!mesh || !mesh.isMesh) { @@ -95,6 +96,9 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview, case 'SLICE': { slicerWorker.terminate(); + const { gcode } = data; + gcode.gcode = typedArrayToString(gcode.gcode); + // if (data.gcode.linePreview) { // const geometry = new THREE.BufferGeometry(); // @@ -108,7 +112,7 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview, // data.gcode.linePreview = linePreview; // } - resolve(data.gcode); + resolve(gcode); break; } case 'PROGRESS': { diff --git a/src/slicer.worker.js b/src/slicer.worker.js index 4ebcb50..e55f9e9 100644 --- a/src/slicer.worker.js +++ b/src/slicer.worker.js @@ -1,5 +1,6 @@ import 'core-js'; // polyfills import slice from './sliceActions/slice.js'; +import { stringToTypedArray } from './sliceActions/helpers/binary.js'; const onProgress = progress => { self.postMessage({ @@ -15,8 +16,10 @@ self.addEventListener('message', (event) => { const { settings, geometry, constructLinePreview, openObjectIndexes } = data; const gcode = slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress); + gcode.gcode = stringToTypedArray(gcode.gcode); + + const buffers = [gcode.gcode.buffer]; - const buffers = []; // if (gcode.linePreview) { // const position = gcode.linePreview.geometry.getAttribute('position').array; // const color = gcode.linePreview.geometry.getAttribute('color').array;