2020-06-03 22:39:30 -04:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
2017-04-18 17:09:33 -04:00
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
2020-06-03 22:39:30 -04:00
|
|
|
const KIRI = self.kiri,
|
|
|
|
|
PRO = Slice.prototype,
|
2017-04-18 17:09:33 -04:00
|
|
|
BASE = self.base,
|
|
|
|
|
UTIL = BASE.util,
|
|
|
|
|
POLY = BASE.polygons,
|
2020-11-07 14:55:15 -05:00
|
|
|
NOKEY = BASE.key.NONE;
|
2017-04-18 17:09:33 -04:00
|
|
|
|
|
|
|
|
KIRI.Top = Top;
|
|
|
|
|
KIRI.Slice = Slice;
|
2020-11-07 19:57:11 -05:00
|
|
|
KIRI.newTop = newTop;
|
2017-04-18 17:09:33 -04:00
|
|
|
KIRI.newSlice = newSlice;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Object encapsulates a z-slice from an object. This code is shared by the
|
|
|
|
|
* client and the worker thread. As such, the view layers are ignored in the
|
|
|
|
|
* worker code paths.
|
|
|
|
|
*
|
|
|
|
|
* @param {number} z offset from ground
|
|
|
|
|
* @param {THREE.Group} [view] optional view parent object for layers
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
function Slice(z, view) {
|
|
|
|
|
this.z = z; // z-index
|
|
|
|
|
this.index = 0; // slice index
|
2020-11-07 14:55:15 -05:00
|
|
|
this.lines = null; // slice raw
|
|
|
|
|
this.groups = null; // grouped lines
|
2017-04-18 17:09:33 -04:00
|
|
|
this.up = null; // slice above (linked list)
|
|
|
|
|
this.down = null; // slice below (linked list)
|
|
|
|
|
this.tops = []; // array of Top objects
|
|
|
|
|
this.view = view; // for rendering this slice
|
2020-02-11 09:27:34 -05:00
|
|
|
this.finger = null; // cached fingerprint
|
2020-11-30 22:36:06 -05:00
|
|
|
this.layers = null; // will replace most of the layer output data
|
2017-04-18 17:09:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a top-level (outer) polygon in a slice. Slices may contain
|
|
|
|
|
* multiple tops each with nested structures. Top objects contain cached
|
|
|
|
|
* and computed objects for quick access for rendering and dependent computations.
|
|
|
|
|
*
|
|
|
|
|
* @param {Polygon} polygon
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
function Top(polygon) {
|
|
|
|
|
this.poly = polygon; // outline poly
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 14:55:15 -05:00
|
|
|
Top.prototype.clone = function(deep) {
|
|
|
|
|
let top = new Top(this.poly.clone(deep));
|
|
|
|
|
return top;
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-18 17:09:33 -04:00
|
|
|
/**
|
|
|
|
|
* return innermost traces under a given top. for FDM, this represents
|
2020-11-07 14:55:15 -05:00
|
|
|
* the outline shell that the fill touches. used by Print, Laser
|
2017-04-18 17:09:33 -04:00
|
|
|
*/
|
2020-11-08 11:09:15 -05:00
|
|
|
Top.prototype.innerShells = function() {
|
|
|
|
|
let shells = this.shells,
|
2017-04-18 17:09:33 -04:00
|
|
|
array = [];
|
2020-11-08 11:09:15 -05:00
|
|
|
if (shells) shells.forEach(function(p) {
|
2017-04-18 17:09:33 -04:00
|
|
|
if (p.inner) array.appendAll(p.inner);
|
|
|
|
|
});
|
|
|
|
|
return array;
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-06 19:01:08 -05:00
|
|
|
/**
|
2020-11-08 08:19:05 -05:00
|
|
|
* Returns shell polygons of a given depth
|
|
|
|
|
* used by Print (FDM)
|
2017-12-06 19:01:08 -05:00
|
|
|
*
|
|
|
|
|
* @param {Polygon[]} out array to populate
|
|
|
|
|
* @returns {Polygon[]} array of top polygons
|
|
|
|
|
*/
|
2020-11-08 08:19:05 -05:00
|
|
|
Top.prototype.shellsAtDepth = function(depth) {
|
2020-11-19 11:10:49 -05:00
|
|
|
return this.shells ? this.shells.filter(poly => poly.depth === depth) : [];
|
2017-12-06 19:01:08 -05:00
|
|
|
};
|
|
|
|
|
|
2017-04-18 17:09:33 -04:00
|
|
|
/** ******************************************************************
|
|
|
|
|
* Slice Prototype Functions
|
|
|
|
|
******************************************************************* */
|
|
|
|
|
|
2020-11-06 21:43:28 -05:00
|
|
|
/**
|
2020-11-30 22:36:06 -05:00
|
|
|
* return Layers object for this slice. creates it if necessary.
|
2020-11-06 21:43:28 -05:00
|
|
|
*/
|
|
|
|
|
PRO.output = function() {
|
2020-11-30 22:36:06 -05:00
|
|
|
if (this.layers) return this.layers;
|
|
|
|
|
return this.layers = new KIRI.Layers();
|
2020-11-06 21:43:28 -05:00
|
|
|
};
|
|
|
|
|
|
2017-04-18 17:09:33 -04:00
|
|
|
/**
|
|
|
|
|
* returns a cloned slice the option of a deep clone on the top polys
|
|
|
|
|
*/
|
2017-10-28 13:30:08 -04:00
|
|
|
PRO.clone = function(deep) {
|
2020-11-07 14:55:15 -05:00
|
|
|
const from = this, slice = newSlice(from.z, from.view);
|
2017-04-18 17:09:33 -04:00
|
|
|
from.tops.forEach(function(top) {
|
|
|
|
|
slice.addTop(top.poly.clone(deep));
|
|
|
|
|
});
|
|
|
|
|
return slice;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-07 14:55:15 -05:00
|
|
|
PRO.topPolys = function() {
|
|
|
|
|
return this.tops.map(top => top.poly);
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-07 15:18:43 -05:00
|
|
|
// CAM only
|
|
|
|
|
PRO.topPolyInners = function() {
|
|
|
|
|
return this.tops.map(top => top.poly.inner).flat().filter(poly => poly);
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-07 14:55:15 -05:00
|
|
|
// FDM / SLA only
|
|
|
|
|
PRO.topInners = function() {
|
|
|
|
|
return this.tops.map(top => top.last).flat().filter(poly => poly);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FDM / SLA only
|
|
|
|
|
PRO.topFillOff = function() {
|
|
|
|
|
return this.tops.map(top => top.fill_off).flat().filter(poly => poly);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FDM only
|
|
|
|
|
PRO.topFill = function() {
|
|
|
|
|
return this.tops.map(top => top.fill_lines).flat().filter(poly => poly);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FDM only
|
|
|
|
|
PRO.topShells = function() {
|
|
|
|
|
return this.tops.map(top => top.shells).flat().filter(poly => poly);
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-11 09:27:34 -05:00
|
|
|
/**
|
|
|
|
|
* produces a fingerprint for a slice that should be the same for
|
|
|
|
|
* layers that are identical. this happens in parts with unchanging
|
|
|
|
|
* vertical wall regions. this allows us to eliminate expensive diffs
|
|
|
|
|
* and infill computation when we detect the layers are the same.
|
|
|
|
|
*/
|
|
|
|
|
PRO.fingerprint = function() {
|
|
|
|
|
if (this.finger) {
|
|
|
|
|
return this.finger;
|
|
|
|
|
}
|
2020-11-07 14:55:15 -05:00
|
|
|
return this.finger = POLY.fingerprint(this.topPolys());
|
2020-02-11 09:27:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* returns true if the layers' fingerprints are the same
|
|
|
|
|
*/
|
|
|
|
|
PRO.fingerprintSame = function(slice) {
|
2020-11-07 14:55:15 -05:00
|
|
|
return slice ? POLY.fingerprintCompare(this.fingerprint(), slice.fingerprint()) : false;
|
2017-04-18 17:09:33 -04:00
|
|
|
};
|
|
|
|
|
|
2020-11-03 14:49:01 -05:00
|
|
|
PRO.addTops = function(polys) {
|
|
|
|
|
polys.forEach(p => {
|
|
|
|
|
this.addTop(p);
|
|
|
|
|
});
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-28 13:30:08 -04:00
|
|
|
PRO.addTop = function(poly) {
|
2020-04-13 19:28:17 -04:00
|
|
|
let top = new Top(poly);
|
2017-04-18 17:09:33 -04:00
|
|
|
this.tops.push(top);
|
|
|
|
|
return top;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-08 08:19:05 -05:00
|
|
|
PRO.findClosestPointTo = function(target) {
|
|
|
|
|
let min, find;
|
|
|
|
|
|
|
|
|
|
if (this.tops.length) {
|
|
|
|
|
this.tops.forEach(function(top) {
|
|
|
|
|
find = top.poly.findClosestPointTo(target);
|
|
|
|
|
if (!min || find.distance < min.distance) {
|
|
|
|
|
min = find;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (this.supports) {
|
|
|
|
|
this.supports.forEach(function(poly) {
|
|
|
|
|
find = poly.findClosestPointTo(target);
|
|
|
|
|
if (!min || find.distance < min.distance) {
|
|
|
|
|
min = find;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return min;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-17 00:45:34 -05:00
|
|
|
PRO.xray = function(dash = 3) {
|
2020-12-02 14:18:16 -05:00
|
|
|
// console.log('xray', this);
|
2020-11-17 00:45:34 -05:00
|
|
|
this.output().setLayer(`xp`, 0x888800).addPolys(this.topPolys());
|
|
|
|
|
this.lines.forEach((line, i) => {
|
|
|
|
|
const group = i % dash;
|
|
|
|
|
const color = [ 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff ][group];
|
|
|
|
|
this.output().setLayer(`xl-${group}`, color).addLine(line.p1, line.p2);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-18 17:09:33 -04:00
|
|
|
function newTop(poly) {
|
|
|
|
|
return new Top(poly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {number} z
|
|
|
|
|
* @param {THREE.Group} view
|
|
|
|
|
* @returns {Slice}
|
|
|
|
|
*/
|
|
|
|
|
function newSlice(z, view) {
|
|
|
|
|
return new Slice(z, view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|