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

Commit ec3ceb3

Browse files
committed
test: add and modify tests for coverage
1 parent b2f4130 commit ec3ceb3

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/geometry/__tests__/getPerspectiveWarp.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@ describe('warping tests', () => {
4646
describe('openCV comparison', () => {
4747
test('nearest interpolation plants', () => {
4848
const image = testUtils.load('various/plants.png');
49-
5049
const openCvResult = testUtils.load(
5150
'opencv/test_perspective_warp_plants_nearest.png',
5251
);
5352

5453
const points = [
55-
{ column: 166.5, row: 195 },
5654
{ column: 858.5, row: 9 },
5755
{ column: 911.5, row: 786 },
5856
{ column: 154.5, row: 611 },
57+
{ column: 166.5, row: 195 },
5958
];
6059
const matrix = getPerspectiveWarp(points, {
6160
width: 1080,
@@ -81,17 +80,17 @@ describe('openCV comparison', () => {
8180
expect(result.height).toEqual(openCvResult.height);
8281
expect(croppedPiece).toEqual(croppedPieceOpenCv);
8382
});
83+
8484
test('nearest interpolation card', () => {
8585
const image = testUtils.load('various/card.png');
86-
8786
const openCvResult = testUtils.load(
8887
'opencv/test_perspective_warp_card_nearest.png',
8988
);
9089
const points = [
91-
{ column: 55, row: 140 },
92-
{ column: 680, row: 38 },
9390
{ column: 840, row: 340 },
9491
{ column: 145, row: 460 },
92+
{ column: 55, row: 140 },
93+
{ column: 680, row: 38 },
9594
];
9695
const matrix = getPerspectiveWarp(points, {
9796
width: 700,
@@ -121,16 +120,15 @@ describe('openCV comparison', () => {
121120
});
122121
test('nearest interpolation plants', () => {
123122
const image = testUtils.load('various/plants.png');
124-
125123
const openCvResult = testUtils.load(
126124
'opencv/test_perspective_warp_plants_linear.png',
127125
);
128126

129127
const points = [
130128
{ column: 166.5, row: 195 },
131-
{ column: 858.5, row: 9 },
132-
{ column: 911.5, row: 786 },
133129
{ column: 154.5, row: 611 },
130+
{ column: 911.5, row: 786 },
131+
{ column: 858.5, row: 9 },
134132
];
135133
const matrix = getPerspectiveWarp(points, {
136134
width: 1080,
@@ -145,3 +143,13 @@ describe('openCV comparison', () => {
145143
expect(result.height).toEqual(openCvResult.height);
146144
});
147145
});
146+
147+
describe('error testing', () => {
148+
test("should throw if there aren't 4 points", () => {
149+
expect(() => {
150+
getPerspectiveWarp([{ column: 1, row: 1 }]);
151+
}).toThrow(
152+
'The array pts must have four elements, which are the four corners. Currently, pts have 1 elements',
153+
);
154+
});
155+
});

src/geometry/getPerspectiveWarp.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default function getPerspectiveWarp(
8686
}
8787

8888
/**
89-
* Sorts 4 points in order =>[top-left,top-right,bottom-right,bottom-left].
89+
* Sorts 4 points in order =>[top-left,top-right,bottom-right,bottom-left]. Input points must be in clockwise or counter-clockwise order.
9090
* @param pts - Array of 4 points.
9191
* @returns Sorted array of 4 points.
9292
*/
@@ -137,7 +137,6 @@ function order4Points(pts: Point[]) {
137137
br = pts[(indexMinX + 3) % 4];
138138
}
139139
}
140-
141140
return [tl, tr, br, bl];
142141
}
143142
/**

0 commit comments

Comments
 (0)