mindee
Version:
Mindee Client Library for Node.js
49 lines (48 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBoundingBox = getBoundingBox;
exports.getBoundingBoxFromBBox = getBoundingBoxFromBBox;
exports.mergeBbox = mergeBbox;
exports.getBbox = getBbox;
exports.getBBoxForPolygons = getBBoxForPolygons;
const boundingBox_1 = require("./boundingBox");
/**
* Given a Polygon, calculate a polygon that encompasses all points.
*/
function getBoundingBox(polygon) {
const bbox = getBbox(polygon);
return getBoundingBoxFromBBox(bbox);
}
/**
* Given a BBox, generate the associated bounding box.
*/
function getBoundingBoxFromBBox(bbox) {
return [
[bbox.xMin, bbox.yMin],
[bbox.xMax, bbox.yMin],
[bbox.xMax, bbox.yMax],
[bbox.xMin, bbox.yMax],
];
}
/**
* Given 2 bbox, merge them.
*/
function mergeBbox(bbox1, bbox2) {
return new boundingBox_1.BBox(Math.min(bbox1.xMin, bbox2.xMin), Math.min(bbox1.yMin, bbox2.yMin), Math.max(bbox1.xMax, bbox2.xMax), Math.max(bbox1.yMax, bbox2.yMax));
}
/**
* Given a Polygon, calculate a BBox that encompasses all points.
*/
function getBbox(polygon) {
const allY = polygon.map((point) => point[1]);
const allX = polygon.map((point) => point[0]);
return new boundingBox_1.BBox(Math.min(...allX), Math.min(...allY), Math.max(...allX), Math.max(...allY));
}
/**
* Given polygons, calculate a BBox that encompasses all points.
*/
function getBBoxForPolygons(polygons) {
const allY = polygons.flatMap((polygon) => polygon.map((point) => point[1]));
const allX = polygons.flatMap((polygon) => polygon.map((point) => point[0]));
return new boundingBox_1.BBox(Math.min(...allX), Math.min(...allY), Math.max(...allX), Math.max(...allY));
}