Doodle3D-Slicer/src/slicer.js

404 lines
11 KiB
JavaScript
Raw Normal View History

2015-04-24 16:12:48 +02:00
/******************************************************
*
* Slicer
*
* TODO (optimalisatie)
* sorteer lijnen op laagste hoogte -> stop loop wanneer hij een lijn zonder intersectie heeft gevonden
* verwijder lijnen die ooit interactie gehad hebben, maar nu niet meer
2015-04-30 18:26:34 +02:00
* helft van lijnen toevoegen omdat 4face altijd recht is, en 3face dus te veel data bevat
*
* omliggende lagen -> difference && sum omliggende lijnen
* voor laag 5 = 5 diff (3 && 4 && 6 && 7))
2015-04-24 16:12:48 +02:00
*
******************************************************/
2015-04-30 18:26:34 +02:00
var use_deprecated = true;
2015-04-24 16:12:48 +02:00
D3D.Slicer = function () {
2015-04-24 16:12:48 +02:00
"use strict";
this.geometry;
2015-04-24 16:12:48 +02:00
this.lines = [];
this.lineLookup = {};
};
D3D.Slicer.prototype.setGeometry = function (geometry) {
"use strict";
this.geometry = geometry;
this.geometry.mergeVertices();
this.createLines();
return this;
};
2015-04-24 16:12:48 +02:00
D3D.Slicer.prototype.addLine = function (a, b) {
"use stict";
//think lookup can only be b_a, a_b is only possible when face is flipped
var index = this.lineLookup[a + "_" + b] || this.lineLookup[b + "_" + a];
//if (!index) {
if (index === undefined) {
index = this.lines.length;
this.lineLookup[a + "_" + b] = index;
this.lines.push({
line: new THREE.Line3(this.geometry.vertices[a], this.geometry.vertices[b]),
connects: [],
normals: []
2015-04-24 16:12:48 +02:00
});
}
return index;
};
D3D.Slicer.prototype.createLines = function () {
"use strict";
this.lines = [];
this.lineLookup = {};
for (var i = 0; i < this.geometry.faces.length; i ++) {
var face = this.geometry.faces[i];
var normal = new THREE.Vector2().set(face.normal.x, face.normal.z).normalize();
2015-04-24 16:12:48 +02:00
//check for only adding unique lines
//returns index of said line
var a = this.addLine(face.a, face.b);
var b = this.addLine(face.b, face.c);
var c = this.addLine(face.c, face.a);
//set connecting lines (based on face)
2015-04-28 16:08:56 +02:00
//something wrong here, 3 face can go in different direction
2015-04-24 16:12:48 +02:00
this.lines[a].connects.push(b, c);
this.lines[b].connects.push(c, a);
2015-04-24 16:12:48 +02:00
this.lines[c].connects.push(a, b);
2015-04-30 18:26:34 +02:00
this.lines[a].normals.push(normal);
this.lines[b].normals.push(normal);
this.lines[c].normals.push(normal);
2015-04-24 16:12:48 +02:00
}
//sort lines on min height
//this.lines.sort(function (a, b) {
// return Math.min() - Math.min();
//});
};
D3D.Slicer.prototype.slice = function (height, step) {
"use strict";
var slices = [];
var plane = new THREE.Plane();
for (var z = 0; z < height; z += step) {
plane.set(new THREE.Vector3(0, -1, 0), z);
var slice = [];
var intersections = [];
for (var i = 0; i < this.lines.length; i ++) {
var line = this.lines[i].line;
var intersection = plane.intersectLine(line);
if (intersection !== undefined) {
//remove +100 when implimenting good structure for geometry is complete
var point = new THREE.Vector2(intersection.x + 100, intersection.z + 100);
intersections.push(point);
}
else {
intersections.push(false);
}
}
var done = [];
for (var i = 0; i < intersections.length; i ++) {
if (intersections[i] && done.indexOf(i) === -1) {
2015-04-24 16:12:48 +02:00
var index = i;
var shape = [];
while (index !== -1) {
var intersection = intersections[index];
2015-04-30 18:26:34 +02:00
shape.push({X: intersection.x, Y: intersection.y});
2015-04-24 16:12:48 +02:00
done.push(index);
var connects = this.lines[index].connects;
var faceNormals = this.lines[index].normals;
2015-04-24 16:12:48 +02:00
for (var j = 0; j < connects.length; j ++) {
index = connects[j];
if (intersections[index] && done.indexOf(index) === -1) {
var normal = new THREE.Vector2().copy(intersection).sub(intersections[index]).normal().normalize();
2015-04-30 18:26:34 +02:00
var faceNormal = faceNormals[Math.floor(j/2)];
2015-04-28 18:10:16 +02:00
if (normal.dot(faceNormal) > 0) {
break;
}
else {
index = -1;
}
2015-04-24 16:12:48 +02:00
}
else {
index = -1;
}
}
}
//think this check is not nescesary, always higher as 0
if (shape.length > 0) {
slice.push(shape);
}
}
}
2015-04-30 18:26:34 +02:00
//stop when ther are no intersects
if (slice.length > 0) {
slices.push(slice);
}
else {
break;
}
2015-04-24 16:12:48 +02:00
}
return slices;
};
2015-04-30 18:26:34 +02:00
D3D.Slicer.prototype.getInset = function (slice, offset) {
2015-04-24 16:12:48 +02:00
"use strict";
2015-04-30 18:26:34 +02:00
var solution = new ClipperLib.Paths();
var co = new ClipperLib.ClipperOffset(1, 1);
co.AddPaths(slice, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
co.Execute(solution, -offset);
2015-04-24 16:12:48 +02:00
2015-04-30 18:26:34 +02:00
return solution;
};
D3D.Slicer.prototype.getFillTemplate = function (dimension, size, even, uneven) {
var paths = new ClipperLib.Paths();
2015-04-24 16:12:48 +02:00
2015-04-30 18:26:34 +02:00
if (even) {
for (var length = 0; length <= dimension; length += size) {
paths.push([{X: length, Y: 0}, {X: length, Y: dimension}]);
}
}
if (uneven) {
for (var length = 0; length <= dimension; length += size) {
paths.push([{X: 0, Y: length}, {X: dimension, Y: length}]);
}
}
return paths;
};
D3D.Slicer.prototype.slicesToData = function (slices, config) {
var data = [];
2015-04-24 16:12:48 +02:00
2015-04-30 18:26:34 +02:00
//scale because of clipper crap
var scale = 100;
var layerHeight = config["printer.layerHeight"] * scale;
var dimensionsZ = config["printer.dimensions.z"] * scale;
//should come from config???
//aan rick voorleggen
var nozzleSize = 0.4 * scale;
var shellThickness = 0.8 * scale;
var fillSize = 5 * scale;
var lowFillTemplate = this.getFillTemplate(dimensionsZ, fillSize, true, true);
2015-04-24 16:12:48 +02:00
for (var layer = 0; layer < slices.length; layer ++) {
var slice = slices[layer];
2015-04-30 18:26:34 +02:00
var highFillTemplate = this.getFillTemplate(dimensionsZ, nozzleSize*2, (layer % 2 === 0), (layer % 2 === 1));
2015-04-24 16:12:48 +02:00
2015-04-30 18:26:34 +02:00
//var outerLayer = ClipperLib.JS.Clean(slice, 1.0);
var outerLayer = slice.clone();
ClipperLib.JS.ScaleUpPaths(outerLayer, scale);
var innerLayer = [];
for (var i = nozzleSize; i < shellThickness; i += nozzleSize) {
var inset = this.getInset(outerLayer, i);
innerLayer = innerLayer.concat(inset);
2015-04-24 16:12:48 +02:00
}
2015-04-30 18:26:34 +02:00
var fillArea = this.getInset((inset || outerLayer), nozzleSize);
var highFill;
var fillAbove;
//for (var i = 1; i < shellThickness/layerHeight; i ++) {
var newLayer = ClipperLib.JS.Clone(slices[layer + 1] || []);
ClipperLib.JS.ScaleUpPaths(newLayer, scale);
fillAbove = newLayer;
//}
//kijkt alleen nog naar boven
//omliggende lagen hebben inhoud van lowFill;
//inset moet opgevult worden;
//verschill tussen lowFill en inset moet vol, rest is raster
var c = new ClipperLib.Clipper();
var highFillArea = new ClipperLib.Paths();
c.AddPaths(fillArea, ClipperLib.PolyType.ptSubject, true);
c.AddPaths(fillAbove, ClipperLib.PolyType.ptClip, true);
c.Execute(ClipperLib.ClipType.ctDifference, highFillArea);
var c = new ClipperLib.Clipper();
var lowFillArea = new ClipperLib.Paths();
c.AddPaths(fillArea, ClipperLib.PolyType.ptSubject, true);
c.AddPaths(highFillArea, ClipperLib.PolyType.ptClip, true);
c.Execute(ClipperLib.ClipType.ctDifference, lowFillArea);
var fill = [];
var c = new ClipperLib.Clipper();
var solution = new ClipperLib.Paths();
c.AddPaths(lowFillTemplate, ClipperLib.PolyType.ptSubject, false);
c.AddPaths(lowFillArea, ClipperLib.PolyType.ptClip, true);
c.Execute(ClipperLib.ClipType.ctIntersection, solution);
fill = fill.concat(solution);
var c = new ClipperLib.Clipper();
var solution = new ClipperLib.Paths();
c.AddPaths(highFillTemplate, ClipperLib.PolyType.ptSubject, false);
c.AddPaths(highFillArea, ClipperLib.PolyType.ptClip, true);
c.Execute(ClipperLib.ClipType.ctIntersection, solution);
fill = fill.concat(solution);
ClipperLib.JS.ScaleDownPaths(outerLayer, scale);
ClipperLib.JS.ScaleDownPaths(innerLayer, scale);
ClipperLib.JS.ScaleDownPaths(fill, scale);
data.push({
outerLayer: outerLayer,
innerLayer: innerLayer,
fill: fill
})
}
return data;
};
D3D.Slicer.prototype.getGcode = function (config) {
"use strict";
function sliceToGcode (slice) {
var gcode = [];
2015-04-24 16:12:48 +02:00
for (var i = 0; i < slice.length; i ++) {
var shape = slice[i];
var previousPoint;
for (var j = 0; j <= shape.length; j ++) {
//Finish shape by going to first point
var point = shape[(j % shape.length)];
if (j === 0) {
//TODO
//add retraction
2015-04-30 18:26:34 +02:00
if (extruder > retractionMinDistance && retractionEnabled) {
2015-04-24 16:12:48 +02:00
gcode.push([
"G0",
2015-04-24 21:32:39 +02:00
"E" + (extruder - retractionAmount).toFixed(3),
"F" + (retractionSpeed * 60).toFixed(3)
2015-04-24 16:12:48 +02:00
].join(" "));
}
gcode.push([
"G0",
2015-04-30 18:26:34 +02:00
"X" + point.X.toFixed(3) + " Y" + point.Y.toFixed(3) + " Z" + z,
2015-04-24 16:12:48 +02:00
"F" + (travelSpeed*60)
].join(" "));
2015-04-30 18:26:34 +02:00
if (extruder > retractionMinDistance && retractionEnabled) {
2015-04-24 16:12:48 +02:00
gcode.push([
"G0",
"E" + extruder.toFixed(3),
2015-04-24 21:32:39 +02:00
"F" + (retractionSpeed * 60).toFixed(3)
2015-04-24 16:12:48 +02:00
].join(" "));
}
}
else {
2015-04-30 18:26:34 +02:00
var a = new THREE.Vector2().set(point.X, point.Y);
var b = new THREE.Vector2().set(previousPoint.X, previousPoint.Y);
var lineLength = a.distanceTo(b);
2015-04-24 16:12:48 +02:00
extruder += lineLength * wallThickness * layerHeight / filamentSurfaceArea * flowRate;
gcode.push([
"G1",
2015-04-30 18:26:34 +02:00
"X" + point.X.toFixed(3) + " Y" + point.Y.toFixed(3) + " Z" + z,
2015-04-24 16:12:48 +02:00
"F" + speed,
"E" + extruder.toFixed(3)
].join(" "));
}
previousPoint = point;
}
}
2015-04-30 18:26:34 +02:00
return gcode;
}
var normalSpeed = config["printer.speed"];
var bottomSpeed = config["printer.bottomLayerSpeed"];
var firstLayerSlow = config["printer.firstLayerSlow"];
var bottomFlowRate = config["printer.bottomFlowRate"];
var travelSpeed = config["printer.travelSpeed"];
var filamentThickness = config["printer.filamentThickness"];
var wallThickness = config["printer.wallThickness"];
var layerHeight = config["printer.layerHeight"];
var enableTraveling = config["printer.enableTraveling"];
var retractionEnabled = config["printer.retraction.enabled"];
var retractionSpeed = config["printer.retraction.speed"];
var retractionMinDistance = config["printer.retraction.minDistance"];
var retractionAmount = config["printer.retraction.amount"];
var dimensionsZ = config["printer.dimensions.z"];
var gcode = doodleBox.printer.getStartCode();
var extruder = 0.0;
var speed = firstLayerSlow ? (bottomSpeed*60).toFixed(3) : (normalSpeed*60).toFixed(3);
var filamentSurfaceArea = Math.pow((filamentThickness/2), 2) * Math.PI;
var flowRate = bottomFlowRate;
var slices = [];
var slices = this.slice(dimensionsZ, layerHeight);
//still error in first layer, so remove first layer
//see https://github.com/Doodle3D/Doodle3D-Slicer/issues/1
slices.shift();
//code for only printing the first layer
//var slices = [slices.shift()];
var data = this.slicesToData(slices, config);
//return data;
for (var layer = 0; layer < data.length; layer ++) {
var slice = data[layer];
//turn on fan on layer 2
if (layer === 2) {
gcode.push("M106");
speed = (normalSpeed*60).toFixed(3);
flowRate = 1;
}
var z = ((layer + 1) * layerHeight).toFixed(3);
gcode = gcode.concat(sliceToGcode(slice.outerLayer));
gcode = gcode.concat(sliceToGcode(slice.innerLayer));
gcode = gcode.concat(sliceToGcode(slice.fill));
2015-04-24 16:12:48 +02:00
}
gcode = gcode.concat(doodleBox.printer.getEndCode());
return gcode;
};