From 3db03cd873af336e5bd7bd5b45f19918c21d47ef Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 00:26:58 +0200 Subject: [PATCH 01/64] separate into actions into different files --- .../calculateLayersIntersections.js | 50 ++ src/sliceActions/createLines.js | 49 ++ src/sliceActions/generateInfills.js | 82 ++ src/sliceActions/generateInnerLines.js | 44 ++ src/sliceActions/generateSupport.js | 71 ++ src/sliceActions/getFillTemplate.js | 31 + src/sliceActions/intersectionsToShapes.js | 127 ++++ src/sliceActions/optimizePaths.js | 42 ++ src/sliceActions/shapesToSlices.js | 65 ++ src/sliceActions/slicesToGCode.js | 73 ++ src/slicer.js | 706 +----------------- 11 files changed, 673 insertions(+), 667 deletions(-) create mode 100644 src/sliceActions/calculateLayersIntersections.js create mode 100644 src/sliceActions/createLines.js create mode 100644 src/sliceActions/generateInfills.js create mode 100644 src/sliceActions/generateInnerLines.js create mode 100644 src/sliceActions/generateSupport.js create mode 100644 src/sliceActions/getFillTemplate.js create mode 100644 src/sliceActions/intersectionsToShapes.js create mode 100644 src/sliceActions/optimizePaths.js create mode 100644 src/sliceActions/shapesToSlices.js create mode 100644 src/sliceActions/slicesToGCode.js diff --git a/src/sliceActions/calculateLayersIntersections.js b/src/sliceActions/calculateLayersIntersections.js new file mode 100644 index 0000000..ccdcca2 --- /dev/null +++ b/src/sliceActions/calculateLayersIntersections.js @@ -0,0 +1,50 @@ +import THREE from 'three.js'; + +export default function calculateLayersIntersections(lines, settings) { + console.log('calculating layer intersections'); + + var layerHeight = settings.config["layerHeight"]; + var height = settings.config["dimensionsZ"]; + + var numLayers = Math.floor(height / layerHeight); + + var layerIntersectionIndexes = []; + var layerIntersectionPoints = []; + for (var layer = 0; layer < numLayers; layer ++) { + layerIntersectionIndexes[layer] = []; + layerIntersectionPoints[layer] = []; + } + + for (var lineIndex = 0; lineIndex < lines.length; lineIndex ++) { + var line = lines[lineIndex].line; + + var min = Math.ceil(Math.min(line.start.y, line.end.y) / layerHeight); + var max = Math.floor(Math.max(line.start.y, line.end.y) / layerHeight); + + for (var layerIndex = min; layerIndex <= max; layerIndex ++) { + if (layerIndex >= 0 && layerIndex < numLayers) { + + layerIntersectionIndexes[layerIndex].push(lineIndex); + + var y = layerIndex * layerHeight; + + if (line.start.y === line.end.y) { + var x = line.start.x; + var z = line.start.z; + } + else { + var alpha = (y - line.start.y) / (line.end.y - line.start.y); + var x = line.end.x * alpha + line.start.x * (1 - alpha); + var z = line.end.z * alpha + line.start.z * (1 - alpha); + } + + layerIntersectionPoints[layerIndex][lineIndex] = new THREE.Vector2(z, x); + } + } + } + + return { + layerIntersectionIndexes, + layerIntersectionPoints + }; +} diff --git a/src/sliceActions/createLines.js b/src/sliceActions/createLines.js new file mode 100644 index 0000000..1fa6603 --- /dev/null +++ b/src/sliceActions/createLines.js @@ -0,0 +1,49 @@ +import THREE from 'three.js'; + +export default function createLines(geometry, settings) { + console.log('constructing unique lines from geometry'); + + var lines = []; + var lineLookup = {}; + + var addLine = (a, b) => { + var index = lineLookup[b + '_' + a]; + + if (index === undefined) { + index = lines.length; + lineLookup[a + '_' + b] = index; + + lines.push({ + line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]), + connects: [], + normals: [] + }); + } + + return index; + } + + for (var i = 0; i < geometry.faces.length; i ++) { + var face = geometry.faces[i]; + if (face.normal.y !== 1 && face.normal.y !== -1) { + var normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize(); + + // check for only adding unique lines + // returns index of said line + var a = addLine(face.a, face.b); + var b = addLine(face.b, face.c); + var c = addLine(face.c, face.a); + + // set connecting lines (based on face) + lines[a].connects.push(b, c); + lines[b].connects.push(c, a); + lines[c].connects.push(a, b); + + lines[a].normals.push(normal); + lines[b].normals.push(normal); + lines[c].normals.push(normal); + } + } + + return lines; +} diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js new file mode 100644 index 0000000..b284b7d --- /dev/null +++ b/src/sliceActions/generateInfills.js @@ -0,0 +1,82 @@ +import getFillTemplate from './getFillTemplate.js'; +import Paths from '../paths.js'; + +export default function generateInfills(slices, settings) { + console.log("generating infills"); + + // need to scale up everything because of clipper rounding errors + var scale = 100; + + var layerHeight = settings.config["layerHeight"]; + var fillGridSize = settings.config["fillGridSize"] * scale; + var bottomThickness = settings.config["bottomThickness"]; + var topThickness = settings.config["topThickness"]; + var nozzleDiameter = settings.config["nozzleDiameter"] * scale; + var infillOverlap = settings.config["infillOverlap"] * scale; + + var bottomSkinCount = Math.ceil(bottomThickness/layerHeight); + var topSkinCount = Math.ceil(topThickness/layerHeight); + var nozzleRadius = nozzleDiameter / 2; + var hightemplateSize = Math.sqrt(2 * Math.pow(nozzleDiameter, 2)); + + for (var layer = 0; layer < slices.length; layer ++) { + var slice = slices[layer]; + + if (layer - bottomSkinCount >= 0 && layer + topSkinCount < slices.length) { + var downSkin = slices[layer - bottomSkinCount].getOutline(); + var upSkin = slices[layer + topSkinCount].getOutline(); + var surroundingLayer = upSkin.intersect(downSkin); + } + else { + var surroundingLayer = false; + } + + for (var i = 0; i < slice.parts.length; i ++) { + var part = slice.parts[i]; + + if (!part.intersect.closed) { + continue; + } + + var outerLine = part.outerLine; + + if (outerLine.length > 0) { + var inset = (part.innerLines.length > 0) ? part.innerLines[part.innerLines.length - 1] : outerLine; + + var fillArea = inset.offset(-nozzleRadius); + var lowFillArea = false; + if (surroundingLayer) { + var highFillArea = fillArea.difference(surroundingLayer); + + if (infillOverlap > 0) { + highFillArea = highFillArea.offset(infillOverlap); + } + + highFillArea = highFillArea.intersect(fillArea); + + var lowFillArea = fillArea.difference(highFillArea); + } + else { + var highFillArea = fillArea; + } + + var fill = new Paths([], false); + + if (lowFillArea && lowFillArea.length > 0) { + var bounds = lowFillArea.bounds(); + var lowFillTemplate = getFillTemplate(bounds, fillGridSize, true, true); + + part.fill.join(lowFillTemplate.intersect(lowFillArea)); + } + + if (highFillArea.length > 0) { + var bounds = highFillArea.bounds(); + var even = (layer % 2 === 0); + var highFillTemplate = getFillTemplate(bounds, hightemplateSize, even, !even); + + part.fill.join(highFillTemplate.intersect(highFillArea)); + } + } + } + } +} diff --git a/src/sliceActions/generateInnerLines.js b/src/sliceActions/generateInnerLines.js new file mode 100644 index 0000000..fb9c850 --- /dev/null +++ b/src/sliceActions/generateInnerLines.js @@ -0,0 +1,44 @@ +export default function generateInnerLines(slices, settings) { + console.log("generating outer lines and inner lines"); + + // need to scale up everything because of clipper rounding errors + var scale = 100; + + var layerHeight = settings.config["layerHeight"]; + var nozzleDiameter = settings.config["nozzleDiameter"] * scale; + var shellThickness = settings.config["shellThickness"] * scale; + var nozzleRadius = nozzleDiameter / 2; + var shells = Math.round(shellThickness / nozzleDiameter); + + for (var layer = 0; layer < slices.length; layer ++) { + var slice = slices[layer]; + + for (var i = 0; i < slice.parts.length; i ++) { + var part = slice.parts[i]; + + if (!part.intersect.closed) { + continue; + } + + // var outerLine = part.intersect.clone().scaleUp(scale).offset(-nozzleRadius); + var outerLine = part.intersect.scaleUp(scale).offset(-nozzleRadius); + + if (outerLine.length > 0) { + part.outerLine = outerLine; + + for (var shell = 1; shell < shells; shell += 1) { + var offset = shell * nozzleDiameter; + + var innerLine = outerLine.offset(-offset); + + if (innerLine.length > 0) { + part.innerLines.push(innerLine); + } + else { + break; + } + } + } + } + } +} diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js new file mode 100644 index 0000000..05fe0a5 --- /dev/null +++ b/src/sliceActions/generateSupport.js @@ -0,0 +1,71 @@ +import getFillTemplate from './getFillTemplate.js'; +import Paths from '../paths.js'; + +export default function generateSupport(slices, settings) { + console.log("generating support"); + + // need to scale up everything because of clipper rounding errors + var scale = 100; + + var layerHeight = settings.config["layerHeight"]; + var supportGridSize = settings.config["supportGridSize"] * scale; + var supportAcceptanceMargin = settings.config["supportAcceptanceMargin"] * scale; + var supportMargin = settings.config["supportMargin"] * scale; + var plateSize = settings.config["supportPlateSize"] * scale; + var supportDistanceY = settings.config["supportDistanceY"]; + var supportDistanceLayers = Math.max(Math.ceil(supportDistanceY / layerHeight), 1); + var nozzleDiameter = settings.config["nozzleDiameter"] * scale; + + var supportAreas = new Paths([], true); + + for (var layer = slices.length - 1 - supportDistanceLayers; layer >= 0; layer --) { + var currentSlice = slices[layer]; + + if (supportAreas.length > 0) { + + if (layer >= supportDistanceLayers) { + var sliceSkin = slices[layer - supportDistanceLayers].getOutline(); + sliceSkin = sliceSkin; + + var supportAreasSlimmed = supportAreas.difference(sliceSkin.offset(supportMargin)); + if (supportAreasSlimmed.area() < 100.0) { + supportAreas = supportAreas.difference(sliceSkin); + } + else { + supportAreas = supportAreasSlimmed; + } + } + + + var supportTemplate = getFillTemplate(supportAreas.bounds(), supportGridSize, true, true); + var supportFill = supportTemplate.intersect(supportAreas); + if (supportFill.length === 0) { + currentSlice.support = supportAreas.clone(); + } + else { + currentSlice.support = supportFill; + } + } + + var supportSkin = slices[layer + supportDistanceLayers - 1].getOutline(); + + var slice = slices[layer + supportDistanceLayers]; + for (var i = 0; i < slice.parts.length; i ++) { + var slicePart = slice.parts[i]; + + if (slicePart.intersect.closed) { + var outerLine = slicePart.outerLine; + } + else { + var outerLine = slicePart.intersect.offset(supportAcceptanceMargin); + } + + var overlap = supportSkin.offset(supportAcceptanceMargin).intersect(outerLine); + var overhang = outerLine.difference(overlap); + + if (overlap.length === 0 || overhang.length > 0) { + supportAreas = supportAreas.join(overhang); + } + } + } +} diff --git a/src/sliceActions/getFillTemplate.js b/src/sliceActions/getFillTemplate.js new file mode 100644 index 0000000..034e51f --- /dev/null +++ b/src/sliceActions/getFillTemplate.js @@ -0,0 +1,31 @@ +import Paths from '../paths.js'; + +export default function getFillTemplate(bounds, size, even, uneven) { + var paths = new Paths([], false); + + var left = Math.floor(bounds.left / size) * size; + var right = Math.ceil(bounds.right / size) * size; + var top = Math.floor(bounds.top / size) * size; + var bottom = Math.ceil(bounds.bottom / size) * size; + + var width = right - left; + + if (even) { + for (var y = top; y <= bottom + width; y += size) { + paths.push([ + {X: left, Y: y}, + {X: right, Y: y - width} + ]); + } + } + if (uneven) { + for (var y = top - width; y <= bottom; y += size) { + paths.push([ + {X: left, Y: y}, + {X: right, Y: y + width} + ]); + } + } + + return paths; +} diff --git a/src/sliceActions/intersectionsToShapes.js b/src/sliceActions/intersectionsToShapes.js new file mode 100644 index 0000000..fe4f2fa --- /dev/null +++ b/src/sliceActions/intersectionsToShapes.js @@ -0,0 +1,127 @@ +import THREE from 'three.js'; +import Paths from '../paths.js'; + +export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) { + console.log("generating slices"); + + var shapes = []; + + for (var layer = 1; layer < layerIntersectionIndexes.length; layer ++) { + var intersectionIndexes = layerIntersectionIndexes[layer]; + var intersectionPoints = layerIntersectionPoints[layer]; + + if (intersectionIndexes.length === 0) { + continue; + } + + var shapeParts = []; + for (var i = 0; i < intersectionIndexes.length; i ++) { + var index = intersectionIndexes[i]; + + if (intersectionPoints[index] === undefined) { + continue; + } + + var firstPoints = [index]; + var isFirstPoint = true; + var closed = false; + + var shape = []; + + while (index !== -1) { + var intersection = intersectionPoints[index]; + // uppercase X and Y because clipper vector + shape.push({X: intersection.x, Y: intersection.y}); + + delete intersectionPoints[index]; + + var connects = lines[index].connects; + var faceNormals = lines[index].normals; + + for (var j = 0; j < connects.length; j ++) { + var index = connects[j]; + + if (firstPoints.indexOf(index) !== -1 && shape.length > 2) { + closed = true; + index = -1; + break; + } + + // Check if index has an intersection or is already used + if (intersectionPoints[index] !== undefined) { + var faceNormal = faceNormals[Math.floor(j / 2)]; + + var a = new THREE.Vector2(intersection.x, intersection.y); + var b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y); + + // can't calculate normal between points if distance is smaller as 0.0001 + if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) { + if (isFirstPoint) { + firstPoints.push(index); + } + + delete intersectionPoints[index]; + + connects = connects.concat(lines[index].connects); + faceNormals = faceNormals.concat(lines[index].normals); + index = -1; + } + else { + // make sure the path goes the right direction + // THREE.Vector2.normal is not yet implimented + // var normal = a.sub(b).normal().normalize(); + var normal = a.sub(b); + normal.set(-normal.y, normal.x).normalize(); + + if (normal.dot(faceNormal) > 0) { + break; + } + else { + index = -1; + } + } + } + else { + index = -1; + } + } + isFirstPoint = false; + } + + if (!closed) { + var index = firstPoints[0]; + + while (index !== -1) { + if (firstPoints.indexOf(index) === -1) { + var intersection = intersectionPoints[index]; + shape.unshift({X: intersection.x, Y: intersection.y}); + + delete intersectionPoints[index]; + } + + var connects = lines[index].connects; + + for (var i = 0; i < connects.length; i ++) { + var index = connects[i]; + + if (intersectionPoints[index] !== undefined) { + break; + } + else { + index = -1; + } + } + } + } + + var part = new Paths([shape], closed).clean(0.01); + if (part.length > 0) { + shapeParts.push(part); + } + } + + shapes.push(shapeParts); + } + + return shapes; +} diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js new file mode 100644 index 0000000..9ed9b3a --- /dev/null +++ b/src/sliceActions/optimizePaths.js @@ -0,0 +1,42 @@ +import THREE from 'three.js'; + +export default function optimizePaths(slices, settings) { + console.log("opimize paths"); + + // need to scale up everything because of clipper rounding errors + var scale = 100; + + var brimOffset = settings.config["brimOffset"] * scale; + + var start = new THREE.Vector2(0, 0); + + for (var layer = 0; layer < slices.length; layer ++) { + var slice = slices[layer]; + + if (layer === 0) { + slice.brim = slice.getOutline().offset(brimOffset); + } + + start = slice.optimizePaths(start); + + for (var i = 0; i < slice.parts.length; i ++) { + var part = slice.parts[i]; + + if (part.intersect.closed) { + part.outerLine.scaleDown(scale); + for (var j = 0; j < part.innerLines.length; j ++) { + var innerLine = part.innerLines[j]; + innerLine.scaleDown(scale); + } + part.fill.scaleDown(scale); + } + } + + if (slice.support !== undefined) { + slice.support.scaleDown(scale); + } + if (slice.brim !== undefined) { + slice.brim.scaleDown(scale); + } + } +} diff --git a/src/sliceActions/shapesToSlices.js b/src/sliceActions/shapesToSlices.js new file mode 100644 index 0000000..d261ca1 --- /dev/null +++ b/src/sliceActions/shapesToSlices.js @@ -0,0 +1,65 @@ +import Slice from '../slice.js'; + +export default function shapesToSlices(shapes, settings) { + var slices = []; + + for (var layer = 0; layer < shapes.length; layer ++) { + var shapeParts = shapes[layer]; + + var slice = new Slice(); + + var holes = []; + var outlines = []; + + for (var i = 0; i < shapeParts.length; i ++) { + var shape = shapeParts[i]; + + if (!shape.closed) { + slice.add(shape); + } + else if (shape.isHole()) { + holes.push(shape); + } + else { + slice.add(shape); + outlines.push(shape); + } + } + + outlines.sort((a, b) => { + return a.boundSize() - b.boundSize(); + }); + + if (holes.length > outlines.length) { + [holes, outlines] = [outlines, holes]; + } + else if (holes.length === outlines.length) { + holes.sort((a, b) => { + return a.boundSize() - b.boundSize(); + }); + + if (holes[0].boundSize > outlines[0].boundSize()) { + [holes, outlines] = [outlines, holes]; + } + } + + for (var i = 0; i < holes.length; i ++) { + var hole = holes[i]; + + for (var j = 0; j < outlines.length; j ++) { + var outline = outlines[j]; + + if (outline.pointCollision(hole[0][0])) { + outline.join(hole); + break; + } + } + } + + slice.removeSelfIntersect(); + + slices.push(slice); + } + + return slices; +} diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js new file mode 100644 index 0000000..6d9ab1a --- /dev/null +++ b/src/sliceActions/slicesToGCode.js @@ -0,0 +1,73 @@ +import GCode from '../gcode.js'; + +export default function slicesToGCode(slices, settings) { + var gcode = new GCode().setSettings(settings); + + function pathToGCode (path, retract, unRetract, type) { + + for (var i = 0; i < path.length; i ++) { + var shape = path[i]; + + var length = path.closed ? (shape.length + 1) : shape.length; + + for (var j = 0; j < length; j ++) { + var point = shape[j % shape.length]; + + if (j === 0) { + // TODO + // moveTo should impliment combing + gcode.moveTo(point.X, point.Y, layer); + + if (unRetract) { + gcode.unRetract(); + } + } + else { + gcode.lineTo(point.X, point.Y, layer, type); + } + } + } + + if (retract) { + gcode.retract(); + } + } + + for (var layer = 0; layer < slices.length; layer ++) { + var slice = slices[layer]; + + if (layer === 1) { + gcode.turnFanOn(); + gcode.bottom = false; + } + + if (slice.brim !== undefined) { + pathToGCode(slice.brim, true, true, "brim"); + } + + for (var i = 0; i < slice.parts.length; i ++) { + var part = slice.parts[i]; + + if (part.intersect.closed) { + pathToGCode(part.outerLine, false, true, "outerLine"); + + for (var j = 0; j < part.innerLines.length; j ++) { + var innerLine = part.innerLines[j]; + pathToGCode(innerLine, false, false, "innerLine"); + } + + pathToGCode(part.fill, true, false, "fill"); + } + else { + var retract = !(slice.parts.length === 1 && slice.support === undefined); + pathToGCode(part.intersect, retract, retract, "outerLine"); + } + } + + if (slice.support !== undefined) { + pathToGCode(slice.support, true, true, "support"); + } + } + + return gcode.getGCode(); +} diff --git a/src/slicer.js b/src/slicer.js index e0d0dec..4b0a36b 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -1,7 +1,13 @@ import THREE from 'three.js'; -import Paths from './paths.js'; -import Slice from './slice.js'; -import GCode from './gcode.js'; +import calculateLayersIntersections from './sliceActions/calculateLayersIntersections.js'; +import createLines from './sliceActions/createLines.js'; +import generateInfills from './sliceActions/generateInfills.js'; +import generateInnerLines from './sliceActions/generateInnerLines.js'; +import generateSupport from './sliceActions/generateSupport.js'; +import intersectionsToShapes from './sliceActions/intersectionsToShapes.js'; +import optimizePaths from './sliceActions/optimizePaths.js'; +import shapesToSlices from './sliceActions/shapesToSlices.js'; +import slicesToGCode from './sliceActions/slicesToGCode.js'; export default class { constructor () { @@ -54,25 +60,43 @@ export default class { var supportEnabled = settings.config['supportEnabled']; // get unique lines from geometry; - var lines = this._createLines(settings); + var lines = createLines(this.geometry, settings); + this.progress.createdLines = true; + this._updateProgress(settings); - var {layerIntersectionIndexes, layerIntersectionPoints} = this._calculateLayersIntersections(lines, settings); + var {layerIntersectionIndexes, layerIntersectionPoints} = calculateLayersIntersections(lines, settings); + this.progress.calculatedLayerIntersections = true; + this._updateProgress(settings); - var shapes = this._intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings); + var shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings); + this.progress.sliced = true; + this._updateProgress(settings); - var slices = this._shapesToSlices(shapes, settings); + var slices = shapesToSlices(shapes, settings); + this.progress.generatedSlices = true; + this._updateProgress(settings); + + generateInnerLines(slices, settings); + this.progress.generatedInnerLines = true; + this._updateProgress(settings); + + generateInfills(slices, settings); + this.progress.generatedInfills = true; + this._updateProgress(settings); - this._generateInnerLines(slices, settings); - - this._generateInfills(slices, settings); - if (supportEnabled) { - this._generateSupport(slices, settings); + generateSupport(slices, settings); + this.progress.generatedSupport = true; + this._updateProgress(settings); } - - this._optimizePaths(slices, settings); - var gcode = this._slicesToGCode(slices, settings); + optimizePaths(slices, settings); + this.progress.optimizedPaths = true; + this._updateProgress(settings); + + var gcode = slicesToGCode(slices, settings); + this.progress.generatedGCode = true; + this._updateProgress(settings); if (this.onfinish !== undefined) { this.onfinish(gcode); @@ -81,658 +105,6 @@ export default class { return gcode; } - _createLines (settings) { - console.log('constructing unique lines from geometry'); - - var lines = []; - var lineLookup = {}; - - var addLine = (a, b) => { - var index = lineLookup[b + '_' + a]; - - if (index === undefined) { - index = lines.length; - lineLookup[a + '_' + b] = index; - - lines.push({ - line: new THREE.Line3(this.geometry.vertices[a], this.geometry.vertices[b]), - connects: [], - normals: [] - }); - } - - return index; - } - - for (var i = 0; i < this.geometry.faces.length; i ++) { - var face = this.geometry.faces[i]; - if (face.normal.y !== 1 && face.normal.y !== -1) { - var normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize(); - - // check for only adding unique lines - // returns index of said line - var a = addLine(face.a, face.b); - var b = addLine(face.b, face.c); - var c = addLine(face.c, face.a); - - // set connecting lines (based on face) - lines[a].connects.push(b, c); - lines[b].connects.push(c, a); - lines[c].connects.push(a, b); - - lines[a].normals.push(normal); - lines[b].normals.push(normal); - lines[c].normals.push(normal); - } - } - - this.progress.createdLines = true; - this._updateProgress(settings); - - return lines; - } - - _calculateLayersIntersections (lines, settings) { - console.log('calculating layer intersections'); - - var layerHeight = settings.config["layerHeight"]; - var height = settings.config["dimensionsZ"]; - - var numLayers = Math.floor(height / layerHeight); - - var layerIntersectionIndexes = []; - var layerIntersectionPoints = []; - for (var layer = 0; layer < numLayers; layer ++) { - layerIntersectionIndexes[layer] = []; - layerIntersectionPoints[layer] = []; - } - - for (var lineIndex = 0; lineIndex < lines.length; lineIndex ++) { - var line = lines[lineIndex].line; - - var min = Math.ceil(Math.min(line.start.y, line.end.y) / layerHeight); - var max = Math.floor(Math.max(line.start.y, line.end.y) / layerHeight); - - for (var layerIndex = min; layerIndex <= max; layerIndex ++) { - if (layerIndex >= 0 && layerIndex < numLayers) { - - layerIntersectionIndexes[layerIndex].push(lineIndex); - - var y = layerIndex * layerHeight; - - if (line.start.y === line.end.y) { - var x = line.start.x; - var z = line.start.z; - } - else { - var alpha = (y - line.start.y) / (line.end.y - line.start.y); - var x = line.end.x * alpha + line.start.x * (1 - alpha); - var z = line.end.z * alpha + line.start.z * (1 - alpha); - } - - layerIntersectionPoints[layerIndex][lineIndex] = new THREE.Vector2(z, x); - } - } - } - - this.progress.calculatedLayerIntersections = true; - this._updateProgress(settings); - - return { - layerIntersectionIndexes, - layerIntersectionPoints - }; - } - - _intersectionsToShapes (layerIntersectionIndexes, layerIntersectionPoints, lines, settings) { - console.log("generating slices"); - - var shapes = []; - - for (var layer = 1; layer < layerIntersectionIndexes.length; layer ++) { - var intersectionIndexes = layerIntersectionIndexes[layer]; - var intersectionPoints = layerIntersectionPoints[layer]; - - if (intersectionIndexes.length === 0) { - continue; - } - - var shapeParts = []; - for (var i = 0; i < intersectionIndexes.length; i ++) { - var index = intersectionIndexes[i]; - - if (intersectionPoints[index] === undefined) { - continue; - } - - var firstPoints = [index]; - var isFirstPoint = true; - var closed = false; - - var shape = []; - - while (index !== -1) { - var intersection = intersectionPoints[index]; - // uppercase X and Y because clipper vector - shape.push({X: intersection.x, Y: intersection.y}); - - delete intersectionPoints[index]; - - var connects = lines[index].connects; - var faceNormals = lines[index].normals; - - for (var j = 0; j < connects.length; j ++) { - var index = connects[j]; - - if (firstPoints.indexOf(index) !== -1 && shape.length > 2) { - closed = true; - index = -1; - break; - } - - // Check if index has an intersection or is already used - if (intersectionPoints[index] !== undefined) { - var faceNormal = faceNormals[Math.floor(j / 2)]; - - var a = new THREE.Vector2(intersection.x, intersection.y); - var b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y); - - // can't calculate normal between points if distance is smaller as 0.0001 - if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) { - if (isFirstPoint) { - firstPoints.push(index); - } - - delete intersectionPoints[index]; - - connects = connects.concat(lines[index].connects); - faceNormals = faceNormals.concat(lines[index].normals); - index = -1; - } - else { - // make sure the path goes the right direction - // THREE.Vector2.normal is not yet implimented - // var normal = a.sub(b).normal().normalize(); - var normal = a.sub(b); - normal.set(-normal.y, normal.x).normalize(); - - if (normal.dot(faceNormal) > 0) { - break; - } - else { - index = -1; - } - } - } - else { - index = -1; - } - } - isFirstPoint = false; - } - - if (!closed) { - var index = firstPoints[0]; - - while (index !== -1) { - if (firstPoints.indexOf(index) === -1) { - var intersection = intersectionPoints[index]; - shape.unshift({X: intersection.x, Y: intersection.y}); - - delete intersectionPoints[index]; - } - - var connects = lines[index].connects; - - for (var i = 0; i < connects.length; i ++) { - var index = connects[i]; - - if (intersectionPoints[index] !== undefined) { - break; - } - else { - index = -1; - } - } - } - } - - var part = new Paths([shape], closed).clean(0.01); - if (part.length > 0) { - shapeParts.push(part); - } - } - - shapes.push(shapeParts); - } - - this.progress.sliced = true; - this._updateProgress(settings); - - return shapes; - } - - _shapesToSlices (shapes, settings) { - var slices = []; - - for (var layer = 0; layer < shapes.length; layer ++) { - var shapeParts = shapes[layer]; - - var slice = new Slice(); - - var holes = []; - var outlines = []; - - for (var i = 0; i < shapeParts.length; i ++) { - var shape = shapeParts[i]; - - if (!shape.closed) { - slice.add(shape); - } - else if (shape.isHole()) { - holes.push(shape); - } - else { - slice.add(shape); - outlines.push(shape); - } - } - - outlines.sort((a, b) => { - return a.boundSize() - b.boundSize(); - }); - - console.log('test'); - - if (holes.length > outlines.length) { - [holes, outlines] = [outlines, holes]; - } - else if (holes.length === outlines.length) { - holes.sort((a, b) => { - return a.boundSize() - b.boundSize(); - }); - - if (holes[0].boundSize > outlines[0].boundSize()) { - [holes, outlines] = [outlines, holes]; - } - } - - for (var i = 0; i < holes.length; i ++) { - var hole = holes[i]; - - for (var j = 0; j < outlines.length; j ++) { - var outline = outlines[j]; - - if (outline.pointCollision(hole[0][0])) { - outline.join(hole); - break; - } - } - } - - slice.removeSelfIntersect(); - - slices.push(slice); - } - - this.progress.generatedSlices = true; - this._updateProgress(settings); - - return slices; - } - - _generateInnerLines (slices, settings) { - console.log("generating outer lines and inner lines"); - - // need to scale up everything because of clipper rounding errors - var scale = 100; - - var layerHeight = settings.config["layerHeight"]; - var nozzleDiameter = settings.config["nozzleDiameter"] * scale; - var shellThickness = settings.config["shellThickness"] * scale; - var nozzleRadius = nozzleDiameter / 2; - var shells = Math.round(shellThickness / nozzleDiameter); - - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; - - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; - - if (!part.intersect.closed) { - continue; - } - - // var outerLine = part.intersect.clone().scaleUp(scale).offset(-nozzleRadius); - var outerLine = part.intersect.scaleUp(scale).offset(-nozzleRadius); - - if (outerLine.length > 0) { - part.outerLine = outerLine; - - for (var shell = 1; shell < shells; shell += 1) { - var offset = shell * nozzleDiameter; - - var innerLine = outerLine.offset(-offset); - - if (innerLine.length > 0) { - part.innerLines.push(innerLine); - } - else { - break; - } - } - } - } - } - - this.progress.generatedInnerLines = true; - this._updateProgress(settings); - } - - _generateInfills (slices, settings) { - console.log("generating infills"); - - // need to scale up everything because of clipper rounding errors - var scale = 100; - - var layerHeight = settings.config["layerHeight"]; - var fillGridSize = settings.config["fillGridSize"] * scale; - var bottomThickness = settings.config["bottomThickness"]; - var topThickness = settings.config["topThickness"]; - var nozzleDiameter = settings.config["nozzleDiameter"] * scale; - var infillOverlap = settings.config["infillOverlap"] * scale; - - var bottomSkinCount = Math.ceil(bottomThickness/layerHeight); - var topSkinCount = Math.ceil(topThickness/layerHeight); - var nozzleRadius = nozzleDiameter / 2; - var hightemplateSize = Math.sqrt(2 * Math.pow(nozzleDiameter, 2)); - - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; - - if (layer - bottomSkinCount >= 0 && layer + topSkinCount < slices.length) { - var downSkin = slices[layer - bottomSkinCount].getOutline(); - var upSkin = slices[layer + topSkinCount].getOutline(); - var surroundingLayer = upSkin.intersect(downSkin); - } - else { - var surroundingLayer = false; - } - - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; - - if (!part.intersect.closed) { - continue; - } - - var outerLine = part.outerLine; - - if (outerLine.length > 0) { - var inset = (part.innerLines.length > 0) ? part.innerLines[part.innerLines.length - 1] : outerLine; - - var fillArea = inset.offset(-nozzleRadius); - var lowFillArea = false; - if (surroundingLayer) { - var highFillArea = fillArea.difference(surroundingLayer); - - if (infillOverlap > 0) { - highFillArea = highFillArea.offset(infillOverlap); - } - - highFillArea = highFillArea.intersect(fillArea); - - var lowFillArea = fillArea.difference(highFillArea); - } - else { - var highFillArea = fillArea; - } - - var fill = new Paths([], false); - - if (lowFillArea && lowFillArea.length > 0) { - var bounds = lowFillArea.bounds(); - var lowFillTemplate = this._getFillTemplate(bounds, fillGridSize, true, true); - - part.fill.join(lowFillTemplate.intersect(lowFillArea)); - } - - if (highFillArea.length > 0) { - var bounds = highFillArea.bounds(); - var even = (layer % 2 === 0); - var highFillTemplate = this._getFillTemplate(bounds, hightemplateSize, even, !even); - - part.fill.join(highFillTemplate.intersect(highFillArea)); - } - } - } - } - - this.progress.generatedInfills = true; - this._updateProgress(settings); - } - - _generateSupport (slices, settings) { - console.log("generating support"); - - // need to scale up everything because of clipper rounding errors - var scale = 100; - - var layerHeight = settings.config["layerHeight"]; - var supportGridSize = settings.config["supportGridSize"] * scale; - var supportAcceptanceMargin = settings.config["supportAcceptanceMargin"] * scale; - var supportMargin = settings.config["supportMargin"] * scale; - var plateSize = settings.config["supportPlateSize"] * scale; - var supportDistanceY = settings.config["supportDistanceY"]; - var supportDistanceLayers = Math.max(Math.ceil(supportDistanceY / layerHeight), 1); - var nozzleDiameter = settings.config["nozzleDiameter"] * scale; - - var supportAreas = new Paths([], true); - - for (var layer = slices.length - 1 - supportDistanceLayers; layer >= 0; layer --) { - var currentSlice = slices[layer]; - - if (supportAreas.length > 0) { - - if (layer >= supportDistanceLayers) { - var sliceSkin = slices[layer - supportDistanceLayers].getOutline(); - sliceSkin = sliceSkin; - - var supportAreasSlimmed = supportAreas.difference(sliceSkin.offset(supportMargin)); - if (supportAreasSlimmed.area() < 100.0) { - supportAreas = supportAreas.difference(sliceSkin); - } - else { - supportAreas = supportAreasSlimmed; - } - } - - - var supportTemplate = this._getFillTemplate(supportAreas.bounds(), supportGridSize, true, true); - var supportFill = supportTemplate.intersect(supportAreas); - if (supportFill.length === 0) { - currentSlice.support = supportAreas.clone(); - } - else { - currentSlice.support = supportFill; - } - } - - var supportSkin = slices[layer + supportDistanceLayers - 1].getOutline(); - - var slice = slices[layer + supportDistanceLayers]; - for (var i = 0; i < slice.parts.length; i ++) { - var slicePart = slice.parts[i]; - - if (slicePart.intersect.closed) { - var outerLine = slicePart.outerLine; - } - else { - var outerLine = slicePart.intersect.offset(supportAcceptanceMargin); - } - - var overlap = supportSkin.offset(supportAcceptanceMargin).intersect(outerLine); - var overhang = outerLine.difference(overlap); - - if (overlap.length === 0 || overhang.length > 0) { - supportAreas = supportAreas.join(overhang); - } - } - } - - this.progress.generatedSupport = true; - this._updateProgress(settings); - } - - _optimizePaths (slices, settings) { - console.log("opimize paths"); - - // need to scale up everything because of clipper rounding errors - var scale = 100; - - var brimOffset = settings.config["brimOffset"] * scale; - - var start = new THREE.Vector2(0, 0); - - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; - - if (layer === 0) { - slice.brim = slice.getOutline().offset(brimOffset); - } - - start = slice.optimizePaths(start); - - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; - - if (part.intersect.closed) { - part.outerLine.scaleDown(scale); - for (var j = 0; j < part.innerLines.length; j ++) { - var innerLine = part.innerLines[j]; - innerLine.scaleDown(scale); - } - part.fill.scaleDown(scale); - } - } - - if (slice.support !== undefined) { - slice.support.scaleDown(scale); - } - if (slice.brim !== undefined) { - slice.brim.scaleDown(scale); - } - } - - this.progress.optimizedPaths = true; - this._updateProgress(settings); - } - - _getFillTemplate (bounds, size, even, uneven) { - var paths = new Paths([], false); - - var left = Math.floor(bounds.left / size) * size; - var right = Math.ceil(bounds.right / size) * size; - var top = Math.floor(bounds.top / size) * size; - var bottom = Math.ceil(bounds.bottom / size) * size; - - var width = right - left; - - if (even) { - for (var y = top; y <= bottom + width; y += size) { - paths.push([ - {X: left, Y: y}, - {X: right, Y: y - width} - ]); - } - } - if (uneven) { - for (var y = top - width; y <= bottom; y += size) { - paths.push([ - {X: left, Y: y}, - {X: right, Y: y + width} - ]); - } - } - - return paths; - } - - _slicesToGCode (slices, settings) { - var gcode = new GCode().setSettings(settings); - - function pathToGCode (path, retract, unRetract, type) { - - for (var i = 0; i < path.length; i ++) { - var shape = path[i]; - - var length = path.closed ? (shape.length + 1) : shape.length; - - for (var j = 0; j < length; j ++) { - var point = shape[j % shape.length]; - - if (j === 0) { - // TODO - // moveTo should impliment combing - gcode.moveTo(point.X, point.Y, layer); - - if (unRetract) { - gcode.unRetract(); - } - } - else { - gcode.lineTo(point.X, point.Y, layer, type); - } - } - } - - if (retract) { - gcode.retract(); - } - } - - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; - - if (layer === 1) { - gcode.turnFanOn(); - gcode.bottom = false; - } - - if (slice.brim !== undefined) { - pathToGCode(slice.brim, true, true, "brim"); - } - - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; - - if (part.intersect.closed) { - pathToGCode(part.outerLine, false, true, "outerLine"); - - for (var j = 0; j < part.innerLines.length; j ++) { - var innerLine = part.innerLines[j]; - pathToGCode(innerLine, false, false, "innerLine"); - } - - pathToGCode(part.fill, true, false, "fill"); - } - else { - var retract = !(slice.parts.length === 1 && slice.support === undefined); - pathToGCode(part.intersect, retract, retract, "outerLine"); - } - } - - if (slice.support !== undefined) { - pathToGCode(slice.support, true, true, "support"); - } - } - - this.progress.generatedGCode = true; - this._updateProgress(settings); - - return gcode.getGCode(); - } - _updateProgress (settings) { if (this.onprogress !== undefined) { var supportEnabled = settings.config["supportEnabled"]; From 02e406f01834acdc1a9337182b1e848142a411a3 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 00:27:06 +0200 Subject: [PATCH 02/64] remove spaces --- src/slicer.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/slicer.js b/src/slicer.js index 4b0a36b..b28d854 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -12,14 +12,14 @@ import slicesToGCode from './sliceActions/slicesToGCode.js'; export default class { constructor () { this.progress = { - createdLines: false, - calculatedLayerIntersections: false, - sliced: false, - generatedSlices: false, - generatedInnerLines: false, - generatedInfills: false, - generatedSupport: false, - optimizedPaths: false, + createdLines: false, + calculatedLayerIntersections: false, + sliced: false, + generatedSlices: false, + generatedInnerLines: false, + generatedInfills: false, + generatedSupport: false, + optimizedPaths: false, generatedGCode: false }; } From ef74eebf8acb11e0267edd7f92670aa5ca0d60b3 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 00:35:53 +0200 Subject: [PATCH 03/64] implemented event dispatcher --- config.js | 1 + example/app.js | 6 +++--- package.json | 1 + src/slicer.js | 45 +++++++++++++++++++++++++-------------------- src/slicerworker.js | 39 ++++++++++++++++++++++----------------- 5 files changed, 52 insertions(+), 40 deletions(-) diff --git a/config.js b/config.js index 5dd6187..59039c6 100644 --- a/config.js +++ b/config.js @@ -18,6 +18,7 @@ System.config({ map: { "babel": "npm:babel-core@5.8.21", "babel-runtime": "npm:babel-runtime@5.8.20", + "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", "clipper-lib": "npm:clipper-lib@1.0.0", "core-js": "npm:core-js@0.9.18", "json": "github:systemjs/plugin-json@0.1.0", diff --git a/example/app.js b/example/app.js index eaa4bfa..a5bbfbe 100644 --- a/example/app.js +++ b/example/app.js @@ -13,7 +13,7 @@ var slicer = new SLICER.Slicer(); //var slicer = new SLICER.SlicerWorker(); slicer.setGeometry(geometry.clone()); -slicer.onfinish = function (gCode) { - document.getElementById('gcode').innerHTML = gCode.replace(/(?:\r\n|\r|\n)/g, '
'); -}; +slicer.addEventListener('finish', ({ gcode }) => { + document.getElementById('gcode').innerHTML = gcode.replace(/(?:\r\n|\r|\n)/g, '
'); +}); slicer.slice(settings); diff --git a/package.json b/package.json index 1b705de..233a5cb 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "lib": "src" }, "dependencies": { + "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", "clipper-lib": "npm:clipper-lib@^1.0.0", "nodeca/js-yaml": "github:nodeca/js-yaml@^3.3.1", "read-yaml": "npm:read-yaml@^1.0.0", diff --git a/src/slicer.js b/src/slicer.js index b28d854..ceae634 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -1,4 +1,5 @@ import THREE from 'three.js'; +import EventDispatcher from 'casperlamboo/EventDispatcher'; import calculateLayersIntersections from './sliceActions/calculateLayersIntersections.js'; import createLines from './sliceActions/createLines.js'; import generateInfills from './sliceActions/generateInfills.js'; @@ -9,8 +10,10 @@ import optimizePaths from './sliceActions/optimizePaths.js'; import shapesToSlices from './sliceActions/shapesToSlices.js'; import slicesToGCode from './sliceActions/slicesToGCode.js'; -export default class { +export default class extends EventDispatcher { constructor () { + super(); + this.progress = { createdLines: false, calculatedLayerIntersections: false, @@ -98,34 +101,36 @@ export default class { this.progress.generatedGCode = true; this._updateProgress(settings); - if (this.onfinish !== undefined) { - this.onfinish(gcode); - } + this.dispatchEvent({ + type: 'finish', + gcode + }); return gcode; } _updateProgress (settings) { - if (this.onprogress !== undefined) { - var supportEnabled = settings.config["supportEnabled"]; + var supportEnabled = settings.config["supportEnabled"]; - var progress = {}; + var progress = {}; - var procent = 0; - var length = 0; - for (var i in this.progress) { - if (!(!supportEnabled && i === "generatedSupport")) { - progress[i] = this.progress[i]; - if (progress[i]) { - procent += 1; - } - length += 1; + var procent = 0; + var length = 0; + for (var i in this.progress) { + if (!(!supportEnabled && i === "generatedSupport")) { + progress[i] = this.progress[i]; + if (progress[i]) { + procent += 1; } + length += 1; } - - progress.procent = procent / length; - - this.onprogress(progress); } + + progress.procent = procent / length; + + this.dispatchEvent({ + type: 'progress', + progress + }); } } diff --git a/src/slicerworker.js b/src/slicerworker.js index 0ca3f9e..fd0e531 100644 --- a/src/slicerworker.js +++ b/src/slicerworker.js @@ -1,30 +1,35 @@ import THREE from 'three.js'; import Settings from './settings.js'; +import EventDispatcher from 'casperlamboo/EventDispatcher'; -export default class { +export default class extends EventDispatcher { constructor () { + super(); + this.worker = new Worker('./worker.js'); this.worker.addEventListener('message', (event) => { switch (event.data['cmd']) { case 'PROGRESS': - if (this.onprogress !== undefined) { - var progress = event.data['progress']; + var progress = event.data['progress']; - this.onprogress(progress); - } + this.dispatchEvent({ + type: 'progress', + progress + }); break; case 'GCODE': - if (this.onfinish !== undefined) { - var reader = new FileReader(); - reader.addEventListener("loadend", () => { - var gcode = reader.result; - this.onfinish(gcode); + var reader = new FileReader(); + reader.addEventListener("loadend", () => { + var gcode = reader.result; + this.dispatchEvent({ + type: 'finish', + gcode }); - reader.readAsBinaryString(event.data['gcode']); - } + }); + reader.readAsBinaryString(event.data['gcode']); break; } }, false); @@ -68,11 +73,11 @@ export default class { delete geometry.boundingSphere; this.worker.postMessage({ - 'cmd': 'SET_MESH', + 'cmd': 'SET_MESH', 'geometry': { - 'attributes': geometry.attributes, + 'attributes': geometry.attributes, 'attributesKeys': geometry.attributesKeys - }, + }, 'matrix': matrix.toArray() }, buffers); @@ -81,7 +86,7 @@ export default class { slice (settings) { this.worker.postMessage({ - 'cmd': 'SLICE', + 'cmd': 'SLICE', 'settings': settings.config }); @@ -95,4 +100,4 @@ export default class { return this; } -} \ No newline at end of file +} From 3531d64dbf9c2805acaedc865191090282a0fed3 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 08:01:07 +0200 Subject: [PATCH 04/64] use s6 string --- src/sliceActions/createLines.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sliceActions/createLines.js b/src/sliceActions/createLines.js index 1fa6603..5f91b5b 100644 --- a/src/sliceActions/createLines.js +++ b/src/sliceActions/createLines.js @@ -7,11 +7,11 @@ export default function createLines(geometry, settings) { var lineLookup = {}; var addLine = (a, b) => { - var index = lineLookup[b + '_' + a]; + var index = lineLookup[`${b}_${a}`]; if (index === undefined) { index = lines.length; - lineLookup[a + '_' + b] = index; + lineLookup[`${a}_${b}`] = index; lines.push({ line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]), From e573a0662b2b706f1bf4e62dc75a67287c5ddea2 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 08:15:30 +0200 Subject: [PATCH 05/64] move adeline to separate file --- src/sliceActions/createLines.js | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/sliceActions/createLines.js b/src/sliceActions/createLines.js index 5f91b5b..b740503 100644 --- a/src/sliceActions/createLines.js +++ b/src/sliceActions/createLines.js @@ -1,28 +1,28 @@ import THREE from 'three.js'; +function addLine(geometry, lineLookup, lines, a, b) { + var index = lineLookup[`${b}_${a}`]; + + if (index === undefined) { + index = lines.length; + lineLookup[`${a}_${b}`] = index; + + lines.push({ + line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]), + connects: [], + normals: [] + }); + } + + return index; +} + export default function createLines(geometry, settings) { console.log('constructing unique lines from geometry'); var lines = []; var lineLookup = {}; - var addLine = (a, b) => { - var index = lineLookup[`${b}_${a}`]; - - if (index === undefined) { - index = lines.length; - lineLookup[`${a}_${b}`] = index; - - lines.push({ - line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]), - connects: [], - normals: [] - }); - } - - return index; - } - for (var i = 0; i < geometry.faces.length; i ++) { var face = geometry.faces[i]; if (face.normal.y !== 1 && face.normal.y !== -1) { @@ -30,9 +30,9 @@ export default function createLines(geometry, settings) { // check for only adding unique lines // returns index of said line - var a = addLine(face.a, face.b); - var b = addLine(face.b, face.c); - var c = addLine(face.c, face.a); + var a = addLine(geometry, lineLookup, lines, face.a, face.b); + var b = addLine(geometry, lineLookup, lines, face.b, face.c); + var c = addLine(geometry, lineLookup, lines, face.c, face.a); // set connecting lines (based on face) lines[a].connects.push(b, c); From aa9de5ec47ad36906a8268a5b8bc939eb137385a Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 08:49:35 +0200 Subject: [PATCH 06/64] use deconstructs --- .../calculateLayersIntersections.js | 3 +-- src/sliceActions/generateInfills.js | 21 ++++++++++++------ src/sliceActions/generateInnerLines.js | 10 ++++----- src/sliceActions/generateSupport.js | 22 ++++++++++++------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/sliceActions/calculateLayersIntersections.js b/src/sliceActions/calculateLayersIntersections.js index ccdcca2..f56a0b8 100644 --- a/src/sliceActions/calculateLayersIntersections.js +++ b/src/sliceActions/calculateLayersIntersections.js @@ -3,8 +3,7 @@ import THREE from 'three.js'; export default function calculateLayersIntersections(lines, settings) { console.log('calculating layer intersections'); - var layerHeight = settings.config["layerHeight"]; - var height = settings.config["dimensionsZ"]; + const { layerHeight, dimensionsZ: height } = settings.config; var numLayers = Math.floor(height / layerHeight); diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index b284b7d..b3ae254 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -1,18 +1,25 @@ import getFillTemplate from './getFillTemplate.js'; import Paths from '../paths.js'; +const scale = 100; + export default function generateInfills(slices, settings) { console.log("generating infills"); // need to scale up everything because of clipper rounding errors - var scale = 100; - var layerHeight = settings.config["layerHeight"]; - var fillGridSize = settings.config["fillGridSize"] * scale; - var bottomThickness = settings.config["bottomThickness"]; - var topThickness = settings.config["topThickness"]; - var nozzleDiameter = settings.config["nozzleDiameter"] * scale; - var infillOverlap = settings.config["infillOverlap"] * scale; + let { + layerHeight, + fillGridSize, + bottomThickness, + topThickness, + nozzleDiameter, + infillOverlap + } = settings.config; + + fillGridSize *= scale; + nozzleDiameter *= scale; + infillOverlap *= scale; var bottomSkinCount = Math.ceil(bottomThickness/layerHeight); var topSkinCount = Math.ceil(topThickness/layerHeight); diff --git a/src/sliceActions/generateInnerLines.js b/src/sliceActions/generateInnerLines.js index fb9c850..006b5a6 100644 --- a/src/sliceActions/generateInnerLines.js +++ b/src/sliceActions/generateInnerLines.js @@ -1,12 +1,12 @@ +const scale = 100; + export default function generateInnerLines(slices, settings) { console.log("generating outer lines and inner lines"); // need to scale up everything because of clipper rounding errors - var scale = 100; - - var layerHeight = settings.config["layerHeight"]; - var nozzleDiameter = settings.config["nozzleDiameter"] * scale; - var shellThickness = settings.config["shellThickness"] * scale; + let {layerHeight, nozzleDiameter, shellThickness} = settings.config; + nozzleDiameter *= scale; + shellThickness *= scale; var nozzleRadius = nozzleDiameter / 2; var shells = Math.round(shellThickness / nozzleDiameter); diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index 05fe0a5..a974399 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -1,20 +1,26 @@ import getFillTemplate from './getFillTemplate.js'; import Paths from '../paths.js'; +const scale = 100; + export default function generateSupport(slices, settings) { console.log("generating support"); // need to scale up everything because of clipper rounding errors - var scale = 100; + let { + layerHeight, + supportGridSize, + supportAcceptanceMargin, + supportPlateSize: plateSize, + supportDistanceY, + nozzleDiameter + } = settings.config; - var layerHeight = settings.config["layerHeight"]; - var supportGridSize = settings.config["supportGridSize"] * scale; - var supportAcceptanceMargin = settings.config["supportAcceptanceMargin"] * scale; - var supportMargin = settings.config["supportMargin"] * scale; - var plateSize = settings.config["supportPlateSize"] * scale; - var supportDistanceY = settings.config["supportDistanceY"]; + supportGridSize *= scale; + supportMargin *= scale; + plateSize *= scale; + nozzleDiameter *= scale; var supportDistanceLayers = Math.max(Math.ceil(supportDistanceY / layerHeight), 1); - var nozzleDiameter = settings.config["nozzleDiameter"] * scale; var supportAreas = new Paths([], true); From bd511c35094bdcb11e836de13fa452410541588f Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 09:53:12 +0200 Subject: [PATCH 07/64] remove var for const or let in calculateLayerIntersections.js --- .../calculateLayersIntersections.js | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/sliceActions/calculateLayersIntersections.js b/src/sliceActions/calculateLayersIntersections.js index f56a0b8..c3fb5af 100644 --- a/src/sliceActions/calculateLayersIntersections.js +++ b/src/sliceActions/calculateLayersIntersections.js @@ -5,36 +5,37 @@ export default function calculateLayersIntersections(lines, settings) { const { layerHeight, dimensionsZ: height } = settings.config; - var numLayers = Math.floor(height / layerHeight); + const numLayers = Math.floor(height / layerHeight); - var layerIntersectionIndexes = []; - var layerIntersectionPoints = []; - for (var layer = 0; layer < numLayers; layer ++) { + const layerIntersectionIndexes = []; + const layerIntersectionPoints = []; + for (let layer = 0; layer < numLayers; layer ++) { layerIntersectionIndexes[layer] = []; layerIntersectionPoints[layer] = []; } - for (var lineIndex = 0; lineIndex < lines.length; lineIndex ++) { - var line = lines[lineIndex].line; + for (let lineIndex = 0; lineIndex < lines.length; lineIndex ++) { + const line = lines[lineIndex].line; - var min = Math.ceil(Math.min(line.start.y, line.end.y) / layerHeight); - var max = Math.floor(Math.max(line.start.y, line.end.y) / layerHeight); + const min = Math.ceil(Math.min(line.start.y, line.end.y) / layerHeight); + const max = Math.floor(Math.max(line.start.y, line.end.y) / layerHeight); - for (var layerIndex = min; layerIndex <= max; layerIndex ++) { + for (let layerIndex = min; layerIndex <= max; layerIndex ++) { if (layerIndex >= 0 && layerIndex < numLayers) { layerIntersectionIndexes[layerIndex].push(lineIndex); - var y = layerIndex * layerHeight; + const y = layerIndex * layerHeight; + let x, z; if (line.start.y === line.end.y) { - var x = line.start.x; - var z = line.start.z; + x = line.start.x; + z = line.start.z; } else { - var alpha = (y - line.start.y) / (line.end.y - line.start.y); - var x = line.end.x * alpha + line.start.x * (1 - alpha); - var z = line.end.z * alpha + line.start.z * (1 - alpha); + const alpha = (y - line.start.y) / (line.end.y - line.start.y); + x = line.end.x * alpha + line.start.x * (1 - alpha); + z = line.end.z * alpha + line.start.z * (1 - alpha); } layerIntersectionPoints[layerIndex][lineIndex] = new THREE.Vector2(z, x); From bd910a270c5935eb9e93c42983720d970593c920 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 09:53:48 +0200 Subject: [PATCH 08/64] geplakt var with const or let in createLines --- src/sliceActions/createLines.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/sliceActions/createLines.js b/src/sliceActions/createLines.js index b740503..b57b429 100644 --- a/src/sliceActions/createLines.js +++ b/src/sliceActions/createLines.js @@ -1,7 +1,7 @@ import THREE from 'three.js'; function addLine(geometry, lineLookup, lines, a, b) { - var index = lineLookup[`${b}_${a}`]; + let index = lineLookup[`${b}_${a}`]; if (index === undefined) { index = lines.length; @@ -20,19 +20,19 @@ function addLine(geometry, lineLookup, lines, a, b) { export default function createLines(geometry, settings) { console.log('constructing unique lines from geometry'); - var lines = []; - var lineLookup = {}; + const lines = []; + const lineLookup = {}; - for (var i = 0; i < geometry.faces.length; i ++) { - var face = geometry.faces[i]; + for (let i = 0; i < geometry.faces.length; i ++) { + const face = geometry.faces[i]; if (face.normal.y !== 1 && face.normal.y !== -1) { - var normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize(); + const normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize(); // check for only adding unique lines // returns index of said line - var a = addLine(geometry, lineLookup, lines, face.a, face.b); - var b = addLine(geometry, lineLookup, lines, face.b, face.c); - var c = addLine(geometry, lineLookup, lines, face.c, face.a); + const a = addLine(geometry, lineLookup, lines, face.a, face.b); + const b = addLine(geometry, lineLookup, lines, face.b, face.c); + const c = addLine(geometry, lineLookup, lines, face.c, face.a); // set connecting lines (based on face) lines[a].connects.push(b, c); From 2b10f388f7d3c0e20ba3f091bd0960f0711a3ab5 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Tue, 29 Mar 2016 15:56:32 +0200 Subject: [PATCH 09/64] simplified add gcode --- src/gcode.js | 58 +++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/gcode.js b/src/gcode.js index fb0320f..59b40a4 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -11,33 +11,35 @@ export default class { this.isFanOn = false; this._nozzlePosition = new THREE.Vector2(0, 0); } - - _addGCode (command) { - var str = ''; - var first = true; - for (var i in command) { + _addGCode (command) { + let str = ''; + let first = true; + + for (const action in command) { + const value = command[action]; + const currentValue = this.current[action]; if (first) { - str = i + command[i]; + str = action + value; first = false; } - else if (this.current[i] !== command[i]) { - str += ' ' + i + command[i]; + else if (currentValue !== value) { + str += ` ${action}${value}`; - this.current[i] = command[i]; + this.current[action] = value; } } this.gcode += str + '\n'; } - + setSettings (settings) { this.settings = settings; return this; } - + turnFanOn (fanSpeed) { this.isFanOn = true; @@ -53,7 +55,7 @@ export default class { return this; } - + turnFanOff () { this.isFanOn = false; @@ -63,25 +65,25 @@ export default class { return this; } - + moveTo (x, y, layer) { var layerHeight = this.settings.config['layerHeight']; var travelSpeed = this.settings.config['travelSpeed']; - + var z = (layer + 1) * layerHeight; var speed = travelSpeed * 60; this._addGCode({ - 'G': 0, - 'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), + 'G': 0, + 'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), 'F': speed.toFixed(3) }); - + this._nozzlePosition.set(x, y); return this; } - + lineTo (x, y, layer, type) { var newNozzlePosition = new THREE.Vector2(x, y); @@ -103,8 +105,8 @@ export default class { this._addGCode({ 'G': 1, - 'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), - 'F': speed.toFixed(3), + 'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), + 'F': speed.toFixed(3), 'E': this.extruder.toFixed(3) }); @@ -112,7 +114,7 @@ export default class { return this; } - + unRetract () { var retractionEnabled = this.settings.config['retractionEnabled']; var retractionMinDistance = this.settings.config['retractionMinDistance']; @@ -125,8 +127,8 @@ export default class { if (this.extruder > retractionMinDistance) { this._addGCode({ - 'G': 0, - 'E': this.extruder.toFixed(3), + 'G': 0, + 'E': this.extruder.toFixed(3), 'F': speed.toFixed(3) }); } @@ -134,7 +136,7 @@ export default class { return this; } - + retract () { var retractionAmount = this.settings.config['retractionAmount']; var retractionEnabled = this.settings.config['retractionEnabled']; @@ -143,13 +145,13 @@ export default class { if (!this.isRetracted && retractionEnabled) { this.isRetracted = true; - + var speed = retractionSpeed * 60; if (this.extruder > retractionMinDistance && retractionEnabled) { this._addGCode({ - 'G': 0, - 'E': (this.extruder - retractionAmount).toFixed(3), + 'G': 0, + 'E': (this.extruder - retractionAmount).toFixed(3), 'F': speed.toFixed(3) }); } @@ -157,7 +159,7 @@ export default class { return this; } - + getGCode () { return this.settings.startCode() + this.gcode + this.settings.endCode(); } From 4edb1985c8ff3df153e182efd186b377b40c4cee Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Thu, 21 Apr 2016 22:14:22 +0200 Subject: [PATCH 10/64] rafacter --- config.js | 58 +++++++++--------- package.json | 1 + src/slice.js | 59 +++++------------- src/sliceActions/generateInfills.js | 2 +- src/sliceActions/generateInnerLines.js | 22 ++++--- src/sliceActions/getFillTemplate.js | 10 +-- src/sliceActions/intersectionsToShapes.js | 23 ++++--- src/sliceActions/optimizePaths.js | 12 +++- src/sliceActions/shapesToSlices.js | 74 +++++++---------------- src/sliceActions/slicesToGCode.js | 15 +++-- src/slicer.js | 9 ++- 11 files changed, 121 insertions(+), 164 deletions(-) diff --git a/config.js b/config.js index 59039c6..d6eaae0 100644 --- a/config.js +++ b/config.js @@ -16,81 +16,77 @@ System.config({ }, map: { - "babel": "npm:babel-core@5.8.21", - "babel-runtime": "npm:babel-runtime@5.8.20", + "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@0.0.2", + "babel": "npm:babel-core@5.8.38", + "babel-runtime": "npm:babel-runtime@5.8.38", "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", "clipper-lib": "npm:clipper-lib@1.0.0", "core-js": "npm:core-js@0.9.18", "json": "github:systemjs/plugin-json@0.1.0", - "nodeca/js-yaml": "github:nodeca/js-yaml@3.3.1", + "nodeca/js-yaml": "github:nodeca/js-yaml@3.5.5", "read-yaml": "npm:read-yaml@1.0.0", "systemjs/plugin-json": "github:systemjs/plugin-json@0.1.0", "three.js": "github:mrdoob/three.js@r72", + "github:Doodle3D/clipper-js@0.0.2": { + "clipper-lib": "npm:clipper-lib@1.0.0" + }, "github:jspm/nodelibs-assert@0.1.0": { "assert": "npm:assert@1.3.0" }, "github:jspm/nodelibs-path@0.1.0": { "path-browserify": "npm:path-browserify@0.0.0" }, - "github:jspm/nodelibs-process@0.1.1": { - "process": "npm:process@0.10.1" + "github:jspm/nodelibs-process@0.1.2": { + "process": "npm:process@0.11.2" }, "github:jspm/nodelibs-util@0.1.0": { "util": "npm:util@0.10.3" }, - "npm:argparse@1.0.2": { - "assert": "github:jspm/nodelibs-assert@0.1.0", + "npm:argparse@1.0.7": { "fs": "github:jspm/nodelibs-fs@0.1.2", - "lodash": "npm:lodash@3.10.1", "path": "github:jspm/nodelibs-path@0.1.0", - "process": "github:jspm/nodelibs-process@0.1.1", + "process": "github:jspm/nodelibs-process@0.1.2", "sprintf-js": "npm:sprintf-js@1.0.3", "util": "github:jspm/nodelibs-util@0.1.0" }, "npm:assert@1.3.0": { "util": "npm:util@0.10.3" }, - "npm:babel-runtime@5.8.20": { - "process": "github:jspm/nodelibs-process@0.1.1" + "npm:babel-runtime@5.8.38": { + "process": "github:jspm/nodelibs-process@0.1.2" }, "npm:clipper-lib@1.0.0": { - "process": "github:jspm/nodelibs-process@0.1.1" + "process": "github:jspm/nodelibs-process@0.1.2" }, "npm:core-js@0.9.18": { "fs": "github:jspm/nodelibs-fs@0.1.2", - "process": "github:jspm/nodelibs-process@0.1.1", + "process": "github:jspm/nodelibs-process@0.1.2", "systemjs-json": "github:systemjs/plugin-json@0.1.0" }, - "npm:esprima@2.2.0": { - "fs": "github:jspm/nodelibs-fs@0.1.2", - "process": "github:jspm/nodelibs-process@0.1.1" - }, "npm:inherits@2.0.1": { "util": "github:jspm/nodelibs-util@0.1.0" }, - "npm:js-yaml@3.3.1": { - "argparse": "npm:argparse@1.0.2", - "esprima": "npm:esprima@2.2.0", + "npm:js-yaml@3.5.5": { + "argparse": "npm:argparse@1.0.7", + "esprima": "npm:esprima@2.7.2", "fs": "github:jspm/nodelibs-fs@0.1.2", - "path": "github:jspm/nodelibs-path@0.1.0", - "process": "github:jspm/nodelibs-process@0.1.1", - "systemjs-json": "github:systemjs/plugin-json@0.1.0", - "util": "github:jspm/nodelibs-util@0.1.0" - }, - "npm:lodash@3.10.1": { - "process": "github:jspm/nodelibs-process@0.1.1" + "process": "github:jspm/nodelibs-process@0.1.2", + "systemjs-json": "github:systemjs/plugin-json@0.1.0" }, "npm:path-browserify@0.0.0": { - "process": "github:jspm/nodelibs-process@0.1.1" + "process": "github:jspm/nodelibs-process@0.1.2" + }, + "npm:process@0.11.2": { + "assert": "github:jspm/nodelibs-assert@0.1.0" }, "npm:read-yaml@1.0.0": { "fs": "github:jspm/nodelibs-fs@0.1.2", - "js-yaml": "npm:js-yaml@3.3.1", - "xtend": "npm:xtend@4.0.0" + "js-yaml": "npm:js-yaml@3.5.5", + "xtend": "npm:xtend@4.0.1" }, "npm:util@0.10.3": { "inherits": "npm:inherits@2.0.1", - "process": "github:jspm/nodelibs-process@0.1.1" + "process": "github:jspm/nodelibs-process@0.1.2" } } }); diff --git a/package.json b/package.json index 233a5cb..4424f9d 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "lib": "src" }, "dependencies": { + "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@^0.0.2", "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", "clipper-lib": "npm:clipper-lib@^1.0.0", "nodeca/js-yaml": "github:nodeca/js-yaml@^3.3.1", diff --git a/src/slice.js b/src/slice.js index c3c13d3..73902bb 100644 --- a/src/slice.js +++ b/src/slice.js @@ -1,35 +1,10 @@ -import Paths from './paths.js'; +import Shape from 'Doodle3D/clipper-js'; export default class { constructor () { this.parts = []; } - removeSelfIntersect () { - for (var i = 0; i < this.parts.length; i ++) { - var part1 = this.parts[i].intersect; - - if (!part1.closed) { - continue; - } - - for (var j = i + 1; j < this.parts.length; j ++) { - var part2 = this.parts[j].intersect; - - if (!part2.closed) { - continue; - } - - if (part2.intersect(part1).length > 0) { - part1 = this.parts[i].intersect = part1.union(part2); - - this.parts.splice(j, 1); - j --; - } - } - } - } - optimizePaths (start) { if (this.brim !== undefined && this.brim.length > 0) { this.brim = this.brim.optimizePath(start); @@ -45,11 +20,11 @@ export default class { for (var i = 0; i < this.parts.length; i ++) { var part = this.parts[i]; - if (part.intersect.closed) { + if (part.shape.closed) { var bounds = part.outerLine.bounds(); } else { - var bounds = part.intersect.bounds(); + var bounds = part.shape.bounds(); } var top = bounds.top - start.y; @@ -68,7 +43,7 @@ export default class { var part = this.parts.splice(closestPart, 1)[0]; parts.push(part); - if (part.intersect.closed) { + if (part.shape.closed) { if (part.outerLine.length > 0) { part.outerLine = part.outerLine.optimizePath(start); start = part.outerLine.lastPoint(); @@ -88,8 +63,8 @@ export default class { } } else { - part.intersect.optimizePath(start); - start = part.intersect.lastPoint(); + part.shape.optimizePath(start); + start = part.shape.lastPoint(); } } @@ -105,12 +80,12 @@ export default class { } getOutline () { - var outLines = new Paths([], true); + var outLines = new Shape([], true); for (var i = 0; i < this.parts.length; i ++) { var part = this.parts[i]; - if (part.intersect.closed) { + if (part.shape.closed) { outLines.join(this.parts[i].outerLine); } } @@ -118,17 +93,15 @@ export default class { return outLines; } - add (intersect) { - var parts = { - intersect - }; + add (shape) { + const part = { shape }; - if (intersect.closed) { - parts.innerLines = []; - parts.outerLine = new Paths([], true); - parts.fill = new Paths([], false); + if (shape.closed) { + part.innerLines = []; + part.outerLine = new Shape([], true); + part.fill = new Shape([], false); } - this.parts.push(parts); + this.parts.push(part); } -} \ No newline at end of file +} diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index b3ae254..9c23d50 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -41,7 +41,7 @@ export default function generateInfills(slices, settings) { for (var i = 0; i < slice.parts.length; i ++) { var part = slice.parts[i]; - if (!part.intersect.closed) { + if (!part.shape.closed) { continue; } diff --git a/src/sliceActions/generateInnerLines.js b/src/sliceActions/generateInnerLines.js index 006b5a6..1033ecb 100644 --- a/src/sliceActions/generateInnerLines.js +++ b/src/sliceActions/generateInnerLines.js @@ -1,10 +1,16 @@ const scale = 100; +const offsetOptions = { + jointType: 'jtSquare', + endType: 'etClosedPolygon', + miterLimit: 2.0, + roundPrecision: 0.25 +}; export default function generateInnerLines(slices, settings) { console.log("generating outer lines and inner lines"); // need to scale up everything because of clipper rounding errors - let {layerHeight, nozzleDiameter, shellThickness} = settings.config; + let { layerHeight, nozzleDiameter, shellThickness } = settings.config; nozzleDiameter *= scale; shellThickness *= scale; var nozzleRadius = nozzleDiameter / 2; @@ -16,23 +22,21 @@ export default function generateInnerLines(slices, settings) { for (var i = 0; i < slice.parts.length; i ++) { var part = slice.parts[i]; - if (!part.intersect.closed) { - continue; - } + if (!part.shape.closed) continue; - // var outerLine = part.intersect.clone().scaleUp(scale).offset(-nozzleRadius); - var outerLine = part.intersect.scaleUp(scale).offset(-nozzleRadius); + // var outerLine = part.shape.clone().scaleUp(scale).offset(-nozzleRadius); + var outerLine = part.shape.scaleUp(scale).offset(-nozzleRadius, offsetOptions); if (outerLine.length > 0) { - part.outerLine = outerLine; + part.outerLine.join(outerLine); for (var shell = 1; shell < shells; shell += 1) { var offset = shell * nozzleDiameter; - var innerLine = outerLine.offset(-offset); + var innerLine = outerLine.offset(-offset, offsetOptions); if (innerLine.length > 0) { - part.innerLines.push(innerLine); + part.innerLines.paths.push(innerLine); } else { break; diff --git a/src/sliceActions/getFillTemplate.js b/src/sliceActions/getFillTemplate.js index 034e51f..ea36f39 100644 --- a/src/sliceActions/getFillTemplate.js +++ b/src/sliceActions/getFillTemplate.js @@ -1,7 +1,7 @@ -import Paths from '../paths.js'; +import Shape from 'Doodle3D/clipper-js'; export default function getFillTemplate(bounds, size, even, uneven) { - var paths = new Paths([], false); + var shape = new Shape([], false); var left = Math.floor(bounds.left / size) * size; var right = Math.ceil(bounds.right / size) * size; @@ -12,7 +12,7 @@ export default function getFillTemplate(bounds, size, even, uneven) { if (even) { for (var y = top; y <= bottom + width; y += size) { - paths.push([ + shape.paths.push([ {X: left, Y: y}, {X: right, Y: y - width} ]); @@ -20,12 +20,12 @@ export default function getFillTemplate(bounds, size, even, uneven) { } if (uneven) { for (var y = top - width; y <= bottom; y += size) { - paths.push([ + shape.paths.push([ {X: left, Y: y}, {X: right, Y: y + width} ]); } } - return paths; + return shape; } diff --git a/src/sliceActions/intersectionsToShapes.js b/src/sliceActions/intersectionsToShapes.js index fe4f2fa..a4742f3 100644 --- a/src/sliceActions/intersectionsToShapes.js +++ b/src/sliceActions/intersectionsToShapes.js @@ -1,10 +1,10 @@ import THREE from 'three.js'; -import Paths from '../paths.js'; +import Shape from 'Doodle3D/clipper-js'; export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) { console.log("generating slices"); - var shapes = []; + var layers = []; for (var layer = 1; layer < layerIntersectionIndexes.length; layer ++) { var intersectionIndexes = layerIntersectionIndexes[layer]; @@ -14,7 +14,8 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt continue; } - var shapeParts = []; + var closedShapes = []; + var openShapes = []; for (var i = 0; i < intersectionIndexes.length; i ++) { var index = intersectionIndexes[i]; @@ -31,7 +32,7 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt while (index !== -1) { var intersection = intersectionPoints[index]; // uppercase X and Y because clipper vector - shape.push({X: intersection.x, Y: intersection.y}); + shape.push(intersection); delete intersectionPoints[index]; @@ -94,7 +95,7 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt while (index !== -1) { if (firstPoints.indexOf(index) === -1) { var intersection = intersectionPoints[index]; - shape.unshift({X: intersection.x, Y: intersection.y}); + shape.unshift(intersection); delete intersectionPoints[index]; } @@ -114,14 +115,16 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt } } - var part = new Paths([shape], closed).clean(0.01); - if (part.length > 0) { - shapeParts.push(part); + if (closed) { + closedShapes.push(shape); + } + else { + openShapes.push(shape); } } - shapes.push(shapeParts); + layers.push({ closedShapes, openShapes }); } - return shapes; + return layers; } diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js index 9ed9b3a..7181f1f 100644 --- a/src/sliceActions/optimizePaths.js +++ b/src/sliceActions/optimizePaths.js @@ -1,4 +1,10 @@ import THREE from 'three.js'; +const offsetOptions = { + jointType: 'jtSquare', + endType: 'etClosedPolygon', + miterLimit: 2.0, + roundPrecision: 0.25 +}; export default function optimizePaths(slices, settings) { console.log("opimize paths"); @@ -14,15 +20,15 @@ export default function optimizePaths(slices, settings) { var slice = slices[layer]; if (layer === 0) { - slice.brim = slice.getOutline().offset(brimOffset); + slice.brim = slice.getOutline().offset(brimOffset, offsetOptions); } - start = slice.optimizePaths(start); + // start = slice.optimizePaths(start); for (var i = 0; i < slice.parts.length; i ++) { var part = slice.parts[i]; - if (part.intersect.closed) { + if (part.shape.closed) { part.outerLine.scaleDown(scale); for (var j = 0; j < part.innerLines.length; j ++) { var innerLine = part.innerLines[j]; diff --git a/src/sliceActions/shapesToSlices.js b/src/sliceActions/shapesToSlices.js index d261ca1..36371f9 100644 --- a/src/sliceActions/shapesToSlices.js +++ b/src/sliceActions/shapesToSlices.js @@ -1,65 +1,37 @@ +import Shape from 'Doodle3D/clipper-js'; import Slice from '../slice.js'; export default function shapesToSlices(shapes, settings) { - var slices = []; + const sliceLayers = []; for (var layer = 0; layer < shapes.length; layer ++) { - var shapeParts = shapes[layer]; + var { closedShapes, openShapes } = shapes[layer]; + + closedShapes = new Shape(closedShapes, true, true) + .clean(0.01) + .fixOrientation() + .removeOverlap() + .seperateShapes(); + + openShapes = new Shape(openShapes, false, true) + .clean(0.01); var slice = new Slice(); - var holes = []; - var outlines = []; + for (var i = 0; i < closedShapes.length; i ++) { + var closedShape = closedShapes[i]; + slice.add(closedShape); - for (var i = 0; i < shapeParts.length; i ++) { - var shape = shapeParts[i]; - - if (!shape.closed) { - slice.add(shape); - } - else if (shape.isHole()) { - holes.push(shape); - } - else { - slice.add(shape); - outlines.push(shape); - } + // if (openShapes.path.length > 0) { + // openShapes = openShapes.difference(closedShape); + // } + } + if (openShapes.paths.length > 0) { + slice.add(openShapes); } - outlines.sort((a, b) => { - return a.boundSize() - b.boundSize(); - }); - - if (holes.length > outlines.length) { - [holes, outlines] = [outlines, holes]; - } - else if (holes.length === outlines.length) { - holes.sort((a, b) => { - return a.boundSize() - b.boundSize(); - }); - - if (holes[0].boundSize > outlines[0].boundSize()) { - [holes, outlines] = [outlines, holes]; - } - } - - for (var i = 0; i < holes.length; i ++) { - var hole = holes[i]; - - for (var j = 0; j < outlines.length; j ++) { - var outline = outlines[j]; - - if (outline.pointCollision(hole[0][0])) { - outline.join(hole); - break; - } - } - } - - slice.removeSelfIntersect(); - - slices.push(slice); + sliceLayers.push(slice); } - return slices; + return sliceLayers; } diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js index 6d9ab1a..5abe6f5 100644 --- a/src/sliceActions/slicesToGCode.js +++ b/src/sliceActions/slicesToGCode.js @@ -3,15 +3,14 @@ import GCode from '../gcode.js'; export default function slicesToGCode(slices, settings) { var gcode = new GCode().setSettings(settings); - function pathToGCode (path, retract, unRetract, type) { + function pathToGCode (shape, retract, unRetract, type) { + for (var i = 0; i < shape.paths.length; i ++) { + var line = shape.paths[i]; - for (var i = 0; i < path.length; i ++) { - var shape = path[i]; - - var length = path.closed ? (shape.length + 1) : shape.length; + var length = shape.closed ? (line.length + 1) : line.length; for (var j = 0; j < length; j ++) { - var point = shape[j % shape.length]; + var point = line[j % line.length]; if (j === 0) { // TODO @@ -48,7 +47,7 @@ export default function slicesToGCode(slices, settings) { for (var i = 0; i < slice.parts.length; i ++) { var part = slice.parts[i]; - if (part.intersect.closed) { + if (part.shape.closed) { pathToGCode(part.outerLine, false, true, "outerLine"); for (var j = 0; j < part.innerLines.length; j ++) { @@ -60,7 +59,7 @@ export default function slicesToGCode(slices, settings) { } else { var retract = !(slice.parts.length === 1 && slice.support === undefined); - pathToGCode(part.intersect, retract, retract, "outerLine"); + pathToGCode(part.shape, retract, retract, "outerLine"); } } diff --git a/src/slicer.js b/src/slicer.js index ceae634..536d4d0 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -67,15 +67,18 @@ export default class extends EventDispatcher { this.progress.createdLines = true; this._updateProgress(settings); - var {layerIntersectionIndexes, layerIntersectionPoints} = calculateLayersIntersections(lines, settings); + const { + layerIntersectionIndexes, + layerIntersectionPoints + } = calculateLayersIntersections(lines, settings); this.progress.calculatedLayerIntersections = true; this._updateProgress(settings); - var shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings); + const shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings); this.progress.sliced = true; this._updateProgress(settings); - var slices = shapesToSlices(shapes, settings); + const slices = shapesToSlices(shapes, settings); this.progress.generatedSlices = true; this._updateProgress(settings); From 386a53849f189a90e0827a9db7d4615da7a8e842 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Thu, 21 Apr 2016 22:22:59 +0200 Subject: [PATCH 11/64] remove unused imports --- config.js | 29 ----------------------------- package.json | 4 ---- src/index.js | 7 +------ src/sliceActions/generateInfills.js | 4 ++-- src/sliceActions/generateSupport.js | 4 ++-- 5 files changed, 5 insertions(+), 43 deletions(-) diff --git a/config.js b/config.js index d6eaae0..f4bfbec 100644 --- a/config.js +++ b/config.js @@ -20,12 +20,8 @@ System.config({ "babel": "npm:babel-core@5.8.38", "babel-runtime": "npm:babel-runtime@5.8.38", "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", - "clipper-lib": "npm:clipper-lib@1.0.0", "core-js": "npm:core-js@0.9.18", "json": "github:systemjs/plugin-json@0.1.0", - "nodeca/js-yaml": "github:nodeca/js-yaml@3.5.5", - "read-yaml": "npm:read-yaml@1.0.0", - "systemjs/plugin-json": "github:systemjs/plugin-json@0.1.0", "three.js": "github:mrdoob/three.js@r72", "github:Doodle3D/clipper-js@0.0.2": { "clipper-lib": "npm:clipper-lib@1.0.0" @@ -33,22 +29,12 @@ System.config({ "github:jspm/nodelibs-assert@0.1.0": { "assert": "npm:assert@1.3.0" }, - "github:jspm/nodelibs-path@0.1.0": { - "path-browserify": "npm:path-browserify@0.0.0" - }, "github:jspm/nodelibs-process@0.1.2": { "process": "npm:process@0.11.2" }, "github:jspm/nodelibs-util@0.1.0": { "util": "npm:util@0.10.3" }, - "npm:argparse@1.0.7": { - "fs": "github:jspm/nodelibs-fs@0.1.2", - "path": "github:jspm/nodelibs-path@0.1.0", - "process": "github:jspm/nodelibs-process@0.1.2", - "sprintf-js": "npm:sprintf-js@1.0.3", - "util": "github:jspm/nodelibs-util@0.1.0" - }, "npm:assert@1.3.0": { "util": "npm:util@0.10.3" }, @@ -66,24 +52,9 @@ System.config({ "npm:inherits@2.0.1": { "util": "github:jspm/nodelibs-util@0.1.0" }, - "npm:js-yaml@3.5.5": { - "argparse": "npm:argparse@1.0.7", - "esprima": "npm:esprima@2.7.2", - "fs": "github:jspm/nodelibs-fs@0.1.2", - "process": "github:jspm/nodelibs-process@0.1.2", - "systemjs-json": "github:systemjs/plugin-json@0.1.0" - }, - "npm:path-browserify@0.0.0": { - "process": "github:jspm/nodelibs-process@0.1.2" - }, "npm:process@0.11.2": { "assert": "github:jspm/nodelibs-assert@0.1.0" }, - "npm:read-yaml@1.0.0": { - "fs": "github:jspm/nodelibs-fs@0.1.2", - "js-yaml": "npm:js-yaml@3.5.5", - "xtend": "npm:xtend@4.0.1" - }, "npm:util@0.10.3": { "inherits": "npm:inherits@2.0.1", "process": "github:jspm/nodelibs-process@0.1.2" diff --git a/package.json b/package.json index 4424f9d..94f6285 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,6 @@ "dependencies": { "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@^0.0.2", "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", - "clipper-lib": "npm:clipper-lib@^1.0.0", - "nodeca/js-yaml": "github:nodeca/js-yaml@^3.3.1", - "read-yaml": "npm:read-yaml@^1.0.0", - "systemjs/plugin-json": "github:systemjs/plugin-json@^0.1.0", "three.js": "github:mrdoob/three.js@r72" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index 6105f16..fa0ba99 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,5 @@ import Slicer from './slicer.js'; import SlicerWorker from './slicerworker.js'; import Settings from './settings.js'; -import ClipperLib from 'clipper-lib'; -ClipperLib.Error = function (message) { - console.error(message); -}; - -export {Slicer, SlicerWorker, Settings}; +export { Slicer, SlicerWorker, Settings }; diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index 9c23d50..e6a61d4 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -1,5 +1,5 @@ import getFillTemplate from './getFillTemplate.js'; -import Paths from '../paths.js'; +import Shape from 'Doodle3D/clipper-js'; const scale = 100; @@ -67,7 +67,7 @@ export default function generateInfills(slices, settings) { var highFillArea = fillArea; } - var fill = new Paths([], false); + var fill = new Shape([], false); if (lowFillArea && lowFillArea.length > 0) { var bounds = lowFillArea.bounds(); diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index a974399..1c44374 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -1,5 +1,5 @@ import getFillTemplate from './getFillTemplate.js'; -import Paths from '../paths.js'; +import Shape from 'Doodle3D/clipper-js'; const scale = 100; @@ -22,7 +22,7 @@ export default function generateSupport(slices, settings) { nozzleDiameter *= scale; var supportDistanceLayers = Math.max(Math.ceil(supportDistanceY / layerHeight), 1); - var supportAreas = new Paths([], true); + var supportAreas = new Shape([], true); for (var layer = slices.length - 1 - supportDistanceLayers; layer >= 0; layer --) { var currentSlice = slices[layer]; From 00c94f5d67039e3e230ccc2c9e710de816784733 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Thu, 21 Apr 2016 22:50:02 +0200 Subject: [PATCH 12/64] fix join --- config.js | 4 ++-- package.json | 2 +- src/sliceActions/generateInfills.js | 2 -- src/sliceActions/generateInnerLines.js | 7 ++++--- src/slicer.js | 16 ++++++---------- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/config.js b/config.js index f4bfbec..5e9a068 100644 --- a/config.js +++ b/config.js @@ -16,14 +16,14 @@ System.config({ }, map: { - "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@0.0.2", + "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master", "babel": "npm:babel-core@5.8.38", "babel-runtime": "npm:babel-runtime@5.8.38", "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", "core-js": "npm:core-js@0.9.18", "json": "github:systemjs/plugin-json@0.1.0", "three.js": "github:mrdoob/three.js@r72", - "github:Doodle3D/clipper-js@0.0.2": { + "github:Doodle3D/clipper-js@master": { "clipper-lib": "npm:clipper-lib@1.0.0" }, "github:jspm/nodelibs-assert@0.1.0": { diff --git a/package.json b/package.json index 94f6285..15a35e3 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "lib": "src" }, "dependencies": { - "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@^0.0.2", + "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master", "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", "three.js": "github:mrdoob/three.js@r72" }, diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index e6a61d4..39939b9 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -6,8 +6,6 @@ const scale = 100; export default function generateInfills(slices, settings) { console.log("generating infills"); - // need to scale up everything because of clipper rounding errors - let { layerHeight, fillGridSize, diff --git a/src/sliceActions/generateInnerLines.js b/src/sliceActions/generateInnerLines.js index 1033ecb..ec69641 100644 --- a/src/sliceActions/generateInnerLines.js +++ b/src/sliceActions/generateInnerLines.js @@ -27,7 +27,8 @@ export default function generateInnerLines(slices, settings) { // var outerLine = part.shape.clone().scaleUp(scale).offset(-nozzleRadius); var outerLine = part.shape.scaleUp(scale).offset(-nozzleRadius, offsetOptions); - if (outerLine.length > 0) { + + if (outerLine.paths.length > 0) { part.outerLine.join(outerLine); for (var shell = 1; shell < shells; shell += 1) { @@ -35,8 +36,8 @@ export default function generateInnerLines(slices, settings) { var innerLine = outerLine.offset(-offset, offsetOptions); - if (innerLine.length > 0) { - part.innerLines.paths.push(innerLine); + if (innerLine.paths.length > 0) { + part.innerLines.push(innerLine); } else { break; diff --git a/src/slicer.js b/src/slicer.js index 536d4d0..0892733 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -104,23 +104,22 @@ export default class extends EventDispatcher { this.progress.generatedGCode = true; this._updateProgress(settings); - this.dispatchEvent({ - type: 'finish', - gcode - }); + this.dispatchEvent({ type: 'finish', gcode }); + + console.log(slices); return gcode; } _updateProgress (settings) { - var supportEnabled = settings.config["supportEnabled"]; + var supportEnabled = settings.config['supportEnabled']; var progress = {}; var procent = 0; var length = 0; for (var i in this.progress) { - if (!(!supportEnabled && i === "generatedSupport")) { + if (!(!supportEnabled && i === 'generatedSupport')) { progress[i] = this.progress[i]; if (progress[i]) { procent += 1; @@ -131,9 +130,6 @@ export default class extends EventDispatcher { progress.procent = procent / length; - this.dispatchEvent({ - type: 'progress', - progress - }); + this.dispatchEvent({ type: 'progress', progress }); } } From 09bd688b88c946e7ccf811b5435376ff459d4967 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 22 Apr 2016 19:37:34 +0200 Subject: [PATCH 13/64] fix get fill template --- src/sliceActions/getFillTemplate.js | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/sliceActions/getFillTemplate.js b/src/sliceActions/getFillTemplate.js index ea36f39..25df1e4 100644 --- a/src/sliceActions/getFillTemplate.js +++ b/src/sliceActions/getFillTemplate.js @@ -1,31 +1,31 @@ import Shape from 'Doodle3D/clipper-js'; export default function getFillTemplate(bounds, size, even, uneven) { - var shape = new Shape([], false); + const paths = []; - var left = Math.floor(bounds.left / size) * size; - var right = Math.ceil(bounds.right / size) * size; - var top = Math.floor(bounds.top / size) * size; - var bottom = Math.ceil(bounds.bottom / size) * size; + const left = Math.floor(bounds.left / size) * size; + const right = Math.ceil(bounds.right / size) * size; + const top = Math.floor(bounds.top / size) * size; + const bottom = Math.ceil(bounds.bottom / size) * size; - var width = right - left; + const width = right - left; if (even) { - for (var y = top; y <= bottom + width; y += size) { - shape.paths.push([ - {X: left, Y: y}, - {X: right, Y: y - width} + for (let y = top; y <= bottom + width; y += size) { + paths.push([ + { x: left, y }, + { x: right, y: y - width } ]); } } if (uneven) { - for (var y = top - width; y <= bottom; y += size) { - shape.paths.push([ - {X: left, Y: y}, - {X: right, Y: y + width} + for (let y = top - width; y <= bottom; y += size) { + paths.push([ + { x: left, y }, + { x: right, y: y + width } ]); } } - return shape; + return new Shape(paths, false, true); } From fb691f175c22ff4cf419e17f8441eef379d4c908 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 22 Apr 2016 19:38:00 +0200 Subject: [PATCH 14/64] replace var with const --- src/sliceActions/generateInnerLines.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sliceActions/generateInnerLines.js b/src/sliceActions/generateInnerLines.js index ec69641..4d08e91 100644 --- a/src/sliceActions/generateInnerLines.js +++ b/src/sliceActions/generateInnerLines.js @@ -25,8 +25,7 @@ export default function generateInnerLines(slices, settings) { if (!part.shape.closed) continue; // var outerLine = part.shape.clone().scaleUp(scale).offset(-nozzleRadius); - var outerLine = part.shape.scaleUp(scale).offset(-nozzleRadius, offsetOptions); - + const outerLine = part.shape.scaleUp(scale).offset(-nozzleRadius, offsetOptions); if (outerLine.paths.length > 0) { part.outerLine.join(outerLine); From 6891e09b54d1bce062ed17b63303d9825b3ec795 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 22 Apr 2016 19:38:06 +0200 Subject: [PATCH 15/64] fix infills --- src/sliceActions/generateInfills.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index 39939b9..ce20da4 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -45,13 +45,14 @@ export default function generateInfills(slices, settings) { var outerLine = part.outerLine; - if (outerLine.length > 0) { + if (outerLine.paths.length > 0) { var inset = (part.innerLines.length > 0) ? part.innerLines[part.innerLines.length - 1] : outerLine; - var fillArea = inset.offset(-nozzleRadius); - var lowFillArea = false; + const fillArea = inset.offset(-nozzleRadius); + let lowFillArea; + let highFillArea; if (surroundingLayer) { - var highFillArea = fillArea.difference(surroundingLayer); + highFillArea = fillArea.difference(surroundingLayer); if (infillOverlap > 0) { highFillArea = highFillArea.offset(infillOverlap); @@ -59,23 +60,21 @@ export default function generateInfills(slices, settings) { highFillArea = highFillArea.intersect(fillArea); - var lowFillArea = fillArea.difference(highFillArea); + lowFillArea = fillArea.difference(highFillArea); } else { - var highFillArea = fillArea; + highFillArea = fillArea; } - var fill = new Shape([], false); - - if (lowFillArea && lowFillArea.length > 0) { - var bounds = lowFillArea.bounds(); + if (lowFillArea && lowFillArea.paths.length > 0) { + var bounds = lowFillArea.shapeBounds(); var lowFillTemplate = getFillTemplate(bounds, fillGridSize, true, true); part.fill.join(lowFillTemplate.intersect(lowFillArea)); } - if (highFillArea.length > 0) { - var bounds = highFillArea.bounds(); + if (highFillArea.paths.length > 0) { + var bounds = highFillArea.shapeBounds(); var even = (layer % 2 === 0); var highFillTemplate = getFillTemplate(bounds, hightemplateSize, even, !even); From 1cddce9e697d66857a5a07a28c094758b5e60b2b Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 22 Apr 2016 21:14:21 +0200 Subject: [PATCH 16/64] replace vars with const and let --- src/sliceActions/generateInfills.js | 40 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index ce20da4..6ceb64a 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -19,34 +19,32 @@ export default function generateInfills(slices, settings) { nozzleDiameter *= scale; infillOverlap *= scale; - var bottomSkinCount = Math.ceil(bottomThickness/layerHeight); - var topSkinCount = Math.ceil(topThickness/layerHeight); - var nozzleRadius = nozzleDiameter / 2; - var hightemplateSize = Math.sqrt(2 * Math.pow(nozzleDiameter, 2)); + const bottomSkinCount = Math.ceil(bottomThickness/layerHeight); + const topSkinCount = Math.ceil(topThickness/layerHeight); + const nozzleRadius = nozzleDiameter / 2; + const hightemplateSize = Math.sqrt(2 * Math.pow(nozzleDiameter, 2)); - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; + for (let layer = 0; layer < slices.length; layer ++) { + const slice = slices[layer]; + let surroundingLayer; if (layer - bottomSkinCount >= 0 && layer + topSkinCount < slices.length) { - var downSkin = slices[layer - bottomSkinCount].getOutline(); - var upSkin = slices[layer + topSkinCount].getOutline(); - var surroundingLayer = upSkin.intersect(downSkin); - } - else { - var surroundingLayer = false; + const downSkin = slices[layer - bottomSkinCount].getOutline(); + const upSkin = slices[layer + topSkinCount].getOutline(); + surroundingLayer = upSkin.intersect(downSkin); } - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; + for (let i = 0; i < slice.parts.length; i ++) { + const part = slice.parts[i]; if (!part.shape.closed) { continue; } - var outerLine = part.outerLine; + const outerLine = part.outerLine; if (outerLine.paths.length > 0) { - var inset = (part.innerLines.length > 0) ? part.innerLines[part.innerLines.length - 1] : outerLine; + const inset = (part.innerLines.length > 0) ? part.innerLines[part.innerLines.length - 1] : outerLine; const fillArea = inset.offset(-nozzleRadius); let lowFillArea; @@ -67,16 +65,16 @@ export default function generateInfills(slices, settings) { } if (lowFillArea && lowFillArea.paths.length > 0) { - var bounds = lowFillArea.shapeBounds(); - var lowFillTemplate = getFillTemplate(bounds, fillGridSize, true, true); + const bounds = lowFillArea.shapeBounds(); + const lowFillTemplate = getFillTemplate(bounds, fillGridSize, true, true); part.fill.join(lowFillTemplate.intersect(lowFillArea)); } if (highFillArea.paths.length > 0) { - var bounds = highFillArea.shapeBounds(); - var even = (layer % 2 === 0); - var highFillTemplate = getFillTemplate(bounds, hightemplateSize, even, !even); + const bounds = highFillArea.shapeBounds(); + const even = (layer % 2 === 0); + const highFillTemplate = getFillTemplate(bounds, hightemplateSize, even, !even); part.fill.join(highFillTemplate.intersect(highFillArea)); } From b9a2fdcc81d295326ff3ee37f21cff9347a0f510 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 22 Apr 2016 21:15:33 +0200 Subject: [PATCH 17/64] else on one line --- src/sliceActions/generateInfills.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index 6ceb64a..bced028 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -59,8 +59,7 @@ export default function generateInfills(slices, settings) { highFillArea = highFillArea.intersect(fillArea); lowFillArea = fillArea.difference(highFillArea); - } - else { + } else { highFillArea = fillArea; } From 9d17df0cf3eb12378a9776925f818f693ae27977 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 22 Apr 2016 21:15:39 +0200 Subject: [PATCH 18/64] single quotes --- src/sliceActions/generateInfills.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index bced028..abe796a 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -4,7 +4,7 @@ import Shape from 'Doodle3D/clipper-js'; const scale = 100; export default function generateInfills(slices, settings) { - console.log("generating infills"); + console.log('generating infills'); let { layerHeight, From ebbe75ef2eafe87f9dededd19c4c5726793f52da Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 23 Apr 2016 00:24:01 +0200 Subject: [PATCH 19/64] implement constants --- src/constants.js | 2 + src/gcode.js | 105 +++++++++++----------- src/sliceActions/generateInfills.js | 9 +- src/sliceActions/generateInnerLines.js | 30 +++---- src/sliceActions/generateSupport.js | 11 ++- src/sliceActions/intersectionsToShapes.js | 81 ++++++++--------- src/sliceActions/optimizePaths.js | 31 ++++--- src/sliceActions/shapesToSlices.js | 17 ++-- 8 files changed, 142 insertions(+), 144 deletions(-) create mode 100644 src/constants.js diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..630296d --- /dev/null +++ b/src/constants.js @@ -0,0 +1,2 @@ +export const CLEAN_DELTA = 0.01; +export const PRECISION = 0.01; diff --git a/src/gcode.js b/src/gcode.js index 59b40a4..f21c4e6 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -1,5 +1,14 @@ import THREE from 'three.js'; +const G_COMMAND = 'G'; +const M_COMMAND = 'M'; +const FAN_SPEED = 'S'; +const SPEED = 'F'; +const EXTRUDER = 'E'; +const POSITION_X = 'X'; +const POSITION_Y = 'Y'; +const POSITION_Z = 'Z'; + export default class { constructor () { this.gcode = ''; @@ -23,15 +32,14 @@ export default class { str = action + value; first = false; - } - else if (currentValue !== value) { + } else if (currentValue !== value) { str += ` ${action}${value}`; this.current[action] = value; } } - this.gcode += str + '\n'; + this.gcode += `${str}\n`; } setSettings (settings) { @@ -43,13 +51,8 @@ export default class { turnFanOn (fanSpeed) { this.isFanOn = true; - var gcode = { - 'M': 106 - } - - if (fanSpeed !== undefined) { - gcode['S'] = fanSpeed; - } + const gcode = { [M_COMMAND]: 106 } + if (fanSpeed !== undefined) gcode[FAN_SPEED] = fanSpeed; this._addGCode(gcode); @@ -59,24 +62,24 @@ export default class { turnFanOff () { this.isFanOn = false; - this._addGCode({ - 'M': 107 - }); + this._addGCode({ [M_COMMAND]: 107 }); return this; } moveTo (x, y, layer) { - var layerHeight = this.settings.config['layerHeight']; - var travelSpeed = this.settings.config['travelSpeed']; + const layerHeight = this.settings.config['layerHeight']; + const travelSpeed = this.settings.config['travelSpeed']; - var z = (layer + 1) * layerHeight; - var speed = travelSpeed * 60; + const z = (layer + 1) * layerHeight; + const speed = travelSpeed * 60; this._addGCode({ - 'G': 0, - 'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), - 'F': speed.toFixed(3) + [G_COMMAND]: 0, + [POSITION_X]: x.toFixed(3), + [POSITION_Y]: y.toFixed(3), + [POSITION_Z]: z.toFixed(3), + [SPEED]: speed.toFixed(3) }); this._nozzlePosition.set(x, y); @@ -85,29 +88,31 @@ export default class { } lineTo (x, y, layer, type) { - var newNozzlePosition = new THREE.Vector2(x, y); + const newNozzlePosition = new THREE.Vector2(x, y); - var layerHeight = this.settings.config['layerHeight']; - var nozzleDiameter = this.settings.config['nozzleDiameter']; - var filamentThickness = this.settings.config['filamentThickness']; - var travelSpeed = this.settings.config['travelSpeed']; + const layerHeight = this.settings.config['layerHeight']; + const nozzleDiameter = this.settings.config['nozzleDiameter']; + const filamentThickness = this.settings.config['filamentThickness']; + const travelSpeed = this.settings.config['travelSpeed']; - var profile = this.settings.config[(this.bottom ? 'bottom' : type)]; + const profile = this.settings.config[(this.bottom ? 'bottom' : type)]; - var speed = profile['speed'] * 60; - var flowRate = profile['flowRate']; - var z = (layer + 1) * layerHeight; + const speed = profile['speed'] * 60; + const flowRate = profile['flowRate']; + const z = (layer + 1) * layerHeight; - var lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); + const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); - var filamentSurfaceArea = Math.pow((filamentThickness / 2), 2) * Math.PI; + const filamentSurfaceArea = Math.pow((filamentThickness / 2), 2) * Math.PI; this.extruder += lineLength * nozzleDiameter * layerHeight / filamentSurfaceArea * flowRate; this._addGCode({ - 'G': 1, - 'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), - 'F': speed.toFixed(3), - 'E': this.extruder.toFixed(3) + [G_COMMAND]: 1, + [POSITION_X]: x.toFixed(3), + [POSITION_Y]: y.toFixed(3), + [POSITION_Z]: z.toFixed(3), + [SPEED]: speed.toFixed(3), + [EXTRUDER]: this.extruder.toFixed(3) }); this._nozzlePosition.copy(newNozzlePosition); @@ -116,20 +121,20 @@ export default class { } unRetract () { - var retractionEnabled = this.settings.config['retractionEnabled']; - var retractionMinDistance = this.settings.config['retractionMinDistance']; - var retractionSpeed = this.settings.config['retractionSpeed']; + const retractionEnabled = this.settings.config['retractionEnabled']; + const retractionMinDistance = this.settings.config['retractionMinDistance']; + const retractionSpeed = this.settings.config['retractionSpeed']; if (this.isRetracted && retractionEnabled) { this.isRetracted = false; - var speed = retractionSpeed * 60; + const speed = retractionSpeed * 60; if (this.extruder > retractionMinDistance) { this._addGCode({ - 'G': 0, - 'E': this.extruder.toFixed(3), - 'F': speed.toFixed(3) + [G_COMMAND]: 0, + [EXTRUDER]: this.extruder.toFixed(3), + [SPEED]: speed.toFixed(3) }); } } @@ -138,21 +143,21 @@ export default class { } retract () { - var retractionAmount = this.settings.config['retractionAmount']; - var retractionEnabled = this.settings.config['retractionEnabled']; - var retractionMinDistance = this.settings.config['retractionMinDistance']; - var retractionSpeed = this.settings.config['retractionSpeed']; + const retractionAmount = this.settings.config['retractionAmount']; + const retractionEnabled = this.settings.config['retractionEnabled']; + const retractionMinDistance = this.settings.config['retractionMinDistance']; + const retractionSpeed = this.settings.config['retractionSpeed']; if (!this.isRetracted && retractionEnabled) { this.isRetracted = true; - var speed = retractionSpeed * 60; + const speed = retractionSpeed * 60; if (this.extruder > retractionMinDistance && retractionEnabled) { this._addGCode({ - 'G': 0, - 'E': (this.extruder - retractionAmount).toFixed(3), - 'F': speed.toFixed(3) + [G_COMMAND]: 0, + [EXTRUDER]: (this.extruder - retractionAmount).toFixed(3), + [SPEED]: speed.toFixed(3) }); } } diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index abe796a..bc4ec1b 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -1,8 +1,7 @@ +import { PRECISION } from '../constants.js' import getFillTemplate from './getFillTemplate.js'; import Shape from 'Doodle3D/clipper-js'; -const scale = 100; - export default function generateInfills(slices, settings) { console.log('generating infills'); @@ -15,9 +14,9 @@ export default function generateInfills(slices, settings) { infillOverlap } = settings.config; - fillGridSize *= scale; - nozzleDiameter *= scale; - infillOverlap *= scale; + fillGridSize /= PRECISION; + nozzleDiameter /= PRECISION; + infillOverlap /= PRECISION; const bottomSkinCount = Math.ceil(bottomThickness/layerHeight); const topSkinCount = Math.ceil(topThickness/layerHeight); diff --git a/src/sliceActions/generateInnerLines.js b/src/sliceActions/generateInnerLines.js index 4d08e91..4979a65 100644 --- a/src/sliceActions/generateInnerLines.js +++ b/src/sliceActions/generateInnerLines.js @@ -1,4 +1,5 @@ -const scale = 100; +import { PRECISION } from '../constants.js' + const offsetOptions = { jointType: 'jtSquare', endType: 'etClosedPolygon', @@ -7,33 +8,32 @@ const offsetOptions = { }; export default function generateInnerLines(slices, settings) { - console.log("generating outer lines and inner lines"); + console.log('generating outer lines and inner lines'); // need to scale up everything because of clipper rounding errors let { layerHeight, nozzleDiameter, shellThickness } = settings.config; - nozzleDiameter *= scale; - shellThickness *= scale; - var nozzleRadius = nozzleDiameter / 2; - var shells = Math.round(shellThickness / nozzleDiameter); + nozzleDiameter /= PRECISION; + shellThickness /= PRECISION; + const nozzleRadius = nozzleDiameter / 2; + const shells = Math.round(shellThickness / nozzleDiameter); - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; + for (let layer = 0; layer < slices.length; layer ++) { + const slice = slices[layer]; - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; + for (let i = 0; i < slice.parts.length; i ++) { + const part = slice.parts[i]; if (!part.shape.closed) continue; - // var outerLine = part.shape.clone().scaleUp(scale).offset(-nozzleRadius); - const outerLine = part.shape.scaleUp(scale).offset(-nozzleRadius, offsetOptions); + const outerLine = part.shape.scaleDown(PRECISION).offset(-nozzleRadius, offsetOptions); if (outerLine.paths.length > 0) { part.outerLine.join(outerLine); - for (var shell = 1; shell < shells; shell += 1) { - var offset = shell * nozzleDiameter; + for (let shell = 1; shell < shells; shell += 1) { + const offset = shell * nozzleDiameter; - var innerLine = outerLine.offset(-offset, offsetOptions); + const innerLine = outerLine.offset(-offset, offsetOptions); if (innerLine.paths.length > 0) { part.innerLines.push(innerLine); diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index 1c44374..8140e19 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -1,7 +1,6 @@ import getFillTemplate from './getFillTemplate.js'; import Shape from 'Doodle3D/clipper-js'; - -const scale = 100; +import { PRECISION } from '../constants.js'; export default function generateSupport(slices, settings) { console.log("generating support"); @@ -16,10 +15,10 @@ export default function generateSupport(slices, settings) { nozzleDiameter } = settings.config; - supportGridSize *= scale; - supportMargin *= scale; - plateSize *= scale; - nozzleDiameter *= scale; + supportGridSize /= PRECISION; + supportMargin /= PRECISION; + plateSize /= PRECISION; + nozzleDiameter /= PRECISION; var supportDistanceLayers = Math.max(Math.ceil(supportDistanceY / layerHeight), 1); var supportAreas = new Shape([], true); diff --git a/src/sliceActions/intersectionsToShapes.js b/src/sliceActions/intersectionsToShapes.js index a4742f3..0143cda 100644 --- a/src/sliceActions/intersectionsToShapes.js +++ b/src/sliceActions/intersectionsToShapes.js @@ -4,43 +4,39 @@ import Shape from 'Doodle3D/clipper-js'; export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) { console.log("generating slices"); - var layers = []; + const layers = []; - for (var layer = 1; layer < layerIntersectionIndexes.length; layer ++) { - var intersectionIndexes = layerIntersectionIndexes[layer]; - var intersectionPoints = layerIntersectionPoints[layer]; + for (let layer = 1; layer < layerIntersectionIndexes.length; layer ++) { + const intersectionIndexes = layerIntersectionIndexes[layer]; + const intersectionPoints = layerIntersectionPoints[layer]; - if (intersectionIndexes.length === 0) { - continue; - } + if (intersectionIndexes.length === 0) continue; - var closedShapes = []; - var openShapes = []; - for (var i = 0; i < intersectionIndexes.length; i ++) { - var index = intersectionIndexes[i]; + const closedShapes = []; + const openShapes = []; + for (let i = 0; i < intersectionIndexes.length; i ++) { + let index = intersectionIndexes[i]; - if (intersectionPoints[index] === undefined) { - continue; - } + if (intersectionPoints[index] === undefined) continue; - var firstPoints = [index]; - var isFirstPoint = true; - var closed = false; + const shape = []; - var shape = []; + const firstPoints = [index]; + let isFirstPoint = true; + let closed = false; while (index !== -1) { - var intersection = intersectionPoints[index]; + const intersection = intersectionPoints[index]; // uppercase X and Y because clipper vector shape.push(intersection); delete intersectionPoints[index]; - var connects = lines[index].connects; - var faceNormals = lines[index].normals; + const connects = lines[index].connects; + const faceNormals = lines[index].normals; - for (var j = 0; j < connects.length; j ++) { - var index = connects[j]; + for (let i = 0; i < connects.length; i ++) { + index = connects[i]; if (firstPoints.indexOf(index) !== -1 && shape.length > 2) { closed = true; @@ -50,10 +46,10 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt // Check if index has an intersection or is already used if (intersectionPoints[index] !== undefined) { - var faceNormal = faceNormals[Math.floor(j / 2)]; + const faceNormal = faceNormals[Math.floor(i / 2)]; - var a = new THREE.Vector2(intersection.x, intersection.y); - var b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y); + const a = new THREE.Vector2(intersection.x, intersection.y); + const b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y); // can't calculate normal between points if distance is smaller as 0.0001 if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) { @@ -63,26 +59,23 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt delete intersectionPoints[index]; - connects = connects.concat(lines[index].connects); - faceNormals = faceNormals.concat(lines[index].normals); + connects.push(...lines[index].connects); + faceNormals.push(...lines[index].normals); index = -1; - } - else { + } else { // make sure the path goes the right direction // THREE.Vector2.normal is not yet implimented - // var normal = a.sub(b).normal().normalize(); - var normal = a.sub(b); + // const normal = a.sub(b).normal().normalize(); + const normal = a.sub(b); normal.set(-normal.y, normal.x).normalize(); if (normal.dot(faceNormal) > 0) { break; - } - else { + } else { index = -1; } } - } - else { + } else { index = -1; } } @@ -90,25 +83,24 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt } if (!closed) { - var index = firstPoints[0]; + index = firstPoints[0]; while (index !== -1) { if (firstPoints.indexOf(index) === -1) { - var intersection = intersectionPoints[index]; + const intersection = intersectionPoints[index]; shape.unshift(intersection); delete intersectionPoints[index]; } - var connects = lines[index].connects; + const connects = lines[index].connects; - for (var i = 0; i < connects.length; i ++) { - var index = connects[i]; + for (let i = 0; i < connects.length; i ++) { + index = connects[i]; if (intersectionPoints[index] !== undefined) { break; - } - else { + } else { index = -1; } } @@ -117,8 +109,7 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt if (closed) { closedShapes.push(shape); - } - else { + } else { openShapes.push(shape); } } diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js index 7181f1f..2c873e5 100644 --- a/src/sliceActions/optimizePaths.js +++ b/src/sliceActions/optimizePaths.js @@ -1,4 +1,6 @@ import THREE from 'three.js'; +import { PRECISION } from '../constants.js'; + const offsetOptions = { jointType: 'jtSquare', endType: 'etClosedPolygon', @@ -9,15 +11,12 @@ const offsetOptions = { export default function optimizePaths(slices, settings) { console.log("opimize paths"); - // need to scale up everything because of clipper rounding errors - var scale = 100; + const brimOffset = settings.config["brimOffset"] / PRECISION; - var brimOffset = settings.config["brimOffset"] * scale; + const start = new THREE.Vector2(0, 0); - var start = new THREE.Vector2(0, 0); - - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; + for (let layer = 0; layer < slices.length; layer ++) { + const slice = slices[layer]; if (layer === 0) { slice.brim = slice.getOutline().offset(brimOffset, offsetOptions); @@ -25,24 +24,24 @@ export default function optimizePaths(slices, settings) { // start = slice.optimizePaths(start); - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; + for (let i = 0; i < slice.parts.length; i ++) { + const part = slice.parts[i]; if (part.shape.closed) { - part.outerLine.scaleDown(scale); - for (var j = 0; j < part.innerLines.length; j ++) { - var innerLine = part.innerLines[j]; - innerLine.scaleDown(scale); + part.outerLine.scaleDown(1 / PRECISION); + for (let i = 0; i < part.innerLines.length; i ++) { + const innerLine = part.innerLines[i]; + innerLine.scaleDown(1 / PRECISION); } - part.fill.scaleDown(scale); + part.fill.scaleDown(1 / PRECISION); } } if (slice.support !== undefined) { - slice.support.scaleDown(scale); + slice.support.scaleDown(1 / PRECISION); } if (slice.brim !== undefined) { - slice.brim.scaleDown(scale); + slice.brim.scaleDown(1 / PRECISION); } } } diff --git a/src/sliceActions/shapesToSlices.js b/src/sliceActions/shapesToSlices.js index 36371f9..7be4a88 100644 --- a/src/sliceActions/shapesToSlices.js +++ b/src/sliceActions/shapesToSlices.js @@ -1,31 +1,34 @@ import Shape from 'Doodle3D/clipper-js'; import Slice from '../slice.js'; +import { CLEAN_DELTA } from '../constants.js'; + export default function shapesToSlices(shapes, settings) { const sliceLayers = []; - for (var layer = 0; layer < shapes.length; layer ++) { - var { closedShapes, openShapes } = shapes[layer]; + for (let layer = 0; layer < shapes.length; layer ++) { + let { closedShapes, openShapes } = shapes[layer]; closedShapes = new Shape(closedShapes, true, true) - .clean(0.01) + .clean(CLEAN_DELTA) .fixOrientation() .removeOverlap() .seperateShapes(); openShapes = new Shape(openShapes, false, true) - .clean(0.01); + .clean(CLEAN_DELTA); - var slice = new Slice(); + const slice = new Slice(); - for (var i = 0; i < closedShapes.length; i ++) { - var closedShape = closedShapes[i]; + for (let i = 0; i < closedShapes.length; i ++) { + const closedShape = closedShapes[i]; slice.add(closedShape); // if (openShapes.path.length > 0) { // openShapes = openShapes.difference(closedShape); // } } + if (openShapes.paths.length > 0) { slice.add(openShapes); } From 9e4109a3f8b66e74045f6165680d4e30e926053d Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 23 Apr 2016 09:56:15 +0200 Subject: [PATCH 20/64] cleanup slices to geode --- src/sliceActions/slicesToGCode.js | 75 +++++++++++++++---------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js index 5abe6f5..76e0fc4 100644 --- a/src/sliceActions/slicesToGCode.js +++ b/src/sliceActions/slicesToGCode.js @@ -1,39 +1,37 @@ import GCode from '../gcode.js'; -export default function slicesToGCode(slices, settings) { - var gcode = new GCode().setSettings(settings); +function pathToGCode(gcode, shape, retract, unRetract, layer, type) { + for (let i = 0; i < shape.paths.length; i ++) { + const line = shape.paths[i]; - function pathToGCode (shape, retract, unRetract, type) { - for (var i = 0; i < shape.paths.length; i ++) { - var line = shape.paths[i]; + const length = shape.closed ? (line.length + 1) : line.length; + for (let i = 0; i < length; i ++) { + const point = line[i % line.length]; - var length = shape.closed ? (line.length + 1) : line.length; + if (i === 0) { + // TODO + // moveTo should impliment combing + gcode.moveTo(point.X, point.Y, layer); - for (var j = 0; j < length; j ++) { - var point = line[j % line.length]; - - if (j === 0) { - // TODO - // moveTo should impliment combing - gcode.moveTo(point.X, point.Y, layer); - - if (unRetract) { - gcode.unRetract(); - } - } - else { - gcode.lineTo(point.X, point.Y, layer, type); + if (unRetract) { + gcode.unRetract(); } + } else { + gcode.lineTo(point.X, point.Y, layer, type); } } - - if (retract) { - gcode.retract(); - } } - for (var layer = 0; layer < slices.length; layer ++) { - var slice = slices[layer]; + if (retract) { + gcode.retract(); + } +} + +export default function slicesToGCode(slices, settings) { + const gcode = new GCode().setSettings(settings); + + for (let layer = 0; layer < slices.length; layer ++) { + const slice = slices[layer]; if (layer === 1) { gcode.turnFanOn(); @@ -41,30 +39,29 @@ export default function slicesToGCode(slices, settings) { } if (slice.brim !== undefined) { - pathToGCode(slice.brim, true, true, "brim"); + pathToGCode(gcode, slice.brim, true, true, layer, 'brim'); } - for (var i = 0; i < slice.parts.length; i ++) { - var part = slice.parts[i]; + for (let i = 0; i < slice.parts.length; i ++) { + const part = slice.parts[i]; if (part.shape.closed) { - pathToGCode(part.outerLine, false, true, "outerLine"); + pathToGCode(gcode, part.outerLine, false, true, layer, 'outerLine'); - for (var j = 0; j < part.innerLines.length; j ++) { - var innerLine = part.innerLines[j]; - pathToGCode(innerLine, false, false, "innerLine"); + for (let i = 0; i < part.innerLines.length; i ++) { + const innerLine = part.innerLines[i]; + pathToGCode(gcode, innerLine, false, false, layer, 'innerLine'); } - pathToGCode(part.fill, true, false, "fill"); - } - else { - var retract = !(slice.parts.length === 1 && slice.support === undefined); - pathToGCode(part.shape, retract, retract, "outerLine"); + pathToGCode(gcode, part.fill, true, false, layer, 'fill'); + } else { + const retract = !(slice.parts.length === 1 && slice.support === undefined); + pathToGCode(gcode, part.shape, retract, retract, layer, 'outerLine'); } } if (slice.support !== undefined) { - pathToGCode(slice.support, true, true, "support"); + pathToGCode(gcode, slice.support, true, true, layer, 'support'); } } From b21db81f127c336f5caefa64b0deade46d4d16fd Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 23 Apr 2016 09:56:25 +0200 Subject: [PATCH 21/64] cleanup slice --- src/slice.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/slice.js b/src/slice.js index 73902bb..0e46f8b 100644 --- a/src/slice.js +++ b/src/slice.js @@ -1,11 +1,11 @@ import Shape from 'Doodle3D/clipper-js'; export default class { - constructor () { + constructor() { this.parts = []; } - optimizePaths (start) { + optimizePaths(start) { if (this.brim !== undefined && this.brim.length > 0) { this.brim = this.brim.optimizePath(start); start = this.brim.lastPoint(); @@ -14,7 +14,6 @@ export default class { var parts = []; while (this.parts.length > 0) { - var closestDistance = Infinity; var closestPart; @@ -80,10 +79,10 @@ export default class { } getOutline () { - var outLines = new Shape([], true); + const outLines = new Shape([], true); - for (var i = 0; i < this.parts.length; i ++) { - var part = this.parts[i]; + for (let i = 0; i < this.parts.length; i ++) { + const part = this.parts[i]; if (part.shape.closed) { outLines.join(this.parts[i].outerLine); From 7042ebf5888d9da22df905d9bf74bc0384273373 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 23 Apr 2016 09:56:36 +0200 Subject: [PATCH 22/64] cleanup slicer --- src/slicer.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/slicer.js b/src/slicer.js index 0892733..09b4fcc 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -60,10 +60,10 @@ export default class extends EventDispatcher { } slice (settings) { - var supportEnabled = settings.config['supportEnabled']; + const supportEnabled = settings.config['supportEnabled']; // get unique lines from geometry; - var lines = createLines(this.geometry, settings); + const lines = createLines(this.geometry, settings); this.progress.createdLines = true; this._updateProgress(settings); @@ -106,8 +106,6 @@ export default class extends EventDispatcher { this.dispatchEvent({ type: 'finish', gcode }); - console.log(slices); - return gcode; } From 626867e5543de4fed95c2b07628c545f82c81ad8 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 23 Apr 2016 09:56:47 +0200 Subject: [PATCH 23/64] cleanup calculate layers intersections --- src/sliceActions/calculateLayersIntersections.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/sliceActions/calculateLayersIntersections.js b/src/sliceActions/calculateLayersIntersections.js index c3fb5af..c21578d 100644 --- a/src/sliceActions/calculateLayersIntersections.js +++ b/src/sliceActions/calculateLayersIntersections.js @@ -43,8 +43,5 @@ export default function calculateLayersIntersections(lines, settings) { } } - return { - layerIntersectionIndexes, - layerIntersectionPoints - }; + return { layerIntersectionIndexes, layerIntersectionPoints }; } From 4237195b5e008255278d4dc05e5df052cd6d3878 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:45:03 +0200 Subject: [PATCH 24/64] update create lines --- src/sliceActions/createLines.js | 42 ++++++++++++++++----------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/sliceActions/createLines.js b/src/sliceActions/createLines.js index b57b429..6b43817 100644 --- a/src/sliceActions/createLines.js +++ b/src/sliceActions/createLines.js @@ -1,18 +1,14 @@ import THREE from 'three.js'; function addLine(geometry, lineLookup, lines, a, b) { - let index = lineLookup[`${b}_${a}`]; + const index = lines.length; + lineLookup[`${a}_${b}`] = index; - if (index === undefined) { - index = lines.length; - lineLookup[`${a}_${b}`] = index; - - lines.push({ - line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]), - connects: [], - normals: [] - }); - } + lines.push({ + line: new THREE.Line3(geometry.vertices[a], geometry.vertices[b]), + connects: [], + normals: [] + }); return index; } @@ -28,20 +24,24 @@ export default function createLines(geometry, settings) { if (face.normal.y !== 1 && face.normal.y !== -1) { const normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize(); - // check for only adding unique lines + const lookupA = lineLookup[`${face.b}_${face.a}`]; + const lookupB = lineLookup[`${face.c}_${face.b}`]; + const lookupC = lineLookup[`${face.a}_${face.c}`]; + + // only add unique lines // returns index of said line - const a = addLine(geometry, lineLookup, lines, face.a, face.b); - const b = addLine(geometry, lineLookup, lines, face.b, face.c); - const c = addLine(geometry, lineLookup, lines, face.c, face.a); + const indexA = lookupA !== undefined ? lookupA : addLine(geometry, lineLookup, lines, face.a, face.b); + const indexB = lookupB !== undefined ? lookupB : addLine(geometry, lineLookup, lines, face.b, face.c); + const indexC = lookupC !== undefined ? lookupC : addLine(geometry, lineLookup, lines, face.c, face.a); // set connecting lines (based on face) - lines[a].connects.push(b, c); - lines[b].connects.push(c, a); - lines[c].connects.push(a, b); + lines[indexA].connects.push(indexB, indexC); + lines[indexB].connects.push(indexC, indexA); + lines[indexC].connects.push(indexA, indexB); - lines[a].normals.push(normal); - lines[b].normals.push(normal); - lines[c].normals.push(normal); + lines[indexA].normals.push(normal); + lines[indexB].normals.push(normal); + lines[indexC].normals.push(normal); } } From 51412b55998937e5bfe9d2edc707d60f4cae83f3 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:46:56 +0200 Subject: [PATCH 25/64] remove line break --- src/sliceActions/generateInfills.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index bc4ec1b..b9e7925 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -56,7 +56,6 @@ export default function generateInfills(slices, settings) { } highFillArea = highFillArea.intersect(fillArea); - lowFillArea = fillArea.difference(highFillArea); } else { highFillArea = fillArea; From bf0f504c34ca459db2c13642b6f661763090bf93 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:47:42 +0200 Subject: [PATCH 26/64] comment optimize path code --- src/slice.js | 136 +++++++++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/src/slice.js b/src/slice.js index 0e46f8b..99e0870 100644 --- a/src/slice.js +++ b/src/slice.js @@ -6,74 +6,74 @@ export default class { } optimizePaths(start) { - if (this.brim !== undefined && this.brim.length > 0) { - this.brim = this.brim.optimizePath(start); - start = this.brim.lastPoint(); - } - - var parts = []; - - while (this.parts.length > 0) { - var closestDistance = Infinity; - var closestPart; - - for (var i = 0; i < this.parts.length; i ++) { - var part = this.parts[i]; - if (part.shape.closed) { - var bounds = part.outerLine.bounds(); - } - else { - var bounds = part.shape.bounds(); - } - - var top = bounds.top - start.y; - var bottom = start.y - bounds.bottom; - var left = bounds.left - start.x; - var right = start.x - bounds.right; - - var distance = Math.max(top, bottom, left, right); - - if (distance < closestDistance) { - closestDistance = distance; - closestPart = i; - } - } - - var part = this.parts.splice(closestPart, 1)[0]; - parts.push(part); - - if (part.shape.closed) { - if (part.outerLine.length > 0) { - part.outerLine = part.outerLine.optimizePath(start); - start = part.outerLine.lastPoint(); - } - - for (var j = 0; j < part.innerLines.length; j ++) { - var innerLine = part.innerLines[j]; - if (innerLine.length > 0) { - part.innerLines[j] = innerLine.optimizePath(start); - start = part.innerLines[j].lastPoint(); - } - } - - if (part.fill.length > 0) { - part.fill = part.fill.optimizePath(start); - start = part.fill.lastPoint(); - } - } - else { - part.shape.optimizePath(start); - start = part.shape.lastPoint(); - } - - } - - this.parts = parts; - - if (this.support !== undefined && this.support.length > 0) { - this.support = this.support.optimizePath(start); - start = this.support.lastPoint(); - } + // if (this.brim !== undefined && this.brim.length > 0) { + // this.brim = this.brim.optimizePath(start); + // start = this.brim.lastPoint(); + // } + // + // var parts = []; + // + // while (this.parts.length > 0) { + // var closestDistance = Infinity; + // var closestPart; + // + // for (var i = 0; i < this.parts.length; i ++) { + // var part = this.parts[i]; + // if (part.shape.closed) { + // var bounds = part.outerLine.bounds(); + // } + // else { + // var bounds = part.shape.bounds(); + // } + // + // var top = bounds.top - start.y; + // var bottom = start.y - bounds.bottom; + // var left = bounds.left - start.x; + // var right = start.x - bounds.right; + // + // var distance = Math.max(top, bottom, left, right); + // + // if (distance < closestDistance) { + // closestDistance = distance; + // closestPart = i; + // } + // } + // + // var part = this.parts.splice(closestPart, 1)[0]; + // parts.push(part); + // + // if (part.shape.closed) { + // if (part.outerLine.length > 0) { + // part.outerLine = part.outerLine.optimizePath(start); + // start = part.outerLine.lastPoint(); + // } + // + // for (var j = 0; j < part.innerLines.length; j ++) { + // var innerLine = part.innerLines[j]; + // if (innerLine.length > 0) { + // part.innerLines[j] = innerLine.optimizePath(start); + // start = part.innerLines[j].lastPoint(); + // } + // } + // + // if (part.fill.length > 0) { + // part.fill = part.fill.optimizePath(start); + // start = part.fill.lastPoint(); + // } + // } + // else { + // part.shape.optimizePath(start); + // start = part.shape.lastPoint(); + // } + // + // } + // + // this.parts = parts; + // + // if (this.support !== undefined && this.support.length > 0) { + // this.support = this.support.optimizePath(start); + // start = this.support.lastPoint(); + // } return start; } From 9d828e3fda5292083584c11535552f906ba4b09e Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:48:41 +0200 Subject: [PATCH 27/64] move down path to code --- src/sliceActions/slicesToGCode.js | 54 +++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js index 76e0fc4..cf647ee 100644 --- a/src/sliceActions/slicesToGCode.js +++ b/src/sliceActions/slicesToGCode.js @@ -1,32 +1,5 @@ import GCode from '../gcode.js'; -function pathToGCode(gcode, shape, retract, unRetract, layer, type) { - for (let i = 0; i < shape.paths.length; i ++) { - const line = shape.paths[i]; - - const length = shape.closed ? (line.length + 1) : line.length; - for (let i = 0; i < length; i ++) { - const point = line[i % line.length]; - - if (i === 0) { - // TODO - // moveTo should impliment combing - gcode.moveTo(point.X, point.Y, layer); - - if (unRetract) { - gcode.unRetract(); - } - } else { - gcode.lineTo(point.X, point.Y, layer, type); - } - } - } - - if (retract) { - gcode.retract(); - } -} - export default function slicesToGCode(slices, settings) { const gcode = new GCode().setSettings(settings); @@ -67,3 +40,30 @@ export default function slicesToGCode(slices, settings) { return gcode.getGCode(); } + +function pathToGCode(gcode, shape, retract, unRetract, layer, type) { + for (let i = 0; i < shape.paths.length; i ++) { + const line = shape.paths[i]; + + const length = shape.closed ? (line.length + 1) : line.length; + for (let i = 0; i < length; i ++) { + const point = line[i % line.length]; + + if (i === 0) { + // TODO + // moveTo should impliment combing + gcode.moveTo(point.X, point.Y, layer); + + if (unRetract) { + gcode.unRetract(); + } + } else { + gcode.lineTo(point.X, point.Y, layer, type); + } + } + } + + if (retract) { + gcode.retract(); + } +} From c8150c3bf78c32aaa8f4ba7c022647031d403bf6 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:49:07 +0200 Subject: [PATCH 28/64] use default dimensionsZ name --- src/sliceActions/calculateLayersIntersections.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sliceActions/calculateLayersIntersections.js b/src/sliceActions/calculateLayersIntersections.js index c21578d..1015aae 100644 --- a/src/sliceActions/calculateLayersIntersections.js +++ b/src/sliceActions/calculateLayersIntersections.js @@ -3,9 +3,9 @@ import THREE from 'three.js'; export default function calculateLayersIntersections(lines, settings) { console.log('calculating layer intersections'); - const { layerHeight, dimensionsZ: height } = settings.config; + const { layerHeight, dimensionsZ } = settings.config; - const numLayers = Math.floor(height / layerHeight); + const numLayers = Math.floor(dimensionsZ / layerHeight); const layerIntersectionIndexes = []; const layerIntersectionPoints = []; From e16a5dda9006f9a3f50280f87203eec428e44d05 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:52:31 +0200 Subject: [PATCH 29/64] move brim code to separate function --- src/sliceActions/addBrim.js | 18 ++++++++++++++++++ src/sliceActions/optimizePaths.js | 12 ------------ src/slicer.js | 3 +++ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 src/sliceActions/addBrim.js diff --git a/src/sliceActions/addBrim.js b/src/sliceActions/addBrim.js new file mode 100644 index 0000000..a39e34e --- /dev/null +++ b/src/sliceActions/addBrim.js @@ -0,0 +1,18 @@ +import THREE from 'three.js'; +import { PRECISION } from '../constants.js'; + +const offsetOptions = { + jointType: 'jtSquare', + endType: 'etClosedPolygon', + miterLimit: 2.0, + roundPrecision: 0.25 +}; + +export default function addBrim(slices, settings) { + console.log('add brim'); + + const brimOffset = settings.config['brimOffset'] / PRECISION; + + const fistLayer = slices[0]; + fistLayer.brim = fistLayer.getOutline().offset(brimOffset, offsetOptions); +} diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js index 2c873e5..40e9878 100644 --- a/src/sliceActions/optimizePaths.js +++ b/src/sliceActions/optimizePaths.js @@ -1,27 +1,15 @@ import THREE from 'three.js'; import { PRECISION } from '../constants.js'; -const offsetOptions = { - jointType: 'jtSquare', - endType: 'etClosedPolygon', - miterLimit: 2.0, - roundPrecision: 0.25 -}; export default function optimizePaths(slices, settings) { console.log("opimize paths"); - const brimOffset = settings.config["brimOffset"] / PRECISION; - const start = new THREE.Vector2(0, 0); for (let layer = 0; layer < slices.length; layer ++) { const slice = slices[layer]; - if (layer === 0) { - slice.brim = slice.getOutline().offset(brimOffset, offsetOptions); - } - // start = slice.optimizePaths(start); for (let i = 0; i < slice.parts.length; i ++) { diff --git a/src/slicer.js b/src/slicer.js index 09b4fcc..6b3f303 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -6,6 +6,7 @@ import generateInfills from './sliceActions/generateInfills.js'; import generateInnerLines from './sliceActions/generateInnerLines.js'; import generateSupport from './sliceActions/generateSupport.js'; import intersectionsToShapes from './sliceActions/intersectionsToShapes.js'; +import addBrim from './sliceActions/addBrim.js'; import optimizePaths from './sliceActions/optimizePaths.js'; import shapesToSlices from './sliceActions/shapesToSlices.js'; import slicesToGCode from './sliceActions/slicesToGCode.js'; @@ -96,6 +97,8 @@ export default class extends EventDispatcher { this._updateProgress(settings); } + addBrim(slices, settings); + optimizePaths(slices, settings); this.progress.optimizedPaths = true; this._updateProgress(settings); From 457eaa1bef55e01b2b1ad7e0fd3233b7b831e7c9 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:54:25 +0200 Subject: [PATCH 30/64] move scale up and scale down to separate function --- src/sliceActions/applyPrecision.js | 22 ++++++++++++++++++ src/sliceActions/generateInnerLines.js | 2 +- src/sliceActions/optimizePaths.js | 26 +++------------------ src/sliceActions/removePrecision.js | 32 ++++++++++++++++++++++++++ src/slicer.js | 6 +++++ 5 files changed, 64 insertions(+), 24 deletions(-) create mode 100644 src/sliceActions/applyPrecision.js create mode 100644 src/sliceActions/removePrecision.js diff --git a/src/sliceActions/applyPrecision.js b/src/sliceActions/applyPrecision.js new file mode 100644 index 0000000..506431a --- /dev/null +++ b/src/sliceActions/applyPrecision.js @@ -0,0 +1,22 @@ +import { PRECISION } from '../constants.js' + +export default function applyPrecision(shapes) { + for (let i = 0; i < shapes.length; i ++) { + const { closedShapes, openShapes } = shapes[i]; + + scaleUpShape(closedShapes); + scaleUpShape(openShapes); + } +} + +function scaleUpShape(shape) { + for (let i = 0; i < shape.length; i ++) { + const path = shape[i]; + + for (let i = 0; i < path.length; i ++) { + const point = path[i]; + + point.copy(point.divideScalar(PRECISION)); + } + } +} diff --git a/src/sliceActions/generateInnerLines.js b/src/sliceActions/generateInnerLines.js index 4979a65..bf75a7d 100644 --- a/src/sliceActions/generateInnerLines.js +++ b/src/sliceActions/generateInnerLines.js @@ -25,7 +25,7 @@ export default function generateInnerLines(slices, settings) { if (!part.shape.closed) continue; - const outerLine = part.shape.scaleDown(PRECISION).offset(-nozzleRadius, offsetOptions); + const outerLine = part.shape.offset(-nozzleRadius, offsetOptions); if (outerLine.paths.length > 0) { part.outerLine.join(outerLine); diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js index 40e9878..e7bc71b 100644 --- a/src/sliceActions/optimizePaths.js +++ b/src/sliceActions/optimizePaths.js @@ -1,35 +1,15 @@ import THREE from 'three.js'; -import { PRECISION } from '../constants.js'; - export default function optimizePaths(slices, settings) { - console.log("opimize paths"); + console.log('opimize paths'); const start = new THREE.Vector2(0, 0); for (let layer = 0; layer < slices.length; layer ++) { const slice = slices[layer]; - // start = slice.optimizePaths(start); + const end = slice.optimizePaths(start); - for (let i = 0; i < slice.parts.length; i ++) { - const part = slice.parts[i]; - - if (part.shape.closed) { - part.outerLine.scaleDown(1 / PRECISION); - for (let i = 0; i < part.innerLines.length; i ++) { - const innerLine = part.innerLines[i]; - innerLine.scaleDown(1 / PRECISION); - } - part.fill.scaleDown(1 / PRECISION); - } - } - - if (slice.support !== undefined) { - slice.support.scaleDown(1 / PRECISION); - } - if (slice.brim !== undefined) { - slice.brim.scaleDown(1 / PRECISION); - } + start.copy(end); } } diff --git a/src/sliceActions/removePrecision.js b/src/sliceActions/removePrecision.js new file mode 100644 index 0000000..0e6a8d0 --- /dev/null +++ b/src/sliceActions/removePrecision.js @@ -0,0 +1,32 @@ +import THREE from 'three.js'; +import { PRECISION } from '../constants.js'; + +export default function removePrecision(slices) { + console.log('opimize paths'); + + const start = new THREE.Vector2(0, 0); + + for (let layer = 0; layer < slices.length; layer ++) { + const slice = slices[layer]; + + for (let i = 0; i < slice.parts.length; i ++) { + const part = slice.parts[i]; + + if (part.shape.closed) { + part.outerLine.scaleDown(1 / PRECISION); + for (let i = 0; i < part.innerLines.length; i ++) { + const innerLine = part.innerLines[i]; + innerLine.scaleDown(1 / PRECISION); + } + part.fill.scaleDown(1 / PRECISION); + } + } + + if (slice.support !== undefined) { + slice.support.scaleDown(1 / PRECISION); + } + if (slice.brim !== undefined) { + slice.brim.scaleDown(1 / PRECISION); + } + } +} diff --git a/src/slicer.js b/src/slicer.js index 6b3f303..74225c6 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -10,6 +10,8 @@ import addBrim from './sliceActions/addBrim.js'; import optimizePaths from './sliceActions/optimizePaths.js'; import shapesToSlices from './sliceActions/shapesToSlices.js'; import slicesToGCode from './sliceActions/slicesToGCode.js'; +import applyPrecision from './sliceActions/applyPrecision.js'; +import removePrecision from './sliceActions/removePrecision.js'; export default class extends EventDispatcher { constructor () { @@ -79,6 +81,8 @@ export default class extends EventDispatcher { this.progress.sliced = true; this._updateProgress(settings); + applyPrecision(shapes); + const slices = shapesToSlices(shapes, settings); this.progress.generatedSlices = true; this._updateProgress(settings); @@ -103,6 +107,8 @@ export default class extends EventDispatcher { this.progress.optimizedPaths = true; this._updateProgress(settings); + removePrecision(slices); + var gcode = slicesToGCode(slices, settings); this.progress.generatedGCode = true; this._updateProgress(settings); From 26ed8df38d3f1d6ee8b869eb04d3a96c3c8ce6b8 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:56:08 +0200 Subject: [PATCH 31/64] fix action log --- src/sliceActions/removePrecision.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sliceActions/removePrecision.js b/src/sliceActions/removePrecision.js index 0e6a8d0..d1841e9 100644 --- a/src/sliceActions/removePrecision.js +++ b/src/sliceActions/removePrecision.js @@ -2,7 +2,7 @@ import THREE from 'three.js'; import { PRECISION } from '../constants.js'; export default function removePrecision(slices) { - console.log('opimize paths'); + console.log('remove precision'); const start = new THREE.Vector2(0, 0); From 065b43572d8dea65e0867521e982b8fb68d1b54b Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:56:14 +0200 Subject: [PATCH 32/64] typo --- src/sliceActions/optimizePaths.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js index e7bc71b..82b60e2 100644 --- a/src/sliceActions/optimizePaths.js +++ b/src/sliceActions/optimizePaths.js @@ -1,7 +1,7 @@ import THREE from 'three.js'; export default function optimizePaths(slices, settings) { - console.log('opimize paths'); + console.log('optimize paths'); const start = new THREE.Vector2(0, 0); From 1388bb2c44a28b4b9bbda075c2bc62c544bd69a7 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:58:39 +0200 Subject: [PATCH 33/64] move support enabled check --- src/sliceActions/generateSupport.js | 3 +++ src/slicer.js | 10 +++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index 8140e19..2970e80 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -5,6 +5,8 @@ import { PRECISION } from '../constants.js'; export default function generateSupport(slices, settings) { console.log("generating support"); + if (!settings.config.supportEnabled) return; + // need to scale up everything because of clipper rounding errors let { layerHeight, @@ -15,6 +17,7 @@ export default function generateSupport(slices, settings) { nozzleDiameter } = settings.config; + supportGridSize /= PRECISION; supportMargin /= PRECISION; plateSize /= PRECISION; diff --git a/src/slicer.js b/src/slicer.js index 74225c6..d80cc8a 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -63,8 +63,6 @@ export default class extends EventDispatcher { } slice (settings) { - const supportEnabled = settings.config['supportEnabled']; - // get unique lines from geometry; const lines = createLines(this.geometry, settings); this.progress.createdLines = true; @@ -95,11 +93,9 @@ export default class extends EventDispatcher { this.progress.generatedInfills = true; this._updateProgress(settings); - if (supportEnabled) { - generateSupport(slices, settings); - this.progress.generatedSupport = true; - this._updateProgress(settings); - } + generateSupport(slices, settings); + this.progress.generatedSupport = true; + this._updateProgress(settings); addBrim(slices, settings); From 6c18b44827a0bb72f413a8a475b4dd8a22f2ad15 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 19:59:49 +0200 Subject: [PATCH 34/64] remove update progress code want to implement this in a different way --- src/slicer.js | 55 +-------------------------------------------------- 1 file changed, 1 insertion(+), 54 deletions(-) diff --git a/src/slicer.js b/src/slicer.js index d80cc8a..8ab323c 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -16,18 +16,6 @@ import removePrecision from './sliceActions/removePrecision.js'; export default class extends EventDispatcher { constructor () { super(); - - this.progress = { - createdLines: false, - calculatedLayerIntersections: false, - sliced: false, - generatedSlices: false, - generatedInnerLines: false, - generatedInfills: false, - generatedSupport: false, - optimizedPaths: false, - generatedGCode: false - }; } setMesh (mesh) { @@ -65,74 +53,33 @@ export default class extends EventDispatcher { slice (settings) { // get unique lines from geometry; const lines = createLines(this.geometry, settings); - this.progress.createdLines = true; - this._updateProgress(settings); const { layerIntersectionIndexes, layerIntersectionPoints } = calculateLayersIntersections(lines, settings); - this.progress.calculatedLayerIntersections = true; - this._updateProgress(settings); const shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings); - this.progress.sliced = true; - this._updateProgress(settings); applyPrecision(shapes); const slices = shapesToSlices(shapes, settings); - this.progress.generatedSlices = true; - this._updateProgress(settings); generateInnerLines(slices, settings); - this.progress.generatedInnerLines = true; - this._updateProgress(settings); generateInfills(slices, settings); - this.progress.generatedInfills = true; - this._updateProgress(settings); generateSupport(slices, settings); - this.progress.generatedSupport = true; - this._updateProgress(settings); addBrim(slices, settings); optimizePaths(slices, settings); - this.progress.optimizedPaths = true; - this._updateProgress(settings); removePrecision(slices); - var gcode = slicesToGCode(slices, settings); - this.progress.generatedGCode = true; - this._updateProgress(settings); + const gcode = slicesToGCode(slices, settings); this.dispatchEvent({ type: 'finish', gcode }); - return gcode; } - - _updateProgress (settings) { - var supportEnabled = settings.config['supportEnabled']; - - var progress = {}; - - var procent = 0; - var length = 0; - for (var i in this.progress) { - if (!(!supportEnabled && i === 'generatedSupport')) { - progress[i] = this.progress[i]; - if (progress[i]) { - procent += 1; - } - length += 1; - } - } - - progress.procent = procent / length; - - this.dispatchEvent({ type: 'progress', progress }); - } } From 5f5fac31802491df1e60d4aa54084a4ca0b42e32 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 20:00:39 +0200 Subject: [PATCH 35/64] remove constructor --- src/slicer.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/slicer.js b/src/slicer.js index 8ab323c..64b26b0 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -14,9 +14,6 @@ import applyPrecision from './sliceActions/applyPrecision.js'; import removePrecision from './sliceActions/removePrecision.js'; export default class extends EventDispatcher { - constructor () { - super(); - } setMesh (mesh) { mesh.updateMatrix(); From 44e05bed28ef6e038f6c654b4c60306a3ace670d Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 20:03:15 +0200 Subject: [PATCH 36/64] remove line break --- src/sliceActions/generateSupport.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index 2970e80..ca114c8 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -17,7 +17,6 @@ export default function generateSupport(slices, settings) { nozzleDiameter } = settings.config; - supportGridSize /= PRECISION; supportMargin /= PRECISION; plateSize /= PRECISION; From 896eae7185ef46f67c92b8f05c5b2ad7f61dce8a Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 20:04:12 +0200 Subject: [PATCH 37/64] remove comment --- src/sliceActions/generateSupport.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index ca114c8..e22b989 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -7,7 +7,6 @@ export default function generateSupport(slices, settings) { if (!settings.config.supportEnabled) return; - // need to scale up everything because of clipper rounding errors let { layerHeight, supportGridSize, From 53d5d0a3758a43dbdec8d79a8e05e4ff715327e4 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 20:12:49 +0200 Subject: [PATCH 38/64] move file --- src/{sliceActions => }/getFillTemplate.js | 0 src/sliceActions/generateInfills.js | 2 +- src/sliceActions/generateSupport.js | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{sliceActions => }/getFillTemplate.js (100%) diff --git a/src/sliceActions/getFillTemplate.js b/src/getFillTemplate.js similarity index 100% rename from src/sliceActions/getFillTemplate.js rename to src/getFillTemplate.js diff --git a/src/sliceActions/generateInfills.js b/src/sliceActions/generateInfills.js index b9e7925..e6bb3fd 100644 --- a/src/sliceActions/generateInfills.js +++ b/src/sliceActions/generateInfills.js @@ -1,5 +1,5 @@ import { PRECISION } from '../constants.js' -import getFillTemplate from './getFillTemplate.js'; +import getFillTemplate from '../getFillTemplate.js'; import Shape from 'Doodle3D/clipper-js'; export default function generateInfills(slices, settings) { diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index e22b989..ce71913 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -1,4 +1,4 @@ -import getFillTemplate from './getFillTemplate.js'; +import getFillTemplate from '../getFillTemplate.js'; import Shape from 'Doodle3D/clipper-js'; import { PRECISION } from '../constants.js'; From c8f97389ce3c50694a70abbf918a188c9c9b98f6 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 20:13:30 +0200 Subject: [PATCH 39/64] add action log --- src/sliceActions/slicesToGCode.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js index cf647ee..7ab7df9 100644 --- a/src/sliceActions/slicesToGCode.js +++ b/src/sliceActions/slicesToGCode.js @@ -1,6 +1,8 @@ import GCode from '../gcode.js'; export default function slicesToGCode(slices, settings) { + console.log('slices to gcode'); + const gcode = new GCode().setSettings(settings); for (let layer = 0; layer < slices.length; layer ++) { From 0d1a9881db96fe5be085348b7345d63d00c2356e Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 6 May 2016 22:46:32 +0200 Subject: [PATCH 40/64] use destructure in add brim --- src/sliceActions/addBrim.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sliceActions/addBrim.js b/src/sliceActions/addBrim.js index a39e34e..096669e 100644 --- a/src/sliceActions/addBrim.js +++ b/src/sliceActions/addBrim.js @@ -11,7 +11,8 @@ const offsetOptions = { export default function addBrim(slices, settings) { console.log('add brim'); - const brimOffset = settings.config['brimOffset'] / PRECISION; + let { brimOffset } = settings.config; + brimOffset /= PRECISION; const fistLayer = slices[0]; fistLayer.brim = fistLayer.getOutline().offset(brimOffset, offsetOptions); From 5374a2d9c0d9ef480dd0d9110a2e8efaf826aa65 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 22:33:15 +0200 Subject: [PATCH 41/64] implement optimize paths --- src/slice.js | 73 ---------------- src/sliceActions/optimizePaths.js | 141 +++++++++++++++++++++++++++++- 2 files changed, 139 insertions(+), 75 deletions(-) diff --git a/src/slice.js b/src/slice.js index 99e0870..deb8ecf 100644 --- a/src/slice.js +++ b/src/slice.js @@ -5,79 +5,6 @@ export default class { this.parts = []; } - optimizePaths(start) { - // if (this.brim !== undefined && this.brim.length > 0) { - // this.brim = this.brim.optimizePath(start); - // start = this.brim.lastPoint(); - // } - // - // var parts = []; - // - // while (this.parts.length > 0) { - // var closestDistance = Infinity; - // var closestPart; - // - // for (var i = 0; i < this.parts.length; i ++) { - // var part = this.parts[i]; - // if (part.shape.closed) { - // var bounds = part.outerLine.bounds(); - // } - // else { - // var bounds = part.shape.bounds(); - // } - // - // var top = bounds.top - start.y; - // var bottom = start.y - bounds.bottom; - // var left = bounds.left - start.x; - // var right = start.x - bounds.right; - // - // var distance = Math.max(top, bottom, left, right); - // - // if (distance < closestDistance) { - // closestDistance = distance; - // closestPart = i; - // } - // } - // - // var part = this.parts.splice(closestPart, 1)[0]; - // parts.push(part); - // - // if (part.shape.closed) { - // if (part.outerLine.length > 0) { - // part.outerLine = part.outerLine.optimizePath(start); - // start = part.outerLine.lastPoint(); - // } - // - // for (var j = 0; j < part.innerLines.length; j ++) { - // var innerLine = part.innerLines[j]; - // if (innerLine.length > 0) { - // part.innerLines[j] = innerLine.optimizePath(start); - // start = part.innerLines[j].lastPoint(); - // } - // } - // - // if (part.fill.length > 0) { - // part.fill = part.fill.optimizePath(start); - // start = part.fill.lastPoint(); - // } - // } - // else { - // part.shape.optimizePath(start); - // start = part.shape.lastPoint(); - // } - // - // } - // - // this.parts = parts; - // - // if (this.support !== undefined && this.support.length > 0) { - // this.support = this.support.optimizePath(start); - // start = this.support.lastPoint(); - // } - - return start; - } - getOutline () { const outLines = new Shape([], true); diff --git a/src/sliceActions/optimizePaths.js b/src/sliceActions/optimizePaths.js index 82b60e2..af7b728 100644 --- a/src/sliceActions/optimizePaths.js +++ b/src/sliceActions/optimizePaths.js @@ -1,4 +1,5 @@ import THREE from 'three.js'; +import Shape from 'Doodle3D/clipper-js'; export default function optimizePaths(slices, settings) { console.log('optimize paths'); @@ -8,8 +9,144 @@ export default function optimizePaths(slices, settings) { for (let layer = 0; layer < slices.length; layer ++) { const slice = slices[layer]; - const end = slice.optimizePaths(start); + if (slice.brim !== undefined && slice.brim.paths.length > 0) { + slice.brim = optimizeShape(slice.brim, start); + start.copy(slice.brim.lastPoint()); + } - start.copy(end); + const parts = []; + + while (slice.parts.length > 0) { + let closestDistance = Infinity; + let closestPart; + + for (let i = 0; i < slice.parts.length; i ++) { + const part = slice.parts[i]; + + let bounds; + if (part.shape.closed) { + bounds = part.outerLine.shapeBounds(); + } else { + bounds = part.shape.shapeBounds(); + } + + const top = bounds.top - start.y; + const bottom = start.y - bounds.bottom; + const left = bounds.left - start.x; + const right = start.x - bounds.right; + + const distance = Math.max(top, bottom, left, right); + + if (distance < closestDistance) { + closestDistance = distance; + closestPart = i; + } + } + + const part = slice.parts.splice(closestPart, 1)[0]; + parts.push(part); + + if (part.shape.closed) { + if (part.outerLine.paths.length > 0) { + part.outerLine = optimizeShape(part.outerLine, start); + start.copy(part.outerLine.lastPoint()); + } + + for (let i = 0; i < part.innerLines.length; i ++) { + const innerLine = part.innerLines[i]; + + if (innerLine.paths.length > 0) { + part.innerLines[i] = optimizeShape(innerLine, start); + start.copy(part.innerLines[i].lastPoint()); + } + } + + if (part.fill.paths.length > 0) { + part.fill = optimizeShape(part.fill, start); + start.copy(part.fill.lastPoint()); + } + } else { + part.shape = optimizeShape(part.shape, start); + start.copy(part.shape.lastPoint()); + } + } + + slice.parts = parts; + + if (slice.support !== undefined && slice.support.length > 0) { + slice.support = optimizeShape(slice.support, start); + start.copy(slice.support.lastPoint()); + } } } + +function optimizeShape(shape, start) { + start = start.clone(); + + const inputPaths = shape.mapToLower(); + const optimizedPaths = []; + const donePaths = []; + + while (optimizedPaths.length !== inputPaths.length) { + let minLength = false; + let reverse; + let minPath; + let offset; + let pathIndex; + + for (let i = 0; i < inputPaths.length; i ++) { + if (donePaths.indexOf(i) !== -1) continue; + + const path = inputPaths[i]; + + 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(); + if (minLength === false || length < minLength) { + minPath = path; + minLength = length; + offset = j; + pathIndex = i; + } + } + } else { + const startPoint = new THREE.Vector2().copy(path[0]); + const lengthToStart = startPoint.sub(start).length(); + if (minLength === false || lengthToStart < minLength) { + minPath = path; + minLength = lengthToStart; + reverse = false; + pathIndex = i; + } + + const endPoint = new THREE.Vector2().copy(path[path.length - 1]); + const lengthToEnd = endPoint.sub(start).length(); + if (lengthToEnd < minLength) { + minPath = path; + minLength = lengthToEnd; + reverse = true; + pathIndex = i; + } + } + } + + let point; + if (shape.closed) { + minPath = minPath.concat(minPath.splice(0, offset)); + point = minPath[0]; + } else { + if (reverse) { + minPath.reverse(); + } + point = minPath[minPath.length - 1]; + } + + donePaths.push(pathIndex); + start.copy(point); + + optimizedPaths.push(minPath); + } + + return new Shape(optimizedPaths, shape.closed, true); +} From fdb5e96b6d8601dcb5f286d26bf050c64ab4136d Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 22:33:20 +0200 Subject: [PATCH 42/64] update config --- config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.js b/config.js index 5e9a068..5af4155 100644 --- a/config.js +++ b/config.js @@ -47,7 +47,7 @@ System.config({ "npm:core-js@0.9.18": { "fs": "github:jspm/nodelibs-fs@0.1.2", "process": "github:jspm/nodelibs-process@0.1.2", - "systemjs-json": "github:systemjs/plugin-json@0.1.0" + "systemjs-json": "github:systemjs/plugin-json@0.1.2" }, "npm:inherits@2.0.1": { "util": "github:jspm/nodelibs-util@0.1.0" From edeb24545e94a91c88ac3df2c18f61880b227b4d Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 22:45:36 +0200 Subject: [PATCH 43/64] remove unused code --- src/paths.js | 252 -------------------------------------------- src/slicerworker.js | 103 ------------------ 2 files changed, 355 deletions(-) delete mode 100644 src/paths.js delete mode 100644 src/slicerworker.js diff --git a/src/paths.js b/src/paths.js deleted file mode 100644 index f69bbdf..0000000 --- a/src/paths.js +++ /dev/null @@ -1,252 +0,0 @@ -import ClipperLib from 'clipper-lib'; -import THREE from 'three.js'; - -export default class Paths extends Array { - constructor (paths = [], closed = true) { - super(); - - this.setPaths(paths); - this.closed = closed; - } - - setPaths (paths) { - for (var i = 0; i < paths.length; i ++) { - var path = paths[i]; - - if (path.length > 0) { - this.push(path); - } - } - - return this; - } - - _clip (path, type) { - var solution = new ClipperLib.PolyTree(); - - var clipper = new ClipperLib.Clipper(); - clipper.AddPaths(this, ClipperLib.PolyType.ptSubject, this.closed); - clipper.AddPaths(path, ClipperLib.PolyType.ptClip, path.closed); - clipper.Execute(type, solution); - - if (this.closed) { - var paths = ClipperLib.Clipper.ClosedPathsFromPolyTree(solution); - } - else { - var paths = ClipperLib.Clipper.OpenPathsFromPolyTree(solution); - } - - return new Paths(paths, this.closed); - } - - union (path) { - return this._clip(path, ClipperLib.ClipType.ctUnion); - } - - difference (path) { - return this._clip(path, ClipperLib.ClipType.ctDifference); - } - - intersect (path) { - return this._clip(path, ClipperLib.ClipType.ctIntersection); - } - - xor (path) { - return this._clip(path, ClipperLib.ClipType.ctXor); - } - - offset (offset) { - var solution = new ClipperLib.Paths(); - var co = new ClipperLib.ClipperOffset(1, 1); - co.AddPaths(this, ClipperLib.JoinType.jtSquare, ClipperLib.EndType.etClosedPolygon); - co.Execute(solution, offset); - - return new Paths(solution); - } - - scaleUp (factor) { - ClipperLib.JS.ScaleUpPaths(this, factor); - - return this; - } - - scaleDown (factor) { - ClipperLib.JS.ScaleDownPaths(this, factor); - - return this; - } - - lastPoint () { - if (this.length === 0) { - return new THREE.Vector2(); - } - - var lastPath = this[this.length - 1]; - var lastPoint = this.closed ? lastPath[0] : lastPath[lastPath.length - 1]; - return new THREE.Vector2(lastPoint.X, lastPoint.Y); - } - - optimizePath (start) { - var optimizedPaths = new Paths([], this.closed); - var donePaths = []; - - while (optimizedPaths.length !== this.length) { - var minLength = false; - var reverse; - var minPath; - var offset; - var pathIndex; - - for (var i = 0; i < this.length; i += 1) { - var path = this[i]; - - if (donePaths.indexOf(i) === -1) { - - if (this.closed) { - for (var j = 0; j < path.length; j += 1) { - var point = new THREE.Vector2(path[j].X, path[j].Y); - var length = point.sub(start).length(); - if (minLength === false || length < minLength) { - minPath = path; - minLength = length; - offset = j; - pathIndex = i; - } - } - } - else { - var startPoint = new THREE.Vector2(path[0].X, path[0].Y); - var length = startPoint.sub(start).length(); - if (minLength === false || length < minLength) { - minPath = path; - minLength = length; - reverse = false; - pathIndex = i; - } - var endPoint = new THREE.Vector2(path[path.length - 1].X, path[path.length - 1].Y); - var length = endPoint.sub(start).length(); - if (length < minLength) { - minPath = path; - minLength = length; - reverse = true; - pathIndex = i; - } - } - } - } - - if (this.closed) { - minPath = minPath.concat(minPath.splice(0, offset)); - var point = minPath[0]; - } - else { - if (reverse) { - minPath.reverse(); - } - var point = minPath[minPath.length - 1]; - } - donePaths.push(pathIndex); - start = new THREE.Vector2(point.X, point.Y); - - optimizedPaths.push(minPath); - } - - return optimizedPaths; - } - - areas () { - var areas = []; - - for (var i = 0; i < this.length; i ++) { - var shape = this[i]; - var area = Math.abs(ClipperLib.Clipper.Area(shape)); - areas.push(area); - } - - return areas; - } - - area () { - var areas = this.areas(); - var totalArea = 0; - - for (var i = 0; i < areas.length; i ++) { - var area = areas[i]; - totalArea += area; - } - - return totalArea; - } - - tresholdArea (minArea) { - // code not tested yet - for (var i = 0; i < this.length; i ++) { - var shape = this[i]; - var area = ClipperLib.Clipper.Area(shape); - - if (area < minArea) { - this.splice(i, 1); - i -= 1; - } - } - } - - join (path) { - for (var i = 0; i < path.length; i += 1) { - this.push(path[i]); - } - - return this; - } - - clone () { - return new Paths(ClipperLib.JS.Clone(this), this.closed); - } - - bounds () { - return ClipperLib.Clipper.GetBounds(this); - } - - clean (cleanDelta) { - return new Paths(ClipperLib.Clipper.CleanPolygons(this, cleanDelta), this.closed); - } - - isHole () { - return !ClipperLib.Clipper.Orientation(this[0]); - } - - pointCollision (point) { - var collision = ClipperLib.Clipper.PointInPolygon(point, this[0]); - return ClipperLib.Clipper.PointInPolygon(point, this[0]); - } - - boundSize () { - var bounds = this.bounds(); - - var width = bounds.right - bounds.left; - var height = bounds.bottom - bounds.top; - - return width * height; - } - - draw (context, color) { - context.strokeStyle = color; - for (var i = 0; i < this.length; i += 1) { - var shape = this[i]; - - // var point = shape[0]; - // context.fillText(i, point.X*2, point.Y*2); - - context.beginPath(); - for (var j = 0; j < shape.length; j += 1) { - var point = shape[j % shape.length]; - - context.lineTo(point.X * 2, point.Y * 2); - } - if (this.closed) { - context.closePath(); - } - context.stroke(); - } - } -} diff --git a/src/slicerworker.js b/src/slicerworker.js deleted file mode 100644 index fd0e531..0000000 --- a/src/slicerworker.js +++ /dev/null @@ -1,103 +0,0 @@ -import THREE from 'three.js'; -import Settings from './settings.js'; -import EventDispatcher from 'casperlamboo/EventDispatcher'; - -export default class extends EventDispatcher { - constructor () { - super(); - - this.worker = new Worker('./worker.js'); - - this.worker.addEventListener('message', (event) => { - switch (event.data['cmd']) { - case 'PROGRESS': - - var progress = event.data['progress']; - - this.dispatchEvent({ - type: 'progress', - progress - }); - break; - - case 'GCODE': - var reader = new FileReader(); - reader.addEventListener("loadend", () => { - var gcode = reader.result; - this.dispatchEvent({ - type: 'finish', - gcode - }); - }); - reader.readAsBinaryString(event.data['gcode']); - break; - } - }, false); - - this.worker.onerror = function (error) { - console.warn(error); - }; - } - - setMesh (mesh) { - mesh.updateMatrix(); - - this.setGeometry(mesh.geometry, mesh.matrix); - - return this; - } - - setGeometry (geometry, matrix) { - if (geometry.type === 'Geometry') { - geometry = new THREE.BufferGeometry().fromGeometry(geometry); - } - else if (geometry.type === 'BufferGeometry') { - geometry = geometry.clone(); - } - else { - console.warn('Geometry is not an instance of BufferGeometry or Geometry'); - return; - } - - if (!(matrix instanceof THREE.Matrix4)) { - matrix = new THREE.Matrix4(); - } - - var buffers = []; - for (var i = 0; i < geometry.attributesKeys.length; i ++) { - var key = geometry.attributesKeys[i]; - buffers.push(geometry.attributes[key].array.buffer); - } - - delete geometry.boundingBox; - delete geometry.boundingSphere; - - this.worker.postMessage({ - 'cmd': 'SET_MESH', - 'geometry': { - 'attributes': geometry.attributes, - 'attributesKeys': geometry.attributesKeys - }, - 'matrix': matrix.toArray() - }, buffers); - - return this; - } - - slice (settings) { - this.worker.postMessage({ - 'cmd': 'SLICE', - 'settings': settings.config - }); - - return this; - } - - close () { - this.worker.postMessage({ - 'cmd': 'CLOSE' - }); - - return this; - } -} From b65541ff542079a67a3c2bb8dcdfe1a9ed032ec6 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 22:58:07 +0200 Subject: [PATCH 44/64] remove slicer worker from index --- src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index fa0ba99..a380993 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ import Slicer from './slicer.js'; -import SlicerWorker from './slicerworker.js'; import Settings from './settings.js'; -export { Slicer, SlicerWorker, Settings }; +export { Slicer, Settings }; From 8b9b1af612ae51f3cf5932c5a0be0a10041da6d0 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 22:59:20 +0200 Subject: [PATCH 45/64] update settings --- src/settings.js | 61 +++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/settings.js b/src/settings.js index a21971c..54008ce 100644 --- a/src/settings.js +++ b/src/settings.js @@ -11,47 +11,48 @@ export default class { return this; } - startCode () { - var gcode = this.config["startCode"]; - - gcode = this._subsituteVariables(gcode); - + startCode () { + const { startCode } = this.config; + const gcode = this._subsituteVariables(startCode); return gcode; } - endCode () { - var gcode = this.config["endCode"]; - - gcode = this._subsituteVariables(gcode); - + endCode () { + const { endCode } = this.config; + const gcode = this._subsituteVariables(endCode); return gcode; } _subsituteVariables (gcode) { - var temperature = this.config["temperature"]; - var bedTemperature = this.config["bedTemperature"]; - var preheatTemperature = this.config["heatupTemperature"]; - var preheatBedTemperature = this.config["heatupBedTemperature"]; - var travelSpeed = this.config["travelSpeed"] * 60; - var printerType = this.config["type"]; - var heatedbed = this.config["heatedbed"]; + let { + temperature, + bedTemperature, + heatTemperature, + heatBedTemperature, + travelSpeed, + printerType, + heatedbed + } = this.config; + + travelSpeed *= 60; switch (printerType) { - case "makerbot_replicator2": printerType = "r2"; break; - case "makerbot_replicator2x": printerType = "r2x"; break; - case "makerbot_thingomatic": printerType = "t6"; break; - case "makerbot_generic": printerType = "r2"; break; - case "_3Dison_plus": printerType = "r2"; break; + case 'makerbot_replicator2': printerType = 'r2'; break; + case 'makerbot_replicator2x': printerType = 'r2x'; break; + case 'makerbot_thingomatic': printerType = 't6'; break; + case 'makerbot_generic': printerType = 'r2'; break; + case '_3Dison_plus': printerType = 'r2'; break; } - var heatedBedReplacement = heatedbed ? "" : ";"; - gcode = gcode.replace(/{printingTemp}/gi, temperature); - gcode = gcode.replace(/{printingBedTemp}/gi, bedTemperature); - gcode = gcode.replace(/{preheatTemp}/gi, preheatTemperature); - gcode = gcode.replace(/{preheatBedTemp}/gi, preheatBedTemperature); - gcode = gcode.replace(/{printerType}/gi, printerType); - gcode = gcode.replace(/{travelSpeed}/gi, travelSpeed); - gcode = gcode.replace(/{if heatedBed}/gi, heatedBedReplacement); + const heatedBedReplacement = heatedbed ? '' : ';'; + + gcode = gcode.replace(/{ printingTemp }/gi, temperature); + gcode = gcode.replace(/{ printingBedTemp }/gi, bedTemperature); + gcode = gcode.replace(/{ preheatTemp }/gi, heatTemperature); + gcode = gcode.replace(/{ preheatBedTemp }/gi, heatBedTemperature); + gcode = gcode.replace(/{ printerType }/gi, printerType); + gcode = gcode.replace(/{ travelSpeed }/gi, travelSpeed); + gcode = gcode.replace(/{ if heatedBed }/gi, heatedBedReplacement); return gcode; } From caf62e58d52a7a6f388c45f89bef38b29fa15eaa Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 23:06:56 +0200 Subject: [PATCH 46/64] remove worker --- src/worker.js | 55 --------------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 src/worker.js diff --git a/src/worker.js b/src/worker.js deleted file mode 100644 index 02964dc..0000000 --- a/src/worker.js +++ /dev/null @@ -1,55 +0,0 @@ -importScripts('../jspm_packages/system.js'); -importScripts('../config.js'); - -var Slicer, Settings, THREE; - -function init () { - var slicer = new Slicer(); - slicer.onProgress = function (progress) { - self.postMessage({ - 'cmd': 'PROGRESS', - 'progress': progress - }); - }; - - self.addEventListener('message', function (event) { - switch (event.data['cmd']) { - case 'SET_MESH': - var geometry = new THREE.Geometry().fromBufferGeometry(event.data['geometry']); - var matrix = new THREE.Matrix4().fromArray(event.data['matrix']); - - slicer.setGeometry(geometry, matrix); - break; - - case 'SLICE': - var settings = new Settings().updateConfig(event.data['settings']); - - var gcode = slicer.slice(settings); - var blob = new Blob([gcode], {type: 'text/plain'}); - - self.postMessage({ - 'cmd': 'GCODE', - 'gcode': blob - }); - - //self.close(); - break; - - case 'CLOSE': - self.close(); - break; - } - }); -} - -Promise.all([ - System.import('./slicer'), - System.import('./settings'), - System.import('three.js') -]).then(function(modules) { - Slicer = modules[0].default; - Settings = modules[1].default; - THREE = modules[2]; - - init(); -}); From 0eb2a3994f83c2e7728dfb2a7eb9cd2dc658a010 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 23:31:06 +0200 Subject: [PATCH 47/64] use deconstruction in move --- src/gcode.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/gcode.js b/src/gcode.js index f21c4e6..9c50d3d 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -68,8 +68,10 @@ export default class { } moveTo (x, y, layer) { - const layerHeight = this.settings.config['layerHeight']; - const travelSpeed = this.settings.config['travelSpeed']; + const { + layerHeight, + travelSpeed + } = this.settings; const z = (layer + 1) * layerHeight; const speed = travelSpeed * 60; @@ -90,15 +92,21 @@ export default class { lineTo (x, y, layer, type) { const newNozzlePosition = new THREE.Vector2(x, y); - const layerHeight = this.settings.config['layerHeight']; - const nozzleDiameter = this.settings.config['nozzleDiameter']; - const filamentThickness = this.settings.config['filamentThickness']; - const travelSpeed = this.settings.config['travelSpeed']; + const { + layerHeight, + nozzleDiameter, + filamentThickness, + travelSpeed + } = this.settings; const profile = this.settings.config[(this.bottom ? 'bottom' : type)]; - const speed = profile['speed'] * 60; - const flowRate = profile['flowRate']; + let { + speed, + flowRate + } = profile; + + speed *= 60; const z = (layer + 1) * layerHeight; const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); From 7360fc86bc9a808eb4d051536637bcdaebdfd85b Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 23:45:58 +0200 Subject: [PATCH 48/64] use deconstruction --- src/gcode.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/gcode.js b/src/gcode.js index 9c50d3d..648cfdd 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -151,10 +151,12 @@ export default class { } retract () { - const retractionAmount = this.settings.config['retractionAmount']; - const retractionEnabled = this.settings.config['retractionEnabled']; - const retractionMinDistance = this.settings.config['retractionMinDistance']; - const retractionSpeed = this.settings.config['retractionSpeed']; + const { + retractionAmount, + retractionEnabled, + retractionMinDistance, + retractionSpeed + } = this.settings; if (!this.isRetracted && retractionEnabled) { this.isRetracted = true; From 7517f522d0734aeb24e4500c4287fc8ebb61e026 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sat, 7 May 2016 23:46:08 +0200 Subject: [PATCH 49/64] use single quotes --- src/sliceActions/generateSupport.js | 2 +- src/sliceActions/intersectionsToShapes.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sliceActions/generateSupport.js b/src/sliceActions/generateSupport.js index ce71913..17ca7b3 100644 --- a/src/sliceActions/generateSupport.js +++ b/src/sliceActions/generateSupport.js @@ -3,7 +3,7 @@ import Shape from 'Doodle3D/clipper-js'; import { PRECISION } from '../constants.js'; export default function generateSupport(slices, settings) { - console.log("generating support"); + console.log('generating support'); if (!settings.config.supportEnabled) return; diff --git a/src/sliceActions/intersectionsToShapes.js b/src/sliceActions/intersectionsToShapes.js index 0143cda..8bada93 100644 --- a/src/sliceActions/intersectionsToShapes.js +++ b/src/sliceActions/intersectionsToShapes.js @@ -2,7 +2,7 @@ import THREE from 'three.js'; import Shape from 'Doodle3D/clipper-js'; export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) { - console.log("generating slices"); + console.log('generating slices'); const layers = []; From 5a46e138af390d7e3f90e44629ecaadde5368060 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sun, 8 May 2016 10:26:41 +0200 Subject: [PATCH 50/64] use deconstion in code --- src/gcode.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/gcode.js b/src/gcode.js index 648cfdd..a40a4c3 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -129,9 +129,11 @@ export default class { } unRetract () { - const retractionEnabled = this.settings.config['retractionEnabled']; - const retractionMinDistance = this.settings.config['retractionMinDistance']; - const retractionSpeed = this.settings.config['retractionSpeed']; + const { + retractionEnabled, + retractionMinDistance, + retractionSpeed + } = this.settings.config; if (this.isRetracted && retractionEnabled) { this.isRetracted = false; From d9fca74653a3860f795f477424c34f5de3bbe0d2 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sun, 8 May 2016 10:26:47 +0200 Subject: [PATCH 51/64] fix code --- src/gcode.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gcode.js b/src/gcode.js index a40a4c3..efba660 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -71,7 +71,7 @@ export default class { const { layerHeight, travelSpeed - } = this.settings; + } = this.settings.config; const z = (layer + 1) * layerHeight; const speed = travelSpeed * 60; @@ -97,7 +97,7 @@ export default class { nozzleDiameter, filamentThickness, travelSpeed - } = this.settings; + } = this.settings.config; const profile = this.settings.config[(this.bottom ? 'bottom' : type)]; @@ -158,7 +158,7 @@ export default class { retractionEnabled, retractionMinDistance, retractionSpeed - } = this.settings; + } = this.settings.config; if (!this.isRetracted && retractionEnabled) { this.isRetracted = true; From 06c61a3cf61c34cf665bc2e8f517259e7ab5374d Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sun, 8 May 2016 10:31:00 +0200 Subject: [PATCH 52/64] move set settings to constructor --- src/gcode.js | 6 +++++- src/sliceActions/slicesToGCode.js | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/gcode.js b/src/gcode.js index efba660..506f05e 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -10,7 +10,7 @@ const POSITION_Y = 'Y'; const POSITION_Z = 'Z'; export default class { - constructor () { + constructor (settings) { this.gcode = ''; this.current = {}; @@ -19,6 +19,10 @@ export default class { this.isRetracted = false; this.isFanOn = false; this._nozzlePosition = new THREE.Vector2(0, 0); + + if (settings !== undefined) { + this.setSettings(settings); + } } _addGCode (command) { diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js index 7ab7df9..cfa6f5e 100644 --- a/src/sliceActions/slicesToGCode.js +++ b/src/sliceActions/slicesToGCode.js @@ -3,7 +3,7 @@ import GCode from '../gcode.js'; export default function slicesToGCode(slices, settings) { console.log('slices to gcode'); - const gcode = new GCode().setSettings(settings); + const gcode = new GCode(settings); for (let layer = 0; layer < slices.length; layer ++) { const slice = slices[layer]; From ecce63f63cb0c98a0c5b06d12fad7f00bb4c0107 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sun, 8 May 2016 10:31:29 +0200 Subject: [PATCH 53/64] fix subsitute variables --- src/settings.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/settings.js b/src/settings.js index 54008ce..470235c 100644 --- a/src/settings.js +++ b/src/settings.js @@ -46,13 +46,13 @@ export default class { const heatedBedReplacement = heatedbed ? '' : ';'; - gcode = gcode.replace(/{ printingTemp }/gi, temperature); - gcode = gcode.replace(/{ printingBedTemp }/gi, bedTemperature); - gcode = gcode.replace(/{ preheatTemp }/gi, heatTemperature); - gcode = gcode.replace(/{ preheatBedTemp }/gi, heatBedTemperature); - gcode = gcode.replace(/{ printerType }/gi, printerType); - gcode = gcode.replace(/{ travelSpeed }/gi, travelSpeed); - gcode = gcode.replace(/{ if heatedBed }/gi, heatedBedReplacement); + gcode = gcode.replace(/{printingTemp}/gi, temperature); + gcode = gcode.replace(/{printingBedTemp}/gi, bedTemperature); + gcode = gcode.replace(/{preheatTemp}/gi, heatTemperature); + gcode = gcode.replace(/{preheatBedTemp}/gi, heatBedTemperature); + gcode = gcode.replace(/{printerType}/gi, printerType); + gcode = gcode.replace(/{travelSpeed}/gi, travelSpeed); + gcode = gcode.replace(/{if heatedBed}/gi, heatedBedReplacement); return gcode; } From 5b9931531b58efa03118a99b1016263ac4c95600 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sun, 8 May 2016 11:09:34 +0200 Subject: [PATCH 54/64] include settings in lib --- example/app.js | 6 ++---- src/index.js | 4 +++- {settings => src/settings}/printer_settings.json | 0 {settings => src/settings}/user_settings.json | 0 4 files changed, 5 insertions(+), 5 deletions(-) rename {settings => src/settings}/printer_settings.json (100%) rename {settings => src/settings}/user_settings.json (100%) diff --git a/example/app.js b/example/app.js index a5bbfbe..75f8c9d 100644 --- a/example/app.js +++ b/example/app.js @@ -1,11 +1,9 @@ import THREE from 'three.js'; -import PRINTER_SETTINGS from 'settings/printer_settings.json!'; -import USER_SETTINGS from 'settings/user_settings.json!'; import * as SLICER from 'src/index'; var settings = new SLICER.Settings(); -settings.updateConfig(PRINTER_SETTINGS["ultimaker2go"]); -settings.updateConfig(USER_SETTINGS); +settings.updateConfig(SLICER.printerSettings['ultimaker2go']); +settings.updateConfig(SLICER.userSettings); var geometry = new THREE.TorusGeometry(20, 10, 30, 30); diff --git a/src/index.js b/src/index.js index a380993..f9fca83 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,6 @@ import Slicer from './slicer.js'; import Settings from './settings.js'; +import printerSettings from './settings/printer_settings.json!'; +import userSettings from './settings/user_settings.json!'; -export { Slicer, Settings }; +export { Slicer, Settings, printerSettings, userSettings }; diff --git a/settings/printer_settings.json b/src/settings/printer_settings.json similarity index 100% rename from settings/printer_settings.json rename to src/settings/printer_settings.json diff --git a/settings/user_settings.json b/src/settings/user_settings.json similarity index 100% rename from settings/user_settings.json rename to src/settings/user_settings.json From 481de15e75e38abf563ead63534c4a2b5895e33c Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Sun, 8 May 2016 11:12:35 +0200 Subject: [PATCH 55/64] update example --- example/app.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/example/app.js b/example/app.js index 75f8c9d..6f93bff 100644 --- a/example/app.js +++ b/example/app.js @@ -1,14 +1,13 @@ import THREE from 'three.js'; import * as SLICER from 'src/index'; -var settings = new SLICER.Settings(); +const settings = new SLICER.Settings(); settings.updateConfig(SLICER.printerSettings['ultimaker2go']); settings.updateConfig(SLICER.userSettings); -var geometry = new THREE.TorusGeometry(20, 10, 30, 30); +const geometry = new THREE.TorusGeometry(20, 10, 30, 30); -var slicer = new SLICER.Slicer(); -//var slicer = new SLICER.SlicerWorker(); +const slicer = new SLICER.Slicer(); slicer.setGeometry(geometry.clone()); slicer.addEventListener('finish', ({ gcode }) => { From 0d4f6fbe5b1874b326bbf0db929ba24c3d852356 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 9 May 2016 11:17:49 +0200 Subject: [PATCH 56/64] remove line breaks --- src/slicer.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/slicer.js b/src/slicer.js index 64b26b0..d9dd952 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -14,7 +14,6 @@ import applyPrecision from './sliceActions/applyPrecision.js'; import removePrecision from './sliceActions/removePrecision.js'; export default class extends EventDispatcher { - setMesh (mesh) { mesh.updateMatrix(); @@ -22,15 +21,12 @@ export default class extends EventDispatcher { return this; } - setGeometry (geometry, matrix) { if (geometry.type === 'BufferGeometry') { geometry = new THREE.Geometry().fromBufferGeometry(geometry); - } - else if (geometry.type.endsWith('Geometry')) { + } else if (geometry.type.endsWith('Geometry')) { geometry = geometry.clone(); - } - else { + } else { console.warn('Geometry is not an instance of BufferGeometry or Geometry'); return; } @@ -46,7 +42,6 @@ export default class extends EventDispatcher { return this; } - slice (settings) { // get unique lines from geometry; const lines = createLines(this.geometry, settings); @@ -63,15 +58,10 @@ export default class extends EventDispatcher { const slices = shapesToSlices(shapes, settings); generateInnerLines(slices, settings); - generateInfills(slices, settings); - generateSupport(slices, settings); - addBrim(slices, settings); - optimizePaths(slices, settings); - removePrecision(slices); const gcode = slicesToGCode(slices, settings); From 47e5090444185fc47f7b2caa49ac56ae1875d9e9 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 9 May 2016 11:40:54 +0200 Subject: [PATCH 57/64] remove line breaks --- src/settings.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/settings.js b/src/settings.js index 470235c..1eb72fc 100644 --- a/src/settings.js +++ b/src/settings.js @@ -1,29 +1,25 @@ export default class { - constructor () { + constructor() { this.config = {}; } - - updateConfig (config) { - for (var i in config) { + updateConfig(config) { + for (const i in config) { this.config[i] = config[i]; } return this; } - - startCode () { + startCode() { const { startCode } = this.config; const gcode = this._subsituteVariables(startCode); return gcode; } - - endCode () { + endCode() { const { endCode } = this.config; const gcode = this._subsituteVariables(endCode); return gcode; } - - _subsituteVariables (gcode) { + _subsituteVariables(gcode) { let { temperature, bedTemperature, From 08adf33139ff4f74e87e09615c7bbafce78da28f Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 9 May 2016 11:41:21 +0200 Subject: [PATCH 58/64] remove line breaks --- src/slice.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/slice.js b/src/slice.js index deb8ecf..32b75bf 100644 --- a/src/slice.js +++ b/src/slice.js @@ -4,8 +4,7 @@ export default class { constructor() { this.parts = []; } - - getOutline () { + getOutline() { const outLines = new Shape([], true); for (let i = 0; i < this.parts.length; i ++) { @@ -18,8 +17,7 @@ export default class { return outLines; } - - add (shape) { + add(shape) { const part = { shape }; if (shape.closed) { From 74e0a5d0ef94fc66d74f791ba4e5514cdfbc2ae4 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 9 May 2016 11:42:23 +0200 Subject: [PATCH 59/64] remove clone function in example --- example/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/app.js b/example/app.js index 6f93bff..829f75d 100644 --- a/example/app.js +++ b/example/app.js @@ -9,7 +9,7 @@ const geometry = new THREE.TorusGeometry(20, 10, 30, 30); const slicer = new SLICER.Slicer(); -slicer.setGeometry(geometry.clone()); +slicer.setGeometry(geometry); slicer.addEventListener('finish', ({ gcode }) => { document.getElementById('gcode').innerHTML = gcode.replace(/(?:\r\n|\r|\n)/g, '
'); }); From ab3b241fcb086712d526733cb61846a330ab2c46 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 18 Jul 2016 18:28:47 +0200 Subject: [PATCH 60/64] update to jspm 0.17 --- config.js | 63 ----------------------------------------- example/index.html | 4 +-- jspm.config.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 19 +++++++++++-- src/index.js | 4 +-- 5 files changed, 90 insertions(+), 70 deletions(-) delete mode 100644 config.js create mode 100644 jspm.config.js diff --git a/config.js b/config.js deleted file mode 100644 index 5af4155..0000000 --- a/config.js +++ /dev/null @@ -1,63 +0,0 @@ -System.config({ - baseURL: "/", - defaultJSExtensions: true, - transpiler: "babel", - babelOptions: { - "optional": [ - "runtime" - ] - }, - paths: { - "github:*": "jspm_packages/github/*", - "npm:*": "jspm_packages/npm/*" - }, - bundles: { - "bundle.js": [] - }, - - map: { - "Doodle3D/clipper-js": "github:Doodle3D/clipper-js@master", - "babel": "npm:babel-core@5.8.38", - "babel-runtime": "npm:babel-runtime@5.8.38", - "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master", - "core-js": "npm:core-js@0.9.18", - "json": "github:systemjs/plugin-json@0.1.0", - "three.js": "github:mrdoob/three.js@r72", - "github:Doodle3D/clipper-js@master": { - "clipper-lib": "npm:clipper-lib@1.0.0" - }, - "github:jspm/nodelibs-assert@0.1.0": { - "assert": "npm:assert@1.3.0" - }, - "github:jspm/nodelibs-process@0.1.2": { - "process": "npm:process@0.11.2" - }, - "github:jspm/nodelibs-util@0.1.0": { - "util": "npm:util@0.10.3" - }, - "npm:assert@1.3.0": { - "util": "npm:util@0.10.3" - }, - "npm:babel-runtime@5.8.38": { - "process": "github:jspm/nodelibs-process@0.1.2" - }, - "npm:clipper-lib@1.0.0": { - "process": "github:jspm/nodelibs-process@0.1.2" - }, - "npm:core-js@0.9.18": { - "fs": "github:jspm/nodelibs-fs@0.1.2", - "process": "github:jspm/nodelibs-process@0.1.2", - "systemjs-json": "github:systemjs/plugin-json@0.1.2" - }, - "npm:inherits@2.0.1": { - "util": "github:jspm/nodelibs-util@0.1.0" - }, - "npm:process@0.11.2": { - "assert": "github:jspm/nodelibs-assert@0.1.0" - }, - "npm:util@0.10.3": { - "inherits": "npm:inherits@2.0.1", - "process": "github:jspm/nodelibs-process@0.1.2" - } - } -}); diff --git a/example/index.html b/example/index.html index 1117bf4..f85f1ec 100644 --- a/example/index.html +++ b/example/index.html @@ -11,7 +11,7 @@ - +