All files / Sources/Filters/General/Calculator index.js

64.39% Statements 85/132
54.23% Branches 32/59
53.48% Functions 23/43
65.38% Lines 85/130

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              1x               10x   10x 8x     8x 8x 8x     10x   10x                       10x           1x 1x     1x                                         1x 1x 1x 1x 1x 65536x     1x 1x 1x 1x 1x 1x 65536x 65536x                                     10x             1x                   10x 9x 9x 9x 7x 6x   1x   1x           6x 6x     1x     1x 1x 1x                                               9x 15x   15x 15x 1x   15x                   15x     1x 1x       14x 14x                                             90x 90x   15x 15x 15x 15x 15x   15x             15x 15x 15x 15x 15x       9x   15x                     9x     10x 9x       9x   9x 9x 9x   9x 9x   9x               1x                   10x     10x     10x     10x         1x          
import vtk from 'vtk.js/Sources/vtk';
import macro from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';
import { FieldDataTypes } from 'vtk.js/Sources/Common/DataModel/DataSet/Constants';
import { AttributeTypes } from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/Constants';
 
const { vtkWarningMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkCalculator methods
// ----------------------------------------------------------------------------
 
function vtkCalculator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkCalculator');
 
  publicAPI.setFormula = (formula) => {
    Iif (formula === model.formula) {
      return false;
    }
    model.formula = formula;
    publicAPI.modified();
    return true;
  };
 
  publicAPI.getFormula = () => model.formula;
 
  publicAPI.augmentInputArrays = (locn, arraysIn) => {
    const arraysOut = arraysIn.slice(0); // shallow-copy the inputs
    // Make point coordinates available whenever the field-data is associated with
    // points or graph vertices:
    if (locn === FieldDataTypes.POINT || locn === FieldDataTypes.VERTEX) {
      arraysOut.push({ location: FieldDataTypes.COORDINATE });
    }
    // TODO: Make cell connectivity available when field-data is associated with
    // cells or graph edges.
    return arraysOut;
  };
 
  publicAPI.createSimpleFormulaObject = (
    locn,
    arrNames,
    resultName,
    singleValueFormula,
    options = {}
  ) => ({
    getArrays: (inData) => ({
      // don't augment input data array in case of structured input dataset
      input: inData[0].isA('vtkImageData')
        ? arrNames.map((x) => ({ location: locn, name: x }))
        : publicAPI.augmentInputArrays(
            locn,
            arrNames.map((x) => ({ location: locn, name: x }))
          ),
      output: [
        {
          location: locn,
          name: resultName,
          attribute:
            'outputAttributeType' in options
              ? options.outputAttributeType
              : AttributeTypes.SCALARS,
          numberOfComponents:
            'numberOfOutputComponents' in options
              ? options.numberOfOutputComponents
              : 1,
        },
      ],
    }),
    evaluate: (arraysIn, arraysOut) => {
      const tuples = new Array(arraysIn.length);
      const arrayInAccessors = arraysIn.map((x, jj) => {
        const nc = x.getNumberOfComponents();
        const rawData = x.getData();
        return nc === 1
          ? (ii) => rawData[ii]
          : (ii) => x.getTuple(ii, tuples[jj]);
      });
      const arrayOut = arraysOut[0];
      const arrayOutRaw = arrayOut.getData();
      const nc = arrayOut.getNumberOfComponents();
      let tupleOut = new Array(nc);
      if (nc === 1) {
        arrayOutRaw.forEach((xxx, ii) => {
          arrayOutRaw[ii] = singleValueFormula(
            ...arrayInAccessors.map((x) => x(ii)),
            ii,
            tupleOut
          );
        });
      } else E{
        const nt = arrayOut.getNumberOfTuples();
        for (let ii = 0; ii < nt; ++ii) {
          tupleOut = singleValueFormula(
            ...arrayInAccessors.map((x) => x(ii)),
            ii,
            tupleOut
          );
          arrayOut.setTuple(ii, tupleOut);
        }
      }
    },
  });
 
  publicAPI.setFormulaSimple = (
    locn,
    arrNames,
    resultName,
    formula,
    options = {}
  ) =>
    publicAPI.setFormula(
      publicAPI.createSimpleFormulaObject(
        locn,
        arrNames,
        resultName,
        formula,
        options
      )
    );
 
  publicAPI.prepareArrays = (arraySpec, inData, outData) => {
    const arraysIn = [];
    const arraysOut = [];
    arraySpec.input.forEach((spec) => {
      if (spec.location === FieldDataTypes.COORDINATE) {
        arraysIn.push(inData.getPoints());
      } else {
        const fetchArrayContainer = [
          [FieldDataTypes.UNIFORM, (x) => x.getFieldData()],
          [FieldDataTypes.POINT, (x) => x.getPointData()],
          [FieldDataTypes.CELL, (x) => x.getCellData()],
          [FieldDataTypes.VERTEX, (x) => x.getVertexData()],
          [FieldDataTypes.EDGE, (x) => x.getEdgeData()],
          [FieldDataTypes.ROW, (x) => x.getRowData()],
        ].reduce((result, value) => {
          result[value[0]] = value[1];
          return result;
        }, {});
        const dsa =
          'location' in spec && spec.location in fetchArrayContainer
            ? fetchArrayContainer[spec.location](inData)
            : null;
        if (dsa) {
          if (spec.name) {
            arraysIn.push(dsa.getArrayByName(spec.name));
          } else Eif ('index' in spec) {
            arraysIn.push(dsa.getArrayByIndex(spec.index));
          } else if (
            'attribute' in spec &&
            spec.location !== FieldDataTypes.UNIFORM
          ) {
            arraysIn.push(dsa.getActiveAttribute(spec.attribute));
          } else {
            vtkWarningMacro(
              `No matching array for specifier "${JSON.stringify(spec)}".`
            );
            arraysIn.push(null);
          }
        } else E{
          vtkWarningMacro(
            `Specifier "${JSON.stringify(
              spec
            )}" did not provide a usable location.`
          );
          arraysIn.push(null);
        }
      }
    });
    arraySpec.output.forEach((spec) => {
      const fullSpec = { ...spec };
      const ncomp =
        'numberOfComponents' in fullSpec ? fullSpec.numberOfComponents : 1;
      if (spec.location === FieldDataTypes.UNIFORM && 'tuples' in fullSpec) {
        fullSpec.size = ncomp * fullSpec.tuples;
      }
      Iif (spec.location === FieldDataTypes.COORDINATE) {
        const inPts = inData.getPoints();
        const pts = vtkPoints.newInstance({ dataType: inPts.getDataType() });
        pts.setNumberOfPoints(
          inPts.getNumberOfPoints(),
          inPts.getNumberOfComponents()
        );
        outData.setPoints(pts);
        arraysOut.push(pts);
      } else {
        const fetchArrayContainer = [
          [
            FieldDataTypes.UNIFORM,
            (x) => x.getFieldData(),
            (x, y) => ('tuples' in y ? y.tuples : 0),
          ],
          [
            FieldDataTypes.POINT,
            (x) => x.getPointData(),
            (x) => x.getNumberOfPoints(),
          ],
          [
            FieldDataTypes.CELL,
            (x) => x.getCellData(),
            (x) => x.getNumberOfCells(),
          ],
          [
            FieldDataTypes.VERTEX,
            (x) => x.getVertexData(),
            (x) => x.getNumberOfVertices(),
          ],
          [
            FieldDataTypes.EDGE,
            (x) => x.getEdgeData(),
            (x) => x.getNumberOfEdges(),
          ],
          [
            FieldDataTypes.ROW,
            (x) => x.getRowData(),
            (x) => x.getNumberOfRows(),
          ],
        ].reduce((result, value) => {
          result[value[0]] = { getData: value[1], getSize: value[2] };
          return result;
        }, {});
        let dsa = null;
        let tuples = 0;
        if ('location' in spec && spec.location in fetchArrayContainer) {
          dsa = fetchArrayContainer[spec.location].getData(outData);
          tuples = fetchArrayContainer[spec.location].getSize(inData, fullSpec);
        }
        Iif (tuples <= 0) {
          vtkWarningMacro(
            `Output array size could not be determined for ${JSON.stringify(
              spec
            )}.`
          );
          arraysOut.push(null);
        } else if (dsa) {
          fullSpec.size = ncomp * tuples;
          const arrOut = vtkDataArray.newInstance(fullSpec);
          const arrIdx = dsa.addArray(arrOut);
          if (
            'attribute' in fullSpec &&
            spec.location !== FieldDataTypes.UNIFORM
          ) {
            dsa.setActiveAttributeByIndex(arrIdx, fullSpec.attribute);
          }
          arraysOut.push(arrOut);
        } else E{
          vtkWarningMacro(
            `Specifier "${JSON.stringify(
              spec
            )}" did not provide a usable location.`
          );
          arraysOut.push(null);
        }
      }
    });
    return { arraysIn, arraysOut };
  };
 
  publicAPI.requestData = (inData, outData) => {
    Iif (!model.formula) {
      return 0;
    }
 
    const arraySpec = model.formula.getArrays(inData);
 
    const newDataSet = vtk({ vtkClass: inData[0].getClassName() });
    newDataSet.shallowCopy(inData[0]);
    outData[0] = newDataSet;
 
    const arrays = publicAPI.prepareArrays(arraySpec, inData[0], outData[0]);
    model.formula.evaluate(arrays.arraysIn, arrays.arraysOut);
 
    return 1;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  formula: {
    getArrays: () => ({ input: [], output: [] }),
    evaluate: () => null,
  },
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  // Object specific methods
  vtkCalculator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkCalculator');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };