Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

3.5 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import { applyToPoint, fromTRS, getScale, identity, invert, multiply, rotate, scale, translate } from './mat2d';

describe('translate', () => {
  it('shifts points by the translation vector', () => {
    const m = translate(identity(), { x: 10, y: 20 });
    expect(applyToPoint(m, { x: 0, y: 0 })).toEqual({ x: 10, y: 20 });
    expect(applyToPoint(m, { x: 5, y: 5 })).toEqual({ x: 15, y: 25 });
  });
});

describe('scale', () => {
  it('scales points uniformly when sy is omitted', () => {
    const m = scale(identity(), 2);
    expect(applyToPoint(m, { x: 3, y: 4 })).toEqual({ x: 6, y: 8 });
  });

  it('scales points non-uniformly when sy is given', () => {
    const m = scale(identity(), 2, 3);
    expect(applyToPoint(m, { x: 1, y: 1 })).toEqual({ x: 2, y: 3 });
  });
});

describe('rotate', () => {
  it('rotates a point 90 degrees clockwise', () => {
    const m = rotate(identity(), Math.PI / 2);
    const p = applyToPoint(m, { x: 1, y: 0 });
    expect(p.x).toBeCloseTo(0, 10);
    expect(p.y).toBeCloseTo(1, 10);
  });
});

describe('multiply', () => {
  it('composes transforms so the right operand applies first', () => {
    // translate(10,0) * scale(2) applied to (1,1): scale first -> (2,2), then translate -> (12, 2)
    const t = translate(identity(), { x: 10, y: 0 });
    const s = scale(identity(), 2);
    const m = multiply(t, s);
    expect(applyToPoint(m, { x: 1, y: 1 })).toEqual({ x: 12, y: 2 });
  });
});

describe('invert', () => {
  it('round-trips applyToPoint through a matrix and its inverse', () => {
    const m = fromTRS({ x: 5, y: -3 }, Math.PI / 6, 2, 0.5);
    const inv = invert(m);
    expect(inv).not.toBeNull();
    const p = { x: 13, y: -4 };
    const roundTripped = applyToPoint(inv as NonNullable<typeof inv>, applyToPoint(m, p));
    expect(roundTripped.x).toBeCloseTo(p.x, 8);
    expect(roundTripped.y).toBeCloseTo(p.y, 8);
  });

  it('returns null for a singular matrix', () => {
    expect(invert({ a: 1, b: 2, c: 2, d: 4, e: 0, f: 0 })).toBeNull();
    expect(invert({ a: 0, b: 0, c: 0, d: 0, e: 1, f: 1 })).toBeNull();
  });
});

describe('fromTRS', () => {
  it('matches manual translate . rotate . scale composition', () => {
    const translation = { x: 4, y: -2 };
    const rad = 0.7;
    const sx = 1.5;
    const sy = 2.5;

    const t = translate(identity(), translation);
    const r = rotate(identity(), rad);
    const s = scale(identity(), sx, sy);
    const manual = multiply(multiply(t, r), s);
    const composed = fromTRS(translation, rad, sx, sy);

    expect(composed.a).toBeCloseTo(manual.a, 10);
    expect(composed.b).toBeCloseTo(manual.b, 10);
    expect(composed.c).toBeCloseTo(manual.c, 10);
    expect(composed.d).toBeCloseTo(manual.d, 10);
    expect(composed.e).toBeCloseTo(manual.e, 10);
    expect(composed.f).toBeCloseTo(manual.f, 10);
  });

  it('defaults scaleY to scaleX for uniform scale', () => {
    const composed = fromTRS({ x: 0, y: 0 }, 0, 3, 3);
    const defaulted = fromTRS({ x: 0, y: 0 }, 0, 3);
    expect(defaulted).toEqual(composed);
  });
});

describe('getScale', () => {
  it('returns the scale factor for a uniformly scaled matrix', () => {
    expect(getScale(scale(identity(), 3))).toBeCloseTo(3, 10);
  });

  it('returns the geometric mean for a non-uniformly scaled matrix', () => {
    expect(getScale(scale(identity(), 2, 8))).toBeCloseTo(4, 10);
  });

  it('is unaffected by rotation for a uniformly scaled matrix', () => {
    const m = rotate(scale(identity(), 5), 1.234);
    expect(getScale(m)).toBeCloseTo(5, 8);
  });
});