don't use three.js in worker

This commit is contained in:
casperlamboo 2018-02-01 17:57:20 +01:00
parent c927c7bec1
commit 6a63077b55
4 changed files with 25 additions and 35 deletions

View File

@ -1,4 +1,3 @@
import * as THREE from 'three';
import { normalize } from './helpers/VectorUtils.js'; import { normalize } from './helpers/VectorUtils.js';
function addLine(geometry, lineLookup, lines, a, b, faceIndex) { function addLine(geometry, lineLookup, lines, a, b, faceIndex) {
@ -9,7 +8,7 @@ function addLine(geometry, lineLookup, lines, a, b, faceIndex) {
index = lines.length; index = lines.length;
lineLookup[`${a}_${b}`] = index; 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 = []; const faces = [];
lines.push({ line, faces }); lines.push({ line, faces });
} }

View File

@ -1,4 +1,4 @@
import * as THREE from 'three'; import { divide, distanceTo } from './VectorUtils.js';
import { PRECISION, VERSION } from '../../constants.js'; import { PRECISION, VERSION } from '../../constants.js';
export const MOVE = 'G'; export const MOVE = 'G';
@ -20,7 +20,7 @@ export default class {
this._nozzleToFilamentRatio = 1; this._nozzleToFilamentRatio = 1;
this._gcode = [`; Generated with Doodle3D Slicer V${VERSION}`]; this._gcode = [`; Generated with Doodle3D Slicer V${VERSION}`];
this._currentValues = {}; this._currentValues = {};
this._nozzlePosition = new THREE.Vector2(0, 0); this._nozzlePosition = { x: 0, y: 0 };
this._extruder = 0.0; this._extruder = 0.0;
this._duration = 0.0; this._duration = 0.0;
this._isRetracted = false; this._isRetracted = false;
@ -57,8 +57,8 @@ export default class {
} }
moveTo(x, y, z, { speed }) { moveTo(x, y, z, { speed }) {
const newNozzlePosition = new THREE.Vector2(x, y).multiplyScalar(PRECISION); const newNozzlePosition = divide({ x, y }, PRECISION_INVERSE);
const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); const lineLength = distanceTo(this._nozzlePosition, newNozzlePosition);
this._duration += lineLength / speed; this._duration += lineLength / speed;
@ -70,14 +70,14 @@ export default class {
[SPEED]: toFixedTrimmed(speed * 60) [SPEED]: toFixedTrimmed(speed * 60)
}); });
this._nozzlePosition.copy(newNozzlePosition); this._nozzlePosition = newNozzlePosition;
return this; return this;
} }
lineTo(x, y, z, { speed, flowRate }) { lineTo(x, y, z, { speed, flowRate }) {
const newNozzlePosition = new THREE.Vector2(x, y).multiplyScalar(PRECISION); const newNozzlePosition = divide({ x, y }, PRECISION_INVERSE);
const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); const lineLength = distanceTo(this._nozzlePosition, newNozzlePosition);
this._extruder += this._nozzleToFilamentRatio * lineLength * flowRate; this._extruder += this._nozzleToFilamentRatio * lineLength * flowRate;
this._duration += lineLength / speed; this._duration += lineLength / speed;
@ -91,7 +91,7 @@ export default class {
[EXTRUDER]: toFixedTrimmed(this._extruder) [EXTRUDER]: toFixedTrimmed(this._extruder)
}); });
this._nozzlePosition.copy(newNozzlePosition); this._nozzlePosition = newNozzlePosition;
return this; return this;
} }

View File

@ -1,15 +1,15 @@
import * as THREE from 'three'; import { length, distanceTo } from './helpers/VectorUtils.js';
import Shape from 'clipper-js'; import Shape from 'clipper-js';
export default function optimizePaths(slices, settings) { 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 ++) { for (let layer = 0; layer < slices.length; layer ++) {
const slice = slices[layer]; const slice = slices[layer];
if (typeof slice.brim !== 'undefined' && slice.brim.paths.length > 0) { if (typeof slice.brim !== 'undefined' && slice.brim.paths.length > 0) {
slice.brim = optimizeShape(slice.brim, start); slice.brim = optimizeShape(slice.brim, start);
start.copy(slice.brim.lastPoint(true)); start = slice.brim.lastPoint(true);
} }
const parts = []; const parts = [];
@ -54,21 +54,21 @@ export default function optimizePaths(slices, settings) {
if (shell.paths.length === 0) continue; if (shell.paths.length === 0) continue;
part.shell[i] = optimizeShape(shell, start); 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) { if (part.outerFill.paths.length > 0) {
part.outerFill = optimizeShape(part.outerFill, start); part.outerFill = optimizeShape(part.outerFill, start);
start.copy(part.outerFill.lastPoint(true)); start = part.outerFill.lastPoint(true);
} }
if (part.innerFill.paths.length > 0) { if (part.innerFill.paths.length > 0) {
part.innerFill = optimizeShape(part.innerFill, start); part.innerFill = optimizeShape(part.innerFill, start);
start.copy(part.innerFill.lastPoint(true)); start = part.innerFill.lastPoint(true);
} }
} else { } else {
part.shape = optimizeShape(part.shape, start); 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) { if (typeof slice.support !== 'undefined' && slice.support.paths.length > 0) {
slice.support = optimizeShape(slice.support, start); slice.support = optimizeShape(slice.support, start);
start.copy(slice.support.lastPoint(true)); start = slice.support.lastPoint(true);
} }
} }
} }
function optimizeShape(shape, start) { function optimizeShape(shape, start) {
start = start.clone();
const inputPaths = shape.mapToLower(); const inputPaths = shape.mapToLower();
const optimizedPaths = []; const optimizedPaths = [];
const donePaths = []; const donePaths = [];
@ -102,8 +100,7 @@ function optimizeShape(shape, start) {
if (shape.closed) { if (shape.closed) {
for (let j = 0; j < path.length; j += 1) { for (let j = 0; j < path.length; j += 1) {
const point = new THREE.Vector2().copy(path[j]); const length = distanceTo(path[j], start);
const length = point.sub(start).length();
if (minLength === false || length < minLength) { if (minLength === false || length < minLength) {
minPath = path; minPath = path;
minLength = length; minLength = length;
@ -112,8 +109,7 @@ function optimizeShape(shape, start) {
} }
} }
} else { } else {
const startPoint = new THREE.Vector2().copy(path[0]); const lengthToStart = distanceTo(path[0], start);
const lengthToStart = startPoint.sub(start).length();
if (minLength === false || lengthToStart < minLength) { if (minLength === false || lengthToStart < minLength) {
minPath = path; minPath = path;
minLength = lengthToStart; minLength = lengthToStart;
@ -121,8 +117,7 @@ function optimizeShape(shape, start) {
pathIndex = i; pathIndex = i;
} }
const endPoint = new THREE.Vector2().copy(path[path.length - 1]); const lengthToEnd = distanceTo(path[path.length - 1], start);
const lengthToEnd = endPoint.sub(start).length();
if (lengthToEnd < minLength) { if (lengthToEnd < minLength) {
minPath = path; minPath = path;
minLength = lengthToEnd; minLength = lengthToEnd;
@ -132,20 +127,15 @@ function optimizeShape(shape, start) {
} }
} }
let point;
if (shape.closed) { if (shape.closed) {
minPath = minPath.concat(minPath.splice(0, offset)); minPath = minPath.concat(minPath.splice(0, offset));
point = minPath[0]; start = minPath[0];
} else { } else {
if (reverse) { if (reverse) minPath.reverse();
minPath.reverse(); start = minPath[minPath.length - 1];
}
point = minPath[minPath.length - 1];
} }
donePaths.push(pathIndex); donePaths.push(pathIndex);
start.copy(point);
optimizedPaths.push(minPath); optimizedPaths.push(minPath);
} }

View File

@ -1,5 +1,6 @@
import GCode from './helpers/GCode.js'; import GCode from './helpers/GCode.js';
import comb from './helpers/comb.js'; import comb from './helpers/comb.js';
import { divide } from './helpers/VectorUtils.js';
import { PRECISION, Z_OFFSET } from '../constants.js'; import { PRECISION, Z_OFFSET } from '../constants.js';
const PROFILE_TYPES = ['support', 'innerShell', 'outerShell', 'innerInfill', 'outerInfill', 'brim']; 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 (i === 0) {
if (combing) { 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 ++) { for (let i = 0; i < combPath.length; i ++) {
const combPoint = combPath[i]; const combPoint = combPath[i];
gcode.moveTo(combPoint.x, combPoint.y, z, travelProfile); gcode.moveTo(combPoint.x, combPoint.y, z, travelProfile);