From 6a63077b55facc02c8400275d1bfe7005ee5bbcb Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Thu, 1 Feb 2018 17:57:20 +0100 Subject: [PATCH] don't use three.js in worker --- src/sliceActions/createLines.js | 3 +-- src/sliceActions/helpers/GCode.js | 16 ++++++------- src/sliceActions/optimizePaths.js | 38 ++++++++++++------------------- src/sliceActions/slicesToGCode.js | 3 ++- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/sliceActions/createLines.js b/src/sliceActions/createLines.js index b0c23ca..3ccc624 100644 --- a/src/sliceActions/createLines.js +++ b/src/sliceActions/createLines.js @@ -1,4 +1,3 @@ -import * as THREE from 'three'; import { normalize } from './helpers/VectorUtils.js'; function addLine(geometry, lineLookup, lines, a, b, faceIndex) { @@ -9,7 +8,7 @@ function addLine(geometry, lineLookup, lines, a, b, faceIndex) { index = lines.length; lineLookup[`${a}_${b}`] = index; - const line = new THREE.Line3(geometry.vertices[a], geometry.vertices[b]); + const line = { start: geometry.vertices[a], end: geometry.vertices[b] }; const faces = []; lines.push({ line, faces }); } diff --git a/src/sliceActions/helpers/GCode.js b/src/sliceActions/helpers/GCode.js index b04d25c..bb3fd2a 100644 --- a/src/sliceActions/helpers/GCode.js +++ b/src/sliceActions/helpers/GCode.js @@ -1,4 +1,4 @@ -import * as THREE from 'three'; +import { divide, distanceTo } from './VectorUtils.js'; import { PRECISION, VERSION } from '../../constants.js'; export const MOVE = 'G'; @@ -20,7 +20,7 @@ export default class { this._nozzleToFilamentRatio = 1; this._gcode = [`; Generated with Doodle3D Slicer V${VERSION}`]; this._currentValues = {}; - this._nozzlePosition = new THREE.Vector2(0, 0); + this._nozzlePosition = { x: 0, y: 0 }; this._extruder = 0.0; this._duration = 0.0; this._isRetracted = false; @@ -57,8 +57,8 @@ export default class { } moveTo(x, y, z, { speed }) { - const newNozzlePosition = new THREE.Vector2(x, y).multiplyScalar(PRECISION); - const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); + const newNozzlePosition = divide({ x, y }, PRECISION_INVERSE); + const lineLength = distanceTo(this._nozzlePosition, newNozzlePosition); this._duration += lineLength / speed; @@ -70,14 +70,14 @@ export default class { [SPEED]: toFixedTrimmed(speed * 60) }); - this._nozzlePosition.copy(newNozzlePosition); + this._nozzlePosition = newNozzlePosition; return this; } lineTo(x, y, z, { speed, flowRate }) { - const newNozzlePosition = new THREE.Vector2(x, y).multiplyScalar(PRECISION); - const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); + const newNozzlePosition = divide({ x, y }, PRECISION_INVERSE); + const lineLength = distanceTo(this._nozzlePosition, newNozzlePosition); this._extruder += this._nozzleToFilamentRatio * lineLength * flowRate; this._duration += lineLength / speed; @@ -91,7 +91,7 @@ export default class { [EXTRUDER]: toFixedTrimmed(this._extruder) }); - this._nozzlePosition.copy(newNozzlePosition); + this._nozzlePosition = newNozzlePosition; return this; } diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js index cde3fb3..1392311 100644 --- a/src/sliceActions/optimizePaths.js +++ b/src/sliceActions/optimizePaths.js @@ -1,15 +1,15 @@ -import * as THREE from 'three'; +import { length, distanceTo } from './helpers/VectorUtils.js'; import Shape from 'clipper-js'; export default function optimizePaths(slices, settings) { - const start = new THREE.Vector2(0, 0); + let start = { x: 0, y: 0 }; for (let layer = 0; layer < slices.length; layer ++) { const slice = slices[layer]; if (typeof slice.brim !== 'undefined' && slice.brim.paths.length > 0) { slice.brim = optimizeShape(slice.brim, start); - start.copy(slice.brim.lastPoint(true)); + start = slice.brim.lastPoint(true); } const parts = []; @@ -54,21 +54,21 @@ export default function optimizePaths(slices, settings) { if (shell.paths.length === 0) continue; part.shell[i] = optimizeShape(shell, start); - start.copy(part.shell[i].lastPoint(true)); + start = part.shell[i].lastPoint(true); } if (part.outerFill.paths.length > 0) { part.outerFill = optimizeShape(part.outerFill, start); - start.copy(part.outerFill.lastPoint(true)); + start = part.outerFill.lastPoint(true); } if (part.innerFill.paths.length > 0) { part.innerFill = optimizeShape(part.innerFill, start); - start.copy(part.innerFill.lastPoint(true)); + start = part.innerFill.lastPoint(true); } } else { part.shape = optimizeShape(part.shape, start); - start.copy(part.shape.lastPoint(true)); + start = part.shape.lastPoint(true); } } @@ -76,14 +76,12 @@ export default function optimizePaths(slices, settings) { if (typeof slice.support !== 'undefined' && slice.support.paths.length > 0) { slice.support = optimizeShape(slice.support, start); - start.copy(slice.support.lastPoint(true)); + start = slice.support.lastPoint(true); } } } function optimizeShape(shape, start) { - start = start.clone(); - const inputPaths = shape.mapToLower(); const optimizedPaths = []; const donePaths = []; @@ -102,8 +100,7 @@ function optimizeShape(shape, start) { if (shape.closed) { for (let j = 0; j < path.length; j += 1) { - const point = new THREE.Vector2().copy(path[j]); - const length = point.sub(start).length(); + const length = distanceTo(path[j], start); if (minLength === false || length < minLength) { minPath = path; minLength = length; @@ -112,8 +109,7 @@ function optimizeShape(shape, start) { } } } else { - const startPoint = new THREE.Vector2().copy(path[0]); - const lengthToStart = startPoint.sub(start).length(); + const lengthToStart = distanceTo(path[0], start); if (minLength === false || lengthToStart < minLength) { minPath = path; minLength = lengthToStart; @@ -121,8 +117,7 @@ function optimizeShape(shape, start) { pathIndex = i; } - const endPoint = new THREE.Vector2().copy(path[path.length - 1]); - const lengthToEnd = endPoint.sub(start).length(); + const lengthToEnd = distanceTo(path[path.length - 1], start); if (lengthToEnd < minLength) { minPath = path; minLength = lengthToEnd; @@ -132,20 +127,15 @@ function optimizeShape(shape, start) { } } - let point; if (shape.closed) { minPath = minPath.concat(minPath.splice(0, offset)); - point = minPath[0]; + start = minPath[0]; } else { - if (reverse) { - minPath.reverse(); - } - point = minPath[minPath.length - 1]; + if (reverse) minPath.reverse(); + start = minPath[minPath.length - 1]; } donePaths.push(pathIndex); - start.copy(point); - optimizedPaths.push(minPath); } diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js index 1b7aa98..58a3ea8 100644 --- a/src/sliceActions/slicesToGCode.js +++ b/src/sliceActions/slicesToGCode.js @@ -1,5 +1,6 @@ import GCode from './helpers/GCode.js'; import comb from './helpers/comb.js'; +import { divide } from './helpers/VectorUtils.js'; import { PRECISION, Z_OFFSET } from '../constants.js'; const PROFILE_TYPES = ['support', 'innerShell', 'outerShell', 'innerInfill', 'outerInfill', 'brim']; @@ -94,7 +95,7 @@ function pathToGCode(outline, combing, gcode, shape, retract, unRetract, z, { li if (i === 0) { if (combing) { - const combPath = comb(outline, gcode._nozzlePosition.divideScalar(PRECISION), point); + const combPath = comb(outline, divide(gcode._nozzlePosition, PRECISION), point); for (let i = 0; i < combPath.length; i ++) { const combPoint = combPath[i]; gcode.moveTo(combPoint.x, combPoint.y, z, travelProfile);