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

85.48% Statements 53/62
68% Branches 17/25
81.25% Functions 13/16
86.2% Lines 50/58

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              1x         4x 12x   4x 12x                               2x                 2x   2x       2x 1x 1x 1x   1x       2x 2x 2x 1x   2x       2x 1x 1x 1x   1x     2x 1x     1x 1x     2x 8x   4x             4x 4x     4x           4x 4x                                 2x     2x 1x 1x 1x 1x           2x             1x                         2x   2x 2x 2x                   2x     2x         1x                
import macro from 'vtk.js/Sources/macros';
 
import vtkPiecewiseFunction from 'vtk.js/Sources/Common/DataModel/PiecewiseFunction';
import vtkPiecewiseGaussianWidget from 'vtk.js/Sources/Interaction/Widgets/PiecewiseGaussianWidget';
 
import Constants from './Constants';
 
const { Mode, Defaults } = Constants;
 
// ----------------------------------------------------------------------------
 
function applyPointsToPiecewiseFunction(points, range, pwf) {
  const width = range[1] - range[0];
  const rescaled = points.map(([x, y]) => [x * width + range[0], y]);
 
  pwf.removeAllPoints();
  rescaled.forEach(([x, y]) => pwf.addPoint(x, y));
}
 
// ----------------------------------------------------------------------------
 
function applyNodesToPiecewiseFunction(nodes, range, pwf) {
  const width = range[1] - range[0];
  const rescaled = nodes.map((n) => ({ ...n, x: n.x * width + range[0] }));
 
  pwf.setNodes(rescaled);
}
 
// ----------------------------------------------------------------------------
 
function copyGaussians(gaussians) {
  // gaussians is assumed to be an array of gaussian objects
  return gaussians.map((g) => ({ ...g }));
}
 
// ----------------------------------------------------------------------------
// vtkPiecewiseFunctionProxy methods
// ----------------------------------------------------------------------------
 
function vtkPiecewiseFunctionProxy(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPiecewiseFunctionProxy');
 
  model.piecewiseFunction =
    model.piecewiseFunction || vtkPiecewiseFunction.newInstance();
 
  // Takes an array of gaussians
  publicAPI.setGaussians = (gaussians) => {
    model.gaussians = copyGaussians(gaussians || []);
    if (model.gaussians.length === 0) {
      model.gaussians = copyGaussians(Defaults.Gaussians);
    }
    publicAPI.applyMode();
  };
 
  // Takes an array of points [x, y]
  publicAPI.setPoints = (points) => {
    model.points = (points || []).slice();
    if (model.points.length === 0) {
      model.points = Defaults.Points.slice();
    }
    publicAPI.applyMode();
  };
 
  // Takes an array of PiecewiseFunction nodes
  publicAPI.setNodes = (nodes) => {
    model.nodes = (nodes || []).slice();
    if (model.nodes.length === 0) {
      model.nodes = Defaults.Nodes.slice();
    }
    publicAPI.applyMode();
  };
 
  publicAPI.setMode = (mode) => {
    Iif (model.mode === mode) {
      return;
    }
    model.mode = mode;
    publicAPI.applyMode();
  };
 
  publicAPI.applyMode = () => {
    switch (model.mode) {
      case Mode.Gaussians:
        vtkPiecewiseGaussianWidget.applyGaussianToPiecewiseFunction(
          model.gaussians,
          255,
          model.dataRange,
          model.piecewiseFunction
        );
 
        publicAPI.modified();
        break;
 
      case Mode.Points:
        applyPointsToPiecewiseFunction(
          model.points,
          model.dataRange,
          model.piecewiseFunction
        );
 
        publicAPI.modified();
        break;
 
      case Mode.Nodes:
        applyNodesToPiecewiseFunction(
          model.nodes,
          model.dataRange,
          model.piecewiseFunction
        );
 
        publicAPI.modified();
        break;
 
      default:
      // noop
    }
  };
 
  publicAPI.getLookupTableProxy = () =>
    model.proxyManager.getLookupTable(model.arrayName);
 
  publicAPI.setDataRange = (min, max) => {
    if (model.dataRange[0] !== min || model.dataRange[1] !== max) {
      model.dataRange[0] = min;
      model.dataRange[1] = max;
      publicAPI.applyMode();
    }
  };
 
  // Initialization ------------------------------------------------------------
 
  publicAPI.applyMode();
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  mode: Mode.Gaussians,
  gaussians: Defaults.Gaussians,
  points: Defaults.Points,
  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, [
    'piecewiseFunction',
    'gaussians',
    'nodes',
    'points',
    'mode',
    'dataRange',
  ]);
 
  // Object specific methods
  vtkPiecewiseFunctionProxy(publicAPI, model);
 
  // Proxy handling
  macro.proxy(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkPiecewiseFunctionProxy'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, Mode, Defaults };