projection-3d-2d
Version:
Project (transform) point coordinates from 3D to 2D and unproject it back.
83 lines (81 loc) • 2.57 kB
JavaScript
const assert = require('assert');
const { ProjectionCalculator3d, ProjectionCalculator2d } = require('../index.js');
describe('Projection calculator', () => {
const EPSILON = 0.0001;
it('Test 2d projection', () => {
const points3d = [
[],
[],
[],
[],
];
const points2d = [
[],
[],
[],
[],
];
const projectionCalculator = new ProjectionCalculator2d(points3d, points2d);
const projectedPoint = projectionCalculator.getUnprojectedPoint(points2d[0]);
const restoredPoint = projectionCalculator.getProjectedPoint(projectedPoint);
assert.strictEqual(points2d[0][0] - EPSILON < restoredPoint[0], true);
assert.strictEqual(restoredPoint[0] > points2d[0][0] - EPSILON, true);
assert.strictEqual(points2d[0][1] - EPSILON < restoredPoint[1], true);
assert.strictEqual(restoredPoint[1] > points2d[0][1] - EPSILON, true);
});
it('Less than 4 points must throw an error', () => {
const points3d = [
[],
[],
[],
];
const points2d = [
[],
[],
[],
];
assert.throws(() => { new ProjectionCalculator2d(points3d, points2d) }, Error);
});
it('Test 3d projection', () => {
const points3d = [
[],
[],
[],
[],
[],
[],
];
const points2d = [
[],
[],
[],
[],
[],
[],
];
const projectionCalculator = new ProjectionCalculator3d(points3d, points2d);
const projectedPoint = projectionCalculator.getUnprojectedPoint(points2d[0], 0);
const restoredPoint = projectionCalculator.getProjectedPoint(projectedPoint);
assert.strictEqual(points2d[0][0] - EPSILON < restoredPoint[0], true);
assert.strictEqual(restoredPoint[0] > points2d[0][0] - EPSILON, true);
assert.strictEqual(points2d[0][1] - EPSILON < restoredPoint[1], true);
assert.strictEqual(restoredPoint[1] > points2d[0][1] - EPSILON, true);
});
it('Less than 6 points must throw an error', () => {
const points3d = [
[],
[],
[],
[],
[],
];
const points2d = [
[],
[],
[],
[],
[],
];
assert.throws(() => { new ProjectionCalculator3d(points3d, points2d) }, Error);
});
});