From 6d69a9f306c78fc85d42052be1b5c0544aee2c8b Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 15 Jun 2015 17:22:36 +0200 Subject: [PATCH] fixed bug https://github.com/Doodle3D/Doodle3D-Slicer/issues/3 --- .gitignore | 2 + src/slicer.js | 56 +++-- .../editor_slicer/js/Sidebar.Printer.js | 207 ++++++++++++++++++ 3 files changed, 250 insertions(+), 15 deletions(-) create mode 100644 three.js-master/editor_slicer/js/Sidebar.Printer.js diff --git a/.gitignore b/.gitignore index 5a44eb4..ff9e52f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ src/oldcode.js test.html print_manager.html + +testdata.json diff --git a/src/slicer.js b/src/slicer.js index 8205bd1..8a52632 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -23,12 +23,15 @@ D3D.Slicer.prototype.setMesh = function (geometry, matrix) { if (geometry instanceof THREE.BufferGeometry) { geometry = new THREE.Geometry().fromBufferGeometry(geometry); } + else { + geometry = geometry.clone(); + } //apply mesh matrix on geometry; - geometry.mergeVertices(); geometry.applyMatrix(matrix); geometry.computeBoundingBox(); geometry.computeFaceNormals(); + geometry.mergeVertices(); this.geometry = geometry; @@ -82,7 +85,7 @@ D3D.Slicer.prototype.createLines = function () { var face = this.geometry.faces[i]; if (face.normal.y !== 1 && face.normal.y !== -1) { - var normal = new THREE.Vector2().set(face.normal.z, face.normal.x).normalize(); + var normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize(); //check for only adding unique lines //returns index of said line @@ -107,6 +110,8 @@ D3D.Slicer.prototype.createLines = function () { D3D.Slicer.prototype.slice = function (layerHeight, height) { "use strict"; + var testData = []; + var numLayers = height / layerHeight; var layersIntersections = []; @@ -132,6 +137,8 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) { for (var layer = 1; layer < layersIntersections.length; layer ++) { var layerIntersections = layersIntersections[layer]; + var testPoints = []; + if (layerIntersections.length > 0) { var y = layer * layerHeight; @@ -152,6 +159,13 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) { } intersections[index] = new THREE.Vector2(z, x); + testPoints.push({ + x: z, + y: x, + connects: this.lines[index].connects, + index: index, + normals: this.lines[index].normals + }); } var done = []; @@ -163,23 +177,26 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) { var shape = []; while (index !== -1) { + done.push(index); + var intersection = intersections[index]; shape.push({X: intersection.x, Y: intersection.y}); - var connects = this.lines[index].connects; - var faceNormals = this.lines[index].normals; + var connects = this.lines[index].connects.clone(); + var faceNormals = this.lines[index].normals.clone(); for (var j = 0; j < connects.length; j ++) { index = connects[j]; if (intersections[index] !== undefined && done.indexOf(index) === -1) { - done.push(index); var a = new THREE.Vector2(intersection.x, intersection.y); var b = intersections[index]; var faceNormal = faceNormals[Math.floor(j/2)]; - if (a.distanceTo(b) === 0 || faceNormal.equals(new THREE.Vector2(0, 0))) { + if (a.distanceTo(b) === 0 || faceNormal.length() === 0) { + done.push(index); + connects = connects.concat(this.lines[index].connects); faceNormals = faceNormals.concat(this.lines[index].normals); index = -1; @@ -187,8 +204,7 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) { else { var normal = a.sub(b).normal().normalize(); - if (normal.dot(faceNormal) >= 0) { - //if (true) { + if (normal.dot(faceNormal) > 0) { break; } else { @@ -202,8 +218,8 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) { } } - if (shape.length > 1) { - var part = new D3D.Paths([shape]).clean(0.01); + var part = new D3D.Paths([shape]).clean(0.01); + if (part.length > 0) { sliceParts.push(part); } } @@ -231,10 +247,20 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) { slices.push(slice); + if (layer === 222) { + testData.push({ + testPoints: testPoints, + pathData: slice.parts + }); + } + this.progress.sliceLayer = layer; this.updateProgress(); } } + + console.log(JSON.stringify(testData)); + return slices; }; D3D.Slicer.prototype.slicesToData = function (slices, printer) { @@ -393,7 +419,7 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) { slice.brim = slice.getOutline().offset(brimOffset); } - var start = slice.optimizePaths(start); + start = slice.optimizePaths(start); for (var i = 0; i < slice.parts.length; i ++) { var part = slice.parts[i]; @@ -423,8 +449,8 @@ D3D.Slicer.prototype.getFillTemplate = function (bounds, size, even, uneven) { var paths = new D3D.Paths([], false); if (even) { - var left = Math.floor(bounds.left/size)*size; - var right = Math.ceil(bounds.right/size)*size; + var left = Math.floor(bounds.left / size) * size; + var right = Math.ceil(bounds.right / size) * size; for (var length = left; length <= right; length += size) { paths.push([ @@ -434,8 +460,8 @@ D3D.Slicer.prototype.getFillTemplate = function (bounds, size, even, uneven) { } } if (uneven) { - var top = Math.floor(bounds.top/size)*size; - var bottom = Math.floor(bounds.bottom/size)*size; + var top = Math.floor(bounds.top / size) * size; + var bottom = Math.floor(bounds.bottom / size) * size; for (var length = top; length <= bottom; length += size) { paths.push([ diff --git a/three.js-master/editor_slicer/js/Sidebar.Printer.js b/three.js-master/editor_slicer/js/Sidebar.Printer.js new file mode 100644 index 0000000..b779cff --- /dev/null +++ b/three.js-master/editor_slicer/js/Sidebar.Printer.js @@ -0,0 +1,207 @@ +Sidebar.Printer = function ( editor ) { + + var USER_SETTINGS, PRINTER_SETTINGS, selectedPrinter; + + function settingsLoaded () { + + printer.updateConfig(USER_SETTINGS); + + var options = {}; + + for (var i in PRINTER_SETTINGS) { + options[i] = i; + } + + printerType.setOptions(options); + + } + + var container = new UI.CollapsiblePanel(); + container.setCollapsed( editor.config.getKey( 'ui/sidebar/printer/collapsed' ) ); + container.onCollapsedChange( function ( boolean ) { + + editor.config.setKey( 'ui/sidebar/printer/collapsed', boolean ); + + } ); + + var printer = new D3D.Printer(); + + var localIp = location.hash.substring(1); + var doodleBox = new D3D.Box(localIp).init(); + + container.addStatic( new UI.Text( 'PRINTER' ) ); + container.add( new UI.Break() ); + + function createRow (name) { + + var row = new UI.Panel(); + + row.add( new UI.Text( name ).setWidth( '150px' ) ); + + var fill = new UI.Text( '' ).setWidth( '90px' ); + row.add(fill); + + container.add(row); + + return fill; + + } + + var state = createRow('State'); + var bedTemperature = createRow('Bed Temperature'); + var bedTargetTemperature = createRow('Bed Target Temperature'); + var nozzleTemperature = createRow('Nozzle Temperature'); + var nozzleTargetTemperature = createRow('Nozzle Target Temperature'); + var currentLine = createRow('Current Line'); + var bufferedLines = createRow('Buffered Lines'); + var totalLines = createRow('Total Lines'); + var printBatches = createRow('Print Batches'); + + doodleBox.onupdate = function (data) { + state.setValue(data["state"]); + bedTemperature.setValue(data["bed"]); + bedTargetTemperature.setValue(data["bed_target"]); + nozzleTemperature.setValue(data["hotend"]); + nozzleTargetTemperature.setValue(data["hotend_target"]); + currentLine.setValue(data["current_line"]); + bufferedLines.setValue(data["buffered_lines"]); + totalLines.setValue(data["total_lines"]); + printBatches.setValue(doodleBox.printBatches.length); + }; + + var printerTypeRow = new UI.Panel(); + var printerType = new UI.Select().setWidth( '150px' ); + printerType.onChange( function () { + + var type = printerType.getValue(); + selectedPrinter = type; + + printer.updateConfig(PRINTER_SETTINGS[selectedPrinter]); + + } ); + + printerTypeRow.add( new UI.Text( 'Printer' ).setWidth( '90px' ) ); + printerTypeRow.add( printerType ); + + container.add( printerTypeRow ); + + var progress = createRow("Progress"); + + var slice = new UI.Button( 'Slice' ); + slice.onClick( function () { + + if (selectedPrinter === undefined) { + alert("No Printer Selected"); + return; + } + + var geometryCombined = new THREE.Geometry(); + + for (var i = 0; i < editor.scene.children.length; i ++) { + + var child = editor.scene.children[i]; + + if (child instanceof THREE.Mesh) { + + var mesh = child; + mesh.updateMatrix(); + var geometry = mesh.geometry.clone(); + geometry.applyMatrix(mesh.matrix); + + if (geometry instanceof THREE.BufferGeometry) { + geometry = new THREE.Geometry().fromBufferGeometry(geometry); + } + + geometryCombined.merge(geometry); + } + } + + geometryCombined.computeBoundingBox(); + + var mesh = new THREE.Mesh(geometryCombined, new THREE.MeshBasicMaterial); + mesh.position.y = -geometryCombined.boundingBox.min.y; + + var slicer = new D3D.SlicerWorker(); + + slicer.onprogress = function (_progress) { + progress.setValue(Math.round(_progress.procent * 100) + "%"); + }; + slicer.onfinish = function (gcode) { + + var print = new UI.Button( 'Start Print' ); + print.onClick( function () { + + doodleBox.print(gcode); + + } ); + container.add( print ); + + var download = new UI.Button( 'Download GCode' ); + download.onClick( function () { + + downloadFile("gcode.gcode", gcode); + + } ); + container.add( download ); + + }; + + slicer.setSettings(USER_SETTINGS, PRINTER_SETTINGS[selectedPrinter]); + slicer.setMesh(mesh); + + slicer.slice(); + + slicer.close(); + } ); + container.add( slice ); + + var stop = new UI.Button( 'Stop Print' ); + stop.onClick( function () { + + if (selectedPrinter === undefined) { + alert("No Printer Selected"); + return; + } + + doodleBox.stopPrint(printer); + + } ); + container.add( stop ); + + container.add( new UI.Break() ); + + (function () { + 'use strict'; + + var loadedItems = 0; + function loaded () { + loadedItems ++; + if (loadedItems === 2) { + //finish loading + + settingsLoaded(); + } + } + + $.ajax({ + url: '../../settings/user_settings.json', + dataType: 'json', + success: function (response) { + USER_SETTINGS = response; + loaded(); + } + }); + + $.ajax({ + url: '../../settings/printer_settings.json', + dataType: 'json', + success: function (response) { + PRINTER_SETTINGS = response; + loaded(); + } + }); + })(); + + return container; + +}