Skip to content

Commit

Permalink
feat: implement computeCropOrigins
Browse files Browse the repository at this point in the history
Closes: #427
  • Loading branch information
opatiny committed Jan 26, 2024
1 parent a235155 commit ac4175b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/stack/align/utils/__tests__/computeCropOrigins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Image } from '../../../../Image';
import { Stack } from '../../../../Stack';
import { computeCropOrigins } from '../computeCropOrigins';
import { findCommonArea } from '../findCommonArea';

const image1 = new Image(4, 4);
const image2 = new Image(3, 4);
const image3 = new Image(6, 5);
const image4 = new Image(4, 2);
const absoluteTranslations = [
{ column: 0, row: 0 },
{ column: 2, row: -2 },
{ column: 1, row: 1 },
{ column: -1, row: 1 },
];
test('stack of 2 images', () => {
const stack = new Stack([image1, image2]);
const absTranslations = absoluteTranslations.slice(0, 2);

const commonArea = findCommonArea(stack, absTranslations);

const result = computeCropOrigins(stack, commonArea, absTranslations);

expect(result).toStrictEqual([
{ column: 2, row: 0 },
{ column: 0, row: 2 },
]);
});

test('stack of 3 images', () => {
const stack = new Stack([image1, image2, image3]);
const absTranslations = absoluteTranslations.slice(0, 3);

const commonArea = findCommonArea(stack, absTranslations);

const result = computeCropOrigins(stack, commonArea, absTranslations);

expect(result).toStrictEqual([
{ column: 2, row: 1 },
{ column: 0, row: 3 },
{ column: 1, row: 0 },
]);
});

test('stack of 4 images', () => {
const stack = new Stack([image1, image2, image3, image4]);
const absTranslations = absoluteTranslations.slice(0, 4);

const commonArea = findCommonArea(stack, absTranslations);

const result = computeCropOrigins(stack, commonArea, absTranslations);

expect(result).toStrictEqual([
{ column: 2, row: 1 },
{ column: 0, row: 3 },
{ column: 1, row: 0 },
{ column: 3, row: 0 },
]);
});
18 changes: 17 additions & 1 deletion src/stack/align/utils/computeCropOrigins.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { Stack } from '../../../Stack';
import { Point } from '../../../geometry';
import { difference } from '../../../utils/geometry/points';

import { CommonArea } from './findCommonArea';

/**
* Compute the origins of the crops relative to the top left corner of each image in order
* to crop the common area of each of the images of an aligned stack.
* @param stack - Stack to process.
* @param commonArea - The data of the common area of the stack.
* @param absoluteTranslations - The absolute translations of the images of the stack (relative to the first image).
* @returns The origins of the crops.
*/
export function computeCropOrigins(
stack: Stack,
commonArea: CommonArea,
): Point[] {}
absoluteTranslations: Point[],
): Point[] {
const cropOrigins: Point[] = [];
let currentCropOrigin = commonArea.origin;
cropOrigins.push(currentCropOrigin);
for (let i = 1; i < stack.size; i++) {
currentCropOrigin = difference(commonArea.origin, absoluteTranslations[i]);
cropOrigins.push(currentCropOrigin);
}

return cropOrigins;
}

0 comments on commit ac4175b

Please sign in to comment.