All files / Sources/Common/DataModel/DataSetAttributes index.js

84.34% Statements 97/115
66.66% Branches 34/51
86.2% Functions 25/29
83.92% Lines 94/112

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          1x 1x             4609x                           1326x   3374x       1326x     1326x       4609x 4609x   4609x   4609x 1275x 1275x                   1275x           1275x 1275x 7x       7x     1275x 1262x 1262x   13x   1275x 1275x     4609x 28x   4609x           4609x 37x 37x 30x 29x 29x           29x             30x 30x 30x     7x 7x 7x     7x     4609x     14x 14x       4609x               4609x 8x 8x 56x 7x 49x 3x       8x     4609x 32263x 32263x 27718x 32263x 32263x 27x       32263x 12x 12x       32263x 4x 4x           4609x   4611x 4611x 18444x   13833x   124497x   110664x 110664x       4611x     4611x     4611x         4609x           4609x                     4609x 4609x 45x 45x 17x 17x 17x       4609x             1x                         4609x     4609x 4609x                   4609x         4609x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkFieldData from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/FieldData';
import Constants from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/Constants';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
 
const { AttributeTypes, AttributeCopyOperations } = Constants;
const { vtkWarningMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkDataSetAttributes methods
// ----------------------------------------------------------------------------
 
function vtkDataSetAttributes(publicAPI, model) {
  const attrTypes = [
    'Scalars',
    'Vectors',
    'Normals',
    'TCoords',
    'Tensors',
    'GlobalIds',
    'PedigreeIds',
  ];
 
  function cleanAttributeType(attType) {
    // Given an integer or string, convert the result to one of the
    // strings in the "attrTypes" array above or null (if
    // no match is found)
    let cleanAttType = attrTypes.find(
      (ee) =>
        AttributeTypes[ee.toUpperCase()] === attType ||
        (typeof attType !== 'number' &&
          ee.toLowerCase() === attType.toLowerCase())
    );
    Iif (typeof cleanAttType === 'undefined') {
      cleanAttType = null;
    }
    return cleanAttType;
  }
 
  // Set our className
  model.classHierarchy.push('vtkDataSetAttributes');
  const superClass = { ...publicAPI };
 
  publicAPI.checkNumberOfComponents = (x) => true; // TODO
 
  publicAPI.setAttribute = (arr, uncleanAttType) => {
    const attType = cleanAttributeType(uncleanAttType);
    Iif (
      arr &&
      attType.toUpperCase() === 'PEDIGREEIDS' &&
      !arr.isA('vtkDataArray')
    ) {
      vtkWarningMacro(
        `Cannot set attribute ${attType}. The attribute must be a vtkDataArray.`
      );
      return -1;
    }
    Iif (arr && !publicAPI.checkNumberOfComponents(arr, attType)) {
      vtkWarningMacro(
        `Cannot set attribute ${attType}. Incorrect number of components.`
      );
      return -1;
    }
    let currentAttribute = model[`active${attType}`];
    if (currentAttribute >= 0 && currentAttribute < model.arrays.length) {
      Iif (model.arrays[currentAttribute] === arr) {
        return currentAttribute;
      }
      // FIXME setting an array actually changes its index
      publicAPI.removeArrayByIndex(currentAttribute);
    }
 
    if (arr) {
      currentAttribute = publicAPI.addArray(arr);
      model[`active${attType}`] = currentAttribute;
    } else {
      model[`active${attType}`] = -1;
    }
    publicAPI.modified();
    return model[`active${attType}`];
  };
 
  publicAPI.getAttributes = (arr) =>
    attrTypes.filter((attrType) => publicAPI[`get${attrType}`]() === arr);
 
  publicAPI.setActiveAttributeByName = (arrayName, attType) =>
    publicAPI.setActiveAttributeByIndex(
      publicAPI.getArrayWithIndex(arrayName).index,
      attType
    );
 
  publicAPI.setActiveAttributeByIndex = (arrayIdx, uncleanAttType) => {
    const attType = cleanAttributeType(uncleanAttType);
    if (arrayIdx >= 0 && arrayIdx < model.arrays.length) {
      if (attType.toUpperCase() !== 'PEDIGREEIDS') {
        const arr = publicAPI.getArrayByIndex(arrayIdx);
        Iif (!arr.isA('vtkDataArray')) {
          vtkWarningMacro(
            `Cannot set attribute ${attType}. Only vtkDataArray subclasses can be set as active attributes.`
          );
          return -1;
        }
        Iif (!publicAPI.checkNumberOfComponents(arr, attType)) {
          vtkWarningMacro(
            `Cannot set attribute ${attType}. Incorrect number of components.`
          );
          return -1;
        }
      }
      model[`active${attType}`] = arrayIdx;
      publicAPI.modified();
      return arrayIdx;
    }
 
    if (arrayIdx === -1) {
      model[`active${attType}`] = arrayIdx;
      publicAPI.modified();
    }
 
    return -1;
  };
 
  publicAPI.getActiveAttribute = (attType) => {
    // Given an integer enum value or a string (with random capitalization),
    // find the matching string in attrTypes.
    const cleanAttType = cleanAttributeType(attType);
    return publicAPI[`get${cleanAttType}`]();
  };
 
  // Override to allow proper handling of active attributes
  publicAPI.removeAllArrays = () => {
    attrTypes.forEach((attType) => {
      model[`active${attType}`] = -1;
    });
    superClass.removeAllArrays();
  };
 
  // Override to allow proper handling of active attributes
  publicAPI.removeArrayByIndex = (arrayIdx) => {
    if (arrayIdx !== -1) {
      attrTypes.forEach((attType) => {
        if (arrayIdx === model[`active${attType}`]) {
          model[`active${attType}`] = -1;
        } else if (arrayIdx < model[`active${attType}`]) {
          model[`active${attType}`] -= 1;
        }
      });
    }
    return superClass.removeArrayByIndex(arrayIdx);
  };
 
  attrTypes.forEach((value) => {
    const activeVal = `active${value}`;
    publicAPI[`get${value}`] = () =>
      publicAPI.getArrayByIndex(model[activeVal]);
    publicAPI[`set${value}`] = (da) => publicAPI.setAttribute(da, value);
    publicAPI[`setActive${value}`] = (arrayName) =>
      publicAPI.setActiveAttributeByIndex(
        publicAPI.getArrayWithIndex(arrayName).index,
        value
      );
    publicAPI[`copy${value}Off`] = () => {
      const attType = value.toUpperCase();
      model.copyAttributeFlags[AttributeCopyOperations.PASSDATA][
        AttributeTypes[attType]
      ] = false;
    };
    publicAPI[`copy${value}On`] = () => {
      const attType = value.toUpperCase();
      model.copyAttributeFlags[AttributeCopyOperations.PASSDATA][
        AttributeTypes[attType]
      ] = true;
    };
  });
 
  publicAPI.initializeAttributeCopyFlags = () => {
    // Default to copying all attributes in every circumstance:
    model.copyAttributeFlags = [];
    Object.keys(AttributeCopyOperations)
      .filter((op) => op !== 'ALLCOPY')
      .forEach((attCopyOp) => {
        model.copyAttributeFlags[AttributeCopyOperations[attCopyOp]] =
          Object.keys(AttributeTypes)
            .filter((ty) => ty !== 'NUM_ATTRIBUTES')
            .reduce((a, b) => {
              a[AttributeTypes[b]] = true;
              return a;
            }, []);
      });
    // Override some operations where we don't want to copy:
    model.copyAttributeFlags[AttributeCopyOperations.COPYTUPLE][
      AttributeTypes.GLOBALIDS
    ] = false;
    model.copyAttributeFlags[AttributeCopyOperations.INTERPOLATE][
      AttributeTypes.GLOBALIDS
    ] = false;
    model.copyAttributeFlags[AttributeCopyOperations.COPYTUPLE][
      AttributeTypes.PEDIGREEIDS
    ] = false;
  };
 
  publicAPI.initialize = macro.chain(
    publicAPI.initialize,
    publicAPI.initializeAttributeCopyFlags
  );
 
  // Process dataArrays if any
  Iif (model.dataArrays && Object.keys(model.dataArrays).length) {
    Object.keys(model.dataArrays).forEach((name) => {
      if (
        !model.dataArrays[name].ref &&
        model.dataArrays[name].type === 'vtkDataArray'
      ) {
        publicAPI.addArray(vtkDataArray.newInstance(model.dataArrays[name]));
      }
    });
  }
 
  const superShallowCopy = publicAPI.shallowCopy;
  publicAPI.shallowCopy = (other, debug) => {
    superShallowCopy(other, debug);
    model.arrays = other.getArrays().map((arr) => {
      const arrNew = arr.newClone();
      arrNew.shallowCopy(arr, debug);
      return { data: arrNew };
    });
  };
 
  publicAPI.initializeAttributeCopyFlags();
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  activeScalars: -1,
  activeVectors: -1,
  activeTensors: -1,
  activeNormals: -1,
  activeTCoords: -1,
  activeGlobalIds: -1,
  activePedigreeIds: -1,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  vtkFieldData.extend(publicAPI, model, initialValues);
  macro.setGet(publicAPI, model, [
    'activeScalars',
    'activeNormals',
    'activeTCoords',
    'activeVectors',
    'activeTensors',
    'activeGlobalIds',
    'activePedigreeIds',
  ]);
 
  Iif (!model.arrays) {
    model.arrays = {};
  }
 
  // Object specific methods
  vtkDataSetAttributes(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkDataSetAttributes');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...Constants };