Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 6de5168

Browse files
committed
feat: add different return type for perspective warp
1 parent 8eb8297 commit 6de5168

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/geometry/getPerspectiveWarp.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ import { Matrix, SingularValueDecomposition } from 'ml-matrix';
33
import type { Point } from '../utils/geometry/points.js';
44

55
interface GetPerspectiveWarpOptions {
6+
/**
7+
* The horizontal dimension (in pixels) of the final rectified rectangular image.
8+
*/
69
width?: number;
10+
/**
11+
* The vertical dimension (in pixels) of the final rectified rectangular image.
12+
*/
713
height?: number;
814
}
15+
/**
16+
* Returns result matrix along with vertical and horizontal dimensions for the rectangular image.
17+
*/
18+
type GetPerspectiveWarpData = Required<GetPerspectiveWarpOptions> & {
19+
matrix: number[][];
20+
};
921

1022
// REFERENCES :
1123
// https://stackoverflow.com/questions/38285229/calculating-aspect-ratio-of-perspective-transform-destination-image/38402378#38402378
@@ -21,7 +33,7 @@ interface GetPerspectiveWarpOptions {
2133
export default function getPerspectiveWarp(
2234
pts: Point[],
2335
options: GetPerspectiveWarpOptions = {},
24-
) {
36+
): GetPerspectiveWarpData {
2537
if (pts.length !== 4) {
2638
throw new Error(
2739
`The array pts must have four elements, which are the four corners. Currently, pts have ${pts.length} elements`,
@@ -82,15 +94,15 @@ export default function getPerspectiveWarp(
8294
}
8395
M.push(row);
8496
}
85-
return M;
97+
return { matrix: M, width: widthRect, height: heightRect };
8698
}
8799

88100
/**
89101
* Sorts 4 points in order =>[top-left,top-right,bottom-right,bottom-left]. Input points must be in clockwise or counter-clockwise order.
90102
* @param pts - Array of 4 points.
91103
* @returns Sorted array of 4 points.
92104
*/
93-
function order4Points(pts: Point[]) {
105+
export function order4Points(pts: Point[]) {
94106
let tl: Point;
95107
let tr: Point;
96108
let br: Point;

0 commit comments

Comments
 (0)