Skip to content

Latest commit

 

History

History
599 lines (565 loc) · 17 KB

File metadata and controls

599 lines (565 loc) · 17 KB

Install

npm install @lunit/is-complex-polygon

API

isComplexPolygon(polygon: [number, number][]): boolean

Sample Codes

Stories

__stories__/isComplexPolygon.stories.tsx

import {
  ContourDrawer,
  CornerstoneImage,
  CornerstoneSingleImage,
  InsightViewer,
  InsightViewerContainer,
  installWADOImageLoader,
  Point,
  unloadWADOImage,
  useInsightViewerSync,
  withInsightViewerStorybookGlobalStyle,
} from '@lunit/insight-viewer';
import { isComplexPolygon } from '@lunit/is-complex-polygon';
import { withOPTComponentsStorybookGlobalStyle } from '@lunit/opt-components';
import { storiesOf } from '@storybook/react';
import React, { ReactNode, useCallback, useState } from 'react';

installWADOImageLoader();

const resetTime: number = Date.now();
const image: CornerstoneImage = new CornerstoneSingleImage(
  `wadouri:https://lunit-frontend-fixtures.netlify.com/dcm-files/series/CT000010.dcm`,
  { unload: unloadWADOImage },
);

function doNothing() {
  // DO NOTHING
}

function Sample() {
  const width: number = 400;
  const height: number = 500;

  const [interactionElement, setInteractionElement] = useState<HTMLElement | null>(null);

  const { cornerstoneRenderData, updateCornerstoneRenderData } = useInsightViewerSync();

  const [checkResult, setCheckResult] = useState<ReactNode>(null);

  const check = useCallback((polygon: Point[]) => {
    const result: boolean = isComplexPolygon(polygon);

    setCheckResult(
      <div>
        <h3>
          <span role="img" aria-label="polygon">
            🧬
          </span>{' '}
          POLYGON
        </h3>
        <pre>
          <code>{JSON.stringify(polygon)}</code>
        </pre>
        <p>
          <span role="img" aria-label="question">
            🤷‍♂️
          </span>{' '}
          IS COMPLEX POLYGON? → {result ? 'YES' : 'NO'}
        </p>
      </div>,
    );
  }, []);

  return (
    <div>
      <InsightViewerContainer ref={setInteractionElement} width={width} height={height}>
        <InsightViewer
          width={width}
          height={height}
          invert={false}
          flip={false}
          pan={false}
          adjust={false}
          zoom={false}
          resetTime={resetTime}
          image={image}
          updateCornerstoneRenderData={updateCornerstoneRenderData}
        />
        {cornerstoneRenderData && (
          <ContourDrawer
            width={width}
            height={height}
            contours={[]}
            draw={interactionElement}
            onFocus={doNothing}
            onAdd={check}
            onRemove={doNothing}
            cornerstoneRenderData={cornerstoneRenderData}
          />
        )}
      </InsightViewerContainer>
      <div>
        <pre>
          <code>{checkResult}</code>
        </pre>
      </div>
    </div>
  );
}

storiesOf('in-complex-polygon', module)
  .addDecorator(withOPTComponentsStorybookGlobalStyle)
  .addDecorator(withInsightViewerStorybookGlobalStyle)
  .add('isComplexPolygon()', () => <Sample />);

Tests

__tests__/isComplexPolygon.test.ts

import { isComplexPolygon } from '@lunit/is-complex-polygon';

describe('isComplexPolygon()', () => {
  test('Polygon은 Complex Polygon이다', () => {
    expect(
      isComplexPolygon([
        [267.52, 179.20000000000002],
        [266.24, 179.20000000000002],
        [261.12, 175.36],
        [254.72, 170.24],
        [249.6, 163.84],
        [243.20000000000002, 153.6],
        [234.24, 140.8],
        [229.12, 131.84],
        [216.32, 115.20000000000002],
        [204.8, 98.56],
        [195.84, 87.03999999999999],
        [181.76, 72.96000000000001],
        [175.36, 69.12],
        [166.4, 62.72],
        [161.28, 60.16],
        [154.88, 57.60000000000001],
        [149.76, 56.32000000000001],
        [144.64000000000001, 56.32000000000001],
        [139.52, 56.32000000000001],
        [134.4, 56.32000000000001],
        [130.56, 58.879999999999995],
        [125.44, 62.72],
        [121.60000000000001, 67.84],
        [115.2, 74.24000000000001],
        [111.36, 80.64000000000001],
        [107.52, 88.32],
        [101.12, 98.56],
        [98.56, 104.96000000000001],
        [94.72, 115.20000000000002],
        [92.16, 126.72],
        [90.88, 135.68],
        [90.88, 145.92000000000002],
        [90.88, 156.16],
        [92.16, 165.12],
        [97.28, 172.8],
        [102.4, 179.20000000000002],
        [110.08, 188.16],
        [116.48, 190.72],
        [122.88, 192],
        [129.28, 193.28000000000003],
        [136.96, 193.28000000000003],
        [142.08, 192],
        [148.48, 186.88],
        [158.72, 177.92000000000002],
        [166.4, 168.96],
        [171.52, 162.56],
        [176.64000000000001, 154.88],
        [181.76, 144.64000000000001],
        [186.88, 135.68],
        [192, 125.44],
        [195.84, 117.75999999999999],
        [202.24, 106.24000000000001],
        [207.36, 94.72],
        [213.76, 85.75999999999999],
        [221.44, 75.52000000000001],
        [230.4, 66.56],
        [240.64000000000001, 56.32000000000001],
        [249.6, 49.92],
        [262.4, 40.96000000000001],
        [271.36, 38.400000000000006],
        [280.32, 34.56],
        [294.40000000000003, 29.439999999999998],
        [302.08, 28.159999999999997],
        [311.04, 26.879999999999995],
        [320, 26.879999999999995],
        [327.68, 26.879999999999995],
        [334.08, 29.439999999999998],
        [339.2, 30.72],
        [344.32, 33.28],
        [349.44, 38.400000000000006],
        [354.56, 42.24000000000001],
        [358.40000000000003, 47.36],
        [362.24, 52.480000000000004],
        [366.08, 58.879999999999995],
        [369.92, 66.56],
        [372.48, 75.52000000000001],
        [376.32, 83.20000000000002],
        [377.6, 88.32],
        [377.6, 96],
        [378.88, 101.12],
        [378.88, 107.52000000000001],
        [378.88, 112.64000000000001],
        [377.6, 119.03999999999999],
        [373.76, 122.88],
        [369.92, 128],
      ]),
    ).toBeTruthy();

    expect(
      isComplexPolygon([
        [312.32, 107.52000000000001],
        [312.32, 106.24000000000001],
        [311.04, 102.4],
        [308.48, 97.28],
        [304.64, 88.32],
        [300.8, 81.92000000000002],
        [298.24, 74.24000000000001],
        [293.12, 67.84],
        [286.72, 57.60000000000001],
        [275.2, 44.8],
        [264.96, 37.120000000000005],
        [256, 32],
        [241.92000000000002, 21.760000000000005],
        [235.52, 19.200000000000003],
        [225.28, 16.64],
        [216.32, 15.36],
        [207.36, 14.079999999999998],
        [199.68, 14.079999999999998],
        [193.28, 14.079999999999998],
        [186.88, 15.36],
        [180.48, 16.64],
        [174.08, 19.200000000000003],
        [170.24, 21.760000000000005],
        [161.28, 28.159999999999997],
        [154.88, 32],
        [149.76, 38.400000000000006],
        [144.64000000000001, 44.8],
        [138.24, 51.2],
        [133.12, 58.879999999999995],
        [129.28, 65.28],
        [125.44, 72.96000000000001],
        [121.60000000000001, 80.64000000000001],
        [120.32000000000001, 88.32],
        [119.04, 97.28],
        [119.04, 106.24000000000001],
        [119.04, 116.47999999999999],
        [119.04, 125.44],
        [120.32000000000001, 134.4],
        [125.44, 144.64000000000001],
        [130.56, 151.04],
        [135.68, 158.72],
        [142.08, 163.84],
        [148.48, 168.96],
        [154.88, 171.52],
        [157.44, 171.52],
        [165.12, 172.8],
        [168.96, 174.08],
        [172.8, 174.08],
        [180.48, 170.24],
        [184.32, 166.4],
        [190.72, 158.72],
        [197.12, 148.48000000000002],
        [202.24, 140.8],
        [206.08, 133.12],
        [212.48000000000002, 119.03999999999999],
        [216.32, 110.08000000000001],
        [221.44, 98.56],
        [225.28, 85.75999999999999],
        [229.12, 75.52000000000001],
        [231.68, 66.56],
        [234.24, 56.32000000000001],
        [238.08, 46.08],
        [240.64000000000001, 38.400000000000006],
        [243.20000000000002, 34.56],
        [247.04, 26.879999999999995],
        [250.88, 21.760000000000005],
        [253.44, 16.64],
        [259.84000000000003, 11.519999999999996],
        [266.24, 8.960000000000008],
        [273.92, 6.400000000000006],
        [282.88, 2.5600000000000023],
        [293.12, 1.2800000000000011],
        [303.36, -1.2800000000000011],
        [316.16, -1.2800000000000011],
        [332.8, -2.5600000000000023],
        [343.04, -2.5600000000000023],
        [353.28000000000003, -1.2800000000000011],
        [362.24, 1.2800000000000011],
        [372.48, 3.8400000000000034],
        [381.44, 7.680000000000007],
        [389.12, 12.799999999999997],
        [396.8, 16.64],
        [403.2, 21.760000000000005],
        [409.6, 29.439999999999998],
        [414.72, 35.84],
        [419.84000000000003, 42.24000000000001],
        [423.68, 49.92],
        [427.52, 55.040000000000006],
        [430.08, 61.44],
        [431.36, 65.28],
        [432.64, 71.68],
        [435.2, 78.08000000000001],
        [435.2, 81.92000000000002],
        [435.2, 87.03999999999999],
        [435.2, 90.88],
        [433.92, 94.72],
        [431.36, 98.56],
        [428.8, 101.12],
        [423.68, 104.96000000000001],
        [419.84000000000003, 107.52000000000001],
        [409.6, 112.64000000000001],
        [401.92, 115.20000000000002],
        [399.36, 116.47999999999999],
        [395.52, 119.03999999999999],
      ]),
    ).toBeTruthy();

    expect(
      isComplexPolygon([
        [322.56, 332.8],
        [322.56, 332.8],
        [317.44, 330.24],
        [311.04, 326.40000000000003],
        [302.08, 322.56],
        [290.56, 316.16],
        [279.04, 309.76],
        [263.68, 302.08],
        [252.16, 295.68],
        [239.36, 286.72],
        [226.56, 279.04],
        [220.16, 275.2],
        [209.92000000000002, 268.8],
        [200.96, 262.40000000000003],
        [193.28, 256],
        [185.6, 249.60000000000002],
        [179.20000000000002, 243.2],
        [172.8, 238.07999999999998],
        [165.12, 231.68],
        [160, 225.28000000000003],
        [153.6, 218.88],
        [145.92000000000002, 208.64],
        [138.24, 200.95999999999998],
        [134.4, 195.84000000000003],
        [128, 189.44],
        [122.88, 183.04],
        [119.04, 175.36],
        [116.48, 168.96],
        [115.2, 162.56],
        [113.92, 156.16],
        [113.92, 151.04],
        [113.92, 140.8],
        [119.04, 133.12],
        [128, 122.88],
        [139.52, 112.64000000000001],
        [158.72, 98.56],
        [170.24, 90.88],
        [188.16, 80.64000000000001],
        [203.52, 71.68],
        [216.32, 66.56],
        [227.84, 61.44],
        [238.08, 57.60000000000001],
        [249.6, 53.760000000000005],
        [257.28000000000003, 53.760000000000005],
        [263.68, 53.760000000000005],
        [270.08, 55.040000000000006],
        [275.2, 57.60000000000001],
        [279.04, 62.72],
        [282.88, 69.12],
        [288, 78.08000000000001],
        [290.56, 85.75999999999999],
        [293.12, 94.72],
        [295.68, 102.4],
        [296.96, 111.36000000000001],
        [296.96, 120.32],
        [296.96, 130.56],
        [293.12, 138.24],
        [290.56, 144.64000000000001],
        [284.16, 154.88],
        [279.04, 161.28],
        [272.64, 168.96],
        [267.52, 175.36],
        [261.12, 180.48000000000002],
        [253.44, 186.88],
        [247.04, 192],
        [239.36, 197.12],
        [234.24, 202.24],
        [226.56, 206.07999999999998],
        [220.16, 211.2],
        [212.48000000000002, 215.04000000000002],
        [207.36, 220.16000000000003],
        [200.96, 224],
        [194.56, 227.84000000000003],
        [188.16, 232.95999999999998],
        [183.04, 238.07999999999998],
        [176.64000000000001, 241.92000000000002],
        [172.8, 247.04000000000002],
        [168.96, 252.16000000000003],
        [166.4, 254.72000000000003],
        [162.56, 259.84000000000003],
        [161.28, 262.40000000000003],
        [160, 266.24],
        [158.72, 268.8],
        [156.16, 276.48],
        [156.16, 279.04],
        [156.16, 282.88],
        [154.88, 286.72],
        [154.88, 289.28000000000003],
        [154.88, 294.40000000000003],
        [154.88, 296.96],
        [154.88, 299.52],
        [156.16, 303.36],
        [157.44, 307.2],
        [160, 311.04],
        [161.28, 314.88],
        [163.84, 318.72],
        [166.4, 322.56],
        [170.24, 326.40000000000003],
        [172.8, 330.24],
        [176.64000000000001, 334.08],
        [180.48, 337.92],
        [184.32, 341.76],
        [189.44, 344.32],
        [193.28, 348.16],
        [198.4, 349.44],
        [202.24, 352],
        [209.92000000000002, 353.28000000000003],
        [215.04, 355.84000000000003],
        [221.44, 357.12],
        [227.84, 357.12],
        [234.24, 358.40000000000003],
        [243.20000000000002, 359.68],
        [250.88, 359.68],
        [257.28000000000003, 359.68],
        [263.68, 359.68],
        [270.08, 358.40000000000003],
        [275.2, 355.84000000000003],
      ]),
    ).toBeTruthy();
  });

  test('Polygon은 Simple Polygon이다', () => {
    expect(
      isComplexPolygon([
        [179.20000000000002, -15.36],
        [179.20000000000002, -15.36],
        [174.08, -15.36],
        [168.96, -12.799999999999997],
        [161.28, -11.519999999999996],
        [153.6, -10.240000000000002],
        [145.92000000000002, -8.96],
        [134.4, -7.68],
        [119.04, -3.8399999999999963],
        [103.68, 1.2800000000000011],
        [89.60000000000001, 8.960000000000008],
        [83.2, 14.079999999999998],
        [72.96000000000001, 21.760000000000005],
        [66.56, 29.439999999999998],
        [61.44, 37.120000000000005],
        [57.6, 46.08],
        [56.32, 55.040000000000006],
        [56.32, 64],
        [57.6, 75.52000000000001],
        [61.44, 84.47999999999999],
        [70.4, 97.28],
        [80.64, 108.80000000000001],
        [93.44, 120.32],
        [110.08, 130.56],
        [125.44, 140.8],
        [143.36, 147.20000000000002],
        [160, 152.32],
        [177.92000000000002, 153.6],
        [194.56, 153.6],
        [208.64000000000001, 153.6],
        [218.88, 148.48000000000002],
        [230.4, 142.08],
        [241.92000000000002, 134.4],
        [250.88, 129.28],
        [259.84000000000003, 122.88],
        [267.52, 113.92000000000002],
        [271.36, 108.80000000000001],
        [279.04, 97.28],
        [282.88, 88.32],
        [285.44, 78.08000000000001],
        [285.44, 66.56],
        [285.44, 53.760000000000005],
        [282.88, 43.519999999999996],
        [279.04, 33.28],
        [275.2, 24.320000000000007],
        [272.64, 20.480000000000004],
        [268.8, 12.799999999999997],
        [264.96, 5.1200000000000045],
        [262.4, 0],
      ]),
    ).toBeFalsy();

    expect(
      isComplexPolygon([
        [253.44, 97.28],
        [252.16, 97.28],
        [247.04, 93.44],
        [239.36, 90.88],
        [231.68, 87.03999999999999],
        [225.28, 83.20000000000002],
        [217.6, 79.36000000000001],
        [211.20000000000002, 78.08000000000001],
        [200.96, 74.24000000000001],
        [186.88, 72.96000000000001],
        [177.92000000000002, 71.68],
        [168.96, 71.68],
        [156.16, 74.24000000000001],
        [148.48, 75.52000000000001],
        [140.8, 80.64000000000001],
        [133.12, 85.75999999999999],
        [126.72, 92.16],
        [120.32000000000001, 98.56],
        [111.36, 111.36000000000001],
        [107.52, 119.03999999999999],
        [102.4, 129.28],
        [97.28, 142.08],
        [94.72, 152.32],
        [92.16, 163.84],
        [88.32000000000001, 180.48000000000002],
        [87.04, 192],
        [87.04, 200.95999999999998],
        [87.04, 211.2],
        [87.04, 222.72000000000003],
        [88.32000000000001, 234.24],
        [90.88, 240.64],
        [96, 248.32],
        [101.12, 256],
        [108.8, 262.40000000000003],
        [117.76, 270.08],
        [126.72, 273.92],
        [139.52, 279.04],
        [152.32, 281.6],
        [166.4, 282.88],
        [184.32, 284.16],
        [209.92000000000002, 284.16],
        [224, 281.6],
        [244.48000000000002, 277.76],
        [266.24, 275.2],
        [281.6, 271.36],
        [298.24, 266.24],
        [308.48, 262.40000000000003],
        [321.28000000000003, 257.28000000000003],
        [331.52, 250.88],
        [340.48, 243.2],
        [346.88, 236.8],
        [352, 230.40000000000003],
        [355.84000000000003, 221.44],
        [357.12, 213.76],
        [358.40000000000003, 207.36],
        [358.40000000000003, 199.68],
        [359.68, 193.28000000000003],
        [359.68, 185.6],
        [359.68, 180.48000000000002],
        [359.68, 176.64000000000001],
        [358.40000000000003, 170.24],
        [358.40000000000003, 165.12],
        [357.12, 160],
        [354.56, 154.88],
        [352, 149.76],
        [348.16, 144.64000000000001],
        [344.32, 140.8],
        [340.48, 135.68],
        [335.36, 133.12],
      ]),
    ).toBeFalsy();
  });
});