Use transformation-matrix package for transforms

This commit is contained in:
Shailesh Pandit 2021-12-29 10:32:01 +05:30
parent 385acec1ab
commit 162d760b80
3 changed files with 18 additions and 48 deletions

View file

@ -71,6 +71,7 @@
"react-window-infinite-loader": "^1.0.5",
"scrypt-js": "^3.0.1",
"styled-components": "^5.2.0",
"transformation-matrix": "^2.10.0",
"tsne-js": "^1.0.3",
"workbox-precaching": "^6.1.5",
"workbox-recipes": "^6.1.5",

View file

@ -1,55 +1,24 @@
import { Box, Point } from '../../../thirdparty/face-api/classes';
import { Matrix } from 'ml-matrix';
import { newBoxFromPoints } from '.';
export function translation(x: number, y: number) {
return new Matrix([
[1, 0, x],
[0, 1, y],
[0, 0, 1],
]);
}
export function scale(sx: number, sy: number) {
return new Matrix([
[sx, 0, 0],
[0, sy, 0],
[0, 0, 1],
]);
}
export function rotation(angle: number) {
const cosa = Math.cos(angle);
const sina = Math.sin(angle);
return new Matrix([
[cosa, -sina, 0],
[sina, cosa, 0],
[0, 0, 1],
]);
}
import {
scale,
translate,
compose,
Matrix,
applyToPoint,
} from 'transformation-matrix';
export function computeTransformToBox(inBox: Box, toBox: Box): Matrix {
return translation(toBox.x, toBox.y).mmul(
return compose(
translate(toBox.x, toBox.y),
scale(toBox.width / inBox.width, toBox.height / inBox.height)
);
}
export function pointToArray(point: Point) {
return [point.x, point.y];
}
export function transformPointVec(point: number[], transform: Matrix) {
point[2] = 1;
const mat = new Matrix([point]).transpose();
const mulmat = new Matrix(transform).mmul(mat).to1DArray();
// console.log({point, mat, mulmat});
return mulmat;
}
export function transformPoint(point: Point, transform: Matrix) {
const pointVec = transformPointVec(pointToArray(point), transform);
return new Point(pointVec[0], pointVec[1]);
const txdPoint = applyToPoint(transform, point);
return new Point(txdPoint.x, txdPoint.y);
}
export function transformPoints(points: Point[], transform: Matrix) {

View file

@ -3634,12 +3634,7 @@ eve@~0.5.1:
resolved "https://registry.npmjs.org/eve/-/eve-0.5.4.tgz"
integrity sha1-Z9CAuXJSkdfjieNMJoYN2X8d66o=
eventemitter3@^4.0.0:
version "4.0.7"
resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
eventemitter3@^4.0.7:
eventemitter3@^4.0.0, eventemitter3@^4.0.7:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
@ -7113,6 +7108,11 @@ tr46@~0.0.3:
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
transformation-matrix@^2.10.0:
version "2.10.0"
resolved "https://registry.yarnpkg.com/transformation-matrix/-/transformation-matrix-2.10.0.tgz#a135638fdbf91da2e75316cf68648e2436c60a1d"
integrity sha512-KH+vJ3eW9rZQMJY2nbhtX9ogUSb4vlolFaVJwe0J1nChBoE/G5+EVqwMgd8kxxL0pdb5PIeRypf3RjzgyDC+0A==
tryer@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz"