2023-01-09 10:16:28 -05:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
2025-07-09 13:00:52 -04:00
|
|
|
import { newPoint } from '../../../geo/point.js';
|
2025-12-06 00:09:17 -05:00
|
|
|
import {
|
|
|
|
|
checkOverUnderOn,
|
|
|
|
|
makeZLine,
|
|
|
|
|
removeDuplicateLines,
|
|
|
|
|
intersectPoints
|
|
|
|
|
} from '../../../geo/slicer.js';
|
2023-01-09 10:16:28 -05:00
|
|
|
|
2025-07-09 11:56:46 -04:00
|
|
|
/** Slicer used in Topo3 and Topo4 to find contour lines */
|
2025-07-06 23:44:33 -04:00
|
|
|
export class Slicer {
|
2023-01-09 10:16:28 -05:00
|
|
|
|
2023-01-11 19:23:46 -05:00
|
|
|
constructor(index = 0) {
|
2023-01-09 10:16:28 -05:00
|
|
|
this.min = Infinity;
|
|
|
|
|
this.max = -Infinity;
|
2023-01-11 19:23:46 -05:00
|
|
|
this.index = index;
|
2023-01-09 10:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// position is a non-indexed 3js geometry position attribute array
|
|
|
|
|
// create Point array from optionally filtered Z range
|
|
|
|
|
setFromArray(position, options = {}) {
|
|
|
|
|
const min = options.min || -Infinity;
|
|
|
|
|
const max = options.max || Infinity;
|
|
|
|
|
const len = position.length;
|
|
|
|
|
const pts = this.points = [];
|
2025-07-15 18:39:48 -04:00
|
|
|
for (let i = 0; i < len;) {
|
2023-01-09 10:16:28 -05:00
|
|
|
const x0 = position[i++];
|
|
|
|
|
const y0 = position[i++];
|
|
|
|
|
const z0 = position[i++];
|
|
|
|
|
const x1 = position[i++];
|
|
|
|
|
const y1 = position[i++];
|
|
|
|
|
const z1 = position[i++];
|
|
|
|
|
const x2 = position[i++];
|
|
|
|
|
const y2 = position[i++];
|
|
|
|
|
const z2 = position[i++];
|
2023-01-09 16:03:31 -05:00
|
|
|
const minz = Math.min(z0, z1, z2);
|
|
|
|
|
const maxz = Math.max(z0, z1, z2);
|
|
|
|
|
if (maxz < min || minz > max) {
|
|
|
|
|
continue;
|
2023-01-09 10:16:28 -05:00
|
|
|
}
|
2023-01-09 16:03:31 -05:00
|
|
|
pts.push(
|
|
|
|
|
newPoint(x0, y0, z0),
|
|
|
|
|
newPoint(x1, y1, z1),
|
|
|
|
|
newPoint(x2, y2, z2)
|
|
|
|
|
);
|
|
|
|
|
this.min = options.min || Math.min(this.min, minz);
|
|
|
|
|
this.max = options.max || Math.max(this.max, maxz);
|
2023-01-09 10:16:28 -05:00
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setFromPoints(points, options = {}) {
|
|
|
|
|
const pts = this.points = [];
|
|
|
|
|
const len = points.length;
|
|
|
|
|
const min = options.min || -Infinity;
|
|
|
|
|
const max = options.max || Infinity;
|
2025-07-15 18:39:48 -04:00
|
|
|
for (let i = 0; i < len;) {
|
2023-01-09 10:16:28 -05:00
|
|
|
const p0 = points[i++];
|
|
|
|
|
const p1 = points[i++];
|
|
|
|
|
const p2 = points[i++];
|
2023-01-09 16:03:31 -05:00
|
|
|
const minz = Math.min(p0.z, p1.z, p2.z);
|
|
|
|
|
const maxz = Math.max(p0.z, p1.z, p2.z);
|
|
|
|
|
if (maxz < min || minz > max) {
|
|
|
|
|
continue;
|
2023-01-09 10:16:28 -05:00
|
|
|
}
|
2023-01-09 16:03:31 -05:00
|
|
|
pts.push(p0, p1, p2);
|
|
|
|
|
this.min = options.min || Math.min(this.min, minz);
|
|
|
|
|
this.max = options.max || Math.max(this.max, maxz);
|
2023-01-09 10:16:28 -05:00
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slice(interval = 1) {
|
|
|
|
|
const ret = [];
|
|
|
|
|
for (let z = this.min; z < this.max; z += interval) {
|
2023-01-11 19:23:46 -05:00
|
|
|
ret.push({ z, index: this.index++, lines: this.sliceZ(z) });
|
2023-01-09 10:16:28 -05:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sliceZ(z, options = {}) {
|
|
|
|
|
const points = this.points;
|
|
|
|
|
const zMin = this.min;
|
|
|
|
|
const zMax = this.max;
|
|
|
|
|
const phash = {};
|
|
|
|
|
const lines = [];
|
2023-01-09 14:55:17 -05:00
|
|
|
|
2023-01-09 10:16:28 -05:00
|
|
|
let { under, over, both } = options;
|
|
|
|
|
let p1, p2, p3;
|
2023-01-09 14:55:17 -05:00
|
|
|
|
2023-01-09 10:16:28 -05:00
|
|
|
// default to 'over' selection with 2 points on a line
|
|
|
|
|
if (!under && !both) over = true;
|
2023-01-09 14:55:17 -05:00
|
|
|
|
2023-01-09 10:16:28 -05:00
|
|
|
// iterate over matching buckets for this z offset
|
2025-07-15 18:39:48 -04:00
|
|
|
for (let i = 0; i < points.length;) {
|
2023-01-09 10:16:28 -05:00
|
|
|
p1 = points[i++];
|
|
|
|
|
p2 = points[i++];
|
|
|
|
|
p3 = points[i++];
|
2025-07-15 18:39:48 -04:00
|
|
|
let where = { under: [], over: [], on: [] };
|
2023-01-09 10:16:28 -05:00
|
|
|
checkOverUnderOn(p1, z, where);
|
|
|
|
|
checkOverUnderOn(p2, z, where);
|
|
|
|
|
checkOverUnderOn(p3, z, where);
|
|
|
|
|
if (where.under.length === 3 || where.over.length === 3) {
|
|
|
|
|
// does not intersect (all 3 above or below)
|
|
|
|
|
} else if (where.on.length === 2) {
|
|
|
|
|
// one side of triangle is on the Z plane and 3rd is below
|
|
|
|
|
// drop lines with 3rd above because that leads to ambiguities
|
|
|
|
|
// with complex nested polygons on flat surface
|
|
|
|
|
let add2 = both ||
|
|
|
|
|
(over && (where.over.length === 1 || z === zMax)) ||
|
|
|
|
|
(under && (where.under.length === 1 || z === zMin));
|
|
|
|
|
if (add2) {
|
|
|
|
|
lines.push(makeZLine(phash, where.on[0], where.on[1], false, true));
|
|
|
|
|
}
|
|
|
|
|
} else if (where.on.length === 3) {
|
|
|
|
|
// triangle is coplanar with Z
|
|
|
|
|
// we drop these because this face is attached to 3 others
|
|
|
|
|
// that will satisfy the if above (line) with 2 points
|
|
|
|
|
} else if (where.under.length === 0 || where.over.length === 0) {
|
|
|
|
|
// does not intersect but one point is on the slice Z plane
|
|
|
|
|
} else {
|
|
|
|
|
// compute two point intersections and construct line
|
|
|
|
|
let line = intersectPoints(where.over, where.under, z);
|
|
|
|
|
if (line.length < 2 && where.on.length === 1) {
|
|
|
|
|
line.push(where.on[0]);
|
|
|
|
|
}
|
|
|
|
|
if (line.length === 2) {
|
|
|
|
|
lines.push(makeZLine(phash, line[0], line[1]));
|
|
|
|
|
} else {
|
2025-07-15 18:39:48 -04:00
|
|
|
console.log({ msg: "invalid ips", line: line, where: where });
|
2023-01-09 10:16:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 14:55:17 -05:00
|
|
|
|
2023-01-09 10:16:28 -05:00
|
|
|
if (lines.length == 0 && options.noEmpty) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-01-09 14:55:17 -05:00
|
|
|
|
2023-01-09 10:16:28 -05:00
|
|
|
return removeDuplicateLines(lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|