diff --git a/README.md b/README.md index 780497a..bd2c19f 100644 --- a/README.md +++ b/README.md @@ -10,16 +10,18 @@ This is an example of code. var localIp = "192.168.5.1"; var doodleBox = new D3D.Box(localIp); +var geometry = new THREE.TorusGeometry(40, 20, 10, 10); +var material = new THREE.MeshBasicMaterial({color: 0x000000, wireframe: true}); +var mesh = new THREE.Mesh(geometry, material); + doodleBox.onload = function () { "use strict"; - var geometry = new THREE.TorusGeometry(40, 20, 10, 10); - - var slicer = new D3D.Slicer().setGeometry(geometry); + var slicer = new D3D.Slicer().setGeometry(mesh); var gcode = slicer.getGcode(doodleBox.printer); doodleBox.print(gcode); }; ``` -For more information see http://www.doodle3d.com/help/api-documentation +For more information see http://www.doodle3d.com/ diff --git a/build/d3d.js b/build/d3d.js index 34c41cf..8fd3451 100644 --- a/build/d3d.js +++ b/build/d3d.js @@ -41,7 +41,7 @@ function sendAPI (url, data, callback) { } } }).fail(function () { - console.warn("failed connecting to " + url); + console.warn("Failed connecting to " + url); sendAPI(url, data, callback); }); } @@ -64,7 +64,7 @@ function getAPI (url, callback) { } } }).fail(function () { - console.warn("failed connecting to " + url); + console.warn("Failed connecting to " + url); getAPI(url, callback); }); } @@ -203,8 +203,8 @@ D3D.Box.prototype.update = function () { //Bij error wordt gelijk zelfde data opnieuw gestuurd //Als DoodleBox ontkoppeld wordt komt er een error in de loop waardoor pagina breekt en ververst moet worden - //if (this.printBatches.length > 0 && (this.progress["buffered_lines"] + this.batchSize) <= this.maxBufferedLines) { - if (this.printBatches.length > 0 ) { + if (this.printBatches.length > 0 && (this.printer.status["buffered_lines"] + this.batchSize) <= this.maxBufferedLines) { + //if (this.printBatches.length > 0 ) { this.printBatch(); } else { @@ -286,6 +286,7 @@ D3D.Box.prototype.stopPrint = function () { ]; this.setPrinterStop({ + //"gcode": {} "gcode": finishMove.join("\n") }, function (data) { console.log("Printer stop command sent"); @@ -297,19 +298,20 @@ D3D.Box.prototype.stopPrint = function () { //COMMUNICATION SHELL //see http://www.doodle3d.com/help/api-documentation D3D.Box.prototype.getConfig = function (keys, callback) { - //works "use strict"; getAPI(this.api + "config/?" + keys.join("=&") + "=", callback); + + return this; }; D3D.Box.prototype.getConfigAll = function (callback) { - //works "use strict"; getAPI(this.api + "config/all", callback); + + return this; }; D3D.Box.prototype.setConfig = function (data, callback) { - //works "use strict"; var self = this; @@ -330,13 +332,13 @@ D3D.Box.prototype.setConfig = function (data, callback) { return this; }; D3D.Box.prototype.getInfo = function (callback) { - //works "use strict"; getAPI(this.api + "info", callback); + + return this; }; D3D.Box.prototype.getInfoStatus = function (callback) { - //works "use strict"; getAPI(this.api + "info/status", callback); @@ -351,7 +353,6 @@ D3D.Box.prototype.downloadInfoLogFiles = function () { window.location = this.api + "info/logfiles"; }; D3D.Box.prototype.getInfoAcces = function (callback) { - //works "use strict"; getAPI(this.api + "info/access", callback); @@ -359,7 +360,6 @@ D3D.Box.prototype.getInfoAcces = function (callback) { return this; }; D3D.Box.prototype.getNetworkScan = function (callback) { - //works "use strict"; getAPI(this.api + "network/scan", callback); @@ -367,7 +367,6 @@ D3D.Box.prototype.getNetworkScan = function (callback) { return this; }; D3D.Box.prototype.getNetworkKnown = function (callback) { - //works "use strict"; getAPI(this.api + "network/known", callback); @@ -375,7 +374,6 @@ D3D.Box.prototype.getNetworkKnown = function (callback) { return this; }; D3D.Box.prototype.getNetworkStatus = function (callback) { - //works "use strict"; getAPI(this.api + "network/status", callback); @@ -383,7 +381,6 @@ D3D.Box.prototype.getNetworkStatus = function (callback) { return this; }; D3D.Box.prototype.setNetworkAssosiate = function (data, callback) { - //works "use strict"; sendAPI(this.api + "network/associate", data, callback); @@ -407,17 +404,15 @@ D3D.Box.prototype.setNetworkOpenAP = function (callback) { return this; }; D3D.Box.prototype.setNetworkRemove = function (ssid, callback) { - //works "use strict"; sendAPI(this.api + "network/remove", { - ssid: ssid + "ssid": ssid }, callback); return this; }; D3D.Box.prototype.getNetworkSignin = function (callback) { - //works "use strict"; getAPI(this.api + "network/signin", callback); @@ -425,7 +420,6 @@ D3D.Box.prototype.getNetworkSignin = function (callback) { return this; }; D3D.Box.prototype.getNetworkAlive = function (callback) { - //works but returns empty array "use strict"; getAPI(this.api + "network/alive", callback); @@ -433,7 +427,6 @@ D3D.Box.prototype.getNetworkAlive = function (callback) { return this; }; D3D.Box.prototype.getPrinterTemperature = function (callback) { - //works "use strict"; getAPI(this.api + "printer/temperature", callback); @@ -441,7 +434,6 @@ D3D.Box.prototype.getPrinterTemperature = function (callback) { return this; }; D3D.Box.prototype.getPrinterProgress = function (callback) { - //works "use strict"; getAPI(this.api + "printer/progress", callback); @@ -449,7 +441,6 @@ D3D.Box.prototype.getPrinterProgress = function (callback) { return this; }; D3D.Box.prototype.getPrinterState = function (callback) { - //works "use strict"; getAPI(this.api + "printer/state", callback); @@ -457,7 +448,6 @@ D3D.Box.prototype.getPrinterState = function (callback) { return this; }; D3D.Box.prototype.getPrinterListAll = function (callback) { - //works "use strict"; getAPI(this.api + "printer/listall", callback); @@ -465,7 +455,6 @@ D3D.Box.prototype.getPrinterListAll = function (callback) { return this; }; D3D.Box.prototype.setPrinterHeatup = function (callback) { - //works "use strict"; sendAPI(this.api + "printer/heatup", {}, callback); @@ -473,7 +462,6 @@ D3D.Box.prototype.setPrinterHeatup = function (callback) { return this; }; D3D.Box.prototype.setPrinterPrint = function (data, callback) { - //works "use strict"; sendAPI(this.api + "printer/print", data, callback); @@ -481,7 +469,6 @@ D3D.Box.prototype.setPrinterPrint = function (data, callback) { return this; }; D3D.Box.prototype.setPrinterStop = function (data, callback) { - //works "use strict"; sendAPI(this.api + "printer/stop", data, callback); @@ -489,7 +476,6 @@ D3D.Box.prototype.setPrinterStop = function (data, callback) { return this; }; D3D.Box.prototype.getSketch = function (id, callback) { - //works "use strict"; getAPI(this.api + "sketch/?id=" + id, callback); @@ -497,7 +483,6 @@ D3D.Box.prototype.getSketch = function (id, callback) { return this; }; D3D.Box.prototype.setSketch = function (data, callback) { - //works "use strict"; sendAPI(this.api + "sketch", { @@ -507,7 +492,6 @@ D3D.Box.prototype.setSketch = function (data, callback) { return this; }; D3D.Box.prototype.getSketchStatus = function (callback) { - //works "use strict"; getAPI(this.api + "sketch/status", callback); @@ -515,7 +499,6 @@ D3D.Box.prototype.getSketchStatus = function (callback) { return this; }; D3D.Box.prototype.setSketchClear = function (callback) { - //works "use strict"; sendAPI(this.api + "sketch/clear", callback); @@ -523,7 +506,6 @@ D3D.Box.prototype.setSketchClear = function (callback) { return this; }; D3D.Box.prototype.getSystemVersions = function (callback) { - //works "use strict"; getAPI(this.api + "system/fwversions", callback); @@ -531,7 +513,6 @@ D3D.Box.prototype.getSystemVersions = function (callback) { return this; }; D3D.Box.prototype.getUpdateStatus = function (callback) { - //works "use strict"; getAPI(this.api + "update/status", callback); @@ -651,15 +632,18 @@ D3D.Slicer = function () { this.lines = []; }; -D3D.Slicer.prototype.setGeometry = function (geometry) { +D3D.Slicer.prototype.setGeometry = function (mesh) { "use strict"; + var geometry = mesh.geometry.clone(); + geometry.mergeVertices(); + geometry.applyMatrix(mesh.matrix); + if (geometry instanceof THREE.BufferGeometry) { geometry = new THREE.Geometry().fromBufferGeometry(geometry); } - this.geometry = geometry.clone(); - this.geometry.mergeVertices(); + this.geometry = geometry; this.createLines(); @@ -702,8 +686,6 @@ D3D.Slicer.prototype.createLines = function () { var c = addLine(face.c, face.a); //set connecting lines (based on face) - - //something wrong here, 3 face can go in different direction this.lines[a].connects.push(b, c); this.lines[b].connects.push(c, a); this.lines[c].connects.push(a, b); @@ -712,48 +694,53 @@ D3D.Slicer.prototype.createLines = function () { this.lines[b].normals.push(normal); this.lines[c].normals.push(normal); } - - //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 layersIntersections = []; + + for (var i = 0; i < this.lines.length; i ++) { + var line = this.lines[i]; + + var min = Math.ceil(Math.min(line.line.start.y, line.line.end.y) / step); + var max = Math.floor(Math.max(line.line.start.y, line.line.end.y) / step); + + for (var layerIndex = min; layerIndex <= max; layerIndex ++) { + if (layerIndex >= 0) { + if (layersIntersections[layerIndex] === undefined) { + layersIntersections[layerIndex] = []; + } + layersIntersections[layerIndex].push(i); + } + } + } + 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 = []; + for (var layer = 1; layer < layersIntersections.length; layer ++) { + var layerIntersections = layersIntersections[layer]; + var y = layer*step; var intersections = []; + for (var i = 0; i < layerIntersections.length; i ++) { + var index = layerIntersections[i]; + var line = this.lines[index].line; - for (var i = 0; i < this.lines.length; i ++) { - var line = this.lines[i].line; + var alpha = (y - line.start.y) / (line.end.y - line.start.y); + var x = line.start.x * alpha + line.end.x * (1 - alpha); + var z = line.start.z * alpha + line.end.z * (1 - alpha); - 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); - } + //remove +100 when implimenting good stucture for creating geometry is complete + intersections[index] = new THREE.Vector2(x + 100, z + 100); } var done = []; - for (var i = 0; i < intersections.length; i ++) { - - if (intersections[i] && done.indexOf(i) === -1) { - var index = i; + var slice = []; + for (var i = 0; i < layerIntersections.length; i ++) { + var index = layerIntersections[i]; + if (done.indexOf(index) === -1) { var shape = []; while (index !== -1) { @@ -788,7 +775,6 @@ D3D.Slicer.prototype.slice = function (height, step) { //think this check is not nescesary, always higher as 0 if (shape.length > 0) { - shape.push({X: shape[0].X, Y: shape[0].Y}); slice.push(shape); } } @@ -1059,7 +1045,7 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) { var layerHeight = printer.config["printer.layerHeight"]; var dimensionsZ = printer.config["printer.dimensions.z"]; - function drawPolygons (paths, color) { + function drawLines (paths, color) { context.fillStyle = color; context.strokeStyle = color; context.beginPath(); @@ -1078,8 +1064,23 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) { context.stroke(); } + function drawVertexes (paths, color) { + context.fillStyle = color; + context.strokeStyle = color; + + for (var i = 0; i < paths.length; i ++) { + var path = paths[i]; + + for (var j = 0; j < path.length; j ++) { + var point = path[j]; + context.beginPath(); + context.arc(point.X * 2, point.Y * 2, 1, 0, Math.PI*2, false); + context.stroke(); + } + } + } + var slices = this.slice(dimensionsZ, layerHeight); - slices.shift(); var data = this.slicesToData(slices, printer); @@ -1091,9 +1092,11 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) { for (var layer = min; layer < max; layer ++) { var slice = data[layer % data.length]; - drawPolygons(slice.outerLayer, "red"); - drawPolygons(slice.innerLayer, "green"); - drawPolygons(slice.fill, "blue"); + drawLines(slice.outerLayer, "red"); + //drawLines(slice.innerLayer, "green"); + //drawLines(slice.fill, "blue"); + + drawVertexes(slice.outerLayer, "green"); } return canvas; @@ -1104,13 +1107,21 @@ D3D.Slicer.prototype.getGcode = function (printer) { var layerHeight = printer.config["printer.layerHeight"]; var dimensionsZ = printer.config["printer.dimensions.z"]; + var start = new Date().getTime(); var slices = this.slice(dimensionsZ, layerHeight); - + var end = new Date().getTime(); + + console.log("Slicing: " + (end - start) + "ms"); + //still error in first layer, so remove first layer //see https://github.com/Doodle3D/Doodle3D-Slicer/issues/1 - slices.shift(); + var start = new Date().getTime(); var data = this.slicesToData(slices, printer); + var end = new Date().getTime(); + + console.log("Data: " + (end - start) + "ms"); + //return data; //TODO @@ -1118,6 +1129,11 @@ D3D.Slicer.prototype.getGcode = function (printer) { //make the printer follow the shortest path from line to line //see https://github.com/Ultimaker/CuraEngine#gcode-generation + var start = new Date().getTime(); var gcode = this.dataToGcode(data, printer); + var end = new Date().getTime(); + + console.log("Gcode: " + (end - start) + "ms"); + return gcode; }; \ No newline at end of file diff --git a/build/d3d.min.js b/build/d3d.min.js index 3650831..885acf5 100644 --- a/build/d3d.min.js +++ b/build/d3d.min.js @@ -1 +1 @@ -function sendAPI(t,e,i){"use strict";$.ajax({url:t,type:"POST",data:e,dataType:"json",timeout:1e4,success:function(t){"success"===t.status?void 0!==i&&i(t.data):console.warn(t.msg)}}).fail(function(){console.warn("failed connecting to "+t),sendAPI(t,e,i)})}function getAPI(t,e){"use strict";$.ajax({url:t,dataType:"json",timeout:5e3,success:function(t){"success"===t.status?void 0!==e&&e(t.data):console.warn(t.msg)}}).fail(function(){console.warn("failed connecting to "+t),getAPI(t,e)})}function downloadFile(t,e){"use strict";$(document.createElement("a")).attr({download:t,href:"data:text/plain,"+e})[0].click()}function applyMouseControls(t,e,i){"use strict";function r(){e.position.x=Math.cos(s)*Math.sin(o)*n,e.position.y=Math.sin(s)*n,e.position.z=Math.cos(s)*Math.cos(o)*n,e.lookAt(new THREE.Vector3(0,0,0))}var n=20,o=0,s=0,p=!1;$(t.domElement).on("mousedown",function(t){p=!0}).on("wheel",function(t){var e=t.originalEvent;e.preventDefault(),n=THREE.Math.clamp(n-e.wheelDelta,1,i),r()}),$(window).on("mouseup",function(t){p=!1}).on("mousemove",function(t){var e=t.originalEvent;p===!0&&(o=(o-e.webkitMovementX/100)%(2*Math.PI),s=THREE.Math.clamp(s+e.webkitMovementY/100,-Math.PI/2,Math.PI/2),r())}),r()}var D3D={version:"0.1",website:"http://www.doodle3d.com/",contact:"develop@doodle3d.com"};THREE.Vector2.prototype.normal=function(){"use strict";var t=this.y,e=-this.x;return this.set(t,e)},Array.prototype.clone=function(){"use strict";for(var t=[],e=0;e0?this.printBatch():this.updateState()},D3D.Box.prototype.updateState=function(){"use strict";var t=this;this.getInfoStatus(function(e){t.printer.status=e,void 0!==t.onupdate&&t.onupdate(e),t.update()})},D3D.Box.prototype.print=function(t){"use strict";for(this.currentBatch=0,t=t.clone();t.length>0;){var e=t.splice(0,Math.min(this.batchSize,t.length));this.printBatches.push(e)}return this},D3D.Box.prototype.printBatch=function(){"use strict";var t=this,e=this.printBatches.shift();this.setPrinterPrint({start:0===this.currentBatch?!0:!1,first:0===this.currentBatch?!0:!1,gcode:e.join("\n")},function(e){console.log("batch sent: "+t.currentBatch,e),t.printBatches.length>0&&t.currentBatch++,t.updateState()})},D3D.Box.prototype.stopPrint=function(){"use strict";this.printBatches=[],this.currentBatch=0;var t=["M107 ;fan off","G91 ;relative positioning","G1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure","G1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more","G28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way","M84 ;disable axes / steppers","G90 ;absolute positioning","M104 S180",";M140 S70","M117 Done ;display message (20 characters to clear whole screen)"];return this.setPrinterStop({gcode:t.join("\n")},function(t){console.log("Printer stop command sent")}),this},D3D.Box.prototype.getConfig=function(t,e){"use strict";getAPI(this.api+"config/?"+t.join("=&")+"=",e)},D3D.Box.prototype.getConfigAll=function(t){"use strict";getAPI(this.api+"config/all",t)},D3D.Box.prototype.setConfig=function(t,e){"use strict";var i=this;return sendAPI(this.api+"config",t,function(r){for(var n in r.validation)"ok"!==r.validation[n]&&delete t[n];i.updateConfig(t),i.printer.updateConfig(t),void 0!==e&&e(r)}),this},D3D.Box.prototype.getInfo=function(t){"use strict";getAPI(this.api+"info",t)},D3D.Box.prototype.getInfoStatus=function(t){"use strict";return getAPI(this.api+"info/status",t),this},D3D.Box.prototype.downloadInfoLogFiles=function(){"use strict";window.location=this.api+"info/logfiles"},D3D.Box.prototype.getInfoAcces=function(t){"use strict";return getAPI(this.api+"info/access",t),this},D3D.Box.prototype.getNetworkScan=function(t){"use strict";return getAPI(this.api+"network/scan",t),this},D3D.Box.prototype.getNetworkKnown=function(t){"use strict";return getAPI(this.api+"network/known",t),this},D3D.Box.prototype.getNetworkStatus=function(t){"use strict";return getAPI(this.api+"network/status",t),this},D3D.Box.prototype.setNetworkAssosiate=function(t,e){"use strict";return sendAPI(this.api+"network/associate",t,e),this},D3D.Box.prototype.setNetworkDisassosiate=function(t){"use strict";return sendAPI(this.api+"network/disassociate",{},t),this},D3D.Box.prototype.setNetworkOpenAP=function(t){"use strict";return sendAPI(this.api+"network/openap",{},t),this},D3D.Box.prototype.setNetworkRemove=function(t,e){"use strict";return sendAPI(this.api+"network/remove",{ssid:t},e),this},D3D.Box.prototype.getNetworkSignin=function(t){"use strict";return getAPI(this.api+"network/signin",t),this},D3D.Box.prototype.getNetworkAlive=function(t){"use strict";return getAPI(this.api+"network/alive",t),this},D3D.Box.prototype.getPrinterTemperature=function(t){"use strict";return getAPI(this.api+"printer/temperature",t),this},D3D.Box.prototype.getPrinterProgress=function(t){"use strict";return getAPI(this.api+"printer/progress",t),this},D3D.Box.prototype.getPrinterState=function(t){"use strict";return getAPI(this.api+"printer/state",t),this},D3D.Box.prototype.getPrinterListAll=function(t){"use strict";return getAPI(this.api+"printer/listall",t),this},D3D.Box.prototype.setPrinterHeatup=function(t){"use strict";return sendAPI(this.api+"printer/heatup",{},t),this},D3D.Box.prototype.setPrinterPrint=function(t,e){"use strict";return sendAPI(this.api+"printer/print",t,e),this},D3D.Box.prototype.setPrinterStop=function(t,e){"use strict";return sendAPI(this.api+"printer/stop",t,e),this},D3D.Box.prototype.getSketch=function(t,e){"use strict";return getAPI(this.api+"sketch/?id="+t,e),this},D3D.Box.prototype.setSketch=function(t,e){"use strict";return sendAPI(this.api+"sketch",{data:t},e),this},D3D.Box.prototype.getSketchStatus=function(t){"use strict";return getAPI(this.api+"sketch/status",t),this},D3D.Box.prototype.setSketchClear=function(t){"use strict";return sendAPI(this.api+"sketch/clear",t),this},D3D.Box.prototype.getSystemVersions=function(t){"use strict";return getAPI(this.api+"system/fwversions",t),this},D3D.Box.prototype.getUpdateStatus=function(t){"use strict";return getAPI(this.api+"update/status",t),this},D3D.Box.prototype.setUpdateDownload=function(t){"use strict";return sendAPI(this.api+"update/download",{},t),this},D3D.Box.prototype.setUpdateInstall=function(t){"use strict";return sendAPI(this.api+"update/install",{},t),this},D3D.Box.prototype.setUpdateClear=function(t){"use strict";return sendAPI(this.api+"update/clear",{},t),this},D3D.Printer=function(t){"use strict";this.status={},this.config={},this.updateConfig(t)},D3D.Printer.prototype.updateConfig=function(t){"use strict";for(var e in t)0===e.indexOf("printer")&&(this.config[e]=t[e]);return this},D3D.Printer.prototype.getStartCode=function(){"use strict";var t=this.config["printer.startcode"];return t=this.subsituteVariables(t),t.split("\n")},D3D.Printer.prototype.getEndCode=function(){"use strict";var t=this.config["printer.endcode"];return t=this.subsituteVariables(t),t.split("\n")},D3D.Printer.prototype.subsituteVariables=function(t){"use strict";var e=this.config["printer.temperature"],i=this.config["printer.bed.temperature"],r=this.config["printer.heatup.temperature"],n=this.config["printer.heatup.bed.temperature"],o=this.config["printer.type"],s=this.config["printer.heatedbed"];switch(o){case"makerbot_replicator2":o="r2";break;case"makerbot_replicator2x":o="r2x";break;case"makerbot_thingomatic":o="t6";break;case"makerbot_generic":o="r2";break;case"_3Dison_plus":o="r2"}var p=s?"":";";return t=t.replace(/{printingTemp}/gi,e),t=t.replace(/{printingBedTemp}/gi,i),t=t.replace(/{preheatTemp}/gi,r),t=t.replace(/{preheatBedTemp}/gi,n),t=t.replace(/{printerType}/gi,o),t=t.replace(/{if heatedBed}/gi,p)},D3D.Slicer=function(){"use strict";this.lines=[]},D3D.Slicer.prototype.setGeometry=function(t){"use strict";return t instanceof THREE.BufferGeometry&&(t=(new THREE.Geometry).fromBufferGeometry(t)),this.geometry=t.clone(),this.geometry.mergeVertices(),this.createLines(),this},D3D.Slicer.prototype.createLines=function(){"use strict";function t(t,r){var n=e[t+"_"+r]||e[r+"_"+t];return void 0===n&&(n=i.lines.length,e[t+"_"+r]=n,i.lines.push({line:new THREE.Line3(i.geometry.vertices[t],i.geometry.vertices[r]),connects:[],normals:[]})),n}this.lines=[];for(var e={},i=this,r=0;rn;n+=e){r.set(new THREE.Vector3(0,-1,0),n);for(var o=[],s=[],p=0;p0)break;l=-1}else l=-1}f.length>0&&(f.push({X:f[0].X,Y:f[0].Y}),o.push(f))}if(!(o.length>0))break;i.push(o)}return i},D3D.Slicer.prototype.getInset=function(t,e){"use strict";var i=new ClipperLib.Paths,r=new ClipperLib.ClipperOffset(1,1);return r.AddPaths(t,ClipperLib.JoinType.jtRound,ClipperLib.EndType.etClosedPolygon),r.Execute(i,-e),i},D3D.Slicer.prototype.getFillTemplate=function(t,e,i,r){"use strict";var n=new ClipperLib.Paths;if(i)for(var o=0;t>=o;o+=e)n.push([{X:o,Y:0},{X:o,Y:t}]);if(r)for(var o=0;t>=o;o+=e)n.push([{X:0,Y:o},{X:t,Y:o}]);return n},D3D.Slicer.prototype.slicesToData=function(t,e){"use strict";for(var i=100,r=e.config["printer.layerHeight"]*i,n=e.config["printer.dimensions.z"]*i,o=e.config["printer.wallThickness"]*i,s=e.config["printer.shellThickness"]*i,p=e.config["printer.fillSize"]*i,a=(e.config["printer.brimOffset"]*i,[]),c=this.getFillTemplate(n,p,!0,!0),u=0;ud;d+=o){var g=this.getInset(l,d);f=f.concat(g)}for(var D=g||l,y=!1,d=1;s/r>d;d++){var P=ClipperLib.JS.Clone(t[u+d]);if(ClipperLib.JS.ScaleUpPaths(P,i),0===P.length||y&&0===y.length){y=[];break}if(y===!1)y=P;else{var m=new ClipperLib.Clipper,v=new ClipperLib.Paths;m.AddPaths(P,ClipperLib.PolyType.ptSubject,!0),m.AddPaths(y,ClipperLib.PolyType.ptClip,!0),m.Execute(ClipperLib.ClipType.ctIntersection,v),y=v}}var b=new ClipperLib.Clipper,w=new ClipperLib.Paths;b.AddPaths(D,ClipperLib.PolyType.ptSubject,!0),b.AddPaths(y,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctDifference,w);var b=new ClipperLib.Clipper,x=new ClipperLib.Paths;b.AddPaths(D,ClipperLib.PolyType.ptSubject,!0),b.AddPaths(w,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctDifference,x);var C=[],b=new ClipperLib.Clipper,B=new ClipperLib.Paths;b.AddPaths(c,ClipperLib.PolyType.ptSubject,!1),b.AddPaths(x,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctIntersection,B),C=C.concat(B);var A=this.getFillTemplate(n,o,u%2===0,u%2===1),b=new ClipperLib.Clipper,S=new ClipperLib.Paths;b.AddPaths(A,ClipperLib.PolyType.ptSubject,!1),b.AddPaths(w,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctIntersection,S),C=C.concat(S),ClipperLib.JS.ScaleDownPaths(l,i),ClipperLib.JS.ScaleDownPaths(f,i),ClipperLib.JS.ScaleDownPaths(C,i),a.push({outerLayer:l,innerLayer:f,fill:C})}return a},D3D.Slicer.prototype.dataToGcode=function(t,e){"use strict";function i(t){for(var e=[],i=0;if&&h&&e.push(["G0","E"+(D-d).toFixed(3),"F"+(60*l).toFixed(3)].join(" ")),e.push(["G0","X"+p.X.toFixed(3)+" Y"+p.Y.toFixed(3)+" Z"+w,"F"+60*a].join(" ")),D>f&&h&&e.push(["G0","E"+D.toFixed(3),"F"+(60*l).toFixed(3)].join(" "));else{var c=(new THREE.Vector2).set(p.X,p.Y),g=(new THREE.Vector2).set(n.X,n.Y),v=c.distanceTo(g);D+=v*u*r/P*m,e.push(["G1","X"+p.X.toFixed(3)+" Y"+p.Y.toFixed(3)+" Z"+w,"F"+y,"E"+D.toFixed(3)].join(" "))}n=p}return e}for(var r=e.config["printer.layerHeight"],n=e.config["printer.speed"],o=e.config["printer.bottomLayerSpeed"],s=e.config["printer.firstLayerSlow"],p=e.config["printer.bottomFlowRate"],a=e.config["printer.travelSpeed"],c=e.config["printer.filamentThickness"],u=e.config["printer.wallThickness"],h=(e.config["printer.enableTraveling"],e.config["printer.retraction.enabled"]),l=e.config["printer.retraction.speed"],f=e.config["printer.retraction.minDistance"],d=e.config["printer.retraction.amount"],g=e.getStartCode(),D=0,y=s?(60*o).toFixed(3):(60*n).toFixed(3),P=Math.pow(c/2,2)*Math.PI,m=p,v=0;vu;u++){var h=p[u%p.length];r(h.outerLayer,"red"),r(h.innerLayer,"green"),r(h.fill,"blue")}return a},D3D.Slicer.prototype.getGcode=function(t){"use strict";var e=t.config["printer.layerHeight"],i=t.config["printer.dimensions.z"],r=this.slice(i,e);r.shift();var n=this.slicesToData(r,t),o=this.dataToGcode(n,t);return o}; \ No newline at end of file +function sendAPI(t,e,i){"use strict";$.ajax({url:t,type:"POST",data:e,dataType:"json",timeout:1e4,success:function(t){"success"===t.status?void 0!==i&&i(t.data):console.warn(t.msg)}}).fail(function(){console.warn("Failed connecting to "+t),sendAPI(t,e,i)})}function getAPI(t,e){"use strict";$.ajax({url:t,dataType:"json",timeout:5e3,success:function(t){"success"===t.status?void 0!==e&&e(t.data):console.warn(t.msg)}}).fail(function(){console.warn("Failed connecting to "+t),getAPI(t,e)})}function downloadFile(t,e){"use strict";$(document.createElement("a")).attr({download:t,href:"data:text/plain,"+e})[0].click()}function applyMouseControls(t,e,i){"use strict";function r(){e.position.x=Math.cos(s)*Math.sin(o)*n,e.position.y=Math.sin(s)*n,e.position.z=Math.cos(s)*Math.cos(o)*n,e.lookAt(new THREE.Vector3(0,0,0))}var n=20,o=0,s=0,a=!1;$(t.domElement).on("mousedown",function(t){a=!0}).on("wheel",function(t){var e=t.originalEvent;e.preventDefault(),n=THREE.Math.clamp(n-e.wheelDelta,1,i),r()}),$(window).on("mouseup",function(t){a=!1}).on("mousemove",function(t){var e=t.originalEvent;a===!0&&(o=(o-e.webkitMovementX/100)%(2*Math.PI),s=THREE.Math.clamp(s+e.webkitMovementY/100,-Math.PI/2,Math.PI/2),r())}),r()}var D3D={version:"0.1",website:"http://www.doodle3d.com/",contact:"develop@doodle3d.com"};THREE.Vector2.prototype.normal=function(){"use strict";var t=this.y,e=-this.x;return this.set(t,e)},Array.prototype.clone=function(){"use strict";for(var t=[],e=0;e0&&this.printer.status.buffered_lines+this.batchSize<=this.maxBufferedLines?this.printBatch():this.updateState()},D3D.Box.prototype.updateState=function(){"use strict";var t=this;this.getInfoStatus(function(e){t.printer.status=e,void 0!==t.onupdate&&t.onupdate(e),t.update()})},D3D.Box.prototype.print=function(t){"use strict";for(this.currentBatch=0,t=t.clone();t.length>0;){var e=t.splice(0,Math.min(this.batchSize,t.length));this.printBatches.push(e)}return this},D3D.Box.prototype.printBatch=function(){"use strict";var t=this,e=this.printBatches.shift();this.setPrinterPrint({start:0===this.currentBatch?!0:!1,first:0===this.currentBatch?!0:!1,gcode:e.join("\n")},function(e){console.log("batch sent: "+t.currentBatch,e),t.printBatches.length>0&&t.currentBatch++,t.updateState()})},D3D.Box.prototype.stopPrint=function(){"use strict";this.printBatches=[],this.currentBatch=0;var t=["M107 ;fan off","G91 ;relative positioning","G1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure","G1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more","G28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way","M84 ;disable axes / steppers","G90 ;absolute positioning","M104 S180",";M140 S70","M117 Done ;display message (20 characters to clear whole screen)"];return this.setPrinterStop({gcode:t.join("\n")},function(t){console.log("Printer stop command sent")}),this},D3D.Box.prototype.getConfig=function(t,e){"use strict";return getAPI(this.api+"config/?"+t.join("=&")+"=",e),this},D3D.Box.prototype.getConfigAll=function(t){"use strict";return getAPI(this.api+"config/all",t),this},D3D.Box.prototype.setConfig=function(t,e){"use strict";var i=this;return sendAPI(this.api+"config",t,function(r){for(var n in r.validation)"ok"!==r.validation[n]&&delete t[n];i.updateConfig(t),i.printer.updateConfig(t),void 0!==e&&e(r)}),this},D3D.Box.prototype.getInfo=function(t){"use strict";return getAPI(this.api+"info",t),this},D3D.Box.prototype.getInfoStatus=function(t){"use strict";return getAPI(this.api+"info/status",t),this},D3D.Box.prototype.downloadInfoLogFiles=function(){"use strict";window.location=this.api+"info/logfiles"},D3D.Box.prototype.getInfoAcces=function(t){"use strict";return getAPI(this.api+"info/access",t),this},D3D.Box.prototype.getNetworkScan=function(t){"use strict";return getAPI(this.api+"network/scan",t),this},D3D.Box.prototype.getNetworkKnown=function(t){"use strict";return getAPI(this.api+"network/known",t),this},D3D.Box.prototype.getNetworkStatus=function(t){"use strict";return getAPI(this.api+"network/status",t),this},D3D.Box.prototype.setNetworkAssosiate=function(t,e){"use strict";return sendAPI(this.api+"network/associate",t,e),this},D3D.Box.prototype.setNetworkDisassosiate=function(t){"use strict";return sendAPI(this.api+"network/disassociate",{},t),this},D3D.Box.prototype.setNetworkOpenAP=function(t){"use strict";return sendAPI(this.api+"network/openap",{},t),this},D3D.Box.prototype.setNetworkRemove=function(t,e){"use strict";return sendAPI(this.api+"network/remove",{ssid:t},e),this},D3D.Box.prototype.getNetworkSignin=function(t){"use strict";return getAPI(this.api+"network/signin",t),this},D3D.Box.prototype.getNetworkAlive=function(t){"use strict";return getAPI(this.api+"network/alive",t),this},D3D.Box.prototype.getPrinterTemperature=function(t){"use strict";return getAPI(this.api+"printer/temperature",t),this},D3D.Box.prototype.getPrinterProgress=function(t){"use strict";return getAPI(this.api+"printer/progress",t),this},D3D.Box.prototype.getPrinterState=function(t){"use strict";return getAPI(this.api+"printer/state",t),this},D3D.Box.prototype.getPrinterListAll=function(t){"use strict";return getAPI(this.api+"printer/listall",t),this},D3D.Box.prototype.setPrinterHeatup=function(t){"use strict";return sendAPI(this.api+"printer/heatup",{},t),this},D3D.Box.prototype.setPrinterPrint=function(t,e){"use strict";return sendAPI(this.api+"printer/print",t,e),this},D3D.Box.prototype.setPrinterStop=function(t,e){"use strict";return sendAPI(this.api+"printer/stop",t,e),this},D3D.Box.prototype.getSketch=function(t,e){"use strict";return getAPI(this.api+"sketch/?id="+t,e),this},D3D.Box.prototype.setSketch=function(t,e){"use strict";return sendAPI(this.api+"sketch",{data:t},e),this},D3D.Box.prototype.getSketchStatus=function(t){"use strict";return getAPI(this.api+"sketch/status",t),this},D3D.Box.prototype.setSketchClear=function(t){"use strict";return sendAPI(this.api+"sketch/clear",t),this},D3D.Box.prototype.getSystemVersions=function(t){"use strict";return getAPI(this.api+"system/fwversions",t),this},D3D.Box.prototype.getUpdateStatus=function(t){"use strict";return getAPI(this.api+"update/status",t),this},D3D.Box.prototype.setUpdateDownload=function(t){"use strict";return sendAPI(this.api+"update/download",{},t),this},D3D.Box.prototype.setUpdateInstall=function(t){"use strict";return sendAPI(this.api+"update/install",{},t),this},D3D.Box.prototype.setUpdateClear=function(t){"use strict";return sendAPI(this.api+"update/clear",{},t),this},D3D.Printer=function(t){"use strict";this.status={},this.config={},this.updateConfig(t)},D3D.Printer.prototype.updateConfig=function(t){"use strict";for(var e in t)0===e.indexOf("printer")&&(this.config[e]=t[e]);return this},D3D.Printer.prototype.getStartCode=function(){"use strict";var t=this.config["printer.startcode"];return t=this.subsituteVariables(t),t.split("\n")},D3D.Printer.prototype.getEndCode=function(){"use strict";var t=this.config["printer.endcode"];return t=this.subsituteVariables(t),t.split("\n")},D3D.Printer.prototype.subsituteVariables=function(t){"use strict";var e=this.config["printer.temperature"],i=this.config["printer.bed.temperature"],r=this.config["printer.heatup.temperature"],n=this.config["printer.heatup.bed.temperature"],o=this.config["printer.type"],s=this.config["printer.heatedbed"];switch(o){case"makerbot_replicator2":o="r2";break;case"makerbot_replicator2x":o="r2x";break;case"makerbot_thingomatic":o="t6";break;case"makerbot_generic":o="r2";break;case"_3Dison_plus":o="r2"}var a=s?"":";";return t=t.replace(/{printingTemp}/gi,e),t=t.replace(/{printingBedTemp}/gi,i),t=t.replace(/{preheatTemp}/gi,r),t=t.replace(/{preheatBedTemp}/gi,n),t=t.replace(/{printerType}/gi,o),t=t.replace(/{if heatedBed}/gi,a)},D3D.Slicer=function(){"use strict";this.lines=[]},D3D.Slicer.prototype.setGeometry=function(t){"use strict";var e=t.geometry.clone();return e.mergeVertices(),e.applyMatrix(t.matrix),e instanceof THREE.BufferGeometry&&(e=(new THREE.Geometry).fromBufferGeometry(e)),this.geometry=e,this.createLines(),this},D3D.Slicer.prototype.createLines=function(){"use strict";function t(t,r){var n=e[t+"_"+r]||e[r+"_"+t];return void 0===n&&(n=i.lines.length,e[t+"_"+r]=n,i.lines.push({line:new THREE.Line3(i.geometry.vertices[t],i.geometry.vertices[r]),connects:[],normals:[]})),n}this.lines=[];for(var e={},i=this,r=0;r=a;a++)a>=0&&(void 0===i[a]&&(i[a]=[]),i[a].push(r));for(var p=[],c=1;c0)break;f=-1}else f=-1}v.length>0&&m.push(v)}}if(!(m.length>0))break;p.push(m)}return p},D3D.Slicer.prototype.getInset=function(t,e){"use strict";var i=new ClipperLib.Paths,r=new ClipperLib.ClipperOffset(1,1);return r.AddPaths(t,ClipperLib.JoinType.jtRound,ClipperLib.EndType.etClosedPolygon),r.Execute(i,-e),i},D3D.Slicer.prototype.getFillTemplate=function(t,e,i,r){"use strict";var n=new ClipperLib.Paths;if(i)for(var o=0;t>=o;o+=e)n.push([{X:o,Y:0},{X:o,Y:t}]);if(r)for(var o=0;t>=o;o+=e)n.push([{X:0,Y:o},{X:t,Y:o}]);return n},D3D.Slicer.prototype.slicesToData=function(t,e){"use strict";for(var i=100,r=e.config["printer.layerHeight"]*i,n=e.config["printer.dimensions.z"]*i,o=e.config["printer.wallThickness"]*i,s=e.config["printer.shellThickness"]*i,a=e.config["printer.fillSize"]*i,p=(e.config["printer.brimOffset"]*i,[]),c=this.getFillTemplate(n,a,!0,!0),u=0;ud;d+=o){var g=this.getInset(h,d);f=f.concat(g)}for(var D=g||h,y=!1,d=1;s/r>d;d++){var m=ClipperLib.JS.Clone(t[u+d]);if(ClipperLib.JS.ScaleUpPaths(m,i),0===m.length||y&&0===y.length){y=[];break}if(y===!1)y=m;else{var v=new ClipperLib.Clipper,P=new ClipperLib.Paths;v.AddPaths(m,ClipperLib.PolyType.ptSubject,!0),v.AddPaths(y,ClipperLib.PolyType.ptClip,!0),v.Execute(ClipperLib.ClipType.ctIntersection,P),y=P}}var b=new ClipperLib.Clipper,w=new ClipperLib.Paths;b.AddPaths(D,ClipperLib.PolyType.ptSubject,!0),b.AddPaths(y,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctDifference,w);var b=new ClipperLib.Clipper,x=new ClipperLib.Paths;b.AddPaths(D,ClipperLib.PolyType.ptSubject,!0),b.AddPaths(w,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctDifference,x);var C=[],b=new ClipperLib.Clipper,B=new ClipperLib.Paths;b.AddPaths(c,ClipperLib.PolyType.ptSubject,!1),b.AddPaths(x,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctIntersection,B),C=C.concat(B);var S=this.getFillTemplate(n,o,u%2===0,u%2===1),b=new ClipperLib.Clipper,T=new ClipperLib.Paths;b.AddPaths(S,ClipperLib.PolyType.ptSubject,!1),b.AddPaths(w,ClipperLib.PolyType.ptClip,!0),b.Execute(ClipperLib.ClipType.ctIntersection,T),C=C.concat(T),ClipperLib.JS.ScaleDownPaths(h,i),ClipperLib.JS.ScaleDownPaths(f,i),ClipperLib.JS.ScaleDownPaths(C,i),p.push({outerLayer:h,innerLayer:f,fill:C})}return p},D3D.Slicer.prototype.dataToGcode=function(t,e){"use strict";function i(t){for(var e=[],i=0;if&&l&&e.push(["G0","E"+(D-d).toFixed(3),"F"+(60*h).toFixed(3)].join(" ")),e.push(["G0","X"+a.X.toFixed(3)+" Y"+a.Y.toFixed(3)+" Z"+w,"F"+60*p].join(" ")),D>f&&l&&e.push(["G0","E"+D.toFixed(3),"F"+(60*h).toFixed(3)].join(" "));else{var c=(new THREE.Vector2).set(a.X,a.Y),g=(new THREE.Vector2).set(n.X,n.Y),P=c.distanceTo(g);D+=P*u*r/m*v,e.push(["G1","X"+a.X.toFixed(3)+" Y"+a.Y.toFixed(3)+" Z"+w,"F"+y,"E"+D.toFixed(3)].join(" "))}n=a}return e}for(var r=e.config["printer.layerHeight"],n=e.config["printer.speed"],o=e.config["printer.bottomLayerSpeed"],s=e.config["printer.firstLayerSlow"],a=e.config["printer.bottomFlowRate"],p=e.config["printer.travelSpeed"],c=e.config["printer.filamentThickness"],u=e.config["printer.wallThickness"],l=(e.config["printer.enableTraveling"],e.config["printer.retraction.enabled"]),h=e.config["printer.retraction.speed"],f=e.config["printer.retraction.minDistance"],d=e.config["printer.retraction.amount"],g=e.getStartCode(),D=0,y=s?(60*o).toFixed(3):(60*n).toFixed(3),m=Math.pow(c/2,2)*Math.PI,v=a,P=0;Pl;l++){var h=p[l%p.length];r(h.outerLayer,"red"),n(h.outerLayer,"green")}return c},D3D.Slicer.prototype.getGcode=function(t){"use strict";var e=t.config["printer.layerHeight"],i=t.config["printer.dimensions.z"],r=(new Date).getTime(),n=this.slice(i,e),o=(new Date).getTime();console.log("Slicing: "+(o-r)+"ms");var r=(new Date).getTime(),s=this.slicesToData(n,t),o=(new Date).getTime();console.log("Data: "+(o-r)+"ms");var r=(new Date).getTime(),a=this.dataToGcode(s,t),o=(new Date).getTime();return console.log("Gcode: "+(o-r)+"ms"),a}; \ No newline at end of file diff --git a/index.html b/index.html index ceaac8c..324bb6f 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,8 @@ Doedel Drie Dee - + + diff --git a/slice_test.html b/slice_test.html index 8a31e81..1952e8e 100644 --- a/slice_test.html +++ b/slice_test.html @@ -65,8 +65,8 @@ var printerConfig = { }; var printer = new D3D.Printer(printerConfig); -var localIp = location.hash.substring(1); -var doodleBox = new D3D.Box(localIp); +//var localIp = location.hash.substring(1); +//var doodleBox = new D3D.Box(localIp); //var printer = doodleBox.printer; var scene = new THREE.Scene(); @@ -102,8 +102,8 @@ var geometry = (function () { return geometry; })(); -var material = new THREE.MeshLambertMaterial({color: 0x000000, wireframe: true}); -//var geometry = new THREE.TorusGeometry(20, 10, 30, 30); +var material = new THREE.MeshBasicMaterial({color: 0x000000, wireframe: true}); +var geometry = new THREE.TorusGeometry(20, 10, 30, 30); //var geometry = new THREE.BoxGeometry(20, 5, 20, 1, 1, 1); var geometry = new THREE.SphereGeometry(10, 10, 10); var mesh = new THREE.Mesh(geometry, material); @@ -112,7 +112,7 @@ scene.add(mesh); var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); -var slicer = new D3D.Slicer().setGeometry(geometry); +var slicer = new D3D.Slicer().setGeometry(mesh); gcode = slicer.getGcode(printer); @@ -120,8 +120,8 @@ var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); var layer = 0; -//var img = slicer.drawPaths(printer, layer, layer + 1); -//context.drawImage(img, 0, 0); +var img = slicer.drawPaths(printer, layer, layer + 1); +context.drawImage(img, 0, 0); /* var loader = new THREE.STLLoader(); diff --git a/src/slicer.js b/src/slicer.js index ef1d849..f20d90f 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -17,15 +17,18 @@ D3D.Slicer = function () { this.lines = []; }; -D3D.Slicer.prototype.setGeometry = function (geometry) { +D3D.Slicer.prototype.setGeometry = function (mesh) { "use strict"; + var geometry = mesh.geometry.clone(); + geometry.mergeVertices(); + geometry.applyMatrix(mesh.matrix); + if (geometry instanceof THREE.BufferGeometry) { geometry = new THREE.Geometry().fromBufferGeometry(geometry); } - this.geometry = geometry.clone(); - this.geometry.mergeVertices(); + this.geometry = geometry; this.createLines(); @@ -68,8 +71,6 @@ D3D.Slicer.prototype.createLines = function () { var c = addLine(face.c, face.a); //set connecting lines (based on face) - - //something wrong here, 3 face can go in different direction this.lines[a].connects.push(b, c); this.lines[b].connects.push(c, a); this.lines[c].connects.push(a, b); @@ -78,16 +79,10 @@ D3D.Slicer.prototype.createLines = function () { this.lines[b].normals.push(normal); this.lines[c].normals.push(normal); } - - //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"; - //something for optimalization... var layersIntersections = []; for (var i = 0; i < this.lines.length; i ++) { @@ -108,19 +103,21 @@ D3D.Slicer.prototype.slice = function (height, step) { var slices = []; - var plane = new THREE.Plane(); - for (var layer = 1; layer < layersIntersections.length; layer ++) { var layerIntersections = layersIntersections[layer]; - var z = layer*step; - plane.set(new THREE.Vector3(0, -1, 0), z); + var y = layer*step; var intersections = []; for (var i = 0; i < layerIntersections.length; i ++) { var index = layerIntersections[i]; var line = this.lines[index].line; - var intersection = plane.intersectLine(line); - intersections[index] = new THREE.Vector2(intersection.x + 100, intersection.z + 100); + + var alpha = (y - line.start.y) / (line.end.y - line.start.y); + var x = line.start.x * alpha + line.end.x * (1 - alpha); + var z = line.start.z * alpha + line.end.z * (1 - alpha); + + //remove +100 when implimenting good stucture for creating geometry is complete + intersections[index] = new THREE.Vector2(x + 100, z + 100); } var done = []; @@ -433,7 +430,7 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) { var layerHeight = printer.config["printer.layerHeight"]; var dimensionsZ = printer.config["printer.dimensions.z"]; - function drawPolygons (paths, color) { + function drawLines (paths, color) { context.fillStyle = color; context.strokeStyle = color; context.beginPath(); @@ -452,11 +449,23 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) { context.stroke(); } - var start = new Date().getTime(); + function drawVertexes (paths, color) { + context.fillStyle = color; + context.strokeStyle = color; + + for (var i = 0; i < paths.length; i ++) { + var path = paths[i]; + + for (var j = 0; j < path.length; j ++) { + var point = path[j]; + context.beginPath(); + context.arc(point.X * 2, point.Y * 2, 1, 0, Math.PI*2, false); + context.stroke(); + } + } + } + var slices = this.slice(dimensionsZ, layerHeight); - slices.shift(); - var end = new Date().getTime(); - console.log(end - start); var data = this.slicesToData(slices, printer); @@ -468,9 +477,11 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) { for (var layer = min; layer < max; layer ++) { var slice = data[layer % data.length]; - drawPolygons(slice.outerLayer, "red"); - drawPolygons(slice.innerLayer, "green"); - drawPolygons(slice.fill, "blue"); + drawLines(slice.outerLayer, "red"); + //drawLines(slice.innerLayer, "green"); + //drawLines(slice.fill, "blue"); + + drawVertexes(slice.outerLayer, "green"); } return canvas; @@ -489,7 +500,6 @@ D3D.Slicer.prototype.getGcode = function (printer) { //still error in first layer, so remove first layer //see https://github.com/Doodle3D/Doodle3D-Slicer/issues/1 - slices.shift(); var start = new Date().getTime(); var data = this.slicesToData(slices, printer);