All files / Sources/Rendering/Core/Mapper index.js

86.21% Statements 294/341
76.15% Branches 115/151
70.58% Functions 24/34
88.3% Lines 287/325

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                              1x   1x   1x 1x 1x         3945x                       525334x 525334x 525334x 132x 132x 132x 132x                           525334x                                         561x 561x     561x 391x 391x 391x   170x 170x 170x     561x 561x     561x 561x 561x 561x             561x     561x 561x 12x 3x   9x     549x       561x 561x 561x           1x                                               134x 134x   938x 938x   134x   134x 134x             134x 134x           134x   134x     134x   134x   134x 134x 134x 134x 134x 134x       134x   134x     134x             134x 134x 134x 134x     1391x               1391x   1391x     1391x         1391x   52x 52x 52x 1339x     427x 427x   18x 409x   18x       427x                         912x                 912x 912x   912x     912x     1391x 2782x       134x       134x                 789x   789x 733x 733x     733x 733x   733x   733x     789x         789x 821x           789x 30x     789x 1x   789x 789x 789x   789x 1x   789x 789x 789x 789x 789x 789x   789x               8629x 3x     8626x 8626x     8626x 8236x 8236x 7544x 7544x   390x   390x     390x 384x 384x     384x   6x 6x 6x 6x     6x                     8626x     789x 899x             899x   899x 752x 752x 752x 752x       147x 147x   147x 141x               147x 134x   13x 13x   13x 13x   13x 13x             147x       789x 134x 134x 134x         134x         134x       134x           134x 134x         134x   134x     134x 134x 134x 134x   134x   134x       134x       134x 134x 134x                                 134x   134x     134x       134x 134x 134x 134x 134x 134x 525334x             525334x   525334x         525334x     134x       134x           134x 134x 134x   134x           134x                   134x                     789x 7645x 7645x             7645x 7645x   6884x   761x 761x   761x 761x         789x 147x 4x     143x 12x       131x 1x     130x         130x                   130x     789x           789x 1086x 30x   1086x     789x 7395x 7395x 1270x 1270x   7395x     789x                                 789x     789x 789x 789x 789x   789x   367x               367x 367x 367x 367x   367x 367x 342x 25x 25x     367x       367x 63643x 21214x 21214x 1x 1x   21214x 21214x 21214x 21214x 21214x   21214x 21214x 21214x 21214x 21214x 42429x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x                     1x                                                                               789x     789x   789x               789x                             789x   789x     789x         1x                      
import macro from 'vtk.js/Sources/macros';
import vtkAbstractMapper3D from 'vtk.js/Sources/Rendering/Core/AbstractMapper3D';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData';
import vtkLookupTable from 'vtk.js/Sources/Common/Core/LookupTable';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkScalarsToColors from 'vtk.js/Sources/Common/Core/ScalarsToColors/Constants'; // Need to go inside Constants otherwise dependency loop
 
import CoincidentTopologyHelper from 'vtk.js/Sources/Rendering/Core/Mapper/CoincidentTopologyHelper';
import Constants from 'vtk.js/Sources/Rendering/Core/Mapper/Constants';
 
import vtkDataSet from 'vtk.js/Sources/Common/DataModel/DataSet';
 
import { PassTypes } from 'vtk.js/Sources/Rendering/OpenGL/HardwareSelector/Constants';
 
const { FieldAssociations } = vtkDataSet;
 
const { staticOffsetAPI, otherStaticMethods } = CoincidentTopologyHelper;
 
const { ColorMode, ScalarMode, GetArray } = Constants;
const { VectorMode } = vtkScalarsToColors;
const { VtkDataTypes } = vtkDataArray;
 
// ----------------------------------------------------------------------------
 
function notImplemented(method) {
  return () => macro.vtkErrorMacro(`vtkMapper::${method} - NOT IMPLEMENTED`);
}
 
/**
 * Increase by one the 3D coordinates
 * It will follow a zigzag pattern so that each coordinate is the neighbor of the next coordinate
 * This enables interpolation between two texels without issues
 * Note: texture coordinates can't be interpolated using this pattern
 * @param {vec3} coordinates The 3D coordinates using integers for each coorinate
 * @param {vec3} dimensions The 3D dimensions of the volume
 */
function updateZigzaggingCoordinates(coordinates, dimensions) {
  const directionX = coordinates[1] % 2 === 0 ? 1 : -1;
  coordinates[0] += directionX;
  if (coordinates[0] >= dimensions[0] || coordinates[0] < 0) {
    const directionY = coordinates[2] % 2 === 0 ? 1 : -1;
    coordinates[0] -= directionX;
    coordinates[1] += directionY;
    Iif (coordinates[1] >= dimensions[1] || coordinates[1] < 0) {
      coordinates[1] -= directionY;
      coordinates[2]++;
    }
  }
}
 
/**
 * Returns the index in the array representing the volume from a 3D coordinate
 * @param {vec3} coordinates The 3D integer coordinates
 * @param {vec3} dimensions The 3D dimensions of the volume
 * @returns The index in a flat array representing the volume
 */
function getIndexFromCoordinates(coordinates, dimensions) {
  return (
    coordinates[0] +
    dimensions[0] * (coordinates[1] + dimensions[1] * coordinates[2])
  );
}
 
/**
 * Write texture coordinates for the given `texelIndexPosition` in `textureCoordinate`.
 * The `texelIndexPosition` is a floating point number that represents the distance in index space
 * from the center of the first texel to the final output position.
 * The output is given in texture coordinates and not in index coordinates (this is done at the very end of the function)
 * @param {vec3} textureCoordinate The output texture coordinates (to avoid allocating a new Array)
 * @param {Number} texelIndexPosition The floating point distance from the center of the first texel, following a zigzag pattern
 * @param {vec3} dimensions The 3D dimensions of the volume
 */
function getZigZagTextureCoordinatesFromTexelPosition(
  textureCoordinate,
  texelIndexPosition,
  dimensions
) {
  // First compute the integer textureCoordinate
  const intTexelIndex = Math.floor(texelIndexPosition);
  const xCoordBeforeWrap = intTexelIndex % (2 * dimensions[0]);
  let xDirection;
  let xEndFlag;
  if (xCoordBeforeWrap < dimensions[0]) {
    textureCoordinate[0] = xCoordBeforeWrap;
    xDirection = 1;
    xEndFlag = textureCoordinate[0] === dimensions[0] - 1;
  } else {
    textureCoordinate[0] = 2 * dimensions[0] - 1 - xCoordBeforeWrap;
    xDirection = -1;
    xEndFlag = textureCoordinate[0] === 0;
  }
 
  const intRowIndex = Math.floor(intTexelIndex / dimensions[0]);
  const yCoordBeforeWrap = intRowIndex % (2 * dimensions[1]);
  let yDirection;
  let yEndFlag;
  if (yCoordBeforeWrap < dimensions[1]) {
    textureCoordinate[1] = yCoordBeforeWrap;
    yDirection = 1;
    yEndFlag = textureCoordinate[1] === dimensions[1] - 1;
  } else E{
    textureCoordinate[1] = 2 * dimensions[1] - 1 - yCoordBeforeWrap;
    yDirection = -1;
    yEndFlag = textureCoordinate[1] === 0;
  }
 
  textureCoordinate[2] = Math.floor(intRowIndex / dimensions[1]);
 
  // Now add the remainder either in x, y or z
  const remainder = texelIndexPosition - intTexelIndex;
  if (xEndFlag) {
    if (yEndFlag) {
      textureCoordinate[2] += remainder;
    } else {
      textureCoordinate[1] += yDirection * remainder;
    }
  } else {
    textureCoordinate[0] += xDirection * remainder;
  }
 
  // textureCoordinates are in index space, convert to texture space
  textureCoordinate[0] = (textureCoordinate[0] + 0.5) / dimensions[0];
  textureCoordinate[1] = (textureCoordinate[1] + 0.5) / dimensions[1];
  textureCoordinate[2] = (textureCoordinate[2] + 0.5) / dimensions[2];
}
 
// Associate an input vtkDataArray to an object { stringHash, textureCoordinates }
// A single dataArray only caches one array of texture coordinates, so this cache is useless when
// the input data array is used with two different lookup tables (which is very unlikely)
const colorTextureCoordinatesCache = new WeakMap();
/**
 * The minimum of the range is mapped to the center of the first texel excluding min texel (texel at index distance 1)
 * The maximum of the range is mapped to the center of the last texel excluding max and NaN texels (texel at index distance numberOfColorsInRange)
 * The result is cached, and is reused if the arguments are the same and the input doesn't change
 * @param {vtkDataArray} input The input data array used for coloring
 * @param {Number} component The component of the input data array that is used for coloring (-1 for magnitude of the vectors)
 * @param {Range} range The range of the scalars
 * @param {Number} numberOfColorsInRange The number of colors that are used in the range
 * @param {vec3} dimensions The dimensions of the texture
 * @param {boolean} useLogScale If log scale should be used to transform input scalars
 * @param {boolean} useZigzagPattern If a zigzag pattern should be used. Otherwise 1 row for colors (including min and max) and 1 row for NaN are used.
 * @returns A vtkDataArray containing the texture coordinates (2D or 3D)
 */
function getOrCreateColorTextureCoordinates(
  input,
  component,
  range,
  numberOfColorsInRange,
  dimensions,
  useLogScale,
  useZigzagPattern
) {
  // Caching using the "arguments" special object (because it is a pure function)
  const argStrings = new Array(arguments.length);
  for (let argIndex = 0; argIndex < arguments.length; ++argIndex) {
    // eslint-disable-next-line prefer-rest-params
    const arg = arguments[argIndex];
    argStrings[argIndex] = arg.getMTime?.() ?? arg;
  }
  const stringHash = argStrings.join('/');
 
  const cachedResult = colorTextureCoordinatesCache.get(input);
  Iif (cachedResult && cachedResult.stringHash === stringHash) {
    return cachedResult.textureCoordinates;
  }
 
  // The range used for computing coordinates have to change
  // slightly to accommodate the special above- and below-range
  // colors that are the first and last texels, respectively.
  const scalarTexelWidth = (range[1] - range[0]) / (numberOfColorsInRange - 1);
  const [paddedRangeMin, paddedRangeMax] = [
    range[0] - scalarTexelWidth,
    range[1] + scalarTexelWidth,
  ];
 
  // Use the center of the voxel
  const textureSOrigin = paddedRangeMin - 0.5 * scalarTexelWidth;
  const textureSCoeff =
    1.0 / (paddedRangeMax - paddedRangeMin + scalarTexelWidth);
 
  // Compute in index space first
  const texelIndexOrigin = paddedRangeMin;
  const texelIndexCoeff =
    (numberOfColorsInRange + 1) / (paddedRangeMax - paddedRangeMin);
 
  const inputV = input.getData();
  const numScalars = input.getNumberOfTuples();
  const numComps = input.getNumberOfComponents();
  const useMagnitude = component < 0 || component >= numComps;
  const numberOfOutputComponents = dimensions[2] <= 1 ? 2 : 3;
  const output = vtkDataArray.newInstance({
    numberOfComponents: numberOfOutputComponents,
    values: new Float32Array(numScalars * numberOfOutputComponents),
  });
  const outputV = output.getData();
 
  const nanTextureCoordinate = [0, 0, 0];
  // Distance of NaN from the beginning:
  // min: 0, ...colorsInRange, max: numberOfColorsInRange + 1, NaN = numberOfColorsInRange + 2
  getZigZagTextureCoordinatesFromTexelPosition(
    nanTextureCoordinate,
    numberOfColorsInRange + 2,
    dimensions
  );
 
  // Set a texture coordinate in the output for each tuple in the input
  let inputIdx = 0;
  let outputIdx = 0;
  const textureCoordinate = [0.5, 0.5, 0.5];
  for (let scalarIdx = 0; scalarIdx < numScalars; ++scalarIdx) {
    // Get scalar value from magnitude or a single component
    let scalarValue;
    Iif (useMagnitude) {
      let sum = 0;
      for (let compIdx = 0; compIdx < numComps; ++compIdx) {
        const compValue = inputV[inputIdx + compIdx];
        sum += compValue * compValue;
      }
      scalarValue = Math.sqrt(sum);
    } else {
      scalarValue = inputV[inputIdx + component];
    }
    inputIdx += numComps;
 
    // Apply log scale if necessary
    Iif (useLogScale) {
      scalarValue = vtkLookupTable.applyLogScale(scalarValue, range, range);
    }
 
    // Convert to texture coordinates and update output
    if (vtkMath.isNan(scalarValue)) {
      // Last texels are NaN colors (there is at least one NaN color)
      textureCoordinate[0] = nanTextureCoordinate[0];
      textureCoordinate[1] = nanTextureCoordinate[1];
      textureCoordinate[2] = nanTextureCoordinate[2];
    } else if (useZigzagPattern) {
      // Texel position is in [0, numberOfColorsInRange + 1]
      let texelIndexPosition =
        (scalarValue - texelIndexOrigin) * texelIndexCoeff;
      if (texelIndexPosition < 1) {
        // Use min color when smaller than range
        texelIndexPosition = 0;
      } else if (texelIndexPosition > numberOfColorsInRange) {
        // Use max color when greater than range
        texelIndexPosition = numberOfColorsInRange + 1;
      }
 
      // Convert the texel position into texture coordinate following a zigzag pattern
      getZigZagTextureCoordinatesFromTexelPosition(
        textureCoordinate,
        texelIndexPosition,
        dimensions
      );
    } else {
      // 0.0 in t coordinate means not NaN.  So why am I setting it to 0.49?
      // Because when you are mapping scalars and you have a NaN adjacent to
      // anything else, the interpolation everywhere should be NaN.  Thus, I
      // want the NaN color everywhere except right on the non-NaN neighbors.
      // To simulate this, I set the t coord for the real numbers close to
      // the threshold so that the interpolation almost immediately looks up
      // the NaN value.
      textureCoordinate[1] = 0.49;
 
      // Some implementations apparently don't handle relatively large
      // numbers (compared to the range [0.0, 1.0]) very well. In fact,
      // values above 1122.0f appear to cause texture wrap-around on
      // some systems even when edge clamping is enabled. Why 1122.0f? I
      // don't know. For safety, we'll clamp at +/- 1000. This will
      // result in incorrect images when the texture value should be
      // above or below 1000, but I don't have a better solution.
      const textureS = (scalarValue - textureSOrigin) * textureSCoeff;
      Iif (textureS > 1000.0) {
        textureCoordinate[0] = 1000.0;
      } else Iif (textureS < -1000.0) {
        textureCoordinate[0] = -1000.0;
      } else {
        textureCoordinate[0] = textureS;
      }
    }
    for (let i = 0; i < numberOfOutputComponents; ++i) {
      outputV[outputIdx++] = textureCoordinate[i];
    }
  }
 
  colorTextureCoordinatesCache.set(input, {
    stringHash,
    textureCoordinates: output,
  });
  return output;
}
 
// ----------------------------------------------------------------------------
// vtkMapper methods
// ----------------------------------------------------------------------------
 
function vtkMapper(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMapper');
 
  publicAPI.getBounds = () => {
    const input = publicAPI.getInputData();
    Iif (!input) {
      model.bounds = vtkMath.createUninitializedBounds();
    } else {
      if (!model.static) {
        publicAPI.update();
      }
      model.bounds = input.getBounds();
    }
    return model.bounds;
  };
 
  publicAPI.setForceCompileOnly = (v) => {
    model.forceCompileOnly = v;
    // make sure we do NOT call modified()
  };
 
  publicAPI.setSelectionWebGLIdsToVTKIds = (selectionWebGLIdsToVTKIds) => {
    model.selectionWebGLIdsToVTKIds = selectionWebGLIdsToVTKIds;
    // make sure we do NOT call modified()
    // this attribute is only used when processing a selection made with the hardware selector
    // the mtime of the mapper doesn't need to be changed
  };
 
  publicAPI.createDefaultLookupTable = () => {
    model.lookupTable = vtkLookupTable.newInstance();
  };
 
  publicAPI.getColorModeAsString = () =>
    macro.enumToString(ColorMode, model.colorMode);
 
  publicAPI.setColorModeToDefault = () => publicAPI.setColorMode(0);
  publicAPI.setColorModeToMapScalars = () => publicAPI.setColorMode(1);
  publicAPI.setColorModeToDirectScalars = () => publicAPI.setColorMode(2);
 
  publicAPI.getScalarModeAsString = () =>
    macro.enumToString(ScalarMode, model.scalarMode);
 
  publicAPI.setScalarModeToDefault = () => publicAPI.setScalarMode(0);
  publicAPI.setScalarModeToUsePointData = () => publicAPI.setScalarMode(1);
  publicAPI.setScalarModeToUseCellData = () => publicAPI.setScalarMode(2);
  publicAPI.setScalarModeToUsePointFieldData = () => publicAPI.setScalarMode(3);
  publicAPI.setScalarModeToUseCellFieldData = () => publicAPI.setScalarMode(4);
  publicAPI.setScalarModeToUseFieldData = () => publicAPI.setScalarMode(5);
 
  publicAPI.getAbstractScalars = (
    input,
    scalarMode,
    arrayAccessMode,
    arrayId,
    arrayName
  ) => {
    // make sure we have an input
    if (!input || !model.scalarVisibility) {
      return { scalars: null, cellFlag: false };
    }
 
    let scalars = null;
    let cellFlag = false;
 
    // get and scalar data according to scalar mode
    if (scalarMode === ScalarMode.DEFAULT) {
      scalars = input.getPointData().getScalars();
      if (!scalars) {
        scalars = input.getCellData().getScalars();
        cellFlag = true;
      }
    } else Iif (scalarMode === ScalarMode.USE_POINT_DATA) {
      scalars = input.getPointData().getScalars();
    } else Iif (scalarMode === ScalarMode.USE_CELL_DATA) {
      scalars = input.getCellData().getScalars();
      cellFlag = true;
    } else if (scalarMode === ScalarMode.USE_POINT_FIELD_DATA) {
      const pd = input.getPointData();
      Iif (arrayAccessMode === GetArray.BY_ID) {
        scalars = pd.getArrayByIndex(arrayId);
      } else {
        scalars = pd.getArrayByName(arrayName);
      }
    } else if (scalarMode === ScalarMode.USE_CELL_FIELD_DATA) {
      const cd = input.getCellData();
      cellFlag = true;
      Iif (arrayAccessMode === GetArray.BY_ID) {
        scalars = cd.getArrayByIndex(arrayId);
      } else {
        scalars = cd.getArrayByName(arrayName);
      }
    } else Eif (scalarMode === ScalarMode.USE_FIELD_DATA) {
      const fd = input.getFieldData();
      if (arrayAccessMode === GetArray.BY_ID) {
        scalars = fd.getArrayByIndex(arrayId);
      } else {
        scalars = fd.getArrayByName(arrayName);
      }
    }
 
    return { scalars, cellFlag };
  };
 
  publicAPI.mapScalars = (input, alpha) => {
    const { scalars, cellFlag } = publicAPI.getAbstractScalars(
      input,
      model.scalarMode,
      model.arrayAccessMode,
      model.arrayId,
      model.colorByArrayName
    );
    model.areScalarsMappedFromCells = cellFlag;
 
    if (!scalars) {
      model.colorCoordinates = null;
      model.colorTextureMap = null;
      model.colorMapColors = null;
      return;
    }
 
    // we want to only recompute when something has changed
    const toString = `${publicAPI.getMTime()}${scalars.getMTime()}${alpha}`;
    Iif (model.colorBuildString === toString) return;
 
    if (!model.useLookupTableScalarRange) {
      publicAPI
        .getLookupTable()
        .setRange(model.scalarRange[0], model.scalarRange[1]);
    }
 
    // Decide between texture color or vertex color.
    // Cell data always uses vertex color.
    // Only point data can use both texture and vertex coloring.
    if (publicAPI.canUseTextureMapForColoring(scalars, cellFlag)) {
      model.mapScalarsToTexture(scalars, cellFlag, alpha);
    } else {
      model.colorCoordinates = null;
      model.colorTextureMap = null;
 
      const lut = publicAPI.getLookupTable();
      if (lut) {
        // Ensure that the lookup table is built
        lut.build();
        model.colorMapColors = lut.mapScalars(
          scalars,
          model.colorMode,
          model.fieldDataTupleId
        );
      }
    }
    model.colorBuildString = `${publicAPI.getMTime()}${scalars.getMTime()}${alpha}`;
  };
 
  // Protected method
  model.mapScalarsToTexture = (scalars, cellFlag, alpha) => {
    const range = model.lookupTable.getRange();
    const useLogScale = model.lookupTable.usingLogScale();
    Iif (useLogScale) {
      // convert range to log.
      vtkLookupTable.getLogRange(range, range);
    }
 
    const origAlpha = model.lookupTable.getAlpha();
 
    // Get rid of vertex color array.  Only texture or vertex coloring
    // can be active at one time.  The existence of the array is the
    // signal to use that technique.
    model.colorMapColors = null;
 
    // If the lookup table has changed, then recreate the color texture map.
    // Set a new lookup table changes this->MTime.
    if (
      model.colorTextureMap == null ||
      publicAPI.getMTime() > model.colorTextureMap.getMTime() ||
      model.lookupTable.getMTime() > model.colorTextureMap.getMTime() ||
      model.lookupTable.getAlpha() !== alpha
    ) {
      model.lookupTable.setAlpha(alpha);
      model.colorTextureMap = null;
 
      // Get the texture map from the lookup table.
      // Create a dummy ramp of scalars.
      // In the future, we could extend vtkScalarsToColors.
      model.lookupTable.build();
      const numberOfAvailableColors =
        model.lookupTable.getNumberOfAvailableColors();
 
      // Maximum dimensions and number of colors in range
      const maxTextureWidthForCells = 2048;
      const maxColorsInRangeForCells = maxTextureWidthForCells ** 3 - 3; // 3D but keep a color for min, max and NaN
      const maxTextureWidthForPoints = 4096;
      const maxColorsInRangeForPoints = maxTextureWidthForPoints - 2; // 1D but keep a color for min and max (NaN is in a different row)
      // Minimum number of colors in range (excluding special colors like minColor, maxColor and NaNColor)
      const minColorsInRange = 2;
      // Maximum number of colors, limited by the maximum possible texture size
      const maxColorsInRange = cellFlag
        ? maxColorsInRangeForCells
        : maxColorsInRangeForPoints;
 
      model.numberOfColorsInRange = Math.min(
        Math.max(numberOfAvailableColors, minColorsInRange),
        maxColorsInRange
      );
      const numberOfColorsForCells = model.numberOfColorsInRange + 3; // Add min, max and NaN
      const numberOfColorsInUpperRowForPoints = model.numberOfColorsInRange + 2; // Add min and max ; the lower row will be used for NaN color
      const textureDimensions = cellFlag
        ? [
            Math.min(
              Math.ceil(numberOfColorsForCells / maxTextureWidthForCells ** 0),
              maxTextureWidthForCells
            ),
            Math.min(
              Math.ceil(numberOfColorsForCells / maxTextureWidthForCells ** 1),
              maxTextureWidthForCells
            ),
            Math.min(
              Math.ceil(numberOfColorsForCells / maxTextureWidthForCells ** 2),
              maxTextureWidthForCells
            ),
          ]
        : [numberOfColorsInUpperRowForPoints, 2, 1];
      const textureSize =
        textureDimensions[0] * textureDimensions[1] * textureDimensions[2];
 
      const scalarsArray = new Float64Array(textureSize);
 
      // Colors for NaN by default
      scalarsArray.fill(NaN);
 
      // Colors in range
      // Add 2 to also get color for min and max
      const numberOfNonSpecialColors = model.numberOfColorsInRange;
      const numberOfNonNaNColors = numberOfNonSpecialColors + 2;
      const textureCoordinates = [0, 0, 0];
      const rangeMin = range[0];
      const rangeDifference = range[1] - range[0];
      for (let i = 0; i < numberOfNonNaNColors; ++i) {
        const scalarsArrayIndex = getIndexFromCoordinates(
          textureCoordinates,
          textureDimensions
        );
 
        // Minus 1 start at min color
        const scalarValue =
          rangeMin +
          (rangeDifference * (i - 1)) / (numberOfNonSpecialColors - 1);
        scalarsArray[scalarsArrayIndex] = useLogScale
          ? 10.0 ** scalarValue
          : scalarValue;
 
        // Colors are zigzagging to allow interpolation between two neighbor colors when coloring cells
        updateZigzaggingCoordinates(textureCoordinates, textureDimensions);
      }
 
      const scalarsDataArray = vtkDataArray.newInstance({
        numberOfComponents: 1,
        values: scalarsArray,
      });
      const colorsDataArray = model.lookupTable.mapScalars(
        scalarsDataArray,
        model.colorMode,
        0
      );
 
      model.colorTextureMap = vtkImageData.newInstance();
      model.colorTextureMap.setDimensions(textureDimensions);
      model.colorTextureMap.getPointData().setScalars(colorsDataArray);
 
      model.lookupTable.setAlpha(origAlpha);
    }
 
    // Although I like the feature of applying magnitude to single component
    // scalars, it is not how the old MapScalars for vertex coloring works.
    const scalarComponent =
      model.lookupTable.getVectorMode() === VectorMode.MAGNITUDE &&
      scalars.getNumberOfComponents() > 1
        ? -1
        : model.lookupTable.getVectorComponent();
 
    // Create new coordinates if necessary, this function uses cache if possible.
    // A zigzag pattern can't be used with point data, as interpolation of texture coordinates will be wrong
    // A zigzag pattern can be used with cell data, as there will be no texture coordinates interpolation
    // The texture generated using a zigzag pattern in one dimension is the same as without zigzag
    // Therefore, the same code can be used for texture generation of point/cell data but not for texture coordinates
    model.colorCoordinates = getOrCreateColorTextureCoordinates(
      scalars,
      scalarComponent,
      range,
      model.numberOfColorsInRange,
      model.colorTextureMap.getDimensions(),
      useLogScale,
      cellFlag
    );
  };
 
  publicAPI.getIsOpaque = () => {
    const input = publicAPI.getInputData();
    const gasResult = publicAPI.getAbstractScalars(
      input,
      model.scalarMode,
      model.arrayAccessMode,
      model.arrayId,
      model.colorByArrayName
    );
    const scalars = gasResult.scalars;
    if (!model.scalarVisibility || scalars == null) {
      // No scalar colors.
      return true;
    }
    const lut = publicAPI.getLookupTable();
    if (lut) {
      // Ensure that the lookup table is built
      lut.build();
      return lut.areScalarsOpaque(scalars, model.colorMode, -1);
    }
    return true;
  };
 
  publicAPI.canUseTextureMapForColoring = (scalars, cellFlag) => {
    if (cellFlag && !(model.colorMode === ColorMode.DIRECT_SCALARS)) {
      return true; // cell data always use textures.
    }
 
    if (!model.interpolateScalarsBeforeMapping) {
      return false; // user doesn't want us to use texture maps at all.
    }
 
    // index color does not use textures
    if (model.lookupTable && model.lookupTable.getIndexedLookup()) {
      return false;
    }
 
    Iif (!scalars) {
      // no scalars on this dataset, we don't care if texture is used at all.
      return false;
    }
 
    Iif (
      (model.colorMode === ColorMode.DEFAULT &&
        scalars.getDataType() === VtkDataTypes.UNSIGNED_CHAR) ||
      model.colorMode === ColorMode.DIRECT_SCALARS
    ) {
      // Don't use texture is direct coloring using RGB unsigned chars is
      // requested.
      return false;
    }
 
    return true;
  };
 
  publicAPI.clearColorArrays = () => {
    model.colorMapColors = null;
    model.colorCoordinates = null;
    model.colorTextureMap = null;
  };
 
  publicAPI.getLookupTable = () => {
    if (!model.lookupTable) {
      publicAPI.createDefaultLookupTable();
    }
    return model.lookupTable;
  };
 
  publicAPI.getMTime = () => {
    let mt = model.mtime;
    if (model.lookupTable !== null) {
      const time = model.lookupTable.getMTime();
      mt = time > mt ? time : mt;
    }
    return mt;
  };
 
  publicAPI.getPrimitiveCount = () => {
    const input = publicAPI.getInputData();
    const pcount = {
      points: input.getPoints().getNumberOfValues() / 3,
      verts:
        input.getVerts().getNumberOfValues() -
        input.getVerts().getNumberOfCells(),
      lines:
        input.getLines().getNumberOfValues() -
        2 * input.getLines().getNumberOfCells(),
      triangles:
        input.getPolys().getNumberOfValues() -
        3 * input.getPolys().getNumberOfCells(),
    };
    return pcount;
  };
 
  publicAPI.acquireInvertibleLookupTable = notImplemented(
    'AcquireInvertibleLookupTable'
  );
  publicAPI.valueToColor = notImplemented('ValueToColor');
  publicAPI.colorToValue = notImplemented('ColorToValue');
  publicAPI.useInvertibleColorFor = notImplemented('UseInvertibleColorFor');
  publicAPI.clearInvertibleColor = notImplemented('ClearInvertibleColor');
 
  publicAPI.processSelectorPixelBuffers = (selector, pixelOffsets) => {
    /* eslint-disable no-bitwise */
    Iif (
      !selector ||
      !model.selectionWebGLIdsToVTKIds ||
      !model.populateSelectionSettings
    ) {
      return;
    }
 
    const rawLowData = selector.getRawPixelBuffer(PassTypes.ID_LOW24);
    const rawHighData = selector.getRawPixelBuffer(PassTypes.ID_HIGH24);
    const currentPass = selector.getCurrentPass();
    const fieldAssociation = selector.getFieldAssociation();
 
    let idMap = null;
    if (fieldAssociation === FieldAssociations.FIELD_ASSOCIATION_POINTS) {
      idMap = model.selectionWebGLIdsToVTKIds.points;
    } else if (fieldAssociation === FieldAssociations.FIELD_ASSOCIATION_CELLS) {
      idMap = model.selectionWebGLIdsToVTKIds.cells;
    }
 
    Iif (!idMap) {
      return;
    }
 
    pixelOffsets.forEach((pos) => {
      if (currentPass === PassTypes.ID_LOW24) {
        let inValue = 0;
        if (rawHighData) {
          inValue += rawHighData[pos];
          inValue *= 256;
        }
        inValue += rawLowData[pos + 2];
        inValue *= 256;
        inValue += rawLowData[pos + 1];
        inValue *= 256;
        inValue += rawLowData[pos];
 
        const outValue = idMap[inValue];
        const lowData = selector.getPixelBuffer(PassTypes.ID_LOW24);
        lowData[pos] = outValue & 0xff;
        lowData[pos + 1] = (outValue & 0xff00) >> 8;
        lowData[pos + 2] = (outValue & 0xff0000) >> 16;
      } else if (currentPass === PassTypes.ID_HIGH24 && rawHighData) {
        let inValue = 0;
        inValue += rawHighData[pos];
        inValue *= 256;
        inValue += rawLowData[pos + 2];
        inValue *= 256;
        inValue += rawLowData[pos + 1];
        inValue *= 256;
        inValue += rawLowData[pos];
 
        const outValue = idMap[inValue];
        const highData = selector.getPixelBuffer(PassTypes.ID_HIGH24);
        highData[pos] = (outValue & 0xff000000) >> 24;
      }
    });
    /* eslint-enable no-bitwise */
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  colorMapColors: null, // Same as this->Colors
  areScalarsMappedFromCells: false,
 
  static: false,
  lookupTable: null,
 
  scalarVisibility: true,
  scalarRange: [0, 1],
  useLookupTableScalarRange: false,
 
  colorMode: 0,
  scalarMode: 0,
  arrayAccessMode: 1, // By_NAME
 
  renderTime: 0,
 
  colorByArrayName: null,
 
  fieldDataTupleId: -1,
 
  populateSelectionSettings: true,
  selectionWebGLIdsToVTKIds: null,
 
  interpolateScalarsBeforeMapping: false,
  colorCoordinates: null,
  colorTextureMap: null,
  numberOfColorsInRange: 0,
 
  forceCompileOnly: 0,
 
  useInvertibleColors: false,
  invertibleScalars: null,
 
  customShaderAttributes: [],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkAbstractMapper3D.extend(publicAPI, model, initialValues);
 
  macro.get(publicAPI, model, [
    'areScalarsMappedFromCells',
    'colorCoordinates',
    'colorMapColors',
    'colorTextureMap',
    'numberOfColorsInRange',
    'selectionWebGLIdsToVTKIds',
  ]);
  macro.setGet(publicAPI, model, [
    'colorByArrayName',
    'arrayAccessMode',
    'colorMode',
    'fieldDataTupleId',
    'interpolateScalarsBeforeMapping',
    'lookupTable',
    'populateSelectionSettings',
    'renderTime',
    'scalarMode',
    'scalarVisibility',
    'static',
    'useLookupTableScalarRange',
    'customShaderAttributes', // point data array names that will be transferred to the VBO
  ]);
  macro.setGetArray(publicAPI, model, ['scalarRange'], 2);
 
  CoincidentTopologyHelper.implementCoincidentTopologyMethods(publicAPI, model);
 
  // Object methods
  vtkMapper(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkMapper');
 
// ----------------------------------------------------------------------------
 
export default {
  newInstance,
  extend,
  ...staticOffsetAPI,
  ...otherStaticMethods,
  ...Constants,
};