All files / Sources/Common/Core/LookupTable index.js

75% Statements 150/200
56.45% Branches 35/62
92.85% Functions 13/14
76.31% Lines 145/190

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              1x                         1x 1x 1x               40x           40x                                               40x     40x       40x 1897x 1897x   1897x 3x 1894x 25x   1869x         1869x     1897x     40x 2485x 2485x 588x   1897x   2485x 2485x     40x 200x 200x 20x   200x 200x                 40x 20x 20x 20x 20x         40x           20x 20x 2x     20x   20x           20x   20x 20x 20x   20x 20x   20x 20x 20x 2685x         2685x 2685x 2685x 2685x                                         40x 59x 59x 59x 59x   59x   59x 59x 59x 59x 59x     59x   59x 59x 59x 14600x 14600x 14600x   14600x 14600x     14600x 14600x 14600x 14600x     59x   59x     40x   2x                                                   2x       2x       2x 2x 2x 2x 60x     2x 2x 2x 2x     40x   61x   61x 61x     61x 3x 3x 3x 3x     58x 58x 58x 58x       61x 61x 3x 3x 3x 3x     58x 58x 58x 58x       61x 61x 61x 61x 61x     40x 359x         59x       40x                           1x                                               40x     40x     40x 37x     40x 40x   40x 40x   40x 40x     40x     40x             40x             40x               40x                         40x         1x          
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkScalarsToColors from 'vtk.js/Sources/Common/Core/ScalarsToColors';
import { ScalarMappingTarget } from 'vtk.js/Sources/Common/Core/ScalarsToColors/Constants';
 
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
// Add module-level functions or api that you want to expose statically via
// the next section...
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
 
const BELOW_RANGE_COLOR_INDEX = 0;
const ABOVE_RANGE_COLOR_INDEX = 1;
const NAN_COLOR_INDEX = 2;
 
// ----------------------------------------------------------------------------
// vtkMyClass methods
// ----------------------------------------------------------------------------
 
function vtkLookupTable(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkLookupTable');
 
  //----------------------------------------------------------------------------
  // Description:
  // Return true if all of the values defining the mapping have an opacity
  // equal to 1. Default implementation return true.
  publicAPI.isOpaque = () => {
    if (model.opaqueFlagBuildTime.getMTime() < publicAPI.getMTime()) {
      let opaque = true;
      if (model.nanColor[3] < 1.0) {
        opaque = 0;
      }
      if (model.useBelowRangeColor && model.belowRangeColor[3] < 1.0) {
        opaque = 0;
      }
      if (model.useAboveRangeColor && model.aboveRangeColor[3] < 1.0) {
        opaque = 0;
      }
      for (let i = 3; i < model.table.length && opaque; i += 4) {
        if (model.table[i] < 255) {
          opaque = false;
        }
      }
      model.opaqueFlag = opaque;
      model.opaqueFlagBuildTime.modified();
    }
 
    return model.opaqueFlag;
  };
 
  publicAPI.usingLogScale = () => false;
 
  //----------------------------------------------------------------------------
  publicAPI.getNumberOfAvailableColors = () => model.table.length / 4;
 
  //----------------------------------------------------------------------------
  // Apply shift/scale to the scalar value v and return the index.
  publicAPI.linearIndexLookup = (v, p) => {
    let dIndex = 0;
    const nv = Number(v);
 
    if (nv < p.range[0]) {
      dIndex = p.maxIndex + BELOW_RANGE_COLOR_INDEX + 1.5;
    } else if (nv > p.range[1]) {
      dIndex = p.maxIndex + ABOVE_RANGE_COLOR_INDEX + 1.5;
    } else {
      dIndex = (nv + p.shift) * p.scale;
 
      // This conditional is needed because when v is very close to
      // p.Range[1], it may map above p.MaxIndex in the linear mapping
      // above.
      dIndex = dIndex < p.maxIndex ? dIndex : p.maxIndex;
    }
 
    return Math.floor(dIndex);
  };
 
  publicAPI.linearLookup = (v, table, p) => {
    let index = 0;
    if (vtkMath.isNan(v)) {
      index = Math.floor(p.maxIndex + 1.5 + NAN_COLOR_INDEX);
    } else {
      index = publicAPI.linearIndexLookup(v, p);
    }
    const offset = 4 * index;
    return table.slice(offset, offset + 4);
  };
 
  publicAPI.indexedLookupFunction = (v, table, p) => {
    let index = publicAPI.getAnnotatedValueIndexInternal(v);
    if (index === -1) {
      index = model.numberOfColors + NAN_COLOR_INDEX;
    }
    const offset = 4 * index;
    return [
      table[offset],
      table[offset + 1],
      table[offset + 2],
      table[offset + 3],
    ];
  };
 
  //----------------------------------------------------------------------------
  publicAPI.lookupShiftAndScale = (range, p) => {
    p.shift = -range[0];
    p.scale = Number.MAX_VALUE;
    if (range[1] > range[0]) {
      p.scale = (p.maxIndex + 1) / (range[1] - range[0]);
    }
  };
 
  // Public API methods
  publicAPI.mapScalarsThroughTable = (
    input,
    output,
    outFormat,
    inputOffset
  ) => {
    let lookupFunc = publicAPI.linearLookup;
    if (model.indexedLookup) {
      lookupFunc = publicAPI.indexedLookupFunction;
    }
 
    const trange = publicAPI.getMappingRange();
 
    const p = {
      maxIndex: publicAPI.getNumberOfColors() - 1,
      range: trange,
      shift: 0.0,
      scale: 0.0,
    };
    publicAPI.lookupShiftAndScale(trange, p);
 
    const alpha = publicAPI.getAlpha();
    const length = input.getNumberOfTuples();
    const inIncr = input.getNumberOfComponents();
 
    const outputV = output.getData();
    const inputV = input.getData();
 
    if (alpha >= 1.0) {
      if (outFormat === ScalarMappingTarget.RGBA) {
        for (let i = 0; i < length; i++) {
          const cptr = lookupFunc(
            inputV[i * inIncr + inputOffset],
            model.table,
            p
          );
          outputV[i * 4] = cptr[0];
          outputV[i * 4 + 1] = cptr[1];
          outputV[i * 4 + 2] = cptr[2];
          outputV[i * 4 + 3] = cptr[3];
        }
      }
    } else E{
      /* eslint-disable no-lonely-if */
      if (outFormat === ScalarMappingTarget.RGBA) {
        for (let i = 0; i < length; i++) {
          const cptr = lookupFunc(
            inputV[i * inIncr + inputOffset],
            model.table,
            p
          );
          outputV[i * 4] = cptr[0];
          outputV[i * 4 + 1] = cptr[1];
          outputV[i * 4 + 2] = cptr[2];
          outputV[i * 4 + 3] = Math.floor(cptr[3] * alpha + 0.5);
        }
      }
    } // alpha blending
  };
 
  publicAPI.forceBuild = () => {
    let hinc = 0.0;
    let sinc = 0.0;
    let vinc = 0.0;
    let ainc = 0.0;
 
    const maxIndex = model.numberOfColors - 1;
 
    if (maxIndex) {
      hinc = (model.hueRange[1] - model.hueRange[0]) / maxIndex;
      sinc = (model.saturationRange[1] - model.saturationRange[0]) / maxIndex;
      vinc = (model.valueRange[1] - model.valueRange[0]) / maxIndex;
      ainc = (model.alphaRange[1] - model.alphaRange[0]) / maxIndex;
    }
 
    model.table.length = 4 * maxIndex + 16;
 
    const hsv = [];
    const rgba = [];
    for (let i = 0; i <= maxIndex; i++) {
      hsv[0] = model.hueRange[0] + i * hinc;
      hsv[1] = model.saturationRange[0] + i * sinc;
      hsv[2] = model.valueRange[0] + i * vinc;
 
      vtkMath.hsv2rgb(hsv, rgba);
      rgba[3] = model.alphaRange[0] + i * ainc;
 
      //  case VTK_RAMP_LINEAR:
      model.table[i * 4] = rgba[0] * 255.0 + 0.5;
      model.table[i * 4 + 1] = rgba[1] * 255.0 + 0.5;
      model.table[i * 4 + 2] = rgba[2] * 255.0 + 0.5;
      model.table[i * 4 + 3] = rgba[3] * 255.0 + 0.5;
    }
 
    publicAPI.buildSpecialColors();
 
    model.buildTime.modified();
  };
 
  publicAPI.setTable = (table) => {
    // Handle JS array (assume 2D array)
    Iif (Array.isArray(table)) {
      const nbComponents = table[0].length;
      model.numberOfColors = table.length;
      const colorOffset = 4 - nbComponents;
      let offset = 0;
      // fill table
      for (let i = 0; i < model.numberOfColors; i++) {
        model.table[i * 4] = 255;
        model.table[i * 4 + 1] = 255;
        model.table[i * 4 + 2] = 255;
        model.table[i * 4 + 3] = 255;
      }
      // extract colors
      for (let i = 0; i < table.length; i++) {
        const color = table[i];
        for (let j = 0; j < nbComponents; j++) {
          model.table[offset++] = color[j];
        }
        offset += colorOffset;
      }
      publicAPI.buildSpecialColors();
      model.insertTime.modified();
      publicAPI.modified();
      return true;
    }
 
    Iif (table.getNumberOfComponents() !== 4) {
      vtkErrorMacro('Expected 4 components for RGBA colors');
      return false;
    }
    Iif (table.getDataType() !== VtkDataTypes.UNSIGNED_CHAR) {
      vtkErrorMacro('Expected unsigned char values for RGBA colors');
      return false;
    }
    model.numberOfColors = table.getNumberOfTuples();
    const data = table.getData();
    model.table.length = data.length;
    for (let i = 0; i < data.length; i++) {
      model.table[i] = data[i];
    }
 
    publicAPI.buildSpecialColors();
    model.insertTime.modified();
    publicAPI.modified();
    return true;
  };
 
  publicAPI.buildSpecialColors = () => {
    // Add "special" colors (NaN, below range, above range) to table here.
    const { numberOfColors } = model;
 
    const tptr = model.table;
    let base = (numberOfColors + BELOW_RANGE_COLOR_INDEX) * 4;
 
    // Below range color
    if (model.useBelowRangeColor || numberOfColors === 0) {
      tptr[base] = model.belowRangeColor[0] * 255.0 + 0.5;
      tptr[base + 1] = model.belowRangeColor[1] * 255.0 + 0.5;
      tptr[base + 2] = model.belowRangeColor[2] * 255.0 + 0.5;
      tptr[base + 3] = model.belowRangeColor[3] * 255.0 + 0.5;
    } else {
      // Duplicate the first color in the table.
      tptr[base] = tptr[0];
      tptr[base + 1] = tptr[1];
      tptr[base + 2] = tptr[2];
      tptr[base + 3] = tptr[3];
    }
 
    // Above range color
    base = (numberOfColors + ABOVE_RANGE_COLOR_INDEX) * 4;
    if (model.useAboveRangeColor || numberOfColors === 0) {
      tptr[base] = model.aboveRangeColor[0] * 255.0 + 0.5;
      tptr[base + 1] = model.aboveRangeColor[1] * 255.0 + 0.5;
      tptr[base + 2] = model.aboveRangeColor[2] * 255.0 + 0.5;
      tptr[base + 3] = model.aboveRangeColor[3] * 255.0 + 0.5;
    } else {
      // Duplicate the last color in the table.
      tptr[base] = tptr[4 * (numberOfColors - 1) + 0];
      tptr[base + 1] = tptr[4 * (numberOfColors - 1) + 1];
      tptr[base + 2] = tptr[4 * (numberOfColors - 1) + 2];
      tptr[base + 3] = tptr[4 * (numberOfColors - 1) + 3];
    }
 
    // Always use NanColor
    base = (numberOfColors + NAN_COLOR_INDEX) * 4;
    tptr[base] = model.nanColor[0] * 255.0 + 0.5;
    tptr[base + 1] = model.nanColor[1] * 255.0 + 0.5;
    tptr[base + 2] = model.nanColor[2] * 255.0 + 0.5;
    tptr[base + 3] = model.nanColor[3] * 255.0 + 0.5;
  };
 
  publicAPI.build = () => {
    if (
      model.table.length < 1 ||
      (publicAPI.getMTime() > model.buildTime.getMTime() &&
        model.insertTime.getMTime() <= model.buildTime.getMTime())
    ) {
      publicAPI.forceBuild();
    }
  };
 
  Iif (model.table.length > 0) {
    // Ensure that special colors are properly included in the table
    publicAPI.buildSpecialColors();
 
    // ensure insertTime is more recently modified than buildTime if
    // a table is provided via the constructor
    model.insertTime.modified();
  }
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  numberOfColors: 256,
  // table: null,
 
  hueRange: [0.0, 0.66667],
  saturationRange: [1.0, 1.0],
  valueRange: [1.0, 1.0],
  alphaRange: [1.0, 1.0],
 
  nanColor: [0.5, 0.0, 0.0, 1.0],
  belowRangeColor: [0.0, 0.0, 0.0, 1.0],
  aboveRangeColor: [1.0, 1.0, 1.0, 1.0],
  useAboveRangeColor: false,
  useBelowRangeColor: false,
 
  alpha: 1.0,
  // buildTime: null,
  // opaqueFlagBuildTime: null,
  // insertTime: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkScalarsToColors.extend(publicAPI, model, initialValues);
 
  // Internal objects initialization
  if (!model.table) {
    model.table = [];
  }
 
  model.buildTime = {};
  macro.obj(model.buildTime);
 
  model.opaqueFlagBuildTime = {};
  macro.obj(model.opaqueFlagBuildTime, { mtime: 0 });
 
  model.insertTime = {};
  macro.obj(model.insertTime, { mtime: 0 });
 
  // Create get-only macros
  macro.get(publicAPI, model, ['buildTime']);
 
  // Create get-set macros
  macro.setGet(publicAPI, model, [
    'numberOfColors',
    'useAboveRangeColor',
    'useBelowRangeColor',
  ]);
 
  // Create set macros for array (needs to know size)
  macro.setArray(
    publicAPI,
    model,
    ['alphaRange', 'hueRange', 'saturationRange', 'valueRange'],
    2
  );
 
  macro.setArray(
    publicAPI,
    model,
    ['nanColor', 'belowRangeColor', 'aboveRangeColor'],
    4
  );
 
  // Create get macros for array
  macro.getArray(publicAPI, model, [
    'hueRange',
    'saturationRange',
    'valueRange',
    'alphaRange',
    'nanColor',
    'belowRangeColor',
    'aboveRangeColor',
  ]);
 
  // For more macro methods, see "Sources/macros.js"
 
  // Object specific methods
  vtkLookupTable(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkLookupTable');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };