fixed mergin vertexes

This commit is contained in:
casperlamboo 2015-05-15 15:07:47 +02:00
parent 203fe6df19
commit e441c142b4
6 changed files with 108 additions and 44 deletions

86
build/d3d.js vendored
View File

@ -644,41 +644,75 @@ D3D.Paths.prototype.scaleDown = function (factor) {
return this;
};
D3D.Paths.prototype.lastPoint = function () {
"use strict";
var lastPath = this[this.length - 1];
var lastPoint = this.closed ? lastPath[0] : lastPath[lastPath.length - 1];
return new THREE.Vector2(lastPoint.X, lastPoint.Y);
};
D3D.Paths.prototype.optimizePath = function (start) {
"use strict";
var optimizedPaths = new D3D.Paths();
var optimizedPaths = new D3D.Paths([], this.closed);
var donePaths = [];
while (optimizedPaths.length !== this.length) {
var minLength = undefined;
var minLength = false;
var reverse;
var minPath;
var offset;
var pathIndex;
for (var i = 0; i < this.length; i ++) {
var path = this[i];
if (optimizedPaths.indexOf(path) === -1) {
var startPoint = new THREE.Vector2(path[0].X, path[0].Y);
var endPoint = new THREE.Vector2(path[path.length - 1].X, path[path.length - 1].Y);
var length = startPoint.sub(start).length();
if (minLength === undefined || length < minLength) {
minPath = path;
minLength = length;
reverse = false;
if (donePaths.indexOf(i) === -1) {
if (this.closed) {
for (var j = 0; j < path.length; j ++) {
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;
}
}
}
var length = endPoint.sub(start).length();
if (length < minLength) {
minPath = path;
minLength = length;
reverse = true;
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 (reverse) {
minPath.reverse();
if (this.closed) {
minPath = minPath.concat(minPath.splice(0, offset));
var point = minPath[0];
}
var point = minPath[minPath.length - 1];
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);
}
@ -728,8 +762,8 @@ D3D.Paths.prototype.draw = function (context, color) {
for (var i = 0; i < this.length; i ++) {
var shape = this[i];
//var point = shape[0];
//context.fillText(i, point.X*2, point.Y*2);
var point = shape[0];
context.fillText(i, point.X*2, point.Y*2);
context.beginPath();
var length = this.closed ? (shape.length + 1) : shape.length;
@ -978,6 +1012,8 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
var brimOffset = printer.config["printer.brimOffset"] * scale;
var skinCount = Math.ceil(shellThickness/layerHeight);
var start = new THREE.Vector2(0, 0);
var data = [];
var lowFillTemplate = this.getFillTemplate({
@ -1038,10 +1074,10 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
fill.join(highFillTemplate.intersect(highFillArea));
}
var lastPath = insets[insets.length - 1];
var lastPoint = lastPath[lastPath.length - 1];
var start = new THREE.Vector2(lastPoint.X, lastPoint.Y);
fill = fill.optimizePath(start);
outerLayer = outerLayer.optimizePath(start);
insets = insets.optimizePath(outerLayer.lastPoint());
fill = fill.optimizePath(insets.lastPoint());
start = fill.lastPoint();
layerData.push({
outerLayer: outerLayer.scaleDown(scale),
@ -1116,7 +1152,7 @@ D3D.Slicer.prototype.dataToGcode = function (data, printer) {
gcode.push([
"G0",
"X" + point.X.toFixed(3) + " Y" + point.Y.toFixed(3) + " Z" + z,
"F" + (travelSpeed*60)
"F" + (travelSpeed * 60)
].join(" "));
if (extruder > retractionMinDistance && retractionEnabled) {

2
build/d3d.min.js vendored

File diff suppressed because one or more lines are too long

BIN
models/sonder_one_piece.stl Normal file

Binary file not shown.

View File

@ -115,12 +115,12 @@ mesh.position.z = 100;
//scene.add(mesh);
var loader = new THREE.STLLoader();
loader.load("models/diamond.stl", function (geometry) {
var geometry = new THREE.BoxGeometry(10, 10, 10, 1, 1, 1);
loader.load("models/sonder_one_piece.stl", function (geometry) {
//var geometry = new THREE.BoxGeometry(10, 10, 10, 1, 1, 1);
//var geometry = new THREE.SphereGeometry(10, 10, 10);
//var geometry = new THREE.TorusGeometry(20, 10, 30, 30);
/*
var geometry = (function () {
"use strict";
@ -136,7 +136,7 @@ loader.load("models/diamond.stl", function (geometry) {
matrix.makeRotationX(Math.PI*1.5);
var geometry = new THREE.ExtrudeGeometry(circle, {
amount: 3,
amount: 10,
bevelEnabled: false,
steps: 1
});
@ -144,19 +144,19 @@ loader.load("models/diamond.stl", function (geometry) {
return geometry;
})();
*/
var mesh = new THREE.Mesh(geometry, material);
mesh.position.x = 100;
mesh.position.z = 100;
/*
mesh.rotation.x = -Math.PI/2;
mesh.scale.x = mesh.scale.y = mesh.scale.z = 3;
///*
//mesh.rotation.x = -Math.PI/2;
mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
//mesh is lifted a little bit...
mesh.position.y = -0.6552429199218741;
*/
//mesh.position.y = 26.5;
//*/
scene.add(mesh);
var slicer = new D3D.Slicer().setMesh(mesh);
@ -164,8 +164,8 @@ loader.load("models/diamond.stl", function (geometry) {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
//var img = slicer.drawPaths(printer, 7, 8);
//context.drawImage(img, 0, 0);
var img = slicer.drawPaths(printer, 7, 8);
context.drawImage(img, 0, 0);
gcode = slicer.getGcode(printer);
});

View File

@ -200,8 +200,8 @@ D3D.Paths.prototype.draw = function (context, color) {
for (var i = 0; i < this.length; i ++) {
var shape = this[i];
var point = shape[0];
context.fillText(i, point.X*2, point.Y*2);
//var point = shape[0];
//context.fillText(i, point.X*2, point.Y*2);
context.beginPath();
var length = this.closed ? (shape.length + 1) : shape.length;

View File

@ -20,19 +20,38 @@ D3D.Slicer = function () {
D3D.Slicer.prototype.setMesh = function (mesh) {
"use strict";
mesh.updateMatrix();
//convert buffergeometry to geometry;
var geometry = mesh.geometry.clone();
if (geometry instanceof THREE.BufferGeometry) {
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
}
//remove duplicate vertices;
for (var i = 0; i < geometry.vertices.length; i ++) {
var vertexA = geometry.vertices[i];
for (var j = i + 1; j < geometry.vertices.length; j ++) {
var vertexB = geometry.vertices[j];
if (vertexA.equals(vertexB)) {
geometry.vertices[j] = vertexA;
}
}
}
geometry.mergeVertices();
//apply mesh matrix on geometry;
mesh.updateMatrix();
geometry.applyMatrix(mesh.matrix);
geometry.computeFaceNormals();
this.geometry = geometry;
//get unique lines from geometry;
this.createLines();
console.log(this.lines);
return this;
};
D3D.Slicer.prototype.createLines = function () {
@ -283,6 +302,7 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
}
var fillArea = (inset || outerLayer).offset(-wallThickness/2);
//var fillArea = (inset || outerLayer).clone();
var highFillArea = fillArea.difference(surroundingLayer);
@ -290,17 +310,25 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
var fill = new D3D.Paths([], false);
fill.join(lowFillTemplate.intersect(lowFillArea));
if (lowFillTemplate.length > 0) {
fill.join(lowFillTemplate.intersect(lowFillArea));
}
if (highFillArea.length > 0) {
var highFillTemplate = this.getFillTemplate(highFillArea.bounds(), wallThickness, (layer % 2 === 0), (layer % 2 === 1));
var bounds = highFillArea.bounds();
var even = (layer % 2 === 0);
var highFillTemplate = this.getFillTemplate(bounds, wallThickness, even, !even);
fill.join(highFillTemplate.intersect(highFillArea));
}
/*
outerLayer = outerLayer.optimizePath(start);
insets = insets.optimizePath(outerLayer.lastPoint());
fill = fill.optimizePath(insets.lastPoint());
start = fill.lastPoint();
*/
fill = fill.optimizePath(insets.lastPoint());
layerData.push({
outerLayer: outerLayer.scaleDown(scale),