class Bbox {
static isEqual(bbox1, bbox2) {
if (bbox1 == bbox2) return true;
return (bbox1?.length == 4 &&
bbox1?.length == 4 &&
bbox1[0] == bbox2[0] &&
bbox1[1] == bbox2[1] &&
bbox1[2] == bbox2[2] &&
bbox1[3] == bbox2[3]);
}
static contains(bbox1, bbox2) {
if (bbox1 == bbox2) return false;
return (bbox1?.length === bbox2?.length &&
bbox1[0] < bbox2[0] &&
bbox1[1] < bbox2[1] &&
bbox1[2] > bbox2[2] &&
bbox1[3] > bbox2[3]);
}
static covers(bbox1, bbox2) {
if (bbox1 == bbox2) return true;
return (bbox1?.length === bbox2?.length &&
bbox1[0] <= bbox2[0] &&
bbox1[1] <= bbox2[1] &&
bbox1[2] >= bbox2[2] &&
bbox1[3] >= bbox2[3]);
}
static containsPoint(bbox, point) {
return Bbox.contains(bbox, [...point, ...point]);
}
static coversPoint(bbox, point) {
return Bbox.covers(bbox, [...point, ...point]);
}
static isPointOnEdge(bbox, point) {
return bbox[0] == point[0] ||
bbox[1] == point[1] ||
bbox[2] == point[0] ||
bbox[3] == point[1];
}
static width(bbox) {
return bbox[2] - bbox[0];
}
static height(bbox) {
return bbox[3] - bbox[1];
}
static dims(bbox) {
return { x: bbox[0], y: bbox[1], width: Bbox.width(bbox), height: Bbox.height(bbox) };
}
static fromDims(dims) {
return [
dims.x,
dims.y,
dims.width + dims.x - 1,
dims.height + dims.y - 1
];
}
static fromPoints(points) {
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
for (let [x, y] of points) {
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
return [x0, y0, x1, y1];
}
static area(bbox) {
return Bbox.width(bbox) * Bbox.height(bbox);
}
static buffer(bbox, amount) {
return [bbox[0] - amount, bbox[1] - amount, bbox[2] + amount, bbox[3] + amount];
}
static split(bbox, stripCount, minStripSize) {
stripCount ??= Math.max(1, navigator.hardwareConcurrency - 1);
minStripSize ??= 512;
debugger;
const splitBboxes = []
if (bbox.width > bbox.height) {
while (stripCount > 1 && bbox.width / stripCount < minStripSize) stripCount--;
const [bboxX0, y0, bboxX1, y1] = Bbox.fromDims(bbox);
const stripSize = Math.max(minStripSize, Math.ceil(bbox.width / stripCount));
for (let x0 of d3.range(bboxX0, bboxX1, stripSize)) {
const split = {...bbox};
const x1 = Math.min(x0 + stripSize - 1, bboxX1);
Object.assign(split, Bbox.dims([x0, y0, x1, y1]));
splitBboxes.push(split);
}
}
else {
while (stripCount > 1 && bbox.height / stripCount < minStripSize) stripCount--;
const [x0, bboxY0, x1, bboxY1] = Bbox.fromDims(bbox);
const stripSize = Math.max(minStripSize, Math.ceil(bbox.height / stripCount));
for (let y0 of d3.range(bboxY0, bboxY1, stripSize)) {
const split = {...bbox};
const y1 = Math.min(y0 + stripSize, bboxY1);
Object.assign(split, Bbox.dims([x0, y0, x1, y1]));
splitBboxes.push(split);
}
}
return splitBboxes;
}
}