fixed path optimizing when paths are empty

This commit is contained in:
casperlamboo 2015-05-15 17:35:18 +02:00
parent e441c142b4
commit a2e7bf4d85
6 changed files with 79 additions and 31 deletions

67
build/d3d.js vendored
View File

@ -583,7 +583,9 @@ D3D.Paths.prototype.setPaths = function (paths) {
for (var i = 0; i < paths.length; i ++) {
var path = paths[i];
this.push(path);
if (path.length > 0) {
this.push(path);
}
}
return this;
@ -598,7 +600,7 @@ D3D.Paths.prototype.clip = function (path, type) {
clipper.AddPaths(path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(type, solution);
return new D3D.Paths(solution);
return new D3D.Paths(solution, this.closed);
};
D3D.Paths.prototype.union = function (path) {
"use strict";
@ -762,8 +764,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;
@ -797,17 +799,34 @@ 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();
return this;
@ -1060,6 +1079,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);
@ -1067,18 +1087,35 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
var fill = new D3D.Paths([], false);
fill.join(lowFillTemplate.intersect(lowFillArea));
if (highFillArea.length > 0) {
var highFillTemplate = this.getFillTemplate(highFillArea.bounds(), wallThickness, (layer % 2 === 0), (layer % 2 === 1));
fill.join(highFillTemplate.intersect(highFillArea));
if (lowFillTemplate.length > 0) {
fill.join(lowFillTemplate.intersect(lowFillArea));
}
if (highFillArea.length > 0) {
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();
if (insets.length > 0) {
insets = insets.optimizePath(outerLayer.lastPoint());
fill = fill.optimizePath(insets.lastPoint());
}
else {
fill = fill.optimizePath(outerLayer.lastPoint());
}
if (fill.length > 0) {
start = fill.lastPoint();
}
else if (insets.length > 0) {
start = insets.lastPoint();
}
else {
start = outerLayer.lastPoint();
}
layerData.push({
outerLayer: outerLayer.scaleDown(scale),
fill: fill.scaleDown(scale),

2
build/d3d.min.js vendored

File diff suppressed because one or more lines are too long

BIN
models/stanford_bunny.stl Normal file

Binary file not shown.

View File

@ -115,7 +115,7 @@ mesh.position.z = 100;
//scene.add(mesh);
var loader = new THREE.STLLoader();
loader.load("models/sonder_one_piece.stl", function (geometry) {
loader.load("models/stanford_bunny.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);
@ -148,12 +148,14 @@ loader.load("models/sonder_one_piece.stl", function (geometry) {
var mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = -Math.PI/2;
mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
mesh.position.x = 100;
mesh.position.z = 100;
//mesh.position.y = -2;
///*
//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 = 26.5;
//*/

View File

@ -21,7 +21,9 @@ D3D.Paths.prototype.setPaths = function (paths) {
for (var i = 0; i < paths.length; i ++) {
var path = paths[i];
this.push(path);
if (path.length > 0) {
this.push(path);
}
}
return this;
@ -36,7 +38,7 @@ D3D.Paths.prototype.clip = function (path, type) {
clipper.AddPaths(path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(type, solution);
return new D3D.Paths(solution);
return new D3D.Paths(solution, this.closed);
};
D3D.Paths.prototype.union = function (path) {
"use strict";

View File

@ -50,8 +50,6 @@ D3D.Slicer.prototype.setMesh = function (mesh) {
//get unique lines from geometry;
this.createLines();
console.log(this.lines);
return this;
};
D3D.Slicer.prototype.createLines = function () {
@ -320,16 +318,25 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
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());
if (insets.length > 0) {
insets = insets.optimizePath(outerLayer.lastPoint());
fill = fill.optimizePath(insets.lastPoint());
}
else {
fill = fill.optimizePath(outerLayer.lastPoint());
}
if (fill.length > 0) {
start = fill.lastPoint();
}
else if (insets.length > 0) {
start = insets.lastPoint();
}
else {
start = outerLayer.lastPoint();
}
layerData.push({
outerLayer: outerLayer.scaleDown(scale),
fill: fill.scaleDown(scale),