2015-07-26 15:32:10 +02:00
|
|
|
import Paths from './paths.js';
|
|
|
|
|
|
|
|
export default class {
|
|
|
|
constructor () {
|
|
|
|
this.parts = [];
|
2015-06-11 14:34:30 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 09:54:02 +02:00
|
|
|
removeSelfIntersect () {
|
|
|
|
for (var i = 0; i < this.parts.length; i ++) {
|
|
|
|
var part1 = this.parts[i].intersect;
|
|
|
|
|
|
|
|
if (!part1.closed) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var j = i + 1; j < this.parts.length; j ++) {
|
|
|
|
var part2 = this.parts[j].intersect;
|
|
|
|
|
|
|
|
if (!part2.closed) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part2.intersect(part1).length > 0) {
|
|
|
|
this.parts[i].intersect = part1.union(part2);
|
|
|
|
|
|
|
|
this.parts.splice(j, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
optimizePaths (start) {
|
|
|
|
if (this.brim !== undefined && this.brim.length > 0) {
|
|
|
|
this.brim = this.brim.optimizePath(start);
|
|
|
|
start = this.brim.lastPoint();
|
|
|
|
}
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
var parts = [];
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
while (this.parts.length > 0) {
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
var closestDistance = Infinity;
|
|
|
|
var closestPart;
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
for (var i = 0; i < this.parts.length; i ++) {
|
|
|
|
var part = this.parts[i];
|
|
|
|
if (part.intersect.closed) {
|
|
|
|
var bounds = part.outerLine.bounds();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var bounds = part.intersect.bounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
var top = bounds.top - start.y;
|
|
|
|
var bottom = start.y - bounds.bottom;
|
|
|
|
var left = bounds.left - start.x;
|
|
|
|
var right = start.x - bounds.right;
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
var distance = Math.max(top, bottom, left, right);
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
if (distance < closestDistance) {
|
|
|
|
closestDistance = distance;
|
|
|
|
closestPart = i;
|
|
|
|
}
|
2015-06-12 15:58:26 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
var part = this.parts.splice(closestPart, 1)[0];
|
|
|
|
parts.push(part);
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
if (part.intersect.closed) {
|
|
|
|
if (part.outerLine.length > 0) {
|
|
|
|
part.outerLine = part.outerLine.optimizePath(start);
|
|
|
|
start = part.outerLine.lastPoint();
|
|
|
|
}
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
for (var j = 0; j < part.innerLines.length; j ++) {
|
|
|
|
var innerLine = part.innerLines[j];
|
|
|
|
if (innerLine.length > 0) {
|
|
|
|
part.innerLines[j] = innerLine.optimizePath(start);
|
|
|
|
start = part.innerLines[j].lastPoint();
|
|
|
|
}
|
2015-07-10 18:04:10 +02:00
|
|
|
}
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
if (part.fill.length > 0) {
|
|
|
|
part.fill = part.fill.optimizePath(start);
|
|
|
|
start = part.fill.lastPoint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
part.intersect.optimizePath(start);
|
|
|
|
start = part.intersect.lastPoint();
|
2015-07-10 18:04:10 +02:00
|
|
|
}
|
2015-07-26 15:32:10 +02:00
|
|
|
|
2015-07-10 18:04:10 +02:00
|
|
|
}
|
2015-07-26 15:32:10 +02:00
|
|
|
|
|
|
|
this.parts = parts;
|
|
|
|
|
|
|
|
if (this.support !== undefined && this.support.length > 0) {
|
|
|
|
this.support = this.support.optimizePath(start);
|
|
|
|
start = this.support.lastPoint();
|
2015-06-11 14:34:30 +02:00
|
|
|
}
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
return start;
|
2015-06-11 14:34:30 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
getOutline () {
|
|
|
|
var outLines = new Paths([], true);
|
2015-06-12 15:58:26 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
for (var i = 0; i < this.parts.length; i ++) {
|
|
|
|
var part = this.parts[i];
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
if (part.intersect.closed) {
|
|
|
|
outLines.join(this.parts[i].outerLine);
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
return outLines;
|
|
|
|
}
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
add (intersect) {
|
|
|
|
var parts = {
|
|
|
|
intersect
|
|
|
|
};
|
2015-07-10 18:04:10 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
if (intersect.closed) {
|
|
|
|
parts.innerLines = [];
|
|
|
|
parts.outerLine = new Paths([], true);
|
|
|
|
parts.fill = new Paths([], false);
|
2015-07-10 18:04:10 +02:00
|
|
|
}
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2015-07-26 15:32:10 +02:00
|
|
|
this.parts.push(parts);
|
2015-07-10 18:04:10 +02:00
|
|
|
}
|
2015-07-26 15:32:10 +02:00
|
|
|
}
|