@davepagurek/flo-mat
Version:
Medial / Scale Axis Transform (MAT/SAT) Library.
44 lines (33 loc) • 1.22 kB
text/typescript
import { getBoundingBoxTight } from "flo-bezier3";
import { memoize } from "flo-memoize";
import { BezierPiece } from "../mat/bezier-piece.js";
import { getClosestSquaredDistanceToRotatedRect } from '../geometry/get-closest-squared-distance-to-rotated-rect.js';
const getBoundingBoxTight_ = memoize(getBoundingBoxTight);
/**
* @internal
* When checking distances, ignore all those with closest possible distance
* further than 'bestSquaredDistance', i.e. cull them.
* @param bezierPieces
* @param p
* @param bestSquaredDistance
*/
function cullByTightBoundingBox(
bezierPieces: BezierPiece[],
p: number[],
bestSquaredDistance: number) {
const candidateBezierPieces = [];
for (let i=0; i<bezierPieces.length; i++) {
const bezierPiece = bezierPieces[i];
const ps = bezierPiece.curve.ps;
const tightBoundingBox = getBoundingBoxTight_(ps);
const d = getClosestSquaredDistanceToRotatedRect(
tightBoundingBox,
p
);
if (d <= bestSquaredDistance) {
candidateBezierPieces.push(bezierPiece);
}
}
return candidateBezierPieces;
}
export { cullByTightBoundingBox }