All files / Sources/Imaging/Core/ImageReslice index.js

70.27% Statements 390/555
58.76% Branches 124/211
51.61% Functions 16/31
71.89% Lines 376/523

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142                                            1x   1x               7x 7x   7x 7x                                                                                                                                       7x 119x 119x     119x     7x 38x 5x     38x 23x   23x 23x   15x     7x   21x   21x               21x 21x 21x 21x 21x   21x 21x 21x 21x   21x 21x 20x   21x 21x   21x           21x 21x 20x     21x 63x 63x 63x 63x   63x 9x 9x 27x 27x 27x 27x       27x 27x   9x 9x 9x   54x 54x 54x 54x     63x 9x   54x     63x 20x 20x 43x 7x 4x   7x 7x       36x 36x     63x 20x 43x 7x   4x     3x         36x   63x     21x 21x 2x     21x         21x       21x             21x 21x 21x 21x 21x 1x   21x   21x   21x 21x 21x                         21x   21x 21x 21x 21x   21x 21x 21x 21x 21x 21x   21x   21x   21x   21x         7x   21x 21x 21x 21x 21x 21x     21x     21x     21x     21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x   21x 21x       21x 21x 21x     21x   21x       21x                           21x 21x 21x       21x 21x 21x 21x 21x 84x 84x 84x 84x       21x 21x 20x         21x                       21x   21x             21x           21x           21x 21x 21x 21x     21x 21x 21x 21x 21x         21x 21x   21x 7187x 7187x   7187x               7187x     7187x 71x 71x 71x 71x 71x 71x       7187x 7187x 7187x 7187x 7187x 7187x       7187x 7187x   7187x 4586x 4586x 4586x 4586x 4586x 4586x   4586x 8436x 1146866x             1146866x 1146866x 1146866x   1146866x 1146866x 1146866x                   1146866x   1081344x 1081344x 1081344x 1081344x     1146866x                                       1146866x   1018070x 1018070x 1018070x 1018070x           1146866x             1146866x 1146866x       1146866x       8436x 8436x   8436x 8436x 4586x       4586x 362x                 4586x                         4586x               3850x   8436x 1146866x     8436x 8436x       2601x 2601x   2601x 2601x 2601x   2601x 2601x 2601x   2601x 2601x 2601x 2601x   2601x 132651x           132651x 132651x 132651x   132651x               132651x   2601x 2601x 2601x           2601x         132651x     132651x         132651x   132651x 132651x                                                     2601x 2601x           2601x       2601x                                       7x 21x 21x   21x 20x   21x                   21x 21x       21x 21x 21x     21x   21x     7x 20x 20x 20x 20x 20x   20x 20x 20x       20x 20x     20x 20x 1x                 20x                 20x 20x 160x 160x   160x   160x   160x 8x     160x       160x   160x 160x 160x 160x   160x 480x 149x   480x 151x       20x     7x 42x           2x                       40x       7x               7x 4586x 4586x 1018070x   4586x     7x             21x 21x         21x 21x 21x 21x 21x 21x 21x         21x     21x                   21x     7x               7x 9052x 9052x     7x 21x   7x 21x   21x   21x 21x                                       21x     7x                     7x             362x 362x 65522x       7x                                               7x                     7x 21x         7x                                                               1x                                                           7x     7x     7x   7x                                   7x 7x 7x 7x   7x     7x         1x          
import { vec4, mat4 } from 'gl-matrix';
 
import macro, { vtkWarningMacro } from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData';
import vtkImageInterpolator from 'vtk.js/Sources/Imaging/Core/ImageInterpolator';
import vtkImagePointDataIterator from 'vtk.js/Sources/Imaging/Core/ImagePointDataIterator';
import {
  ImageBorderMode,
  InterpolationMode,
} from 'vtk.js/Sources/Imaging/Core/AbstractImageInterpolator/Constants';
import {
  vtkInterpolationMathFloor,
  vtkInterpolationMathRound,
  vtkInterpolationMathClamp,
} from 'vtk.js/Sources/Imaging/Core/AbstractImageInterpolator/InterpolationInfo';
import Constants from 'vtk.js/Sources/Imaging/Core/ImageReslice/Constants';
 
const { SlabMode } = Constants;
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkImageReslice methods
// ----------------------------------------------------------------------------
 
function vtkImageReslice(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageReslice');
  const superClass = { ...publicAPI };
 
  const indexMatrix = mat4.identity(new Float64Array(16));
  let optimizedTransform = null;
 
  function getImageResliceSlabTrap(tmpPtr, inComponents, sampleCount, f) {
    const n = sampleCount - 1;
    for (let i = 0; i < inComponents; i += 1) {
      let result = tmpPtr[i] * 0.5;
      for (let j = 1; j < n; j += 1) {
        result += tmpPtr[i + j * inComponents];
      }
      result += tmpPtr[i + n * inComponents] * 0.5;
      tmpPtr[i] = result * f;
    }
  }
 
  function getImageResliceSlabSum(tmpPtr, inComponents, sampleCount, f) {
    for (let i = 0; i < inComponents; i += 1) {
      let result = tmpPtr[i];
      for (let j = 1; j < sampleCount; j += 1) {
        result += tmpPtr[i + j * inComponents];
      }
      tmpPtr[i] = result * f;
    }
  }
 
  function getImageResliceCompositeMinValue(tmpPtr, inComponents, sampleCount) {
    for (let i = 0; i < inComponents; i += 1) {
      let result = tmpPtr[i];
      for (let j = 1; j < sampleCount; j += 1) {
        result = Math.min(result, tmpPtr[i + j * inComponents]);
      }
      tmpPtr[i] = result;
    }
  }
 
  function getImageResliceCompositeMaxValue(tmpPtr, inComponents, sampleCount) {
    for (let i = 0; i < inComponents; i += 1) {
      let result = tmpPtr[i];
      for (let j = 1; j < sampleCount; j += 1) {
        result = Math.max(result, tmpPtr[i + j * inComponents]);
      }
      tmpPtr[i] = result;
    }
  }
 
  function getImageResliceCompositeMeanValue(
    tmpPtr,
    inComponents,
    sampleCount
  ) {
    const f = 1.0 / sampleCount;
    getImageResliceSlabSum(tmpPtr, inComponents, sampleCount, f);
  }
 
  function getImageResliceCompositeMeanTrap(tmpPtr, inComponents, sampleCount) {
    const f = 1.0 / (sampleCount - 1);
    getImageResliceSlabTrap(tmpPtr, inComponents, sampleCount, f);
  }
 
  function getImageResliceCompositeSumValue(tmpPtr, inComponents, sampleCount) {
    const f = 1.0;
    getImageResliceSlabSum(tmpPtr, inComponents, sampleCount, f);
  }
 
  function getImageResliceCompositeSumTrap(tmpPtr, inComponents, sampleCount) {
    const f = 1.0;
    getImageResliceSlabTrap(tmpPtr, inComponents, sampleCount, f);
  }
 
  publicAPI.getMTime = () => {
    let mTime = superClass.getMTime();
    Iif (model.resliceTransform) {
      mTime = Math.max(mTime, model.resliceTransform.getMTime());
    }
    return mTime;
  };
 
  publicAPI.setResliceAxes = (resliceAxes) => {
    if (!model.resliceAxes) {
      model.resliceAxes = mat4.identity(new Float64Array(16));
    }
 
    if (!mat4.exactEquals(model.resliceAxes, resliceAxes)) {
      mat4.copy(model.resliceAxes, resliceAxes);
 
      publicAPI.modified();
      return true;
    }
    return false;
  };
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
 
    Iif (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    // console.time('reslice');
 
    // Retrieve output and volume data
    const origin = input.getOrigin();
    const inSpacing = input.getSpacing();
    const dims = input.getDimensions();
    const inScalars = input.getPointData().getScalars();
    const inWholeExt = [0, dims[0] - 1, 0, dims[1] - 1, 0, dims[2] - 1];
 
    const outOrigin = [0, 0, 0];
    const outSpacing = [1, 1, 1];
    const outWholeExt = [0, 0, 0, 0, 0, 0];
    const outDims = [0, 0, 0];
 
    const matrix = mat4.identity(new Float64Array(16));
    if (model.resliceAxes) {
      mat4.multiply(matrix, matrix, model.resliceAxes);
    }
    const imatrix = new Float64Array(16);
    mat4.invert(imatrix, matrix);
 
    const inCenter = [
      origin[0] + 0.5 * (inWholeExt[0] + inWholeExt[1]) * inSpacing[0],
      origin[1] + 0.5 * (inWholeExt[2] + inWholeExt[3]) * inSpacing[1],
      origin[2] + 0.5 * (inWholeExt[4] + inWholeExt[5]) * inSpacing[2],
    ];
 
    let maxBounds = null;
    if (model.autoCropOutput) {
      maxBounds = publicAPI.getAutoCroppedOutputBounds(input);
    }
 
    for (let i = 0; i < 3; i++) {
      let s = 0; // default output spacing
      let d = 0; // default linear dimension
      let e = 0; // default extent start
      let c = 0; // transformed center-of-volume
 
      if (model.transformInputSampling) {
        let r = 0.0;
        for (let j = 0; j < 3; j++) {
          c += imatrix[4 * j + i] * (inCenter[j] - matrix[4 * 3 + j]);
          const tmp = matrix[4 * i + j] * matrix[4 * i + j];
          s += tmp * Math.abs(inSpacing[j]);
          d +=
            tmp *
            (inWholeExt[2 * j + 1] - inWholeExt[2 * j]) *
            Math.abs(inSpacing[j]);
          e += tmp * inWholeExt[2 * j];
          r += tmp;
        }
        s /= r;
        d /= r * Math.sqrt(r);
        e /= r;
      } else {
        c = inCenter[i];
        s = inSpacing[i];
        d = (inWholeExt[2 * i + 1] - inWholeExt[2 * i]) * s;
        e = inWholeExt[2 * i];
      }
 
      if (model.outputSpacing == null) {
        outSpacing[i] = s;
      } else {
        outSpacing[i] = model.outputSpacing[i];
      }
 
      if (i >= model.outputDimensionality) {
        outWholeExt[2 * i] = 0;
        outWholeExt[2 * i + 1] = 0;
      } else if (model.outputExtent == null) {
        if (model.autoCropOutput) {
          d = maxBounds[2 * i + 1] - maxBounds[2 * i];
        }
        outWholeExt[2 * i] = Math.round(e);
        outWholeExt[2 * i + 1] = Math.round(
          outWholeExt[2 * i] + Math.abs(d / outSpacing[i])
        );
      } else {
        outWholeExt[2 * i] = model.outputExtent[2 * i];
        outWholeExt[2 * i + 1] = model.outputExtent[2 * i + 1];
      }
 
      if (i >= model.outputDimensionality) {
        outOrigin[i] = 0;
      } else if (model.outputOrigin == null) {
        if (model.autoCropOutput) {
          // set origin so edge of extent is edge of bounds
          outOrigin[i] = maxBounds[2 * i] - outWholeExt[2 * i] * outSpacing[i];
        } else {
          // center new bounds over center of input bounds
          outOrigin[i] =
            c -
            0.5 * (outWholeExt[2 * i] + outWholeExt[2 * i + 1]) * outSpacing[i];
        }
      } else {
        outOrigin[i] = model.outputOrigin[i];
      }
      outDims[i] = outWholeExt[2 * i + 1] - outWholeExt[2 * i] + 1;
    }
 
    let dataType = inScalars.getDataType();
    if (model.outputScalarType) {
      dataType = model.outputScalarType;
    }
 
    const numComponents = input
      .getPointData()
      .getScalars()
      .getNumberOfComponents(); // or s.numberOfComponents;
 
    const outScalarsData = macro.newTypedArray(
      dataType,
      outDims[0] * outDims[1] * outDims[2] * numComponents
    );
    const outScalars = vtkDataArray.newInstance({
      name: 'Scalars',
      values: outScalarsData,
      numberOfComponents: numComponents,
    });
 
    // Update output
    const output = vtkImageData.newInstance();
    output.setDimensions(outDims);
    output.setOrigin(outOrigin);
    output.setSpacing(outSpacing);
    if (model.outputDirection) {
      output.setDirection(model.outputDirection);
    }
    output.getPointData().setScalars(outScalars);
 
    publicAPI.getIndexMatrix(input, output);
 
    let interpolationMode = model.interpolationMode;
    model.usePermuteExecute = false;
    Iif (model.optimization) {
      if (
        optimizedTransform == null &&
        model.slabSliceSpacingFraction === 1.0 &&
        model.interpolator.isSeparable() &&
        publicAPI.isPermutationMatrix(indexMatrix)
      ) {
        model.usePermuteExecute = true;
        if (publicAPI.canUseNearestNeighbor(indexMatrix, outWholeExt)) {
          interpolationMode = InterpolationMode.NEAREST;
        }
      }
    }
    model.interpolator.setInterpolationMode(interpolationMode);
 
    let borderMode = ImageBorderMode.CLAMP;
    borderMode = model.wrap ? ImageBorderMode.REPEAT : borderMode;
    borderMode = model.mirror ? ImageBorderMode.MIRROR : borderMode;
    model.interpolator.setBorderMode(borderMode);
 
    const mintol = 7.62939453125e-6;
    const maxtol = 2.0 * 2147483647;
    let tol = 0.5 * model.border;
    tol = borderMode === ImageBorderMode.CLAMP ? tol : maxtol;
    tol = tol > mintol ? tol : mintol;
    model.interpolator.setTolerance(tol);
 
    model.interpolator.initialize(input);
 
    publicAPI.vtkImageResliceExecute(input, output);
 
    model.interpolator.releaseData();
 
    outData[0] = output;
 
    // console.timeEnd('reslice');
  };
 
  publicAPI.vtkImageResliceExecute = (input, output) => {
    // const outDims = output.getDimensions();
    const inScalars = input.getPointData().getScalars();
    const outScalars = output.getPointData().getScalars();
    let outPtr = outScalars.getData();
    const outExt = output.getExtent();
    const newmat = indexMatrix;
    const outputStencil = null;
 
    // multiple samples for thick slabs
    const nsamples = Math.max(model.slabNumberOfSlices, 1);
 
    // spacing between slab samples (as a fraction of slice spacing).
    const slabSampleSpacing = model.slabSliceSpacingFraction;
 
    // check for perspective transformation
    const perspective = publicAPI.isPerspectiveMatrix(newmat);
 
    // extra scalar info for nearest-neighbor optimization
    let inPtr = inScalars.getData();
    const inputScalarSize = 1; // inScalars.getElementComponentSize(); // inScalars.getDataTypeSize();
    const inputScalarType = inScalars.getDataType();
    const inComponents = inScalars.getNumberOfComponents(); // interpolator.GetNumberOfComponents();
    const componentOffset = model.interpolator.getComponentOffset();
    const borderMode = model.interpolator.getBorderMode();
    const inDims = input.getDimensions();
    const inExt = [0, inDims[0] - 1, 0, inDims[1] - 1, 0, inDims[2] - 1]; // interpolator->GetExtent();
    const inInc = [0, 0, 0];
    inInc[0] = inScalars.getNumberOfComponents();
    inInc[1] = inInc[0] * inDims[0];
    inInc[2] = inInc[1] * inDims[1];
 
    const fullSize = inDims[0] * inDims[1] * inDims[2];
    Iif (componentOffset > 0 && componentOffset + inComponents < inInc[0]) {
      inPtr = inPtr.subarray(inputScalarSize * componentOffset);
    }
 
    let interpolationMode = InterpolationMode.NEAREST;
    if (model.interpolator.isA('vtkImageInterpolator')) {
      interpolationMode = model.interpolator.getInterpolationMode();
    }
 
    const convertScalars = null;
    const rescaleScalars =
      model.scalarShift !== 0.0 || model.scalarScale !== 1.0;
 
    // is nearest neighbor optimization possible?
    const optimizeNearest =
      interpolationMode === InterpolationMode.NEAREST &&
      borderMode === ImageBorderMode.CLAMP &&
      !(
        optimizedTransform != null ||
        perspective ||
        convertScalars != null ||
        rescaleScalars
      ) &&
      inputScalarType === outScalars.getDataType() &&
      fullSize === inScalars.getNumberOfTuples() &&
      model.border === true &&
      nsamples <= 1;
 
    // get pixel information
    const scalarType = outScalars.getDataType();
    const scalarSize = 1; // outScalars.getElementComponentSize() // outScalars.scalarSize;
    const outComponents = outScalars.getNumberOfComponents();
 
    // break matrix into a set of axes plus an origin
    // (this allows us to calculate the transform Incrementally)
    const xAxis = [0, 0, 0, 0];
    const yAxis = [0, 0, 0, 0];
    const zAxis = [0, 0, 0, 0];
    const origin = [0, 0, 0, 0];
    for (let i = 0; i < 4; ++i) {
      xAxis[i] = newmat[4 * 0 + i];
      yAxis[i] = newmat[4 * 1 + i];
      zAxis[i] = newmat[4 * 2 + i];
      origin[i] = newmat[4 * 3 + i];
    }
 
    // allocate an output row of type double
    let floatPtr = null;
    if (!optimizeNearest) {
      floatPtr = new Float64Array(
        inComponents * (outExt[1] - outExt[0] + nsamples)
      );
    }
 
    const background = macro.newTypedArray(
      inputScalarType,
      model.backgroundColor
    );
 
    // set color for area outside of input volume extent
    // void *background;
    // vtkAllocBackgroundPixel(&background,
    //    self->GetBackgroundColor(), scalarType, scalarSize, outComponents);
 
    // get various helper functions
    const forceClamping =
      interpolationMode > InterpolationMode.LINEAR ||
      (nsamples > 1 && model.slabMode === SlabMode.SUM);
    const convertpixels = publicAPI.getConversionFunc(
      inputScalarType,
      scalarType,
      model.scalarShift,
      model.scalarScale,
      forceClamping
    );
    const setpixels = publicAPI.getSetPixelsFunc(
      scalarType,
      scalarSize,
      outComponents,
      outPtr
    );
    const composite = publicAPI.getCompositeFunc(
      model.slabMode,
      model.slabTrapezoidIntegration
    );
 
    // create some variables for when we march through the data
    let idY = outExt[2] - 1;
    let idZ = outExt[4] - 1;
    const inPoint0 = [0.0, 0.0, 0.0, 0.0];
    const inPoint1 = [0.0, 0.0, 0.0, 0.0];
 
    // create an iterator to march through the data
    const iter = vtkImagePointDataIterator.newInstance();
    iter.initialize(output, outExt, model.stencil, null);
    const outPtr0 = iter.getScalars(output, 0);
    let outPtrIndex = 0;
    const outTmp = macro.newTypedArray(
      scalarType,
      vtkBoundingBox.getDiagonalLength(outExt) * outComponents * 2
    );
 
    const interpolatedPtr = new Float64Array(inComponents * nsamples);
    const interpolatedPoint = new Float64Array(inComponents);
 
    for (; !iter.isAtEnd(); iter.nextSpan()) {
      const span = iter.spanEndId() - iter.getId();
      outPtrIndex = iter.getId() * scalarSize * outComponents;
 
      Iif (!iter.isInStencil()) {
        // clear any regions that are outside the stencil
        const n = setpixels(outTmp, background, outComponents, span);
        for (let i = 0; i < n; ++i) {
          outPtr0[outPtrIndex++] = outTmp[i];
        }
      } else {
        // get output index, and compute position in input image
        const outIndex = iter.getIndex();
 
        // if Z index increased, then advance position along Z axis
        if (outIndex[2] > idZ) {
          idZ = outIndex[2];
          inPoint0[0] = origin[0] + idZ * zAxis[0];
          inPoint0[1] = origin[1] + idZ * zAxis[1];
          inPoint0[2] = origin[2] + idZ * zAxis[2];
          inPoint0[3] = origin[3] + idZ * zAxis[3];
          idY = outExt[2] - 1;
        }
 
        // if Y index increased, then advance position along Y axis
        if (outIndex[1] > idY) {
          idY = outIndex[1];
          inPoint1[0] = inPoint0[0] + idY * yAxis[0];
          inPoint1[1] = inPoint0[1] + idY * yAxis[1];
          inPoint1[2] = inPoint0[2] + idY * yAxis[2];
          inPoint1[3] = inPoint0[3] + idY * yAxis[3];
        }
 
        // march through one row of the output image
        const idXmin = outIndex[0];
        const idXmax = idXmin + span - 1;
 
        if (!optimizeNearest) {
          let wasInBounds = 1;
          let isInBounds = 1;
          let startIdX = idXmin;
          let idX = idXmin;
          const tmpPtr = floatPtr;
          let pixelIndex = 0;
 
          while (startIdX <= idXmax) {
            for (; idX <= idXmax && isInBounds === wasInBounds; idX++) {
              const inPoint2 = [
                inPoint1[0] + idX * xAxis[0],
                inPoint1[1] + idX * xAxis[1],
                inPoint1[2] + idX * xAxis[2],
                inPoint1[3] + idX * xAxis[3],
              ];
 
              const inPoint3 = [0, 0, 0, 0];
              let inPoint = inPoint2;
              isInBounds = false;
 
              let interpolatedPtrIndex = 0;
              for (let sample = 0; sample < nsamples; ++sample) {
                Iif (nsamples > 1) {
                  let s = sample - 0.5 * (nsamples - 1);
                  s *= slabSampleSpacing;
                  inPoint3[0] = inPoint2[0] + s * zAxis[0];
                  inPoint3[1] = inPoint2[1] + s * zAxis[1];
                  inPoint3[2] = inPoint2[2] + s * zAxis[2];
                  inPoint3[3] = inPoint2[3] + s * zAxis[3];
                  inPoint = inPoint3;
                }
 
                if (perspective) {
                  // only do perspective if necessary
                  const f = 1 / inPoint[3];
                  inPoint[0] *= f;
                  inPoint[1] *= f;
                  inPoint[2] *= f;
                }
 
                Iif (optimizedTransform !== null) {
                  // get the input origin and spacing for conversion purposes
                  const inOrigin = model.interpolator.getOrigin();
                  const inSpacing = model.interpolator.getSpacing();
                  const inInvSpacing = [
                    1.0 / inSpacing[0],
                    1.0 / inSpacing[1],
                    1.0 / inSpacing[2],
                  ];
 
                  // apply the AbstractTransform if there is one
                  // TBD: handle inDirection
                  publicAPI.applyTransform(
                    optimizedTransform,
                    inPoint,
                    inOrigin,
                    inInvSpacing
                  );
                }
 
                if (model.interpolator.checkBoundsIJK(inPoint)) {
                  // do the interpolation
                  isInBounds = 1;
                  model.interpolator.interpolateIJK(inPoint, interpolatedPoint);
                  for (let i = 0; i < inComponents; ++i) {
                    interpolatedPtr[interpolatedPtrIndex++] =
                      interpolatedPoint[i];
                  }
                }
              }
 
              Iif (interpolatedPtrIndex > inComponents) {
                composite(
                  interpolatedPtr,
                  inComponents,
                  interpolatedPtrIndex / inComponents
                );
              }
              for (let i = 0; i < inComponents; ++i) {
                tmpPtr[pixelIndex++] = interpolatedPtr[i];
              }
 
              // set "was in" to "is in" if first pixel
              wasInBounds = idX > idXmin ? wasInBounds : isInBounds;
            }
 
            // write a segment to the output
            const endIdX = idX - 1 - (isInBounds !== wasInBounds);
            const numpixels = endIdX - startIdX + 1;
 
            let n = 0;
            if (wasInBounds) {
              Iif (outputStencil) {
                outputStencil.insertNextExtent(startIdX, endIdX, idY, idZ);
              }
 
              if (rescaleScalars) {
                publicAPI.rescaleScalars(
                  floatPtr,
                  inComponents,
                  idXmax - idXmin + 1,
                  model.scalarShift,
                  model.scalarScale
                );
              }
 
              Iif (convertScalars) {
                convertScalars(
                  floatPtr.subarray(startIdX * inComponents),
                  outTmp,
                  inputScalarType,
                  inComponents,
                  numpixels,
                  startIdX,
                  idY,
                  idZ
                );
                n = numpixels * outComponents * scalarSize;
              } else {
                n = convertpixels(
                  outTmp,
                  floatPtr.subarray(startIdX * inComponents),
                  outComponents,
                  numpixels
                );
              }
            } else {
              n = setpixels(outTmp, background, outComponents, numpixels);
            }
            for (let i = 0; i < n; ++i) {
              outPtr0[outPtrIndex++] = outTmp[i];
            }
 
            startIdX += numpixels;
            wasInBounds = isInBounds;
          }
        } else {
          // optimize for nearest-neighbor interpolation
          const inPtrTmp0 = inPtr;
          const outPtrTmp = outPtr;
 
          const inIncX = inInc[0] * inputScalarSize;
          const inIncY = inInc[1] * inputScalarSize;
          const inIncZ = inInc[2] * inputScalarSize;
 
          const inExtX = inExt[1] - inExt[0] + 1;
          const inExtY = inExt[3] - inExt[2] + 1;
          const inExtZ = inExt[5] - inExt[4] + 1;
 
          let startIdX = idXmin;
          let endIdX = idXmin - 1;
          let isInBounds = false;
          const bytesPerPixel = inputScalarSize * inComponents;
 
          for (let iidX = idXmin; iidX <= idXmax; iidX++) {
            const inPoint = [
              inPoint1[0] + iidX * xAxis[0],
              inPoint1[1] + iidX * xAxis[1],
              inPoint1[2] + iidX * xAxis[2],
            ];
 
            const inIdX = vtkInterpolationMathRound(inPoint[0]) - inExt[0];
            const inIdY = vtkInterpolationMathRound(inPoint[1]) - inExt[2];
            const inIdZ = vtkInterpolationMathRound(inPoint[2]) - inExt[4];
 
            if (
              inIdX >= 0 &&
              inIdX < inExtX &&
              inIdY >= 0 &&
              inIdY < inExtY &&
              inIdZ >= 0 &&
              inIdZ < inExtZ
            ) {
              if (!isInBounds) {
                // clear leading out-of-bounds pixels
                startIdX = iidX;
                isInBounds = true;
                const n = setpixels(
                  outTmp,
                  background,
                  outComponents,
                  startIdX - idXmin
                );
                for (let i = 0; i < n; ++i) {
                  outPtr0[outPtrIndex++] = outTmp[i];
                }
              }
              // set the final index that was within input bounds
              endIdX = iidX;
 
              // perform nearest-neighbor interpolation via pixel copy
              let offset = inIdX * inIncX + inIdY * inIncY + inIdZ * inIncZ;
 
              // when memcpy is used with a constant size, the compiler will
              // optimize away the function call and use the minimum number
              // of instructions necessary to perform the copy
              switch (bytesPerPixel) {
                case 1:
                  outPtr0[outPtrIndex++] = inPtrTmp0[offset];
                  break;
                case 2:
                case 3:
                case 4:
                case 8:
                case 12:
                case 16:
                  for (let i = 0; i < bytesPerPixel; ++i) {
                    outPtr0[outPtrIndex++] = inPtrTmp0[offset + i];
                  }
                  break;
                default: {
                  // TODO: check bytes
                  let oc = 0;
                  do {
                    outPtr0[outPtrIndex++] = inPtrTmp0[offset++];
                  } while (++oc !== bytesPerPixel);
                  break;
                }
              }
            } else Eif (isInBounds) {
              // leaving input bounds
              break;
            }
          }
 
          // clear trailing out-of-bounds pixels
          outPtr = outPtrTmp;
          const n = setpixels(
            outTmp,
            background,
            outComponents,
            idXmax - endIdX
          );
          for (let i = 0; i < n; ++i) {
            outPtr0[outPtrIndex++] = outTmp[i];
          }
 
          Iif (outputStencil && endIdX >= startIdX) {
            outputStencil.insertNextExtent(startIdX, endIdX, idY, idZ);
          }
        }
      }
    }
  };
  /**
   * The transform matrix supplied by the user converts output coordinates
   * to input coordinates.
   * To speed up the pixel lookup, the following function provides a
   * matrix which converts output pixel indices to input pixel indices.
   * This will also concatenate the ResliceAxes and the ResliceTransform
   * if possible (if the ResliceTransform is a 4x4 matrix transform).
   * If it does, this->OptimizedTransform will be set to nullptr, otherwise
   * this->OptimizedTransform will be equal to this->ResliceTransform.
   * @param {vtkImageData} input
   * @param {vtkImageData} output
   * @returns
   */
  publicAPI.getIndexMatrix = (input, output) => {
    const transform = mat4.identity(new Float64Array(16));
    optimizedTransform = null;
 
    if (model.resliceAxes) {
      mat4.copy(transform, model.resliceAxes);
    }
    Iif (model.resliceTransform) {
      if (model.resliceTransform.isA('vtkHomogeneousTransform')) {
        mat4.multiply(transform, model.resliceTransform.getMatrix(), transform);
      } else {
        // TODO
        vtkWarningMacro('Non homogeneous transform have not yet been ported');
      }
    }
 
    // the outMatrix takes OutputData indices to OutputData coordinates,
    const outMatrix = output.getIndexToWorld();
    mat4.multiply(transform, transform, outMatrix);
 
    // the inMatrix takes InputData coordinates to InputData indices
    // the optimizedTransform requires data coords, not index coords, as its input
    if (optimizedTransform == null) {
      const inMatrix = input.getWorldToIndex();
      mat4.multiply(transform, inMatrix, transform);
    }
 
    mat4.copy(indexMatrix, transform);
 
    return indexMatrix;
  };
 
  publicAPI.getAutoCroppedOutputBounds = (input) => {
    const inOrigin = input.getOrigin();
    const inSpacing = input.getSpacing();
    const inDirection = input.getDirection();
    const dims = input.getDimensions();
    const inWholeExt = [0, dims[0] - 1, 0, dims[1] - 1, 0, dims[2] - 1];
 
    const matrix = new Float64Array(16);
    if (model.resliceAxes) {
      mat4.invert(matrix, model.resliceAxes);
    } else E{
      mat4.identity(matrix);
    }
    let transform = null;
    Iif (model.resliceTransform) {
      transform = model.resliceTransform.getInverse();
    }
    let imageTransform = null;
    if (!vtkMath.isIdentity3x3(inDirection)) {
      imageTransform = vtkMatrixBuilder
        .buildFromRadian()
        .translate(inOrigin[0], inOrigin[1], inOrigin[2])
        .multiply3x3(inDirection)
        .translate(-inOrigin[0], -inOrigin[1], -inOrigin[2])
        .invert()
        .getMatrix();
    }
 
    const bounds = [
      Number.MAX_VALUE,
      -Number.MAX_VALUE,
      Number.MAX_VALUE,
      -Number.MAX_VALUE,
      Number.MAX_VALUE,
      -Number.MAX_VALUE,
    ];
 
    const point = [0, 0, 0, 0];
    for (let i = 0; i < 8; ++i) {
      point[0] = inOrigin[0] + inWholeExt[i % 2] * inSpacing[0];
      point[1] =
        inOrigin[1] + inWholeExt[2 + (Math.floor(i / 2) % 2)] * inSpacing[1];
      point[2] =
        inOrigin[2] + inWholeExt[4 + (Math.floor(i / 4) % 2)] * inSpacing[2];
      point[3] = 1.0;
 
      if (imageTransform) {
        vec4.transformMat4(point, point, imageTransform);
      }
 
      Iif (model.resliceTransform) {
        transform.transformPoint(point, point);
      }
 
      vec4.transformMat4(point, point, matrix);
 
      const f = 1.0 / point[3];
      point[0] *= f;
      point[1] *= f;
      point[2] *= f;
 
      for (let j = 0; j < 3; ++j) {
        if (point[j] > bounds[2 * j + 1]) {
          bounds[2 * j + 1] = point[j];
        }
        if (point[j] < bounds[2 * j]) {
          bounds[2 * j] = point[j];
        }
      }
    }
    return bounds;
  };
 
  publicAPI.getDataTypeMinMax = (dataType) => {
    switch (dataType) {
      case 'Int8Array':
        return { min: -128, max: 127 };
      case 'Int16Array':
        return { min: -32768, max: 32767 };
      case 'Uint16Array':
        return { min: 0, max: 65535 };
      case 'Int32Array':
        return { min: -2147483648, max: 2147483647 };
      case 'Uint32Array':
        return { min: 0, max: 4294967295 };
      case 'Float32Array':
        return { min: -1.2e38, max: 1.2e38 };
      case 'Float64Array':
        return { min: -1.2e38, max: 1.2e38 };
      case 'Uint8Array':
      case 'Uint8ClampedArray':
      default:
        return { min: 0, max: 255 };
    }
  };
 
  publicAPI.clamp = (outPtr, inPtr, numscalars, n, min, max) => {
    const count = n * numscalars;
    for (let i = 0; i < count; ++i) {
      outPtr[i] = vtkInterpolationMathClamp(inPtr[i], min, max);
    }
    return count;
  };
 
  publicAPI.convert = (outPtr, inPtr, numscalars, n) => {
    const count = n * numscalars;
    for (let i = 0; i < count; ++i) {
      outPtr[i] = Math.round(inPtr[i]);
    }
    return count;
  };
 
  publicAPI.getConversionFunc = (
    inputType,
    dataType,
    scalarShift,
    scalarScale,
    forceClamping
  ) => {
    let useClamping = forceClamping;
    if (
      dataType !== VtkDataTypes.FLOAT &&
      dataType !== VtkDataTypes.DOUBLE &&
      !forceClamping
    ) {
      const inMinMax = publicAPI.getDataTypeMinMax(inputType);
      let checkMin = (inMinMax.min + scalarShift) * scalarScale;
      let checkMax = (inMinMax.max + scalarShift) * scalarScale;
      const outMinMax = publicAPI.getDataTypeMinMax(dataType);
      const outputMin = outMinMax.min;
      const outputMax = outMinMax.max;
      Iif (checkMin > checkMax) {
        const tmp = checkMax;
        checkMax = checkMin;
        checkMin = tmp;
      }
      useClamping = checkMin < outputMin || checkMax > outputMax;
    }
 
    Iif (
      useClamping &&
      dataType !== VtkDataTypes.FLOAT &&
      dataType !== VtkDataTypes.DOUBLE
    ) {
      const minMax = publicAPI.getDataTypeMinMax(dataType);
      const clamp = (outPtr, inPtr, numscalars, n) =>
        publicAPI.clamp(outPtr, inPtr, numscalars, n, minMax.min, minMax.max);
      return clamp;
    }
    return publicAPI.convert;
  };
 
  publicAPI.set = (outPtr, inPtr, numscalars, n) => {
    const count = numscalars * n;
    for (let i = 0; i < n; ++i) {
      outPtr[i] = inPtr[i];
    }
    return count;
  };
 
  publicAPI.set1 = (outPtr, inPtr, numscalars, n) => {
    outPtr.fill(inPtr[0], 0, n);
    return n;
  };
 
  publicAPI.getSetPixelsFunc = (dataType, dataSize, numscalars, dataPtr) =>
    numscalars === 1 ? publicAPI.set1 : publicAPI.set;
 
  publicAPI.getCompositeFunc = (slabMode, slabTrapezoidIntegration) => {
    let composite = null;
    // eslint-disable-next-line default-case
    switch (slabMode) {
      case SlabMode.MIN:
        composite = getImageResliceCompositeMinValue;
        break;
      case SlabMode.MAX:
        composite = getImageResliceCompositeMaxValue;
        break;
      case SlabMode.MEAN:
        if (slabTrapezoidIntegration) {
          composite = getImageResliceCompositeMeanTrap;
        } else {
          composite = getImageResliceCompositeMeanValue;
        }
        break;
      case SlabMode.SUM:
        if (slabTrapezoidIntegration) {
          composite = getImageResliceCompositeSumTrap;
        } else {
          composite = getImageResliceCompositeSumValue;
        }
        break;
    }
 
    return composite;
  };
 
  publicAPI.applyTransform = (newTrans, inPoint, inOrigin, inInvSpacing) => {
    inPoint[3] = 1;
    vec4.transformMat4(inPoint, inPoint, newTrans);
    inPoint[0] -= inOrigin[0];
    inPoint[1] -= inOrigin[1];
    inPoint[2] -= inOrigin[2];
    inPoint[0] *= inInvSpacing[0];
    inPoint[1] *= inInvSpacing[1];
    inPoint[2] *= inInvSpacing[2];
  };
 
  publicAPI.rescaleScalars = (
    floatData,
    components,
    n,
    scalarShift,
    scalarScale
  ) => {
    const m = n * components;
    for (let i = 0; i < m; ++i) {
      floatData[i] = (floatData[i] + scalarShift) * scalarScale;
    }
  };
 
  publicAPI.isPermutationMatrix = (matrix) => {
    for (let i = 0; i < 3; i++) {
      if (matrix[4 * i + 3] !== 0) {
        return false;
      }
    }
    if (matrix[4 * 3 + 3] !== 1) {
      return false;
    }
    for (let j = 0; j < 3; j++) {
      let k = 0;
      for (let i = 0; i < 3; i++) {
        if (matrix[4 * j + i] !== 0) {
          k++;
        }
      }
      if (k !== 1) {
        return false;
      }
    }
    return true;
  };
 
  // TODO: to move in vtkMath and add tolerance
  publicAPI.isIdentityMatrix = (matrix) => {
    for (let i = 0; i < 4; ++i) {
      for (let j = 0; j < 4; ++j) {
        if ((i === j ? 1.0 : 0.0) !== matrix[4 * j + i]) {
          return false;
        }
      }
    }
    return true;
  };
 
  publicAPI.isPerspectiveMatrix = (matrix) =>
    matrix[4 * 0 + 3] !== 0 ||
    matrix[4 * 1 + 3] !== 0 ||
    matrix[4 * 2 + 3] !== 0 ||
    matrix[4 * 3 + 3] !== 1;
 
  publicAPI.canUseNearestNeighbor = (matrix, outExt) => {
    // loop through dimensions
    for (let i = 0; i < 3; i++) {
      let j;
      for (j = 0; j < 3; j++) {
        if (matrix[4 * j + i] !== 0) {
          break;
        }
      }
      if (j >= 3) {
        return false;
      }
      let x = matrix[4 * j + i];
      let y = matrix[4 * 3 + i];
      if (outExt[2 * j] === outExt[2 * j + 1]) {
        y += x * outExt[2 * i];
        x = 0;
      }
      const fx = vtkInterpolationMathFloor(x, 0).error;
      const fy = vtkInterpolationMathFloor(y, 0).error;
      if (fx !== 0 || fy !== 0) {
        return false;
      }
    }
    return true;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  transformInputSampling: true,
  autoCropOutput: false,
  outputDimensionality: 3,
  outputSpacing: null, // automatically computed if null
  outputOrigin: null, // automatically computed if null
  outputDirection: null, // identity if null
  outputExtent: null, // automatically computed if null
  outputScalarType: null,
  wrap: false, // don't wrap
  mirror: false, // don't mirror
  border: true, // apply a border
  interpolationMode: InterpolationMode.NEAREST, // only NEAREST supported so far
  slabMode: SlabMode.MIN,
  slabTrapezoidIntegration: false,
  slabNumberOfSlices: 1,
  slabSliceSpacingFraction: 1,
  optimization: false, // not supported yet
  scalarShift: 0, // for rescaling the data
  scalarScale: 1,
  backgroundColor: [0, 0, 0, 0],
  resliceAxes: null,
  // resliceTransform: null,
  interpolator: vtkImageInterpolator.newInstance(),
  usePermuteExecute: false, // no supported yet
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  macro.setGet(publicAPI, model, [
    'outputDimensionality',
    'outputScalarType',
    'scalarShift',
    'scalarScale',
    'transformInputSampling',
    'autoCropOutput',
    'wrap',
    'mirror',
    'border',
    'interpolationMode',
    'resliceTransform',
    'slabMode',
    'slabTrapezoidIntegration',
    'slabNumberOfSlices',
    'slabSliceSpacingFraction',
  ]);
 
  macro.setGetArray(publicAPI, model, ['outputOrigin', 'outputSpacing'], 3);
  macro.setGetArray(publicAPI, model, ['outputExtent'], 6);
  macro.setGetArray(publicAPI, model, ['outputDirection'], 9);
  macro.setGetArray(publicAPI, model, ['backgroundColor'], 4);
 
  macro.get(publicAPI, model, ['resliceAxes']);
 
  // Object specific methods
  vtkImageReslice(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageReslice');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...Constants };