update code style for geo classes
This commit is contained in:
parent
a51af1d4c6
commit
7b8a5be1c5
11 changed files with 2777 additions and 3147 deletions
9
app.js
9
app.js
|
|
@ -309,15 +309,13 @@ const script = {
|
||||||
"add/array",
|
"add/array",
|
||||||
"add/three",
|
"add/three",
|
||||||
"geo/base",
|
"geo/base",
|
||||||
// "geo/render",
|
|
||||||
"geo/point",
|
"geo/point",
|
||||||
"geo/points",
|
"geo/points",
|
||||||
"geo/slope",
|
"geo/slope",
|
||||||
"geo/line",
|
"geo/line",
|
||||||
"geo/bounds",
|
"geo/bounds",
|
||||||
"geo/polygon",
|
|
||||||
"geo/polygons",
|
"geo/polygons",
|
||||||
// "geo/gyroid",
|
"geo/polygon",
|
||||||
"geo/mesh",
|
"geo/mesh",
|
||||||
"data/local",
|
"data/local",
|
||||||
"data/index",
|
"data/index",
|
||||||
|
|
@ -383,8 +381,8 @@ const script = {
|
||||||
"geo/slope",
|
"geo/slope",
|
||||||
"geo/line",
|
"geo/line",
|
||||||
"geo/bounds",
|
"geo/bounds",
|
||||||
"geo/polygon",
|
|
||||||
"geo/polygons",
|
"geo/polygons",
|
||||||
|
"geo/polygon",
|
||||||
"geo/gyroid",
|
"geo/gyroid",
|
||||||
"geo/mesh",
|
"geo/mesh",
|
||||||
// "moto/broker",
|
// "moto/broker",
|
||||||
|
|
@ -432,8 +430,8 @@ const script = {
|
||||||
"geo/slope",
|
"geo/slope",
|
||||||
"geo/line",
|
"geo/line",
|
||||||
"geo/bounds",
|
"geo/bounds",
|
||||||
"geo/polygon",
|
|
||||||
"geo/polygons",
|
"geo/polygons",
|
||||||
|
"geo/polygon",
|
||||||
"geo/gyroid",
|
"geo/gyroid",
|
||||||
"kiri-mode/fdm/driver",
|
"kiri-mode/fdm/driver",
|
||||||
"kiri-mode/fdm/slice",
|
"kiri-mode/fdm/slice",
|
||||||
|
|
@ -453,7 +451,6 @@ const script = {
|
||||||
"add/array",
|
"add/array",
|
||||||
"add/three",
|
"add/three",
|
||||||
"geo/base",
|
"geo/base",
|
||||||
// "geo/render",
|
|
||||||
"geo/point",
|
"geo/point",
|
||||||
"geo/points",
|
"geo/points",
|
||||||
"geo/slope",
|
"geo/slope",
|
||||||
|
|
|
||||||
|
|
@ -4,216 +4,217 @@
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
if (self.base.gyroid) return;
|
if (self.base.gyroid) return;
|
||||||
|
|
||||||
const base = self.base;
|
const base = self.base;
|
||||||
const PI2 = Math.PI * 2;
|
const PI2 = Math.PI * 2;
|
||||||
let cache = {};
|
|
||||||
let lastVal;
|
|
||||||
let lastRes = 0;
|
|
||||||
let lastSlice = 0;
|
|
||||||
|
|
||||||
/**
|
let cache = {};
|
||||||
* @param off {number} z offset value from 0-1
|
let lastVal;
|
||||||
* @param res {number} resolution (pixels/slices per side)
|
let lastRes = 0;
|
||||||
*/
|
let lastSlice = 0;
|
||||||
function slice(off, res, val) {
|
|
||||||
// auto clear cach if it hasn't been hit in the last 20 seconds
|
/**
|
||||||
// or the requested resolution or tip values have changed
|
* @param off {number} z offset value from 0-1
|
||||||
let now = Date.now();
|
* @param res {number} resolution (pixels/slices per side)
|
||||||
if (res !== lastRes || val !== lastVal || now - lastSlice > 20000) {
|
*/
|
||||||
// console.log({clear_cache: now});
|
function slice(off, res, val) {
|
||||||
cache = {};
|
// auto clear cach if it hasn't been hit in the last 20 seconds
|
||||||
|
// or the requested resolution or tip values have changed
|
||||||
|
let now = Date.now();
|
||||||
|
if (res !== lastRes || val !== lastVal || now - lastSlice > 20000) {
|
||||||
|
// console.log({clear_cache: now});
|
||||||
|
cache = {};
|
||||||
|
}
|
||||||
|
lastVal = val;
|
||||||
|
lastRes = res;
|
||||||
|
lastSlice = now;
|
||||||
|
let rez = parseInt(res || 200);
|
||||||
|
let inc = PI2 / rez;
|
||||||
|
let z = PI2 * off;
|
||||||
|
let key = (z % PI2).round(3);
|
||||||
|
let hit = cache[key];
|
||||||
|
if (hit) {
|
||||||
|
return hit;
|
||||||
|
}
|
||||||
|
let tip = val || 0;
|
||||||
|
let edge = [];
|
||||||
|
let vals = [];
|
||||||
|
let points = 0;
|
||||||
|
let points_lr = 0;
|
||||||
|
let points_td = 0;
|
||||||
|
for (let x=0; x<PI2; x += inc) {
|
||||||
|
let vrow = []; // raw values row
|
||||||
|
let erow = []; // edge values row
|
||||||
|
edge.push(erow);
|
||||||
|
vals.push(vrow);
|
||||||
|
for (let y=0; y<PI2; y += inc) {
|
||||||
|
erow.push(0);
|
||||||
|
vrow.push(
|
||||||
|
Math.sin(x) * Math.cos(y) +
|
||||||
|
Math.sin(y) * Math.cos(z) +
|
||||||
|
Math.sin(z) * Math.cos(x)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
lastVal = val;
|
}
|
||||||
lastRes = res;
|
|
||||||
lastSlice = now;
|
// left-right threshold search (red)
|
||||||
let rez = parseInt(res || 200);
|
vals.forEach((vrow, y) => {
|
||||||
let inc = PI2 / rez;
|
let erow = edge[y];
|
||||||
let z = PI2 * off;
|
let lval = vrow[vrow.length - 1];
|
||||||
let key = (z % PI2).round(3);
|
vrow.forEach((val, x) => {
|
||||||
let hit = cache[key];
|
if (
|
||||||
if (hit) {
|
(lval <= tip && val >= tip) || (lval >= tip && val <= tip) ||
|
||||||
return hit;
|
(lval <= -tip && val >= -tip) || (lval >= -tip && val <= -tip)
|
||||||
|
) {
|
||||||
|
erow[x] = 1;
|
||||||
|
points++;
|
||||||
|
points_lr++;
|
||||||
|
}
|
||||||
|
lval = val;
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
// top-down threshold search (green)
|
||||||
|
for (let x=0; x<rez; x++) {
|
||||||
|
let lval = vals[vals.length-1][x];
|
||||||
|
for (let y=0; y<rez; y++) {
|
||||||
|
let val = vals[y][x];
|
||||||
|
if (
|
||||||
|
(lval <= tip && val >= tip) || (lval >= tip && val <= tip) ||
|
||||||
|
(lval <= -tip && val >= -tip) || (lval >= -tip && val <= -tip)
|
||||||
|
) {
|
||||||
|
if (edge[y][x]) {
|
||||||
|
edge[y][x] = 3;
|
||||||
|
} else {
|
||||||
|
edge[y][x] = 2;
|
||||||
|
points++
|
||||||
|
}
|
||||||
|
points_td++;
|
||||||
|
}
|
||||||
|
lval = val;
|
||||||
}
|
}
|
||||||
let tip = val || 0;
|
}
|
||||||
let edge = [];
|
|
||||||
let vals = [];
|
// deterime prevailing direction for chaining
|
||||||
let points = 0;
|
let dir = points_td > points_lr ? 'lr' : 'td';
|
||||||
let points_lr = 0;
|
|
||||||
let points_td = 0;
|
// create sparse representation
|
||||||
for (let x=0; x<PI2; x += inc) {
|
let sparse = [];
|
||||||
let vrow = []; // raw values row
|
let center = rez / 2;
|
||||||
let erow = []; // edge values row
|
edge.forEach((row,y) => {
|
||||||
edge.push(erow);
|
row.forEach((val,x) => {
|
||||||
vals.push(vrow);
|
if (val) {
|
||||||
for (let y=0; y<PI2; y += inc) {
|
let dx = Math.abs(x - center);
|
||||||
erow.push(0);
|
let dy = Math.abs(y - center);
|
||||||
vrow.push(
|
sparse.push({x: x/rez, y: y/rez, val, dist: Math.max(dx,dy)});
|
||||||
Math.sin(x) * Math.cos(y) +
|
}
|
||||||
Math.sin(y) * Math.cos(z) +
|
});
|
||||||
Math.sin(z) * Math.cos(x)
|
});
|
||||||
);
|
sparse.sort((a,b) => {
|
||||||
|
return b.dist - a.dist;
|
||||||
|
});
|
||||||
|
|
||||||
|
// join sparse points array by closest distance
|
||||||
|
let polys = [];
|
||||||
|
let chain;
|
||||||
|
let added;
|
||||||
|
let cleared = 0;
|
||||||
|
let maxdist = 0.05;
|
||||||
|
|
||||||
|
do {
|
||||||
|
chain = null;
|
||||||
|
for (let i=0; i<sparse.length; i++) {
|
||||||
|
if (sparse[i]) {
|
||||||
|
chain = [ sparse[i] ];
|
||||||
|
polys.push(chain);
|
||||||
|
sparse[i] = null;
|
||||||
|
cleared++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// left-right threshold search (red)
|
|
||||||
vals.forEach((vrow, y) => {
|
|
||||||
let erow = edge[y];
|
|
||||||
let lval = vrow[vrow.length - 1];
|
|
||||||
vrow.forEach((val, x) => {
|
|
||||||
if (
|
|
||||||
(lval <= tip && val >= tip) || (lval >= tip && val <= tip) ||
|
|
||||||
(lval <= -tip && val >= -tip) || (lval >= -tip && val <= -tip)
|
|
||||||
) {
|
|
||||||
erow[x] = 1;
|
|
||||||
points++;
|
|
||||||
points_lr++;
|
|
||||||
}
|
|
||||||
lval = val;
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
// top-down threshold search (green)
|
|
||||||
for (let x=0; x<rez; x++) {
|
|
||||||
let lval = vals[vals.length-1][x];
|
|
||||||
for (let y=0; y<rez; y++) {
|
|
||||||
let val = vals[y][x];
|
|
||||||
if (
|
|
||||||
(lval <= tip && val >= tip) || (lval >= tip && val <= tip) ||
|
|
||||||
(lval <= -tip && val >= -tip) || (lval >= -tip && val <= -tip)
|
|
||||||
) {
|
|
||||||
if (edge[y][x]) {
|
|
||||||
edge[y][x] = 3;
|
|
||||||
} else {
|
|
||||||
edge[y][x] = 2;
|
|
||||||
points++
|
|
||||||
}
|
|
||||||
points_td++;
|
|
||||||
}
|
|
||||||
lval = val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// deterime prevailing direction for chaining
|
|
||||||
let dir = points_td > points_lr ? 'lr' : 'td';
|
|
||||||
|
|
||||||
// create sparse representation
|
|
||||||
let sparse = [];
|
|
||||||
let center = rez / 2;
|
|
||||||
edge.forEach((row,y) => {
|
|
||||||
row.forEach((val,x) => {
|
|
||||||
if (val) {
|
|
||||||
let dx = Math.abs(x - center);
|
|
||||||
let dy = Math.abs(y - center);
|
|
||||||
sparse.push({x: x/rez, y: y/rez, val, dist: Math.max(dx,dy)});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
sparse.sort((a,b) => {
|
|
||||||
return b.dist - a.dist;
|
|
||||||
});
|
|
||||||
|
|
||||||
// join sparse points array by closest distance
|
|
||||||
let polys = [];
|
|
||||||
let chain;
|
|
||||||
let added;
|
|
||||||
let cleared = 0;
|
|
||||||
let maxdist = 0.05;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
chain = null;
|
added = false;
|
||||||
|
let target = chain[chain.length - 1];
|
||||||
|
let cl_elm = null;
|
||||||
|
let cl_idx = null;
|
||||||
|
let cl_dst = Infinity;
|
||||||
for (let i=0; i<sparse.length; i++) {
|
for (let i=0; i<sparse.length; i++) {
|
||||||
if (sparse[i]) {
|
let test_el = sparse[i];
|
||||||
chain = [ sparse[i] ];
|
if (test_el) {
|
||||||
polys.push(chain);
|
let dst = distTo(target, test_el, dir);
|
||||||
sparse[i] = null;
|
if (cl_idx === null || dst < cl_dst) {
|
||||||
cleared++;
|
cl_idx = i;
|
||||||
|
cl_elm = test_el;
|
||||||
|
cl_dst = dst;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cl_elm) {
|
||||||
|
if (cl_dst > maxdist) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
sparse[cl_idx] = null;
|
||||||
|
cleared++;
|
||||||
|
chain.push(cl_elm);
|
||||||
|
added = true;
|
||||||
}
|
}
|
||||||
do {
|
} while (added);
|
||||||
added = false;
|
} while (cleared < sparse.length);
|
||||||
let target = chain[chain.length - 1];
|
|
||||||
let cl_elm = null;
|
|
||||||
let cl_idx = null;
|
|
||||||
let cl_dst = Infinity;
|
|
||||||
for (let i=0; i<sparse.length; i++) {
|
|
||||||
let test_el = sparse[i];
|
|
||||||
if (test_el) {
|
|
||||||
let dst = distTo(target, test_el, dir);
|
|
||||||
if (cl_idx === null || dst < cl_dst) {
|
|
||||||
cl_idx = i;
|
|
||||||
cl_elm = test_el;
|
|
||||||
cl_dst = dst;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (cl_elm) {
|
|
||||||
if (cl_dst > maxdist) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
sparse[cl_idx] = null;
|
|
||||||
cleared++;
|
|
||||||
chain.push(cl_elm);
|
|
||||||
added = true;
|
|
||||||
}
|
|
||||||
} while (added);
|
|
||||||
} while (cleared < sparse.length);
|
|
||||||
|
|
||||||
let psimple = polys
|
let psimple = polys
|
||||||
.map(poly => filter(poly, 0))
|
.map(poly => filter(poly, 0))
|
||||||
.map(poly => filter(poly, inc));
|
.map(poly => filter(poly, inc));
|
||||||
|
|
||||||
let slice = {edge, points, dir, polys: psimple};
|
let slice = {edge, points, dir, polys: psimple};
|
||||||
cache[key] = slice;
|
cache[key] = slice;
|
||||||
return slice;
|
return slice;
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge co-linear and distance threshold
|
||||||
|
function filter(poly, inc) {
|
||||||
|
if (poly.length <= 2) {
|
||||||
|
return poly;
|
||||||
}
|
}
|
||||||
|
let nupoly = [ poly[0] ];
|
||||||
// merge co-linear and distance threshold
|
let e1 = poly[1];
|
||||||
function filter(poly, inc) {
|
let e2 = null;
|
||||||
if (poly.length <= 2) {
|
let last = poly.length - 2;
|
||||||
return poly;
|
for (let i=1; i<poly.length; i++) {
|
||||||
|
let el = poly[i];
|
||||||
|
let drop = inc ?
|
||||||
|
(distTo(e1, el) <= inc) :
|
||||||
|
(e1.x === el.x || e1.y === el.y);
|
||||||
|
if (drop) {
|
||||||
|
e2 = el;
|
||||||
|
if (i < last) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let nupoly = [ poly[0] ];
|
if (e2) {
|
||||||
let e1 = poly[1];
|
nupoly.push({x:(e1.x + e2.x)/2, y:(e1.y + e2.y)/2});
|
||||||
let e2 = null;
|
e2 = null;
|
||||||
let last = poly.length - 2;
|
} else {
|
||||||
for (let i=1; i<poly.length; i++) {
|
nupoly.push(e1);
|
||||||
let el = poly[i];
|
if (i === last) {
|
||||||
let drop = inc ?
|
nupoly.push(el);
|
||||||
(distTo(e1, el) <= inc) :
|
|
||||||
(e1.x === el.x || e1.y === el.y);
|
|
||||||
if (drop) {
|
|
||||||
e2 = el;
|
|
||||||
if (i < last) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (e2) {
|
|
||||||
nupoly.push({x:(e1.x + e2.x)/2, y:(e1.y + e2.y)/2});
|
|
||||||
e2 = null;
|
|
||||||
} else {
|
|
||||||
nupoly.push(e1);
|
|
||||||
if (i === last) {
|
|
||||||
nupoly.push(el);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e1 = el;
|
|
||||||
}
|
}
|
||||||
nupoly.push(poly[poly.length-1]);
|
e1 = el;
|
||||||
return nupoly;
|
|
||||||
}
|
}
|
||||||
|
nupoly.push(poly[poly.length-1]);
|
||||||
|
return nupoly;
|
||||||
|
}
|
||||||
|
|
||||||
function distTo(a, b, dir) {
|
function distTo(a, b, dir) {
|
||||||
let dx = a.x - b.x;
|
let dx = a.x - b.x;
|
||||||
let dy = a.y - b.y;
|
let dy = a.y - b.y;
|
||||||
// bias distance by prevailing direction of discovery to join stragglers
|
// bias distance by prevailing direction of discovery to join stragglers
|
||||||
if (dir === 'lr') dx = dx / 2;
|
if (dir === 'lr') dx = dx / 2;
|
||||||
if (dir === 'td') dy = dy / 2;
|
if (dir === 'td') dy = dy / 2;
|
||||||
return Math.sqrt(dx * dx + dy * dy);
|
return Math.sqrt(dx * dx + dy * dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
base.gyroid = { slice };
|
base.gyroid = { slice };
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -4,79 +4,45 @@
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
if (self.base.Line) return;
|
if (self.base.Line) return;
|
||||||
|
|
||||||
const BASE = self.base, PRO = Line.prototype;
|
const base = self.base;
|
||||||
|
|
||||||
BASE.Line = Line;
|
class Line {
|
||||||
BASE.newLine = newLine;
|
constructor(p1, p2, key) {
|
||||||
BASE.newOrderedLine = newOrderedLine;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {Point} p1
|
|
||||||
* @param {Point} p2
|
|
||||||
* @param {String} [key]
|
|
||||||
* @constructor
|
|
||||||
*/
|
|
||||||
function Line(p1, p2, key) {
|
|
||||||
if (!key) key = [p1.key, p2.key].join(';');
|
if (!key) key = [p1.key, p2.key].join(';');
|
||||||
this.p1 = p1;
|
this.p1 = p1;
|
||||||
this.p2 = p2;
|
this.p2 = p2;
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.coplanar = false;
|
|
||||||
this.edge = false;
|
|
||||||
this.del = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** ******************************************************************
|
length() {
|
||||||
* Line Prototype Functions
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
PRO.length = function() {
|
|
||||||
return Math.sqrt(this.length2());
|
return Math.sqrt(this.length2());
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {number} square of length
|
* @returns {number} square of length
|
||||||
*/
|
*/
|
||||||
PRO.length2 = function() {
|
length2() {
|
||||||
return this.p1.distToSq2D(this.p2);
|
return this.p1.distToSq2D(this.p2);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
slope() {
|
||||||
* @returns {Slope}
|
return base.newSlope(this.p1.slopeTo(this.p2));
|
||||||
*/
|
}
|
||||||
PRO.slope = function() {
|
|
||||||
return BASE.newSlope(this.p1.slopeTo(this.p2));
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
reverse() {
|
||||||
* @returns {Line}
|
|
||||||
*/
|
|
||||||
PRO.reverse = function() {
|
|
||||||
let t = this.p1;
|
let t = this.p1;
|
||||||
this.p1 = this.p2;
|
this.p1 = this.p2;
|
||||||
this.p2 = t;
|
this.p2 = t;
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
midpoint() {
|
||||||
* @returns {Point}
|
|
||||||
*/
|
|
||||||
PRO.midpoint = function() {
|
|
||||||
return this.p1.midPointTo(this.p2);
|
return this.p1.midPointTo(this.p2);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
isCollinear(line) {
|
||||||
* faulty when line doubles back at 180?
|
|
||||||
* @param {Line} line
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
PRO.isCollinear = function(line) {
|
|
||||||
let p1 = this.p1,
|
let p1 = this.p1,
|
||||||
p2 = this.p2,
|
p2 = this.p2,
|
||||||
p3 = line.p1,
|
p3 = line.p1,
|
||||||
|
|
@ -85,27 +51,20 @@
|
||||||
d1y = (p2.y - p1.y),
|
d1y = (p2.y - p1.y),
|
||||||
d2x = (p4.x - p3.x),
|
d2x = (p4.x - p3.x),
|
||||||
d2y = (p4.y - p3.y);
|
d2y = (p4.y - p3.y);
|
||||||
|
|
||||||
return Math.abs( (d2y * d1x) - (d2x * d1y) ) < 0.0001;
|
return Math.abs( (d2y * d1x) - (d2x * d1y) ) < 0.0001;
|
||||||
};
|
|
||||||
|
|
||||||
/** ******************************************************************
|
|
||||||
* Connect to base and Helpers
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {Point} p1
|
|
||||||
* @param {Point} p2
|
|
||||||
* @param {String} [key]
|
|
||||||
* @returns {Line}
|
|
||||||
*/
|
|
||||||
function newLine(p1, p2, key) {
|
|
||||||
return new Line(p1, p2, key);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function newOrderedLine(p1, p2, key) {
|
function newLine(p1, p2, key) {
|
||||||
return p1.key < p2.key ? newLine(p1,p2,key) : newLine(p2,p1,key);
|
return new Line(p1, p2, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function newOrderedLine(p1, p2, key) {
|
||||||
|
return p1.key < p2.key ? newLine(p1,p2,key) : newLine(p2,p1,key);
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Line = Line;
|
||||||
|
base.newLine = newLine;
|
||||||
|
base.newOrderedLine = newOrderedLine;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
569
src/geo/point.js
569
src/geo/point.js
|
|
@ -4,132 +4,97 @@
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
if (self.base.Point) return;
|
if (self.base.Point) return;
|
||||||
|
|
||||||
const BASE = self.base,
|
const base = self.base;
|
||||||
UTIL = BASE.util,
|
const { util, config, key } = base;
|
||||||
CONF = BASE.config,
|
const { round } = util;
|
||||||
KEYS = BASE.key,
|
|
||||||
ROUND = UTIL.round;
|
|
||||||
|
|
||||||
class Point {
|
class Point {
|
||||||
constructor(x,y,z,key) {
|
constructor(x, y, z, key) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.z = z || 0;
|
this.z = z || 0;
|
||||||
if (key) {
|
if (key) {
|
||||||
this._key = key;
|
this._key = key;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get key() {
|
|
||||||
if (this._key) {
|
|
||||||
return this._key;
|
|
||||||
}
|
|
||||||
return this._key = [
|
|
||||||
((this.x * 100000) | 0),
|
|
||||||
((this.y * 100000) | 0),
|
|
||||||
((this.z * 100000) | 0)
|
|
||||||
].join('');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const PRO = Point.prototype;
|
get key() {
|
||||||
|
if (this._key) {
|
||||||
|
return this._key;
|
||||||
|
}
|
||||||
|
return this._key = [
|
||||||
|
((this.x * 100000) | 0),
|
||||||
|
((this.y * 100000) | 0),
|
||||||
|
((this.z * 100000) | 0)
|
||||||
|
].join('');
|
||||||
|
}
|
||||||
|
|
||||||
BASE.Point = Point;
|
toClipper() {
|
||||||
|
|
||||||
BASE.newPoint = newPoint;
|
|
||||||
|
|
||||||
BASE.pointFromClipper = function(cp, z) {
|
|
||||||
return newPoint(cp.X / CONF.clipper, cp.Y / CONF.clipper, z);
|
|
||||||
};
|
|
||||||
|
|
||||||
/** ******************************************************************
|
|
||||||
* Point Prototype Functions
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
PRO.toClipper = function() {
|
|
||||||
return {
|
return {
|
||||||
X: this.x * CONF.clipper,
|
X: this.x * config.clipper,
|
||||||
Y: this.y * CONF.clipper
|
Y: this.y * config.clipper
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
PRO.setZ = function(z) {
|
setZ(z) {
|
||||||
this.z = z;
|
this.z = z;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
PRO.swapXZ = function() {
|
swapXZ() {
|
||||||
let p = this,
|
let p = this,
|
||||||
t = p.x;
|
t = p.x;
|
||||||
p.x = p.z;
|
p.x = p.z;
|
||||||
p.z = t;
|
p.z = t;
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.swapYZ = function() {
|
swapYZ() {
|
||||||
let p = this,
|
let p = this,
|
||||||
t = p.y;
|
t = p.y;
|
||||||
p.y = p.z;
|
p.y = p.z;
|
||||||
p.z = t;
|
p.z = t;
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.round = function(precision) {
|
round(precision) {
|
||||||
return newPoint(
|
return newPoint(
|
||||||
this.x.round(precision),
|
this.x.round(precision),
|
||||||
this.y.round(precision),
|
this.y.round(precision),
|
||||||
this.z.round(precision));
|
this.z.round(precision));
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.addFacet = function(facet) {
|
addFacet(facet) {
|
||||||
if (!this.group) this.group = [];
|
if (!this.group) this.group = [];
|
||||||
this.group.push(facet);
|
this.group.push(facet);
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.rekey = function() {
|
rekey() {
|
||||||
this._key = undefined;
|
this._key = undefined;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.toString = function() {
|
toString() {
|
||||||
return this.key;
|
return this.key;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
clone() {
|
||||||
* @returns {Point}
|
|
||||||
*/
|
|
||||||
PRO.clone = function() {
|
|
||||||
return newPoint(this.x, this.y, this.z, this._key);
|
return newPoint(this.x, this.y, this.z, this._key);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
slopeTo(p) {
|
||||||
* @param {Point} p
|
return base.newSlope(this, p);
|
||||||
* @returns {Slope}
|
}
|
||||||
*/
|
|
||||||
PRO.slopeTo = function(p) {
|
|
||||||
return BASE.newSlope(this, p);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
lineTo(p, k) {
|
||||||
*
|
return base.newLine(this, p, k);
|
||||||
* @param {Point} p
|
}
|
||||||
* @param {String} [k]
|
|
||||||
* @returns {Line}
|
|
||||||
*/
|
|
||||||
PRO.lineTo = function(p, k) {
|
|
||||||
return BASE.newLine(this, p, k);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
isNear(p, dist) {
|
||||||
* @param {Point} p
|
return util.isCloseTo(this.x, p.x, dist) && util.isCloseTo(this.y, p.y, dist);
|
||||||
* @param {number} [dist]
|
}
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
PRO.isNear = function(p, dist) {
|
|
||||||
return UTIL.isCloseTo(this.x, p.x, dist) && UTIL.isCloseTo(this.y, p.y, dist);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return distance to line connecting points p1, p2
|
* return distance to line connecting points p1, p2
|
||||||
|
|
@ -139,10 +104,9 @@
|
||||||
* @param {Point} p2
|
* @param {Point} p2
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
PRO.distToLine = function(p1, p2) {
|
distToLine(p1, p2) {
|
||||||
// return p2l(this, p1, p2);
|
|
||||||
return Math.sqrt(this.distToLineSq(p1, p2));
|
return Math.sqrt(this.distToLineSq(p1, p2));
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* used exclusively in new fill code. output does not agree with
|
* used exclusively in new fill code. output does not agree with
|
||||||
|
|
@ -151,69 +115,27 @@
|
||||||
* offset clipping. both need to be investigated and a single line
|
* offset clipping. both need to be investigated and a single line
|
||||||
* normal distance needs to be formulated to replace both functions.
|
* normal distance needs to be formulated to replace both functions.
|
||||||
*/
|
*/
|
||||||
PRO.distToLineNew = function(p1, p2) {
|
distToLineNew(p1, p2) {
|
||||||
return p2l(this, p1, p2);
|
return p2l(this, p1, p2);
|
||||||
// return Math.sqrt(this.distToLineSq(p1, p2));
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return square of distance to line connecting points p1, p2
|
* return square of distance to line connecting points p1, p2
|
||||||
* distance is calculated on the perpendicular (normal) to line
|
* distance is calculated on the perpendicular (normal) to line
|
||||||
*
|
|
||||||
* @param {Point} p1
|
|
||||||
* @param {Point} p2
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
*/
|
||||||
PRO.distToLineSq = function(p1, p2) {
|
distToLineSq(p1, p2) {
|
||||||
let p = this,
|
let p = this,
|
||||||
d = UTIL.distSq(p1, p2);
|
d = util.distSq(p1, p2);
|
||||||
|
|
||||||
let t = ((p.x - p1.x) * (p2.x - p1.x) + (p.y - p1.y) * (p2.y - p1.y)) / d;
|
let t = ((p.x - p1.x) * (p2.x - p1.x) + (p.y - p1.y) * (p2.y - p1.y)) / d;
|
||||||
|
|
||||||
if (t < 0) return UTIL.distSq(p, p1);
|
if (t < 0) return util.distSq(p, p1);
|
||||||
if (t > 1) return UTIL.distSq(p, p2);
|
if (t > 1) return util.distSq(p, p2);
|
||||||
|
|
||||||
return UTIL.distSqv2(p.x, p.y, p1.x + t * (p2.x - p1.x), p1.y + t * (p2.y - p1.y));
|
return util.distSqv2(p.x, p.y, p1.x + t * (p2.x - p1.x), p1.y + t * (p2.y - p1.y));
|
||||||
};
|
|
||||||
|
|
||||||
// ---( begin fix distToLine )---
|
|
||||||
|
|
||||||
function dot(u, v) {
|
|
||||||
return u.x * v.x + u.y * v.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function norm(v) {
|
withinDist2(p1, p2, dist2) {
|
||||||
return Math.sqrt(dot(v,v));
|
|
||||||
}
|
|
||||||
|
|
||||||
function d(u, v) {
|
|
||||||
return norm({x: u.x - v.x, y: u.y - v.y});
|
|
||||||
}
|
|
||||||
|
|
||||||
function p2l(p, l1, l2) {
|
|
||||||
let v = {x: l2.x - l1.x, y: l2.y - l1.y};
|
|
||||||
let w = {x: p.x - l1.x, y: p.y - l1.y};
|
|
||||||
let c1 = dot(w, v);
|
|
||||||
let c2 = dot(v, v);
|
|
||||||
let b = c1 / c2;
|
|
||||||
if (isNaN(b)) {
|
|
||||||
// console.log('nan', {p, l1, l2, v, w, c1, c2});
|
|
||||||
b = 0;
|
|
||||||
}
|
|
||||||
let pb = {x: l1.x + b * v.x, y: l1.y + b * v.y};
|
|
||||||
return d(p, pb);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---( end fix distToLine )---
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {Point} p1
|
|
||||||
* @param {Point} p2
|
|
||||||
* @param {number} dist2
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
PRO.withinDist2 = function(p1, p2, dist2) {
|
|
||||||
let ll2 = p1.distToSq2D(p2),
|
let ll2 = p1.distToSq2D(p2),
|
||||||
dp1 = this.distToSq2D(p1),
|
dp1 = this.distToSq2D(p1),
|
||||||
dp2 = this.distToSq2D(p2);
|
dp2 = this.distToSq2D(p2);
|
||||||
|
|
@ -231,71 +153,53 @@
|
||||||
// dist2 to the described line segment.
|
// dist2 to the described line segment.
|
||||||
if (dp1 > ll2 && dp2 > ll2) return false;
|
if (dp1 > ll2 && dp2 > ll2) return false;
|
||||||
return this.distToLineSq(p1, p2) < dist2;
|
return this.distToLineSq(p1, p2) < dist2;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
midPointTo(p2) {
|
||||||
* @param {Point} p2
|
return newPoint((this.x + p2.x) / 2, (this.y + p2.y) / 2, this.z);
|
||||||
* @returns {Point}
|
}
|
||||||
*/
|
|
||||||
PRO.midPointTo = function(p2) {
|
|
||||||
return newPoint((this.x + p2.x)/2, (this.y + p2.y)/2, this.z);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
midPointTo3D(p2) {
|
||||||
* @param {Point} p2
|
|
||||||
* @returns {Point}
|
|
||||||
*/
|
|
||||||
PRO.midPointTo3D = function(p2) {
|
|
||||||
return newPoint(
|
return newPoint(
|
||||||
(this.x + p2.x)/2,
|
(this.x + p2.x) / 2,
|
||||||
(this.y + p2.y)/2,
|
(this.y + p2.y) / 2,
|
||||||
(this.z + p2.z)/2
|
(this.z + p2.z) / 2
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* non-scale corrected version of follow()
|
* non-scale corrected version of follow()
|
||||||
*
|
|
||||||
* @param slope
|
|
||||||
* @param mult
|
|
||||||
* @returns {Point}
|
|
||||||
*/
|
*/
|
||||||
PRO.projectOnSlope = function(slope, mult) {
|
projectOnSlope(slope, mult) {
|
||||||
return newPoint(
|
return newPoint(
|
||||||
this.x + slope.dx * mult,
|
this.x + slope.dx * mult,
|
||||||
this.y + slope.dy * mult,
|
this.y + slope.dy * mult,
|
||||||
this.z);
|
this.z);
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.followTo = function(point, mult) {
|
followTo(point, mult) {
|
||||||
return this.follow(this.slopeTo(point), mult);
|
return this.follow(this.slopeTo(point), mult);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return a point along the line this from point to p2 offset
|
* return a point along the line this from point to p2 offset
|
||||||
* by a distance. positive distances are closer to this point.
|
* by a distance. positive distances are closer to this point.
|
||||||
*
|
|
||||||
* @param p2
|
|
||||||
* @param dist
|
|
||||||
*/
|
*/
|
||||||
PRO.offsetPointFrom = function(p2, dist) {
|
offsetPointFrom(p2, dist) {
|
||||||
let p1 = this,
|
let p1 = this,
|
||||||
dx = p2.x - p1.x,
|
dx = p2.x - p1.x,
|
||||||
dy = p2.y - p1.y,
|
dy = p2.y - p1.y,
|
||||||
ls = dist / Math.sqrt(dx * dx + dy * dy),
|
ls = dist / Math.sqrt(dx * dx + dy * dy),
|
||||||
ox = dx * ls,
|
ox = dx * ls,
|
||||||
oy = dy * ls;
|
oy = dy * ls;
|
||||||
return newPoint(p2.x - ox, p2.y - oy, p2.z, KEYS.NONE);
|
return newPoint(p2.x - ox, p2.y - oy, p2.z, key.NONE);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return a point along the line this from point to p2 offset
|
* return a point along the line this from point to p2 offset
|
||||||
* by a distance. positive distances are farther from this point.
|
* by a distance. positive distances are farther from this point.
|
||||||
*
|
|
||||||
* @param p2
|
|
||||||
* @param dist
|
|
||||||
*/
|
*/
|
||||||
PRO.offsetPointTo = function(p2, dist) {
|
offsetPointTo(p2, dist) {
|
||||||
let p1 = this,
|
let p1 = this,
|
||||||
dx = p2.x - p1.x,
|
dx = p2.x - p1.x,
|
||||||
dy = p2.y - p1.y;
|
dy = p2.y - p1.y;
|
||||||
|
|
@ -306,93 +210,82 @@
|
||||||
ox = dx * ls,
|
ox = dx * ls,
|
||||||
oy = dy * ls;
|
oy = dy * ls;
|
||||||
|
|
||||||
return newPoint(p1.x + ox, p1.y + oy, p2.z, KEYS.NONE);
|
return newPoint(p1.x + ox, p1.y + oy, p2.z, key.NONE);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
offsetLineTo(p2, offset) {
|
||||||
* @param {Point} p2
|
|
||||||
* @param {number} offset
|
|
||||||
* @returns {Line}
|
|
||||||
*/
|
|
||||||
PRO.offsetLineTo = function(p2, offset) {
|
|
||||||
let p1 = this,
|
let p1 = this,
|
||||||
dx = p2.x - p1.x,
|
dx = p2.x - p1.x,
|
||||||
dy = p2.y - p1.y,
|
dy = p2.y - p1.y,
|
||||||
ls = offset / Math.sqrt(dx * dx + dy * dy),
|
ls = offset / Math.sqrt(dx * dx + dy * dy),
|
||||||
ox = dx * ls,
|
ox = dx * ls,
|
||||||
oy = dy * ls,
|
oy = dy * ls,
|
||||||
np1 = newPoint(p1.x - oy, p1.y + ox, p1.z, KEYS.NONE),
|
np1 = newPoint(p1.x - oy, p1.y + ox, p1.z, key.NONE),
|
||||||
np2 = newPoint(p2.x - oy, p2.y + ox, p2.z, KEYS.NONE);
|
np2 = newPoint(p2.x - oy, p2.y + ox, p2.z, key.NONE);
|
||||||
np1.op = p1;
|
np1.op = p1;
|
||||||
np2.op = p2;
|
np2.op = p2;
|
||||||
return BASE.newLine(np1, np2, KEYS.NONE);
|
return base.newLine(np1, np2, key.NONE);
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.offset = function(x, y, z) {
|
offset(x, y, z) {
|
||||||
return newPoint(this.x + x, this.y + y, this.z + z);
|
return newPoint(this.x + x, this.y + y, this.z + z);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* checks if a point is inside of a polygon
|
* checks if a point is inside of a polygon
|
||||||
* does not check children/holes
|
* does not check children/holes
|
||||||
*
|
|
||||||
* @param {Polygon} poly
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.inPolygon = function(poly) {
|
inPolygon(poly) {
|
||||||
if (!poly.bounds.containsXY(this.x, this.y)) return false;
|
if (!poly.bounds.containsXY(this.x, this.y)) return false;
|
||||||
|
|
||||||
let p = poly.points, pl = p.length, p1, p2, i, inside = false;
|
let p = poly.points,
|
||||||
|
pl = p.length,
|
||||||
|
p1, p2, i, inside = false;
|
||||||
|
|
||||||
for (i=0; i<pl; i++) {
|
for (i = 0; i < pl; i++) {
|
||||||
p1 = p[i];
|
p1 = p[i];
|
||||||
p2 = p[(i+1)%pl];
|
p2 = p[(i + 1) % pl];
|
||||||
if ((p1.y >= this.y) != (p2.y >= this.y) &&
|
if ((p1.y >= this.y) != (p2.y >= this.y) &&
|
||||||
(this.x <= (p2.x - p1.x) * (this.y - p1.y) / (p2.y - p1.y) + p1.x))
|
(this.x <= (p2.x - p1.x) * (this.y - p1.y) / (p2.y - p1.y) + p1.x)) {
|
||||||
{
|
|
||||||
inside = !inside;
|
inside = !inside;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return inside;
|
return inside;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if the point is inside of a polygon but
|
* returns true if the point is inside of a polygon but
|
||||||
* not inside any of it's children
|
* not inside any of it's children
|
||||||
*
|
|
||||||
* @param {Polygon | Polygon[]} poly
|
|
||||||
* @return {boolean} true if inside outer but not inner
|
|
||||||
*/
|
*/
|
||||||
PRO.isInPolygon = function(poly) {
|
isInPolygon(poly) {
|
||||||
let point = this, i;
|
let point = this,
|
||||||
|
i;
|
||||||
if (Array.isArray(poly)) {
|
if (Array.isArray(poly)) {
|
||||||
for (i=0; i<poly.length; i++) {
|
for (i = 0; i < poly.length; i++) {
|
||||||
if (point.isInPolygon(poly[i])) return true;
|
if (point.isInPolygon(poly[i])) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let holes = poly.inner;
|
let holes = poly.inner;
|
||||||
if (point.inPolygon(poly) || point.nearPolygon(poly, CONF.precision_merge_sq)) {
|
if (point.inPolygon(poly) || point.nearPolygon(poly, config.precision_merge_sq)) {
|
||||||
for (i=0; holes && i < holes.length; i++) {
|
for (i = 0; holes && i < holes.length; i++) {
|
||||||
if (point.inPolygon(holes[i]) && !point.nearPolygon(holes[i], CONF.precision_merge_sq)) return false;
|
if (point.inPolygon(holes[i]) && !point.nearPolygon(holes[i], config.precision_merge_sq)) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if the point is inside of a polygon but
|
* returns true if the point is inside of a polygon but
|
||||||
* not inside any of it's children
|
* not inside any of it's children
|
||||||
*
|
|
||||||
* @param {Polygon | Polygon[]} poly
|
|
||||||
* @return {boolean} true if inside outer but not inner
|
|
||||||
*/
|
*/
|
||||||
PRO.isInPolygonOnly = function(poly) {
|
isInPolygonOnly(poly) {
|
||||||
let point = this, i;
|
let point = this,
|
||||||
|
i;
|
||||||
if (Array.isArray(poly)) {
|
if (Array.isArray(poly)) {
|
||||||
for (i=0; i<poly.length; i++) {
|
for (i = 0; i < poly.length; i++) {
|
||||||
if (point.isInPolygonOnly(poly[i])) {
|
if (point.isInPolygonOnly(poly[i])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -401,129 +294,96 @@
|
||||||
}
|
}
|
||||||
let holes = poly.inner;
|
let holes = poly.inner;
|
||||||
if (point.inPolygon(poly)) {
|
if (point.inPolygon(poly)) {
|
||||||
for (i=0; holes && i < holes.length; i++) {
|
for (i = 0; holes && i < holes.length; i++) {
|
||||||
if (point.inPolygon(holes[i])) return false;
|
if (point.inPolygon(holes[i])) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* checks if point is near polygon edge. distance is squared.
|
* checks if point is near polygon edge. distance is squared.
|
||||||
*
|
|
||||||
* @param {Polygon} poly
|
|
||||||
* @param {number} dist2
|
|
||||||
* @param {boolean} [inner] process inner polygons
|
* @param {boolean} [inner] process inner polygons
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.nearPolygon = function(poly, dist2, inner) {
|
nearPolygon(poly, dist2, inner) {
|
||||||
// throw new Error("nearPolygon");
|
// throw new Error("nearPolygon");
|
||||||
for (let i=0, p=poly.points, pl=p.length ; i<pl; i++) {
|
for (let i = 0, p = poly.points, pl = p.length; i < pl; i++) {
|
||||||
if (this.withinDist2(p[i], p[(i+1)%pl], dist2)) {
|
if (this.withinDist2(p[i], p[(i + 1) % pl], dist2)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (inner && poly.inner) {
|
if (inner && poly.inner) {
|
||||||
for (let i=0; i<poly.inner.length; i++) {
|
for (let i = 0; i < poly.inner.length; i++) {
|
||||||
if (this.nearPolygon(poly.inner[i], dist2)) return true;
|
if (this.nearPolygon(poly.inner[i], dist2)) return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if point will not be trimmed later
|
* returns true if point will not be trimmed later
|
||||||
*
|
|
||||||
* @param {Polygon} poly
|
|
||||||
* @param {number} offset
|
|
||||||
* @param {number} mindist2
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.insideOffset = function(poly, offset, mindist2) {
|
insideOffset(poly, offset, mindist2) {
|
||||||
return this.inPolygon(poly) === (offset > 0) && !this.nearPolygon(poly, mindist2);
|
return this.inPolygon(poly) === (offset > 0) && !this.nearPolygon(poly, mindist2);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns a new point following given slope for given distance
|
* returns a new point following given slope for given distance
|
||||||
* same as projectOnSlope() but scaled
|
* same as projectOnSlope() but scaled
|
||||||
*
|
|
||||||
* @param {Slope} slope
|
|
||||||
* @param {number} distance
|
|
||||||
* @returns {Point}
|
|
||||||
*/
|
*/
|
||||||
PRO.follow = function(slope, distance) {
|
follow(slope, distance) {
|
||||||
let ls = distance / Math.sqrt(slope.dx * slope.dx + slope.dy * slope.dy);
|
let ls = distance / Math.sqrt(slope.dx * slope.dx + slope.dy * slope.dy);
|
||||||
return newPoint(this.x + slope.dx * ls, this.y + slope.dy * ls, this.z);
|
return newPoint(this.x + slope.dx * ls, this.y + slope.dy * ls, this.z);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* for point, return z-plane intersecting point on line to next point
|
* for point, return intersecting point on z to next point if points
|
||||||
*
|
* are on either size of z
|
||||||
* @param {Point} p
|
|
||||||
* @param {number} z
|
|
||||||
* @returns {Point}
|
|
||||||
*/
|
*/
|
||||||
PRO.intersectZ = function(p, z) {
|
intersectZ(p, z) {
|
||||||
let dx = p.x - this.x,
|
let dx = p.x - this.x,
|
||||||
dy = p.y - this.y,
|
dy = p.y - this.y,
|
||||||
dz = p.z - this.z,
|
dz = p.z - this.z,
|
||||||
pct = 1 - ((p.z - z) / dz);
|
pct = 1 - ((p.z - z) / dz);
|
||||||
return newPoint(this.x + dx * pct, this.y + dy * pct, this.z + dz * pct);
|
return newPoint(this.x + dx * pct, this.y + dy * pct, this.z + dz * pct);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
isEqual2D(p) {
|
||||||
* @param {Point} p
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
PRO.isEqual2D = function(p) {
|
|
||||||
return this === p || (this.x === p.x && this.y === p.y);
|
return this === p || (this.x === p.x && this.y === p.y);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if points are close enough to be considered equivalent
|
* returns true if points are close enough to be considered equivalent
|
||||||
*
|
|
||||||
* @param {Point} p
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.isMergable2D = function(p) {
|
isMergable2D(p) {
|
||||||
return this.isEqual2D(p) || (this.distToSq2D(p) < CONF.precision_merge_sq);
|
return this.isEqual2D(p) || (this.distToSq2D(p) < config.precision_merge_sq);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* compares 3D point
|
* compares 3D point
|
||||||
*
|
|
||||||
* @param {Point} p
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.isEqual = function(p) {
|
isEqual(p) {
|
||||||
return this === p || (this.x === p.x && this.y === p.y && this.z === p.z);
|
return this === p || (this.x === p.x && this.y === p.y && this.z === p.z);
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.isEqual2D = function(p) {
|
isEqual2D(p) {
|
||||||
return this === p || (this.x === p.x && this.y === p.y);
|
return this === p || (this.x === p.x && this.y === p.y);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if points are close enough to be considered equivalent
|
* returns true if points are close enough to be considered equivalent
|
||||||
*
|
|
||||||
* @param {Point} p
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.isMergable3D = function(p) {
|
isMergable3D(p) {
|
||||||
return this.isEqual(p) || (this.distToSq3D(p) < CONF.precision_merge_sq);
|
return this.isEqual(p) || (this.distToSq3D(p) < config.precision_merge_sq);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return true if point is inside 2D square size dist*2 around p
|
* return true if point is inside 2D square size dist*2 around p
|
||||||
*
|
|
||||||
* @param {Point} p
|
|
||||||
* @param {number} dist
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.isInBox = function(p, dist) {
|
isInBox(p, dist) {
|
||||||
return Math.abs(this.x - p.x) < dist && Math.abs(this.y - p.y) < dist;
|
return Math.abs(this.x - p.x) < dist && Math.abs(this.y - p.y) < dist;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return min distance from point to a polygon
|
* return min distance from point to a polygon
|
||||||
|
|
@ -532,7 +392,7 @@
|
||||||
* @param {Polygon} poly
|
* @param {Polygon} poly
|
||||||
* @param {number} [threshold] stop looking if under threshold
|
* @param {number} [threshold] stop looking if under threshold
|
||||||
*/
|
*/
|
||||||
PRO.distToPolySegments = function(poly, threshold) {
|
distToPolySegments(poly, threshold) {
|
||||||
let point = this,
|
let point = this,
|
||||||
mindist = Infinity;
|
mindist = Infinity;
|
||||||
poly.forEachSegment(function(p1, p2) {
|
poly.forEachSegment(function(p1, p2) {
|
||||||
|
|
@ -542,32 +402,31 @@
|
||||||
if (mindist <= threshold) return true;
|
if (mindist <= threshold) return true;
|
||||||
});
|
});
|
||||||
return mindist;
|
return mindist;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Polygon} poly
|
* @param {Polygon} poly
|
||||||
* @param {number} [threshold] stop looking if under threshold
|
* @param {number} [threshold] stop looking if under threshold
|
||||||
*/
|
*/
|
||||||
PRO.distToPolyPoints = function(poly, threshold) {
|
distToPolyPoints(poly, threshold) {
|
||||||
let point = this, mindist = Infinity;
|
let point = this,
|
||||||
|
mindist = Infinity;
|
||||||
poly.forEachPoint(function(pp) {
|
poly.forEachPoint(function(pp) {
|
||||||
mindist = Math.min(mindist, point.distTo2D(pp));
|
mindist = Math.min(mindist, point.distTo2D(pp));
|
||||||
if (mindist < threshold) return true;
|
if (mindist < threshold) return true;
|
||||||
});
|
});
|
||||||
return mindist;
|
return mindist;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Point[]} points
|
|
||||||
* @param {number} max
|
|
||||||
* @returns {Point} nearest point (less than max) from array to this point
|
* @returns {Point} nearest point (less than max) from array to this point
|
||||||
*/
|
*/
|
||||||
PRO.nearestTo = function(points, max) {
|
nearestTo(points, max) {
|
||||||
if (!max) throw "missing max";
|
if (!max) throw "missing max";
|
||||||
let mind = Infinity,
|
let mind = Infinity,
|
||||||
minp = null,
|
minp = null,
|
||||||
i, p, d;
|
i, p, d;
|
||||||
for (i=0; i<points.length; i++) {
|
for (i = 0; i < points.length; i++) {
|
||||||
p = points[i];
|
p = points[i];
|
||||||
if (p === this || p.del) continue;
|
if (p === this || p.del) continue;
|
||||||
d = this.distToSq2D(p);
|
d = this.distToSq2D(p);
|
||||||
|
|
@ -577,14 +436,16 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return minp;
|
return minp;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Point[]} points
|
* @param {Point[]} points
|
||||||
* @return {number} average square dist to cloud of points
|
* @return {number} average square dist to cloud of points
|
||||||
*/
|
*/
|
||||||
PRO.averageDistTo = function(points) {
|
averageDistTo(points) {
|
||||||
let sum = 0.0, count = 0, i;
|
let sum = 0.0,
|
||||||
|
count = 0,
|
||||||
|
i;
|
||||||
for (i = 0; i < points.length; i++) {
|
for (i = 0; i < points.length; i++) {
|
||||||
if (points[i] != this) {
|
if (points[i] != this) {
|
||||||
sum += this.distToSq2D(points[i]);
|
sum += this.distToSq2D(points[i]);
|
||||||
|
|
@ -592,125 +453,125 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sum / count;
|
return sum / count;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* dist to point in 2D
|
* dist to point in 2D
|
||||||
*
|
|
||||||
* @param {Point} p
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
*/
|
||||||
PRO.distTo2D = function(p) {
|
distTo2D(p) {
|
||||||
let dx = this.x - p.x,
|
let dx = this.x - p.x,
|
||||||
dy = this.y - p.y;
|
dy = this.y - p.y;
|
||||||
return Math.sqrt(dx * dx + dy * dy);
|
return Math.sqrt(dx * dx + dy * dy);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* square of distance in 2D
|
* square of distance in 2D
|
||||||
*
|
|
||||||
* @param {Point} p
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
*/
|
||||||
PRO.distToSq2D = function(p) {
|
distToSq2D(p) {
|
||||||
let dx = this.x - p.x,
|
let dx = this.x - p.x,
|
||||||
dy = this.y - p.y;
|
dy = this.y - p.y;
|
||||||
return dx * dx + dy * dy;
|
return dx * dx + dy * dy;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.distTo3D = function(p) {
|
distTo3D(p) {
|
||||||
let dx = this.x - p.x,
|
let dx = this.x - p.x,
|
||||||
dy = this.y - p.y,
|
dy = this.y - p.y,
|
||||||
dz = this.z - p.z;
|
dz = this.z - p.z;
|
||||||
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* square of distance in 3D
|
* square of distance in 3D
|
||||||
*
|
|
||||||
* @param {Point} p
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
*/
|
||||||
PRO.distToSq3D = function(p) {
|
distToSq3D(p) {
|
||||||
let dx = this.x - p.x,
|
let dx = this.x - p.x,
|
||||||
dy = this.y - p.y,
|
dy = this.y - p.y,
|
||||||
dz = this.z - p.z;
|
dz = this.z - p.z;
|
||||||
return dx * dx + dy * dy + dz * dz;
|
return dx * dx + dy * dy + dz * dz;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if point is inside triangle described by three points
|
* returns true if point is inside triangle described by three points
|
||||||
*
|
|
||||||
* @param {Point} a
|
|
||||||
* @param {Point} b
|
|
||||||
* @param {Point} c
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.inTriangle = function(a, b, c) {
|
inTriangle(a, b, c) {
|
||||||
let as_x = this.x - a.x,
|
let as_x = this.x - a.x,
|
||||||
as_y = this.y - a.y,
|
as_y = this.y - a.y,
|
||||||
s_ab = (b.x - a.x) * as_y - (b.y - a.y) * as_x > 0;
|
s_ab = (b.x - a.x) * as_y - (b.y - a.y) * as_x > 0;
|
||||||
if ((c.x - a.x) * as_y - (c.y - a.y) * as_x > 0 == s_ab) return false;
|
if ((c.x - a.x) * as_y - (c.y - a.y) * as_x > 0 == s_ab) return false;
|
||||||
if ((c.x - b.x) * (this.y - b.y) - (c.y - b.y) * (this.x - b.x) > 0 != s_ab) return false;
|
if ((c.x - b.x) * (this.y - b.y) - (c.y - b.y) * (this.x - b.x) > 0 != s_ab) return false;
|
||||||
return true;
|
return true;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if point is on a line described by two points.
|
* returns true if point is on a line described by two points.
|
||||||
* test sum of distances p1->this + this->p2 ~= p1->p2 whens
|
* test sum of distances p1->this + this->p2 ~= p1->p2 whens
|
||||||
* slopes from p1->this same as this->p2
|
* slopes from p1->this same as this->p2
|
||||||
*
|
|
||||||
* @param {Point} p1
|
|
||||||
* @param {Point} p2
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
PRO.onLine = function(p1, p2) {
|
onLine(p1, p2) {
|
||||||
return this.distToLine(p1, p2) < CONF.precision_point_on_line;
|
return this.distToLine(p1, p2) < config.precision_point_on_line;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
add(delta) {
|
||||||
*
|
|
||||||
* @param {THREE.Vector3} delta
|
|
||||||
* @return {Point} new offset point
|
|
||||||
*/
|
|
||||||
PRO.add = function(delta) {
|
|
||||||
return newPoint(this.x + delta.x, this.y + delta.y, this.z + delta.z);
|
return newPoint(this.x + delta.x, this.y + delta.y, this.z + delta.z);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
sub(delta) {
|
||||||
*
|
|
||||||
* @param {THREE.Vector3} delta
|
|
||||||
* @return {Point} new offset point
|
|
||||||
*/
|
|
||||||
PRO.sub = function(delta) {
|
|
||||||
return newPoint(this.x - delta.x, this.y - delta.y, this.z - delta.z);
|
return newPoint(this.x - delta.x, this.y - delta.y, this.z - delta.z);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
move(delta) {
|
||||||
*
|
|
||||||
* @param {THREE.Vector3} delta
|
|
||||||
*/
|
|
||||||
PRO.move = function(delta) {
|
|
||||||
this.x += delta.x;
|
this.x += delta.x;
|
||||||
this.y += delta.y;
|
this.y += delta.y;
|
||||||
this.z += delta.z;
|
this.z += delta.z;
|
||||||
return this;
|
return this;
|
||||||
};
|
|
||||||
|
|
||||||
/** ******************************************************************
|
|
||||||
* Connect to base and Helpers
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {number} x
|
|
||||||
* @param {number} y
|
|
||||||
* @param {number} z
|
|
||||||
* @param {String} [key]
|
|
||||||
* @returns {Point}
|
|
||||||
*/
|
|
||||||
function newPoint(x, y, z, key) {
|
|
||||||
return new Point(x, y, z, key);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function dot(u, v) {
|
||||||
|
return u.x * v.x + u.y * v.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
function norm(v) {
|
||||||
|
return Math.sqrt(dot(v, v));
|
||||||
|
}
|
||||||
|
|
||||||
|
function d(u, v) {
|
||||||
|
return norm({
|
||||||
|
x: u.x - v.x,
|
||||||
|
y: u.y - v.y
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function p2l(p, l1, l2) {
|
||||||
|
let v = {
|
||||||
|
x: l2.x - l1.x,
|
||||||
|
y: l2.y - l1.y
|
||||||
|
};
|
||||||
|
let w = {
|
||||||
|
x: p.x - l1.x,
|
||||||
|
y: p.y - l1.y
|
||||||
|
};
|
||||||
|
let c1 = dot(w, v);
|
||||||
|
let c2 = dot(v, v);
|
||||||
|
let b = c1 / c2;
|
||||||
|
if (isNaN(b)) {
|
||||||
|
// console.log('nan', {p, l1, l2, v, w, c1, c2});
|
||||||
|
b = 0;
|
||||||
|
}
|
||||||
|
let pb = {
|
||||||
|
x: l1.x + b * v.x,
|
||||||
|
y: l1.y + b * v.y
|
||||||
|
};
|
||||||
|
return d(p, pb);
|
||||||
|
}
|
||||||
|
function newPoint(x, y, z, key) {
|
||||||
|
return new Point(x, y, z, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Point = Point;
|
||||||
|
base.newPoint = newPoint;
|
||||||
|
base.pointFromClipper = function(cp, z) {
|
||||||
|
return newPoint(cp.X / config.clipper, cp.Y / config.clipper, z);
|
||||||
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -4,128 +4,125 @@
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
const BASE = self.base;
|
const base = self.base;
|
||||||
const CONF = BASE.config;
|
const { config } = base;
|
||||||
|
|
||||||
BASE.verticesToPoints = verticesToPoints;
|
base.verticesToPoints = verticesToPoints;
|
||||||
BASE.pointsToVertices = pointsToVertices;
|
base.pointsToVertices = pointsToVertices;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* converts a geometry point array into a kiri point array
|
* converts a geometry point array into a kiri point array
|
||||||
* with auto-decimation
|
* with auto-decimation
|
||||||
*
|
*/
|
||||||
* @param {Float32Array} array
|
function verticesToPoints(array, options) {
|
||||||
* @returns {Array}
|
let parr = new Array(array.length / 3),
|
||||||
*/
|
i = 0,
|
||||||
function verticesToPoints(array, options) {
|
j = 0,
|
||||||
let parr = new Array(array.length / 3),
|
t = Date.now(),
|
||||||
i = 0,
|
hash = {},
|
||||||
j = 0,
|
unique = 0,
|
||||||
t = Date.now(),
|
passes = 0,
|
||||||
hash = {},
|
points,
|
||||||
unique = 0,
|
oldpoints = parr.length,
|
||||||
passes = 0,
|
newpoints;
|
||||||
points,
|
|
||||||
oldpoints = parr.length,
|
|
||||||
newpoints;
|
|
||||||
|
|
||||||
// replace point objects with their equivalents
|
// replace point objects with their equivalents
|
||||||
while (i < array.length) {
|
while (i < array.length) {
|
||||||
let p = BASE.newPoint(array[i++], array[i++], array[i++]),
|
let p = base.newPoint(array[i++], array[i++], array[i++]),
|
||||||
k = p.key,
|
k = p.key,
|
||||||
m = hash[k];
|
m = hash[k];
|
||||||
if (!m) {
|
if (!m) {
|
||||||
m = p;
|
m = p;
|
||||||
hash[k] = p;
|
hash[k] = p;
|
||||||
unique++;
|
unique++;
|
||||||
}
|
|
||||||
parr[j++] = m;
|
|
||||||
}
|
}
|
||||||
|
parr[j++] = m;
|
||||||
|
}
|
||||||
|
|
||||||
let {threshold, precision, maxpass} = options || {};
|
let {threshold, precision, maxpass} = options || {};
|
||||||
// threshold = point count for triggering decimation
|
// threshold = point count for triggering decimation
|
||||||
// precision = under which points are merged
|
// precision = under which points are merged
|
||||||
// maxpass = max number of decimations
|
// maxpass = max number of decimations
|
||||||
threshold = threshold > 0 ? threshold : CONF.decimate_threshold;
|
threshold = threshold > 0 ? threshold : config.decimate_threshold;
|
||||||
precision = precision >= 0 ? precision : CONF.precision_decimate;
|
precision = precision >= 0 ? precision : config.precision_decimate;
|
||||||
maxpass = maxpass >= 0 ? maxpass : 10;
|
maxpass = maxpass >= 0 ? maxpass : 10;
|
||||||
|
|
||||||
// decimate until all point spacing > precision
|
// decimate until all point spacing > precision
|
||||||
if (maxpass && precision > 0.0)
|
if (maxpass && precision > 0.0)
|
||||||
while (parr.length > threshold) {
|
while (parr.length > threshold) {
|
||||||
let lines = [], line, dec = 0;
|
let lines = [], line, dec = 0;
|
||||||
for (i=0; i<oldpoints; ) {
|
for (i=0; i<oldpoints; ) {
|
||||||
let p1 = parr[i++],
|
let p1 = parr[i++],
|
||||||
p2 = parr[i++],
|
p2 = parr[i++],
|
||||||
p3 = parr[i++];
|
p3 = parr[i++];
|
||||||
lines.push( {p1:p1, p2:p2, d:Math.sqrt(p1.distToSq3D(p2))} );
|
lines.push( {p1:p1, p2:p2, d:Math.sqrt(p1.distToSq3D(p2))} );
|
||||||
lines.push( {p1:p1, p2:p3, d:Math.sqrt(p1.distToSq3D(p3))} );
|
lines.push( {p1:p1, p2:p3, d:Math.sqrt(p1.distToSq3D(p3))} );
|
||||||
lines.push( {p1:p2, p2:p3, d:Math.sqrt(p2.distToSq3D(p3))} );
|
lines.push( {p1:p2, p2:p3, d:Math.sqrt(p2.distToSq3D(p3))} );
|
||||||
}
|
|
||||||
// sort by ascending line length
|
|
||||||
lines.sort(function(a,b) {
|
|
||||||
return a.d - b.d
|
|
||||||
});
|
|
||||||
// create offset mid-points
|
|
||||||
for (i=0; i<lines.length; i++) {
|
|
||||||
line = lines[i];
|
|
||||||
// skip lines longer than precision threshold
|
|
||||||
if (line.d >= precision) break;
|
|
||||||
// skip lines where one of the points is already offset
|
|
||||||
if (line.p1.op || line.p2.op) continue;
|
|
||||||
// todo skip dropping lines where either point is a "sharp" on 3 vectors
|
|
||||||
// todo skip dropping lines where either point connects to a "long" line
|
|
||||||
line.p1.op = line.p2.op = line.p1.midPointTo3D(line.p2);
|
|
||||||
dec++;
|
|
||||||
}
|
|
||||||
// exit if nothing to decimate
|
|
||||||
if (dec === 0) break;
|
|
||||||
passes++;
|
|
||||||
// create new facets
|
|
||||||
points = new Array(oldpoints);
|
|
||||||
newpoints = 0;
|
|
||||||
for (i=0; i<oldpoints; ) {
|
|
||||||
let p1 = parr[i++],
|
|
||||||
p2 = parr[i++],
|
|
||||||
p3 = parr[i++];
|
|
||||||
// drop facets with two offset points
|
|
||||||
if (p1.op && p1.op === p2.op) continue;
|
|
||||||
if (p1.op && p1.op === p3.op) continue;
|
|
||||||
if (p2.op && p2.op === p3.op) continue;
|
|
||||||
// otherwise emit altered facet
|
|
||||||
points[newpoints++] = p1.op || p1;
|
|
||||||
points[newpoints++] = p2.op || p2;
|
|
||||||
points[newpoints++] = p3.op || p3;
|
|
||||||
}
|
|
||||||
parr = points.slice(0,newpoints);
|
|
||||||
oldpoints = newpoints;
|
|
||||||
if (passes >= maxpass) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// sort by ascending line length
|
||||||
// if (passes) console.trace({passes, threshold, precision, maxpass});
|
lines.sort(function(a,b) {
|
||||||
|
return a.d - b.d
|
||||||
if (passes) console.log({
|
|
||||||
before: array.length / 3,
|
|
||||||
after: parr.length,
|
|
||||||
unique: unique,
|
|
||||||
decimations: passes,
|
|
||||||
time: (Date.now() - t)
|
|
||||||
});
|
});
|
||||||
|
// create offset mid-points
|
||||||
return parr;
|
for (i=0; i<lines.length; i++) {
|
||||||
}
|
line = lines[i];
|
||||||
|
// skip lines longer than precision threshold
|
||||||
function pointsToVertices(points) {
|
if (line.d >= precision) break;
|
||||||
let vertices = new Float32Array(points.length * 3),
|
// skip lines where one of the points is already offset
|
||||||
i = 0, vi = 0;
|
if (line.p1.op || line.p2.op) continue;
|
||||||
while (i < points.length) {
|
// todo skip dropping lines where either point is a "sharp" on 3 vectors
|
||||||
vertices[vi++] = points[i].x;
|
// todo skip dropping lines where either point connects to a "long" line
|
||||||
vertices[vi++] = points[i].y;
|
line.p1.op = line.p2.op = line.p1.midPointTo3D(line.p2);
|
||||||
vertices[vi++] = points[i++].z;
|
dec++;
|
||||||
|
}
|
||||||
|
// exit if nothing to decimate
|
||||||
|
if (dec === 0) break;
|
||||||
|
passes++;
|
||||||
|
// create new facets
|
||||||
|
points = new Array(oldpoints);
|
||||||
|
newpoints = 0;
|
||||||
|
for (i=0; i<oldpoints; ) {
|
||||||
|
let p1 = parr[i++],
|
||||||
|
p2 = parr[i++],
|
||||||
|
p3 = parr[i++];
|
||||||
|
// drop facets with two offset points
|
||||||
|
if (p1.op && p1.op === p2.op) continue;
|
||||||
|
if (p1.op && p1.op === p3.op) continue;
|
||||||
|
if (p2.op && p2.op === p3.op) continue;
|
||||||
|
// otherwise emit altered facet
|
||||||
|
points[newpoints++] = p1.op || p1;
|
||||||
|
points[newpoints++] = p2.op || p2;
|
||||||
|
points[newpoints++] = p3.op || p3;
|
||||||
|
}
|
||||||
|
parr = points.slice(0,newpoints);
|
||||||
|
oldpoints = newpoints;
|
||||||
|
if (passes >= maxpass) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return vertices;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (passes) console.trace({passes, threshold, precision, maxpass});
|
||||||
|
|
||||||
|
if (passes) console.log({
|
||||||
|
before: array.length / 3,
|
||||||
|
after: parr.length,
|
||||||
|
unique: unique,
|
||||||
|
decimations: passes,
|
||||||
|
time: (Date.now() - t)
|
||||||
|
});
|
||||||
|
|
||||||
|
return parr;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pointsToVertices(points) {
|
||||||
|
let vertices = new Float32Array(points.length * 3),
|
||||||
|
i = 0, vi = 0;
|
||||||
|
while (i < points.length) {
|
||||||
|
vertices[vi++] = points[i].x;
|
||||||
|
vertices[vi++] = points[i].y;
|
||||||
|
vertices[vi++] = points[i++].z;
|
||||||
|
}
|
||||||
|
return vertices;
|
||||||
|
}
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
1745
src/geo/polygons.js
1745
src/geo/polygons.js
File diff suppressed because it is too large
Load diff
|
|
@ -1,104 +0,0 @@
|
||||||
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
||||||
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
(function() {
|
|
||||||
|
|
||||||
if (!self.base) self.base = {};
|
|
||||||
if (self.base.render) return;
|
|
||||||
|
|
||||||
const BASE = self.base,
|
|
||||||
materialCache = {},
|
|
||||||
line_width = 1;
|
|
||||||
|
|
||||||
BASE.render = {
|
|
||||||
wireframe : wireframe
|
|
||||||
};
|
|
||||||
|
|
||||||
/** ******************************************************************
|
|
||||||
* Render Functions
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* render triangle array as line segments. use of {@link newOrderedLine}
|
|
||||||
* allows lines to be cached by normalizing key order.
|
|
||||||
*
|
|
||||||
* @param {THREE.Group} group
|
|
||||||
* @param {Point[]} points
|
|
||||||
* @param {number} color
|
|
||||||
* @returns {THREE.Line}
|
|
||||||
*/
|
|
||||||
function wireframe(group, points, color) {
|
|
||||||
if (points.length % 3 != 0) throw "invalid line : "+points.length;
|
|
||||||
let lines = new THREE.BufferGeometry(),
|
|
||||||
hash = {},
|
|
||||||
added = 0,
|
|
||||||
vertices = [];
|
|
||||||
for (let i = 0; i < points.length; i += 3) {
|
|
||||||
let p1 = points[i],
|
|
||||||
p2 = points[i + 1],
|
|
||||||
p3 = points[i + 2],
|
|
||||||
l1 = newOrderedLine(p1, p2),
|
|
||||||
l2 = newOrderedLine(p2, p3),
|
|
||||||
l3 = newOrderedLine(p3, p1);
|
|
||||||
if (!hash[l1.key]) {
|
|
||||||
vertices.appendAll([
|
|
||||||
p1.x, p1.y, p1.z,
|
|
||||||
p2.x, p2.y, p2.z
|
|
||||||
]);
|
|
||||||
hash[l1.key] = ++added;
|
|
||||||
}
|
|
||||||
if (!hash[l2.key]) {
|
|
||||||
vertices.appendAll([
|
|
||||||
p2.x, p2.y, p2.z,
|
|
||||||
p3.x, p3.y, p3.z
|
|
||||||
]);
|
|
||||||
hash[l2.key] = ++added;
|
|
||||||
}
|
|
||||||
if (!hash[l3.key]) {
|
|
||||||
vertices.appendAll([
|
|
||||||
p3.x, p3.y, p3.z,
|
|
||||||
p1.x, p1.y, p1.z
|
|
||||||
]);
|
|
||||||
hash[l3.key] = ++added;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lines.setAttribute('position', new THREE.BufferAttribute( vertices.toFloat32(), 3 ) );
|
|
||||||
let mesh = new THREE.LineSegments(lines, getMaterial(color));
|
|
||||||
group.add(mesh);
|
|
||||||
return mesh;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** ******************************************************************
|
|
||||||
* Connect to base and Helpers
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {number} color
|
|
||||||
* @returns {THREE.LineBasicMaterial}
|
|
||||||
*/
|
|
||||||
function getMaterial(color) {
|
|
||||||
let material = materialCache[color];
|
|
||||||
if (!material) {
|
|
||||||
material = new THREE.LineBasicMaterial({
|
|
||||||
fog:false,
|
|
||||||
color: color,
|
|
||||||
linewidth: line_width
|
|
||||||
});
|
|
||||||
materialCache[color] = material;
|
|
||||||
}
|
|
||||||
return material;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* required for line caching in {@link base.render.wireframe}
|
|
||||||
*
|
|
||||||
* @param {Point} p1
|
|
||||||
* @param {Point} p2
|
|
||||||
* @returns {Line}
|
|
||||||
*/
|
|
||||||
function newOrderedLine(p1, p2) {
|
|
||||||
return p1.key < p2.key ? BASE.newLine(p1, p2) : BASE.newLine(p2, p1);
|
|
||||||
}
|
|
||||||
|
|
||||||
})();
|
|
||||||
1448
src/geo/slicer.js
1448
src/geo/slicer.js
File diff suppressed because it is too large
Load diff
144
src/geo/slope.js
144
src/geo/slope.js
|
|
@ -4,147 +4,105 @@
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
if (self.base.Slope) return;
|
const base = self.base;
|
||||||
|
if (base.Slope) return;
|
||||||
|
|
||||||
const BASE = self.base,
|
const { config } = base;
|
||||||
CONF = BASE.config,
|
const ABS = Math.abs,
|
||||||
ABS = Math.abs,
|
DEG2RAD = Math.PI / 180,
|
||||||
PRO = Slope.prototype,
|
RAD2DEG = 180 / Math.PI;
|
||||||
DEG2RAD = Math.PI / 180,
|
|
||||||
RAD2DEG = 180 / Math.PI;
|
|
||||||
|
|
||||||
BASE.Slope = Slope;
|
class Slope {
|
||||||
BASE.newSlope = newSlope;
|
constructor(p1, p2, dx, dy) {
|
||||||
BASE.newSlopeFromAngle = function(angle) {
|
|
||||||
return newSlope(0,0,
|
|
||||||
Math.cos(angle * DEG2RAD),
|
|
||||||
Math.sin(angle * DEG2RAD)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param p1
|
|
||||||
* @param p2
|
|
||||||
* @param dx
|
|
||||||
* @param dy
|
|
||||||
* @constructor
|
|
||||||
*/
|
|
||||||
function Slope(p1, p2, dx, dy) {
|
|
||||||
this.dx = p1 && p2 ? p2.x - p1.x : dx;
|
this.dx = p1 && p2 ? p2.x - p1.x : dx;
|
||||||
this.dy = p1 && p2 ? p2.y - p1.y : dy;
|
this.dy = p1 && p2 ? p2.y - p1.y : dy;
|
||||||
this.angle = Math.atan2(this.dy, this.dx) * RAD2DEG;
|
this.angle = Math.atan2(this.dy, this.dx) * RAD2DEG;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** ******************************************************************
|
toString() {
|
||||||
* Slope Prototype Functions
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
PRO.toString = function() {
|
|
||||||
return [this.dx, this.dy, this.angle].join(',');
|
return [this.dx, this.dy, this.angle].join(',');
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.clone = function() {
|
clone() {
|
||||||
return new Slope(null, null, this.dx, this.dy);
|
return new Slope(null, null, this.dx, this.dy);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
isSame(s) {
|
||||||
* @param {Slope} s
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
PRO.isSame = function(s) {
|
|
||||||
// if very close to vertical or horizontal, they're the same
|
// if very close to vertical or horizontal, they're the same
|
||||||
if (ABS(this.dx) <= CONF.precision_merge && ABS(s.dx) <= CONF.precision_merge) return true;
|
if (ABS(this.dx) <= config.precision_merge && ABS(s.dx) <= config.precision_merge) return true;
|
||||||
if (ABS(this.dy) <= CONF.precision_merge && ABS(s.dy) <= CONF.precision_merge) return true;
|
if (ABS(this.dy) <= config.precision_merge && ABS(s.dy) <= config.precision_merge) return true;
|
||||||
// check angle within a range
|
// check angle within a range
|
||||||
let prec = Math.min(1/Math.sqrt(this.dx * this.dx + this.dy * this.dy), CONF.precision_slope_merge);
|
let prec = Math.min(1/Math.sqrt(this.dx * this.dx + this.dy * this.dy), config.precision_slope_merge);
|
||||||
return angleWithinDelta(this.angle, s.angle, prec || CONF.precision_slope);
|
return angleWithinDelta(this.angle, s.angle, prec || config.precision_slope);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
normal() {
|
||||||
* turn slope 90 degrees
|
|
||||||
*
|
|
||||||
* @returns {Slope}
|
|
||||||
*/
|
|
||||||
PRO.normal = function() {
|
|
||||||
let t = this.dx;
|
let t = this.dx;
|
||||||
this.dx = -this.dy;
|
this.dx = -this.dy;
|
||||||
this.dy = t;
|
this.dy = t;
|
||||||
this.angle = Math.atan2(this.dy, this.dx) * RAD2DEG;
|
this.angle = Math.atan2(this.dy, this.dx) * RAD2DEG;
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.invert = function() {
|
invert() {
|
||||||
this.dx = -this.dx;
|
this.dx = -this.dx;
|
||||||
this.dy = -this.dy;
|
this.dy = -this.dy;
|
||||||
this.angle = Math.atan2(this.dy, this.dx) * RAD2DEG;
|
this.angle = Math.atan2(this.dy, this.dx) * RAD2DEG;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
PRO.toUnit = function() {
|
toUnit() {
|
||||||
let max = Math.max(ABS(this.dx), ABS(this.dy));
|
let max = Math.max(ABS(this.dx), ABS(this.dy));
|
||||||
this.dx = this.dx / max;
|
this.dx = this.dx / max;
|
||||||
this.dy = this.dy / max;
|
this.dy = this.dy / max;
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.factor = function(f) {
|
factor(f) {
|
||||||
this.dx *= f;
|
this.dx *= f;
|
||||||
this.dy *= f;
|
this.dy *= f;
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
invert() {
|
||||||
* reverse (180 degree) slope
|
|
||||||
*
|
|
||||||
* @returns {Slope}
|
|
||||||
*/
|
|
||||||
PRO.invert = function() {
|
|
||||||
this.dx = -this.dx;
|
this.dx = -this.dx;
|
||||||
this.dy = -this.dy;
|
this.dy = -this.dy;
|
||||||
this.angle = 180 - this.angle;
|
this.angle = 180 - this.angle;
|
||||||
return this;
|
return this;
|
||||||
};
|
}
|
||||||
|
|
||||||
PRO.angleDiff = function(s2,sign) {
|
angleDiff(s2,sign) {
|
||||||
const n1 = this.angle;
|
const n1 = this.angle;
|
||||||
const n2 = s2.angle;
|
const n2 = s2.angle;
|
||||||
let diff = n2 - n1;
|
let diff = n2 - n1;
|
||||||
while (diff < -180) diff += 360;
|
while (diff < -180) diff += 360;
|
||||||
while (diff > 180) diff -= 360;
|
while (diff > 180) diff -= 360;
|
||||||
return sign ? diff : Math.abs(diff);
|
return sign ? diff : Math.abs(diff);
|
||||||
};
|
|
||||||
|
|
||||||
/** ******************************************************************
|
|
||||||
* Connect to base and Helpers
|
|
||||||
******************************************************************* */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns true if the difference between a & b is less than v
|
|
||||||
*
|
|
||||||
* @param {number} a
|
|
||||||
* @param {number} b
|
|
||||||
* @param {number} v
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
function minDeltaABS(a,b,v) {
|
|
||||||
return ABS(a-b) < v;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function angleWithinDelta(a1, a2, delta) {
|
/**
|
||||||
return (ABS(a1-a2) <= delta || 360-ABS(a1-a2) <= delta);
|
* returns true if the difference between a & b is less than v
|
||||||
}
|
*/
|
||||||
|
function minDeltaABS(a,b,v) {
|
||||||
|
return ABS(a-b) < v;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
function angleWithinDelta(a1, a2, delta) {
|
||||||
*
|
return (ABS(a1-a2) <= delta || 360-ABS(a1-a2) <= delta);
|
||||||
* @param p1
|
}
|
||||||
* @param p2
|
|
||||||
* @param dx
|
function newSlope(p1, p2, dx, dy) {
|
||||||
* @param dy
|
return new Slope(p1, p2, dx, dy);
|
||||||
* @returns {Slope}
|
}
|
||||||
*/
|
|
||||||
function newSlope(p1, p2, dx, dy) {
|
base.Slope = Slope;
|
||||||
return new Slope(p1, p2, dx, dy);
|
base.newSlope = newSlope;
|
||||||
}
|
base.newSlopeFromAngle = function(angle) {
|
||||||
|
return newSlope(0,0,
|
||||||
|
Math.cos(angle * DEG2RAD),
|
||||||
|
Math.sin(angle * DEG2RAD)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
354
src/geo/wasm.js
354
src/geo/wasm.js
|
|
@ -5,202 +5,202 @@
|
||||||
// only active in workers
|
// only active in workers
|
||||||
if (!self.window) (function() {
|
if (!self.window) (function() {
|
||||||
|
|
||||||
if (!self.geo) self.geo = {
|
if (!self.geo) self.geo = {
|
||||||
enable,
|
enable,
|
||||||
disable,
|
disable,
|
||||||
count: {
|
count: {
|
||||||
offset: 0,
|
offset: 0,
|
||||||
union: 0,
|
union: 0,
|
||||||
diff: 0
|
diff: 0
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const factor = self.base.config.clipper;
|
|
||||||
const geo = self.geo;
|
|
||||||
|
|
||||||
function log() {
|
|
||||||
console.log(...arguments);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function writePolys(view, polys) {
|
const factor = self.base.config.clipper;
|
||||||
let pcount = 0;
|
const geo = self.geo;
|
||||||
for (let poly of polys) {
|
|
||||||
pcount += writePoly(view, poly);
|
function log() {
|
||||||
}
|
console.log(...arguments);
|
||||||
return pcount;
|
}
|
||||||
|
|
||||||
|
function writePolys(view, polys) {
|
||||||
|
let pcount = 0;
|
||||||
|
for (let poly of polys) {
|
||||||
|
pcount += writePoly(view, poly);
|
||||||
}
|
}
|
||||||
|
return pcount;
|
||||||
|
}
|
||||||
|
|
||||||
function writePoly(view, poly, inner) {
|
function writePoly(view, poly, inner) {
|
||||||
if (inner) {
|
if (inner) {
|
||||||
poly.setCounterClockwise();
|
poly.setCounterClockwise();
|
||||||
|
} else {
|
||||||
|
poly.setClockwise();
|
||||||
|
}
|
||||||
|
let count = 1;
|
||||||
|
let points = poly.points;
|
||||||
|
let inners = poly.inner;
|
||||||
|
view.writeU16(points.length, true);
|
||||||
|
for (let i=0, il=points.length; i<il; i++) {
|
||||||
|
let point = points[i];
|
||||||
|
view.writeI32((point.x * factor)|0, true);
|
||||||
|
view.writeI32((point.y * factor)|0, true);
|
||||||
|
}
|
||||||
|
if (inners) {
|
||||||
|
for (let i=0, il=inners.length; i<il; i++) {
|
||||||
|
count += writePoly(view, inners[i], true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
function readPoly(view, z) {
|
||||||
|
let points = view.readU16(true);
|
||||||
|
if (points === 0) return;
|
||||||
|
let poly = self.base.newPolygon();
|
||||||
|
while (points-- > 0) {
|
||||||
|
poly.add(view.readI32(true)/factor, view.readI32(true)/factor, z || 0);
|
||||||
|
}
|
||||||
|
return poly;
|
||||||
|
}
|
||||||
|
|
||||||
|
function readPolys(view, z, out = []) {
|
||||||
|
for (;;) {
|
||||||
|
let poly = readPoly(view, z);
|
||||||
|
if (poly) {
|
||||||
|
out.push(poly);
|
||||||
} else {
|
} else {
|
||||||
poly.setClockwise();
|
break;
|
||||||
}
|
}
|
||||||
let count = 1;
|
|
||||||
let points = poly.points;
|
|
||||||
let inners = poly.inner;
|
|
||||||
view.writeU16(points.length, true);
|
|
||||||
for (let i=0, il=points.length; i<il; i++) {
|
|
||||||
let point = points[i];
|
|
||||||
view.writeI32((point.x * factor)|0, true);
|
|
||||||
view.writeI32((point.y * factor)|0, true);
|
|
||||||
}
|
|
||||||
if (inners) {
|
|
||||||
for (let i=0, il=inners.length; i<il; i++) {
|
|
||||||
count += writePoly(view, inners[i], true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
function readPoly(view, z) {
|
function polyOffset(polys, offset, z, clean, simple) {
|
||||||
let points = view.readU16(true);
|
geo.count.offset++;
|
||||||
if (points === 0) return;
|
let wasm = geo.wasm,
|
||||||
let poly = self.base.newPolygon();
|
buffer = geo.wasm.shared,
|
||||||
while (points-- > 0) {
|
pcount = writePolys(new DataWriter(wasm.heap, buffer), polys),
|
||||||
poly.add(view.readI32(true)/factor, view.readI32(true)/factor, z || 0);
|
resat = wasm.fn.offset(buffer, pcount, offset * factor, clean, simple),
|
||||||
}
|
out = readPolys(new DataReader(wasm.heap, resat), z);
|
||||||
return poly;
|
return polyNest(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
function polyUnion(polys, z) {
|
||||||
|
geo.count.union++;
|
||||||
|
let wasm = geo.wasm,
|
||||||
|
buffer = geo.wasm.shared,
|
||||||
|
pcount = writePolys(new DataWriter(wasm.heap, buffer), polys),
|
||||||
|
resat = wasm.fn.union(buffer, pcount),
|
||||||
|
out = readPolys(new DataReader(wasm.heap, resat), z);
|
||||||
|
return polyNest(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
function polyDiff(polysA, polysB, z, AB, BA) {
|
||||||
|
geo.count.diff++;
|
||||||
|
let wasm = geo.wasm,
|
||||||
|
buffer = geo.wasm.shared,
|
||||||
|
writer = new DataWriter(wasm.heap, buffer),
|
||||||
|
pcountA = writePolys(writer, polysA),
|
||||||
|
pcountB = writePolys(writer, polysB),
|
||||||
|
resat = wasm.fn.diff(buffer, pcountA, pcountB, AB?1:0, BA?1:0, base.config.clipperClean),
|
||||||
|
reader = new DataReader(wasm.heap, resat);
|
||||||
|
if (AB) {
|
||||||
|
AB.appendAll(polyNest(readPolys(reader, z)));
|
||||||
}
|
}
|
||||||
|
if (BA) {
|
||||||
|
BA.appendAll(polyNest(readPolys(reader, z)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function readPolys(view, z, out = []) {
|
// nest closed polygons without existing parent / child relationships
|
||||||
for (;;) {
|
function polyNest(polys) {
|
||||||
let poly = readPoly(view, z);
|
polys.sort((a,b) => {
|
||||||
if (poly) {
|
return b.bounds.minx - a.bounds.minx;
|
||||||
out.push(poly);
|
});
|
||||||
} else {
|
// from smallest to largest, check for enclosing bounds and nest
|
||||||
|
for (let i=0, il=polys.length; i<il; i++) {
|
||||||
|
let smaller = polys[i];
|
||||||
|
// prevent parent poly from being consumed
|
||||||
|
if (smaller.inner) continue;
|
||||||
|
for (let j=i+1; j<il; j++) {
|
||||||
|
let larger = polys[j];
|
||||||
|
if (larger.bounds.contains(smaller.bounds)) {
|
||||||
|
larger.addInner(smaller);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
let tops = [];
|
||||||
function polyOffset(polys, offset, z, clean, simple) {
|
for (let i=0, il=polys.length; i<il; i++) {
|
||||||
geo.count.offset++;
|
let poly = polys[i];
|
||||||
let wasm = geo.wasm,
|
if (!poly.parent) {
|
||||||
buffer = geo.wasm.shared,
|
tops.push(poly);
|
||||||
pcount = writePolys(new DataWriter(wasm.heap, buffer), polys),
|
|
||||||
resat = wasm.fn.offset(buffer, pcount, offset * factor, clean, simple),
|
|
||||||
out = readPolys(new DataReader(wasm.heap, resat), z);
|
|
||||||
return polyNest(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
function polyUnion(polys, z) {
|
|
||||||
geo.count.union++;
|
|
||||||
let wasm = geo.wasm,
|
|
||||||
buffer = geo.wasm.shared,
|
|
||||||
pcount = writePolys(new DataWriter(wasm.heap, buffer), polys),
|
|
||||||
resat = wasm.fn.union(buffer, pcount),
|
|
||||||
out = readPolys(new DataReader(wasm.heap, resat), z);
|
|
||||||
return polyNest(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
function polyDiff(polysA, polysB, z, AB, BA) {
|
|
||||||
geo.count.diff++;
|
|
||||||
let wasm = geo.wasm,
|
|
||||||
buffer = geo.wasm.shared,
|
|
||||||
writer = new DataWriter(wasm.heap, buffer),
|
|
||||||
pcountA = writePolys(writer, polysA),
|
|
||||||
pcountB = writePolys(writer, polysB),
|
|
||||||
resat = wasm.fn.diff(buffer, pcountA, pcountB, AB?1:0, BA?1:0, base.config.clipperClean),
|
|
||||||
reader = new DataReader(wasm.heap, resat);
|
|
||||||
if (AB) {
|
|
||||||
AB.appendAll(polyNest(readPolys(reader, z)));
|
|
||||||
}
|
|
||||||
if (BA) {
|
|
||||||
BA.appendAll(polyNest(readPolys(reader, z)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return tops;
|
||||||
|
}
|
||||||
|
|
||||||
// nest closed polygons without existing parent / child relationships
|
function readString(pos, len) {
|
||||||
function polyNest(polys) {
|
let view = new DataReader(geo.wasm.heap, pos);
|
||||||
polys.sort((a,b) => {
|
let out = [];
|
||||||
return b.bounds.minx - a.bounds.minx;
|
while (len-- > 0) {
|
||||||
|
out.push(String.fromCharCode(view.readU8()));
|
||||||
|
}
|
||||||
|
return out.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function enable() {
|
||||||
|
if (geo.wasm || geo._wasm) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
geo._wasm = 'loading';
|
||||||
|
fetch('/wasm/kiri-geo.wasm')
|
||||||
|
.then(response => response.arrayBuffer())
|
||||||
|
.then(bytes => WebAssembly.instantiate(bytes, {
|
||||||
|
env: {
|
||||||
|
debug_string: (len, ptr) => { console.log('wasm', readString(ptr, len)) }
|
||||||
|
},
|
||||||
|
wasi_snapshot_preview1: {
|
||||||
|
// args_get: (count,bufsize) => { return 0 },
|
||||||
|
// args_sizes_get: (count,bufsize) => { },
|
||||||
|
// environ_get: (count,bufsize) => { return 0 },
|
||||||
|
// environ_sizes_get: (count,bufsize) => { },
|
||||||
|
proc_exit: (code) => { return code }
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.then(results => {
|
||||||
|
// console.log({enabled: geo.wasm});
|
||||||
|
delete geo._wasm;
|
||||||
|
let { module, instance } = results;
|
||||||
|
let { exports } = instance;
|
||||||
|
let heap = new DataView(exports.memory.buffer);
|
||||||
|
let wasm = geo.wasm = {
|
||||||
|
heap,
|
||||||
|
exports,
|
||||||
|
memory: exports.memory,
|
||||||
|
memmax: exports.memory.buffer.byteLength,
|
||||||
|
malloc: exports.mem_get,
|
||||||
|
free: exports.mem_clr
|
||||||
|
};
|
||||||
|
wasm.shared = wasm.malloc(1024 * 1024 * 30),
|
||||||
|
wasm.fn = {
|
||||||
|
diff: exports.poly_diff,
|
||||||
|
union: exports.poly_union,
|
||||||
|
offset: exports.poly_offset
|
||||||
|
};
|
||||||
|
wasm.js = {
|
||||||
|
diff: polyDiff,
|
||||||
|
union: polyUnion,
|
||||||
|
offset: polyOffset
|
||||||
|
};
|
||||||
});
|
});
|
||||||
// from smallest to largest, check for enclosing bounds and nest
|
}
|
||||||
for (let i=0, il=polys.length; i<il; i++) {
|
|
||||||
let smaller = polys[i];
|
|
||||||
// prevent parent poly from being consumed
|
|
||||||
if (smaller.inner) continue;
|
|
||||||
for (let j=i+1; j<il; j++) {
|
|
||||||
let larger = polys[j];
|
|
||||||
if (larger.bounds.contains(smaller.bounds)) {
|
|
||||||
larger.addInner(smaller);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let tops = [];
|
|
||||||
for (let i=0, il=polys.length; i<il; i++) {
|
|
||||||
let poly = polys[i];
|
|
||||||
if (!poly.parent) {
|
|
||||||
tops.push(poly);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tops;
|
|
||||||
}
|
|
||||||
|
|
||||||
function readString(pos, len) {
|
function disable() {
|
||||||
let view = new DataReader(geo.wasm.heap, pos);
|
if (geo.wasm) {
|
||||||
let out = [];
|
delete geo.wasm;
|
||||||
while (len-- > 0) {
|
// console.log({disabled: geo});
|
||||||
out.push(String.fromCharCode(view.readU8()));
|
|
||||||
}
|
|
||||||
return out.join('');
|
|
||||||
}
|
|
||||||
|
|
||||||
function enable() {
|
|
||||||
if (geo.wasm || geo._wasm) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
geo._wasm = 'loading';
|
|
||||||
fetch('/wasm/kiri-geo.wasm')
|
|
||||||
.then(response => response.arrayBuffer())
|
|
||||||
.then(bytes => WebAssembly.instantiate(bytes, {
|
|
||||||
env: {
|
|
||||||
debug_string: (len, ptr) => { console.log('wasm', readString(ptr, len)) }
|
|
||||||
},
|
|
||||||
wasi_snapshot_preview1: {
|
|
||||||
// args_get: (count,bufsize) => { return 0 },
|
|
||||||
// args_sizes_get: (count,bufsize) => { },
|
|
||||||
// environ_get: (count,bufsize) => { return 0 },
|
|
||||||
// environ_sizes_get: (count,bufsize) => { },
|
|
||||||
proc_exit: (code) => { return code }
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.then(results => {
|
|
||||||
// console.log({enabled: geo.wasm});
|
|
||||||
delete geo._wasm;
|
|
||||||
let { module, instance } = results;
|
|
||||||
let { exports } = instance;
|
|
||||||
let heap = new DataView(exports.memory.buffer);
|
|
||||||
let wasm = geo.wasm = {
|
|
||||||
heap,
|
|
||||||
exports,
|
|
||||||
memory: exports.memory,
|
|
||||||
memmax: exports.memory.buffer.byteLength,
|
|
||||||
malloc: exports.mem_get,
|
|
||||||
free: exports.mem_clr
|
|
||||||
};
|
|
||||||
wasm.shared = wasm.malloc(1024 * 1024 * 30),
|
|
||||||
wasm.fn = {
|
|
||||||
diff: exports.poly_diff,
|
|
||||||
union: exports.poly_union,
|
|
||||||
offset: exports.poly_offset
|
|
||||||
};
|
|
||||||
wasm.js = {
|
|
||||||
diff: polyDiff,
|
|
||||||
union: polyUnion,
|
|
||||||
offset: polyOffset
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function disable() {
|
|
||||||
if (geo.wasm) {
|
|
||||||
delete geo.wasm;
|
|
||||||
// console.log({disabled: geo});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue