All files / Sources/Proxy/Core/LookupTableProxy index.js

46.66% Statements 28/60
13.33% Branches 4/30
27.27% Functions 3/11
46.66% Lines 28/60

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              1x               1x   1x       1x     1x                 1x               1x               1x             1x             1x 1x     1x 1x 1x     1x                                               1x 1x 1x     1x                           1x             1x                           1x   1x 1x 1x                     1x     1x         1x              
import macro from 'vtk.js/Sources/macros';
 
import vtkColorMaps from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction/ColorMaps';
import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction';
 
import Constants from './Constants';
 
const { Mode, Defaults } = Constants;
 
// ----------------------------------------------------------------------------
// vtkLookupTableProxy methods
// ----------------------------------------------------------------------------
 
function vtkLookupTableProxy(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkLookupTableProxy');
 
  model.lookupTable =
    model.lookupTable || vtkColorTransferFunction.newInstance();
 
  // Initialize lookup table
  model.lookupTable.setVectorModeToMagnitude();
 
  // Takes a preset colormap name
  publicAPI.setPresetName = (presetName) => {
    if (model.presetName !== presetName) {
      model.presetName = presetName;
      model.mode = Mode.Preset;
      publicAPI.applyMode();
    }
  };
 
  // Takes an array of points [x, r, g, b, m=0.5, s=0.0]
  publicAPI.setRGBPoints = (rgbPoints) => {
    if (model.rgbPoints !== rgbPoints) {
      model.rgbPoints = (rgbPoints || Defaults.RGBPoints).slice();
      publicAPI.applyMode();
    }
  };
 
  // Takes an array of points [x, h, s, v, m=0.5, s=0.0]
  publicAPI.setHSVPoints = (hsvPoints) => {
    if (model.hsvPoints !== hsvPoints) {
      model.hsvPoints = (hsvPoints || Defaults.HSVPoints).slice();
      publicAPI.applyMode();
    }
  };
 
  // Takes an array of ColorTransferFunction nodes
  publicAPI.setNodes = (nodes) => {
    if (model.nodes !== nodes) {
      model.nodes = (nodes || Defaults.Nodes).slice();
      publicAPI.applyMode();
    }
  };
 
  publicAPI.setMode = (mode) => {
    if (model.mode !== mode) {
      model.mode = mode;
      publicAPI.applyMode();
    }
  };
 
  publicAPI.applyMode = () => {
    switch (model.mode) {
      case Mode.Preset:
        {
          const preset = vtkColorMaps.getPresetByName(model.presetName);
          if (preset) {
            model.lookupTable.applyColorMap(preset);
          }
        }
        break;
 
      case Mode.RGBPoints:
        model.lookupTable.removeAllPoints();
        model.rgbPoints.forEach((point) =>
          model.lookupTable.addRGBPointLong(...point)
        );
        break;
 
      case Mode.HSVPoints:
        model.lookupTable.removeAllPoints();
        model.hsvPoints.forEach((point) =>
          model.lookupTable.addHSVPointLong(...point)
        );
        break;
 
      case Mode.Nodes:
        model.lookupTable.setNodes(model.nodes);
        break;
 
      default:
      // noop
    }
 
    model.lookupTable.setMappingRange(model.dataRange[0], model.dataRange[1]);
    model.lookupTable.updateRange();
    publicAPI.modified();
  };
 
  publicAPI.setDataRange = (min, max) => {
    if (model.dataRange[0] !== min || model.dataRange[1] !== max) {
      model.dataRange[0] = min;
      model.dataRange[1] = max;
 
      model.lookupTable.setMappingRange(model.dataRange[0], model.dataRange[1]);
      model.lookupTable.updateRange();
 
      publicAPI.applyMode();
    }
  };
 
  // Initialization ------------------------------------------------------------
 
  publicAPI.applyMode();
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  mode: Mode.Preset,
  presetName: Defaults.Preset,
  rgbPoints: Defaults.RGBPoints,
  hsvPoints: Defaults.HSVPoints,
  nodes: Defaults.Nodes,
  arrayName: 'No array associated',
  arrayLocation: 'pointData',
  dataRange: [0, 1],
};
 
// ----------------------------------------------------------------------------
 
function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, ['arrayName']);
  macro.get(publicAPI, model, [
    'mode',
    'lookupTable',
    'presetName',
    'rgbPoints',
    'hsvPoints',
    'nodes',
    'dataRange',
  ]);
 
  // Object specific methods
  vtkLookupTableProxy(publicAPI, model);
 
  // Proxy handling
  macro.proxy(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkLookupTableProxy');
 
// ----------------------------------------------------------------------------
 
export { Mode };
 
export default { newInstance, extend, Mode, Defaults };