mindee
Version:
Mindee Client Library for Node.js
38 lines (37 loc) • 1.45 kB
JavaScript
import { BoundingBox } from "./boundingBox.js";
import { BBox } from "./bbox.js";
/**
* Given a Polygon, calculate a polygon that encompasses all points.
*/
export function getBoundingBox(polygon) {
const bbox = getBbox(polygon);
return getBoundingBoxFromBBox(bbox);
}
/**
* Given a BBox, generate the associated bounding box.
*/
export function getBoundingBoxFromBBox(bbox) {
return new BoundingBox([bbox.xMin, bbox.yMin], [bbox.xMax, bbox.yMin], [bbox.xMax, bbox.yMax], [bbox.xMin, bbox.yMax]);
}
/**
* Given 2 bbox, merge them.
*/
export function mergeBbox(bbox1, bbox2) {
return new 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.
*/
export function getBbox(polygon) {
const allY = polygon.map((point) => point[1]);
const allX = polygon.map((point) => point[0]);
return new BBox(Math.min(...allX), Math.min(...allY), Math.max(...allX), Math.max(...allY));
}
/**
* Given polygons, calculate a BBox that encompasses all points.
*/
export 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 BBox(Math.min(...allX), Math.min(...allY), Math.max(...allX), Math.max(...allY));
}