2016-04-21 22:14:22 +02:00
|
|
|
import Shape from 'Doodle3D/clipper-js';
|
2015-07-26 15:32:10 +02:00
|
|
|
|
|
|
|
export default class {
|
|
|
|
constructor () {
|
|
|
|
this.parts = [];
|
2015-06-11 14:34:30 +02:00
|
|
|
}
|
|
|
|
|
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];
|
2016-04-21 22:14:22 +02:00
|
|
|
if (part.shape.closed) {
|
2015-07-26 15:32:10 +02:00
|
|
|
var bounds = part.outerLine.bounds();
|
|
|
|
}
|
|
|
|
else {
|
2016-04-21 22:14:22 +02:00
|
|
|
var bounds = part.shape.bounds();
|
2015-07-26 15:32:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-04-21 22:14:22 +02:00
|
|
|
if (part.shape.closed) {
|
2015-07-26 15:32:10 +02:00
|
|
|
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 {
|
2016-04-21 22:14:22 +02:00
|
|
|
part.shape.optimizePath(start);
|
|
|
|
start = part.shape.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 () {
|
2016-04-21 22:14:22 +02:00
|
|
|
var outLines = new Shape([], 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
|
|
|
|
2016-04-21 22:14:22 +02:00
|
|
|
if (part.shape.closed) {
|
2015-07-26 15:32:10 +02:00
|
|
|
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
|
|
|
|
2016-04-21 22:14:22 +02:00
|
|
|
add (shape) {
|
|
|
|
const part = { shape };
|
2015-07-10 18:04:10 +02:00
|
|
|
|
2016-04-21 22:14:22 +02:00
|
|
|
if (shape.closed) {
|
|
|
|
part.innerLines = [];
|
|
|
|
part.outerLine = new Shape([], true);
|
|
|
|
part.fill = new Shape([], false);
|
2015-07-10 18:04:10 +02:00
|
|
|
}
|
2015-06-11 14:34:30 +02:00
|
|
|
|
2016-04-21 22:14:22 +02:00
|
|
|
this.parts.push(part);
|
2015-07-10 18:04:10 +02:00
|
|
|
}
|
2016-04-21 22:14:22 +02:00
|
|
|
}
|