tests_math_circle.test.js

import {describe, test, assert} from 'vitest';
import {Point2D} from '../../src/math/point.js';
import {Index} from '../../src/math/index.js';
import {Circle} from '../../src/math/circle.js';

/**
 * Tests for the 'math/circle.js' file.
 */

describe('math', () => {

  /**
   * Tests for {@link Circle}.
   *
   * @function module:tests/math~circle-class
   */
  test('Circle class - #DWV-REQ-UI-07-002 Draw circle', () => {
    const center0 = new Point2D(0, 0);
    const c0 = new Circle(center0, 2);
    // getCenter
    assert.equal(c0.getCenter(), center0, 'getCenter');
    // getRadius
    assert.equal(c0.getRadius(), 2, 'getRadius');

    // equals: true
    const c1 = new Circle(center0, 2);
    assert.ok(c0.equals(c1), 'equal circles');
    // equals: false radius
    const c20 = new Circle(center0, 3);
    assert.notOk(c0.equals(c20), 'non equal circles radius');
    // equals: false center
    const center21 = new Point2D(1, 1);
    const c21 = new Circle(center21, 2);
    assert.notOk(c0.equals(c21), 'non equal circles center');

    // getSurface
    assert.equal(c0.getSurface(), Math.PI * 2 * 2, 'getSurface');
    // getWorldSurface
    const spacing2D = {x: 0.5, y: 0.5};
    assert.equal(c0.getWorldSurface(spacing2D), Math.PI, 'getWorldSurface');
  });

  /**
   * Tests for {@link Circle} quantification.
   *
   * @function module:tests/math~circle-quantification
   */
  test('Circle quantification - #DWV-REQ-UI-07-002 Draw circle',
    () => {
      const center0 = new Point2D(2, 2);
      const c0 = new Circle(center0, 2);
      // view controller
      const mockVc0 = {
        canQuantifyImage: function () {
          return true;
        },
        get2DSpacing: function () {
          return {x: 1, y: 1};
        },
        getImageVariableRegionValues: function () {
          return [0, 1, 1, 0, 0, 1, 1, 0];
        },
        getPixelUnit: function () {
          return '';
        },
        getLengthUnit: function () {
          return 'unit.mm';
        }
      };
      const theoQuant0 = {
        min: {value: 0, unit: ''},
        max: {value: 1, unit: ''},
        mean: {value: 0.5, unit: ''},
        stdDev: {value: 0.5, unit: ''},
        surface: {value: 0.12566370614359174, unit: undefined}
      };
      const index0 = new Index([0, 0, 0]);
      const resQuant0 = c0.quantify(mockVc0, index0);
      assert.equal(resQuant0.min.value, theoQuant0.min.value, 'quant min');
      assert.equal(resQuant0.max.value, theoQuant0.max.value, 'quant max');
      assert.equal(resQuant0.mean.value, theoQuant0.mean.value, 'quant mean');
      assert.equal(
        resQuant0.stdDev.value, theoQuant0.stdDev.value, 'quant stdDev');
      assert.equal(
        resQuant0.surface.value, theoQuant0.surface.value, 'quant surface');
    }
  );

});