All files / Sources/Widgets/Widgets3D/ResliceCursorWidget index.js

81.04% Statements 171/211
66.66% Branches 30/45
51.72% Functions 15/29
81.55% Lines 168/206

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                                                    1x 1x             2x   2x                       72x   72x 72x         72x 72x 216x 216x         72x   72x 24x         24x         24x         48x 24x         24x         24x         24x 24x         24x         24x           72x                   3x 3x   3x 3x 3x 3x       3x         3x           3x 3x   3x                         36x   36x   36x             36x 36x   3x           3x         36x 33x   33x 33x 33x 33x         33x 33x           33x             33x             33x 33x 33x     36x       36x             36x                   36x 36x 3x   36x                                                         2x 3x       3x                                                                         2x 1x 1x 1x     2x 1x 1x         1x             2x           36x             36x 3x                         2x           36x 36x 36x     36x       36x     36x   36x           36x 36x 36x         36x                 2x     72x   72x     72x     72x 72x 72x   72x             72x 72x 72x   72x     2x     36x     36x   36x   36x 36x 36x 36x   36x 36x 36x 36x   36x   36x 108x 108x 108x 108x     36x     2x   36x   36x 36x   36x 36x 36x 36x   36x 36x 36x     36x         36x         36x 36x         36x           36x   36x                 36x 36x 288x           36x   36x                 36x 36x 285x       36x 36x   36x 36x     36x           36x       36x               2x                     2x 36x           2x               2x                                                     2x                     2x                       2x                                                                 2x                       2x   2x   2x           2x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkAbstractWidgetFactory from 'vtk.js/Sources/Widgets/Core/AbstractWidgetFactory';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
import vtkBox from 'vtk.js/Sources/Common/DataModel/Box';
import vtkPlane from 'vtk.js/Sources/Common/DataModel/Plane';
import vtkPlaneSource from 'vtk.js/Sources/Filters/Sources/PlaneSource';
import vtkPlaneManipulator from 'vtk.js/Sources/Widgets/Manipulators/PlaneManipulator';
import vtkLineHandleRepresentation from 'vtk.js/Sources/Widgets/Representations/LineHandleRepresentation';
import vtkSphereHandleRepresentation from 'vtk.js/Sources/Widgets/Representations/SphereHandleRepresentation';
 
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
import widgetBehavior from 'vtk.js/Sources/Widgets/Widgets3D/ResliceCursorWidget/behavior';
import stateGenerator from 'vtk.js/Sources/Widgets/Widgets3D/ResliceCursorWidget/state';
import {
  boundPlane,
  updateState,
  transformPlane,
} from 'vtk.js/Sources/Widgets/Widgets3D/ResliceCursorWidget/helpers';
import { viewTypeToPlaneName } from 'vtk.js/Sources/Widgets/Widgets3D/ResliceCursorWidget/Constants';
 
import { ViewTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';
 
import { mat4 } from 'gl-matrix';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
 
const VTK_INT_MAX = 2147483647;
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// Factory
// ----------------------------------------------------------------------------
 
function vtkResliceCursorWidget(publicAPI, model) {
  model.classHierarchy.push('vtkResliceCursorWidget');
 
  model.methodsToLink = ['scaleInPixels'];
 
  // --------------------------------------------------------------------------
  // Private methods
  // --------------------------------------------------------------------------
 
  /**
   * Compute the origin of the reslice plane prior to transformations
   * It does not take into account the current view normal. (always axis aligned)
   * @param {*} viewType axial, coronal or sagittal
   */
  function computeReslicePlaneOrigin(viewType) {
    const bounds = model.widgetState.getImage().getBounds();
 
    const center = publicAPI.getWidgetState().getCenter();
    const imageCenter = model.widgetState.getImage().getCenter();
 
    // Offset based on the center of the image and how far from it the
    // reslice cursor is. This allows us to capture the whole image even
    // if we resliced in awkward places.
    const offset = [];
    for (let i = 0; i < 3; i++) {
      offset[i] = -Math.abs(center[i] - imageCenter[i]);
      offset[i] *= 2; // give us room
    }
 
    // Now set the size of the plane based on the location of the cursor so as to
    // at least completely cover the viewed region
    const planeSource = vtkPlaneSource.newInstance();
 
    if (viewType === ViewTypes.XZ_PLANE) {
      planeSource.setOrigin(
        bounds[0] + offset[0],
        center[1],
        bounds[4] + offset[2]
      );
      planeSource.setPoint1(
        bounds[1] - offset[0],
        center[1],
        bounds[4] + offset[2]
      );
      planeSource.setPoint2(
        bounds[0] + offset[0],
        center[1],
        bounds[5] - offset[2]
      );
    } else if (viewType === ViewTypes.XY_PLANE) {
      planeSource.setOrigin(
        bounds[0] + offset[0],
        bounds[2] + offset[1],
        center[2]
      );
      planeSource.setPoint1(
        bounds[1] - offset[0],
        bounds[2] + offset[1],
        center[2]
      );
      planeSource.setPoint2(
        bounds[0] + offset[0],
        bounds[3] - offset[1],
        center[2]
      );
    } else if (viewType === ViewTypes.YZ_PLANE) {
      planeSource.setOrigin(
        center[0],
        bounds[2] + offset[1],
        bounds[4] + offset[2]
      );
      planeSource.setPoint1(
        center[0],
        bounds[3] - offset[1],
        bounds[4] + offset[2]
      );
      planeSource.setPoint2(
        center[0],
        bounds[2] + offset[1],
        bounds[5] - offset[2]
      );
    }
    return planeSource;
  }
 
  /**
   * Compute the offset between display reslice cursor position and
   * display focal point position
   * This will be used to keep the same offset between reslice cursor
   * center and focal point when needed.
   */
  function computeFocalPointOffsetFromResliceCursorCenter(viewType, renderer) {
    const worldFocalPoint = renderer.getActiveCamera().getFocalPoint();
    const worldResliceCenter = model.widgetState.getCenter();
 
    const view = renderer.getRenderWindow().getViews()[0];
    const dims = view.getViewportSize(renderer);
    const aspect = dims[0] / dims[1];
    const displayFocalPoint = renderer.worldToNormalizedDisplay(
      ...worldFocalPoint,
      aspect
    );
    const displayResliceCenter = renderer.worldToNormalizedDisplay(
      ...worldResliceCenter,
      aspect
    );
 
    const newOffset = vtkMath.subtract(
      displayFocalPoint,
      displayResliceCenter,
      [0, 0, 0]
    );
 
    const cameraOffsets = model.widgetState.getCameraOffsets();
    cameraOffsets[viewType] = newOffset;
 
    model.widgetState.setCameraOffsets(cameraOffsets);
  }
 
  function updateCamera(
    renderer,
    normal,
    viewType,
    resetFocalPoint,
    keepCenterFocalDistance
  ) {
    // When the reslice plane is changed, update the camera to look at the
    // normal to the reslice plane.
 
    const focalPoint = renderer.getActiveCamera().getFocalPoint();
 
    const distance = renderer.getActiveCamera().getDistance();
 
    const estimatedCameraPosition = vtkMath.multiplyAccumulate(
      focalPoint,
      normal,
      distance,
      [0, 0, 0]
    );
 
    let newFocalPoint = focalPoint;
    if (resetFocalPoint) {
      // intersect with the plane to get updated focal point
      const intersection = vtkPlane.intersectWithLine(
        focalPoint,
        estimatedCameraPosition,
        model.widgetState.getCenter(), // reslice cursor center
        normal
      );
      newFocalPoint = intersection.x;
    }
 
    // Update the estimated focal point so that it will be at the same
    // distance from the reslice center
    if (keepCenterFocalDistance) {
      const worldResliceCenter = model.widgetState.getCenter();
 
      const view = renderer.getRenderWindow().getViews()[0];
      const dims = view.getViewportSize(renderer);
      const aspect = dims[0] / dims[1];
      const displayResliceCenter = renderer.worldToNormalizedDisplay(
        ...worldResliceCenter,
        aspect
      );
 
      const realOffset = model.widgetState.getCameraOffsets()[viewType];
      const displayFocal = vtkMath.add(
        displayResliceCenter,
        realOffset,
        [0, 0, 0]
      );
 
      const worldFocal = renderer.normalizedDisplayToWorld(
        ...displayFocal,
        aspect
      );
 
      // Reproject focal point on slice in order to keep it on the
      // same plane as the reslice cursor center
      const intersection2 = vtkPlane.intersectWithLine(
        worldFocal,
        estimatedCameraPosition,
        worldResliceCenter,
        normal
      );
 
      newFocalPoint[0] = intersection2.x[0];
      newFocalPoint[1] = intersection2.x[1];
      newFocalPoint[2] = intersection2.x[2];
    }
 
    renderer
      .getActiveCamera()
      .setFocalPoint(newFocalPoint[0], newFocalPoint[1], newFocalPoint[2]);
 
    const newCameraPosition = vtkMath.multiplyAccumulate(
      newFocalPoint,
      normal,
      distance,
      [0, 0, 0]
    );
 
    renderer
      .getActiveCamera()
      .setPosition(
        newCameraPosition[0],
        newCameraPosition[1],
        newCameraPosition[2]
      );
 
    // Don't clip away any part of the data.
    // Renderer may not have yet actor bounds
    const bounds = model.widgetState.getImage().getBounds();
    if (resetFocalPoint) {
      renderer.resetCamera(bounds);
    }
    renderer.resetCameraClippingRange(bounds);
  }
 
  /**
   * Convenient function to return the widget for a given viewType
   * @param {string} viewType
   * @returns the widget instanced in the given viewType.
   */
  function findWidgetForViewType(viewType) {
    return publicAPI
      .getViewIds()
      .map((viewId) => publicAPI.getWidgetForView({ viewId }))
      .find((widget) => widget.getViewType() === viewType);
  }
  /**
   * Convenient function to return the ResliceCursorRepresentation for a given viewType
   * @param {string} viewType
   * @returns an array of 3 representations (for line handles, rotation handles, center handle)
   * or an empty array if the widget has not yet been added to the view type.
   */
  function findRepresentationsForViewType(viewType) {
    const widgetForViewType = findWidgetForViewType(viewType);
    return widgetForViewType ? widgetForViewType.getRepresentations() : [];
  }
 
  // --------------------------------------------------------------------------
  // initialization
  // --------------------------------------------------------------------------
 
  publicAPI.getRepresentationsForViewType = (viewType) => {
    switch (viewType) {
      case ViewTypes.XY_PLANE:
      case ViewTypes.XZ_PLANE:
      case ViewTypes.YZ_PLANE:
        return [
          {
            builder: vtkLineHandleRepresentation,
            labels: [`lineIn${viewTypeToPlaneName[viewType]}`],
            initialValues: {
              useActiveColor: false,
              scaleInPixels: model.scaleInPixels,
            },
          },
          {
            builder: vtkSphereHandleRepresentation,
            labels: [`rotationIn${viewTypeToPlaneName[viewType]}`],
            initialValues: {
              useActiveColor: false,
              scaleInPixels: model.scaleInPixels,
              lighting: false,
            },
          },
          {
            builder: vtkSphereHandleRepresentation,
            labels: ['center'],
            initialValues: {
              useActiveColor: false,
              scaleInPixels: model.scaleInPixels,
              lighting: false,
            },
          },
        ];
      case ViewTypes.DEFAULT:
      case ViewTypes.GEOMETRY:
      case ViewTypes.SLICE:
      case ViewTypes.VOLUME:
      default:
        return [];
    }
  };
 
  publicAPI.setImage = (image) => {
    model.widgetState.setImage(image);
    const center = image.getCenter();
    publicAPI.setCenter(center);
  };
 
  publicAPI.setCenter = (center) => {
    model.widgetState.setCenter(center);
    updateState(
      model.widgetState,
      model.scaleInPixels,
      model.rotationHandlePosition
    );
    publicAPI.modified();
  };
 
  // --------------------------------------------------------------------------
  // Methods
  // --------------------------------------------------------------------------
 
  publicAPI.updateCameraPoints = (
    renderer,
    viewType,
    resetFocalPoint,
    computeFocalPointOffset
  ) => {
    publicAPI.resetCamera(
      renderer,
      viewType,
      resetFocalPoint,
      !computeFocalPointOffset
    );
 
    if (computeFocalPointOffset) {
      computeFocalPointOffsetFromResliceCursorCenter(viewType, renderer);
    }
  };
 
  /**
   *
   * @param {*} renderer
   * @param {*} viewType
   * @param {*} resetFocalPoint Defines if the focal point is reset to the image center
   * @param {*} keepCenterFocalDistance Defines if the estimated focal point has to be updated
   * in order to keep the same distance to the center (according to the computed focal point
   * shift)
   */
  publicAPI.resetCamera = (
    renderer,
    viewType,
    resetFocalPoint,
    keepCenterFocalDistance
  ) => {
    const center = model.widgetState.getImage().getCenter();
    const focalPoint = renderer.getActiveCamera().getFocalPoint();
    const position = renderer.getActiveCamera().getPosition();
 
    // Distance is preserved
    const distance = Math.sqrt(
      vtkMath.distance2BetweenPoints(position, focalPoint)
    );
 
    const normal = publicAPI.getPlaneNormalFromViewType(viewType);
 
    // ResetFocalPoint will reset focal point to the center of the image
    const estimatedFocalPoint = resetFocalPoint ? center : focalPoint;
 
    const estimatedCameraPosition = vtkMath.multiplyAccumulate(
      estimatedFocalPoint,
      normal,
      distance,
      [0, 0, 0]
    );
    renderer.getActiveCamera().setFocalPoint(...estimatedFocalPoint);
    renderer.getActiveCamera().setPosition(...estimatedCameraPosition);
    renderer
      .getActiveCamera()
      .setViewUp(model.widgetState.getPlanes()[viewType].viewUp);
 
    // Project focalPoint onto image plane and preserve distance
    updateCamera(
      renderer,
      normal,
      viewType,
      resetFocalPoint,
      keepCenterFocalDistance
    );
  };
 
  publicAPI.getPlaneSource = (viewType) => {
    // Compute original (i.e. before rotation) plane (i.e. origin, p1, p2)
    // centered on cursor center.
    const planeSource = computeReslicePlaneOrigin(viewType);
 
    const { normal, viewUp } = model.widgetState.getPlanes()[viewType];
    // Adapt plane orientation in order to fit the correct viewUp
    // so that the rotations will be more understandable than now.
    transformPlane(planeSource, model.widgetState.getCenter(), normal, viewUp);
 
    // Clip to bounds
    const boundedOrigin = [...planeSource.getOrigin()];
    const boundedP1 = [...planeSource.getPoint1()];
    const boundedP2 = [...planeSource.getPoint2()];
 
    boundPlane(
      model.widgetState.getImage().getBounds(),
      boundedOrigin,
      boundedP1,
      boundedP2
    );
 
    planeSource.setOrigin(...boundedOrigin);
    planeSource.setPoint1(...boundedP1);
    planeSource.setPoint2(...boundedP2);
 
    return planeSource;
  };
 
  publicAPI.getResliceAxes = (viewType) => {
    // Compute original (i.e. before rotation) plane (i.e. origin, p1, p2)
    // centered on cursor center.
    const planeSource = publicAPI.getPlaneSource(viewType);
 
    // TBD: use normal from planeSource ?
    const { normal } = model.widgetState.getPlanes()[viewType];
 
    const planeOrigin = planeSource.getOrigin();
 
    const p1 = planeSource.getPoint1();
    const planeAxis1 = [];
    vtkMath.subtract(p1, planeOrigin, planeAxis1);
    vtkMath.normalize(planeAxis1);
 
    const p2 = planeSource.getPoint2();
    const planeAxis2 = [];
    vtkMath.subtract(p2, planeOrigin, planeAxis2);
    vtkMath.normalize(planeAxis2);
 
    const newResliceAxes = mat4.identity(new Float64Array(16));
 
    for (let i = 0; i < 3; i++) {
      newResliceAxes[i] = planeAxis1[i];
      newResliceAxes[4 + i] = planeAxis2[i];
      newResliceAxes[8 + i] = normal[i];
      newResliceAxes[12 + i] = planeOrigin[i];
    }
 
    return newResliceAxes;
  };
 
  publicAPI.updateReslicePlane = (imageReslice, viewType) => {
    // Calculate appropriate pixel spacing for the reslicing
    const spacing = model.widgetState.getImage().getSpacing();
 
    const planeSource = publicAPI.getPlaneSource(viewType);
    const newResliceAxes = publicAPI.getResliceAxes(viewType);
 
    const planeOrigin = planeSource.getOrigin();
    const p1 = planeSource.getPoint1();
    const planeAxis1 = vtkMath.subtract(p1, planeOrigin, []);
    const planeSizeX = vtkMath.normalize(planeAxis1);
 
    const p2 = planeSource.getPoint2();
    const planeAxis2 = vtkMath.subtract(p2, planeOrigin, []);
    const planeSizeY = vtkMath.normalize(planeAxis2);
 
    const spacingX =
      Math.abs(planeAxis1[0] * spacing[0]) +
      Math.abs(planeAxis1[1] * spacing[1]) +
      Math.abs(planeAxis1[2] * spacing[2]);
 
    const spacingY =
      Math.abs(planeAxis2[0] * spacing[0]) +
      Math.abs(planeAxis2[1] * spacing[1]) +
      Math.abs(planeAxis2[2] * spacing[2]);
 
    // Compute a new set of resliced extents
    let extentX = 0;
    let extentY = 0;
 
    // Pad extent up to a power of two for efficient texture mapping
    // make sure we're working with valid values
    const realExtentX =
      spacingX === 0 ? Number.MAX_SAFE_INTEGER : planeSizeX / spacingX;
 
    // Sanity check the input data:
    // * if realExtentX is too large, extentX will wrap
    // * if spacingX is 0, things will blow up.
 
    const value = VTK_INT_MAX >> 1; // eslint-disable-line no-bitwise
 
    Iif (realExtentX > value) {
      vtkErrorMacro(
        'Invalid X extent: ',
        realExtentX,
        ' on view type : ',
        viewType
      );
      extentX = 0;
    } else {
      extentX = 1;
      while (extentX < realExtentX) {
        extentX <<= 1; // eslint-disable-line no-bitwise
      }
    }
 
    // make sure extentY doesn't wrap during padding
    const realExtentY =
      spacingY === 0 ? Number.MAX_SAFE_INTEGER : planeSizeY / spacingY;
 
    Iif (realExtentY > value) {
      vtkErrorMacro(
        'Invalid Y extent:',
        realExtentY,
        ' on view type : ',
        viewType
      );
      extentY = 0;
    } else {
      extentY = 1;
      while (extentY < realExtentY) {
        extentY <<= 1; // eslint-disable-line no-bitwise
      }
    }
 
    const outputSpacingX = extentX === 0 ? 1.0 : planeSizeX / extentX;
    const outputSpacingY = extentY === 0 ? 1.0 : planeSizeY / extentY;
 
    let modified = imageReslice.setResliceAxes(newResliceAxes);
    modified =
      imageReslice.setOutputSpacing([outputSpacingX, outputSpacingY, 1]) ||
      modified;
    modified =
      imageReslice.setOutputOrigin([
        0.5 * outputSpacingX,
        0.5 * outputSpacingY,
        0,
      ]) || modified;
    modified =
      imageReslice.setOutputExtent([0, extentX - 1, 0, extentY - 1, 0, 0]) ||
      modified;
 
    return modified;
  };
 
  /**
   * Returns a plane source with origin at cursor center and
   * normal from the view.
   * @param {ViewType} type: Axial, Coronal or Sagittal
   */
  publicAPI.getPlaneSourceFromViewType = (type) => {
    const planeSource = vtkPlaneSource.newInstance();
    const origin = publicAPI.getWidgetState().getCenter();
    const planeNormal = publicAPI.getPlaneNormalFromViewType(type);
 
    planeSource.setNormal(planeNormal);
    planeSource.setOrigin(origin);
 
    return planeSource;
  };
 
  publicAPI.getPlaneNormalFromViewType = (viewType) =>
    publicAPI.getWidgetState().getPlanes()[viewType].normal;
 
  /**
   * Returns the normals of the planes that are not viewType.
   * @param {ViewType} viewType ViewType to extract other normals
   */
  publicAPI.getOtherPlaneNormals = (viewType) =>
    [ViewTypes.YZ_PLANE, ViewTypes.XZ_PLANE, ViewTypes.XY_PLANE]
      .filter((vt) => vt !== viewType)
      .map((vt) => publicAPI.getPlaneNormalFromViewType(vt));
 
  /**
   * Return the reslice cursor matrix built as such: [YZ, XZ, XY, center]
   */
  publicAPI.getResliceMatrix = () => {
    const resliceMatrix = mat4.identity(new Float64Array(16));
 
    for (let i = 0; i < 3; i++) {
      resliceMatrix[4 * i + 0] = publicAPI.getPlaneNormalFromViewType(
        ViewTypes.YZ_PLANE
      )[i];
      resliceMatrix[4 * i + 1] = publicAPI.getPlaneNormalFromViewType(
        ViewTypes.XZ_PLANE
      )[i];
      resliceMatrix[4 * i + 2] = publicAPI.getPlaneNormalFromViewType(
        ViewTypes.XY_PLANE
      )[i];
    }
 
    const origin = publicAPI.getWidgetState().getCenter();
 
    const m = vtkMatrixBuilder
      .buildFromRadian()
      .translate(...origin)
      .multiply(resliceMatrix)
      .translate(...vtkMath.multiplyScalar([...origin], -1))
      .getMatrix();
 
    return m;
  };
 
  publicAPI.getDisplayScaleParams = () =>
    [ViewTypes.YZ_PLANE, ViewTypes.XZ_PLANE, ViewTypes.XY_PLANE].reduce(
      (res, viewType) => {
        res[viewType] =
          findRepresentationsForViewType(
            viewType
          )[0]?.getDisplayScaleParams?.();
        return res;
      },
      {}
    );
  publicAPI.setScaleInPixels = macro.chain(
    publicAPI.setScaleInPixels,
    (scale) => {
      publicAPI.getViewWidgets().forEach((w) => w.setScaleInPixels(scale));
      updateState(
        model.widgetState,
        model.scaleInPixels,
        model.rotationHandlePosition
      );
    }
  );
 
  publicAPI.getPlaneExtremities = (viewType) => {
    const dirProj = publicAPI.getWidgetState().getPlanes()[viewType].normal;
    const length = vtkBoundingBox.getDiagonalLength(
      publicAPI.getWidgetState().getImage().getBounds()
    );
    const p1 = vtkMath.multiplyAccumulate(
      publicAPI.getWidgetState().getCenter(),
      dirProj,
      -length,
      []
    );
    const p2 = vtkMath.multiplyAccumulate(
      publicAPI.getWidgetState().getCenter(),
      dirProj,
      length,
      []
    );
    // FIXME: support oriented bounds
    const intersectionPoints = vtkBox.intersectWithLine(
      publicAPI.getWidgetState().getImage().getBounds(),
      p1,
      p2
    );
    return [intersectionPoints.x1, intersectionPoints.x2];
  };
}
 
// ----------------------------------------------------------------------------
/**
 * Initializes the model.
 * @param {*} initialValues optional object of member variables. initialValues.planes is an optional list of axis names (e.g. ['X', 'Y'])
 * @returns the initial model object
 */
const defaultValues = (initialValues) => ({
  behavior: widgetBehavior,
  widgetState: stateGenerator(initialValues.planes),
  rotationHandlePosition: 0.5,
  scaleInPixels: true,
  manipulator: vtkPlaneManipulator.newInstance(),
  ...initialValues,
});
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, defaultValues(initialValues));
 
  vtkAbstractWidgetFactory.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, [
    'scaleInPixels',
    'rotationHandlePosition',
    'manipulator',
  ]);
 
  vtkResliceCursorWidget(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkResliceCursorWidget');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };