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

17.88% Statements 27/151
0% Branches 0/83
9.52% Functions 2/21
18.49% Lines 27/146

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                        7x                   7x                                                         7x     7x                       7x                 7x   7x                 7x   7x           7x                   7x                               7x                                                                                                   7x                                                                                                       7x                                                                         7x                 7x               7x 7x                 7x   7x                               1x                     7x   7x 7x 7x     7x 7x        
import * as macro from 'vtk.js/Sources/macros';
import vtkProp from 'vtk.js/Sources/Rendering/Core/Prop';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import vtkScalarsToColors from 'vtk.js/Sources/Common/Core/ScalarsToColors';
import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox';
 
// ----------------------------------------------------------------------------
// vtkAbstractRepresentationProxy methods
// ----------------------------------------------------------------------------
 
function vtkAbstractRepresentationProxy(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkAbstractRepresentationProxy');
 
  function updateConnectivity() {
    if (model.input) {
      for (let i = 0; i < model.sourceDependencies.length; ++i) {
        model.sourceDependencies[i].setInputData(model.input.getDataset());
      }
    }
  }
 
  publicAPI.setInput = (source) => {
    if (model.sourceSubscription) {
      model.sourceSubscription.unsubscribe();
      model.sourceSubscription = null;
    }
    publicAPI.gcPropertyLinks('source');
    model.input = source;
    publicAPI.updateColorByDomain();
 
    if (model.input) {
      updateConnectivity();
      model.sourceSubscription =
        model.input.onDatasetChange(updateConnectivity);
    }
 
    // Allow dynamic registration of links at the source level
    if (model.links) {
      for (let i = 0; i < model.links.length; i++) {
        const { link, property, persistent, updateOnBind, type } =
          model.links[i];
        if (type === undefined || type === 'source') {
          const sLink = source.getPropertyLink(link, persistent);
          publicAPI.registerPropertyLinkForGC(sLink, 'source');
          sLink.bind(publicAPI, property, updateOnBind);
        }
      }
    }
  };
 
  publicAPI.getInputDataSet = () =>
    model.input ? model.input.getDataset() : null;
 
  publicAPI.getDataArray = (arrayName, arrayLocation) => {
    const [selectedArray, selectedLocation] = publicAPI.getColorBy();
    const ds = publicAPI.getInputDataSet();
    const fields = ds
      ? ds.getReferenceByName(arrayLocation || selectedLocation)
      : null;
    const array = fields
      ? fields.getArrayByName(arrayName || selectedArray)
      : null;
    return array;
  };
 
  publicAPI.getLookupTableProxy = (arrayName) => {
    const arrayNameToUse = arrayName || publicAPI.getColorBy()[0];
    if (arrayNameToUse) {
      return model.proxyManager.getLookupTable(arrayNameToUse);
    }
    return null;
  };
 
  // In place edits, no need to re-assign it...
  publicAPI.setLookupTableProxy = () => {};
 
  publicAPI.getPiecewiseFunctionProxy = (arrayName) => {
    const arrayNameToUse = arrayName || publicAPI.getColorBy()[0];
    if (arrayNameToUse) {
      return model.proxyManager.getPiecewiseFunction(arrayNameToUse);
    }
    return null;
  };
 
  // In place edits, no need to re-assign it...
  publicAPI.setPiecewiseFunctionProxy = () => {};
 
  publicAPI.rescaleTransferFunctionToDataRange = (n, l, c = -1) => {
    const array = publicAPI.getDataArray(n, l);
    const dataRange = array.getRange(c);
    model.proxyManager.rescaleTransferFunctionToDataRange(n, dataRange);
  };
 
  publicAPI.isVisible = () => {
    if (model.actors.length) {
      return model.actors[0].getVisibility();
    }
    if (model.volumes.length) {
      return model.volumes[0].getVisibility();
    }
    return false;
  };
 
  publicAPI.setVisibility = (visible) => {
    let change = 0;
    let count = model.actors.length;
    while (count--) {
      change += model.actors[count].setVisibility(visible);
    }
    count = model.volumes.length;
    while (count--) {
      change += model.volumes[count].setVisibility(visible);
    }
 
    if (change) {
      publicAPI.modified();
    }
  };
 
  publicAPI.setColorBy = (arrayName, arrayLocation, componentIndex = -1) => {
    let colorMode = vtkMapper.ColorMode.DEFAULT;
    let scalarMode = vtkMapper.ScalarMode.DEFAULT;
    const colorByArrayName = arrayName;
    const activeArray = publicAPI.getDataArray(arrayName, arrayLocation);
    const scalarVisibility = !!activeArray;
    const lookupTable = arrayName
      ? publicAPI.getLookupTableProxy(arrayName).getLookupTable()
      : null;
 
    if (lookupTable) {
      if (componentIndex === -1) {
        lookupTable.setVectorModeToMagnitude();
      } else {
        lookupTable.setVectorModeToComponent();
        lookupTable.setVectorComponent(componentIndex);
      }
    }
 
    if (scalarVisibility) {
      colorMode = vtkMapper.ColorMode.MAP_SCALARS;
      scalarMode =
        arrayLocation === 'pointData'
          ? vtkMapper.ScalarMode.USE_POINT_FIELD_DATA
          : vtkMapper.ScalarMode.USE_CELL_FIELD_DATA;
 
      if (model.mapper.setLookupTable) {
        model.mapper.setLookupTable(lookupTable);
      }
      if (model.rescaleOnColorBy) {
        publicAPI.rescaleTransferFunctionToDataRange(
          arrayName,
          arrayLocation,
          componentIndex
        );
      }
    }
 
    // Not all mappers have those fields
    model.mapper.set(
      {
        colorByArrayName,
        colorMode,
        scalarMode,
        scalarVisibility,
      },
      true
    );
  };
 
  publicAPI.getColorBy = () => {
    if (!model.mapper.getColorByArrayName) {
      const ds = publicAPI.getInputDataSet();
      if (ds.getPointData().getScalars()) {
        return [ds.getPointData().getScalars().getName(), 'pointData', -1];
      }
      if (ds.getCellData().getScalars()) {
        return [ds.getCellData().getScalars().getName(), 'cellData', -1];
      }
      if (ds.getPointData().getNumberOfArrays()) {
        return [
          ds.getPointData().getArrayByIndex(0).getName(),
          'pointData',
          -1,
        ];
      }
      if (ds.getCellData().getNumberOfArrays()) {
        return [ds.getCellData().getArrayByIndex(0).getName(), 'cellData', -1];
      }
      return [];
    }
    const result = [];
    const { colorByArrayName, colorMode, scalarMode, scalarVisibility } =
      model.mapper.get(
        'colorByArrayName',
        'colorMode',
        'scalarMode',
        'scalarVisibility'
      );
 
    if (scalarVisibility && colorByArrayName) {
      result.push(colorByArrayName);
      result.push(
        scalarMode === vtkMapper.ScalarMode.USE_POINT_FIELD_DATA
          ? 'pointData'
          : 'cellData'
      );
    }
 
    if (colorMode === vtkMapper.ColorMode.MAP_SCALARS && colorByArrayName) {
      const lut = publicAPI
        .getLookupTableProxy(colorByArrayName)
        .getLookupTable();
      const componentIndex =
        lut.getVectorMode() === vtkScalarsToColors.VectorMode.MAGNITUDE
          ? -1
          : lut.getVectorComponent();
      result.push(componentIndex);
    }
    return result;
  };
 
  publicAPI.listDataArrays = () => {
    const arrayList = [];
    if (!model.input) {
      return arrayList;
    }
 
    const dataset = publicAPI.getInputDataSet();
 
    // Point data
    const pointData = dataset.getPointData();
    let size = pointData.getNumberOfArrays();
    for (let idx = 0; idx < size; idx++) {
      const array = pointData.getArrayByIndex(idx);
      arrayList.push({
        name: array.getName(),
        location: 'pointData',
        numberOfComponents: array.getNumberOfComponents(),
        dataRange: array.getRange(),
      });
    }
 
    // Cell data
    const cellData = dataset.getCellData();
    size = cellData.getNumberOfArrays();
    for (let idx = 0; idx < size; idx++) {
      const array = cellData.getArrayByIndex(idx);
      arrayList.push({
        name: array.getName(),
        location: 'cellData',
        numberOfComponents: array.getNumberOfComponents(),
        dataRange: array.getRange(),
      });
    }
 
    return arrayList;
  };
 
  publicAPI.updateColorByDomain = () => {
    publicAPI.updateProxyProperty('colorBy', {
      domain: {
        arrays: publicAPI.listDataArrays(),
        solidColor: !model.disableSolidColor,
      },
    });
  };
 
  publicAPI.delete = macro.chain(() => {
    if (model.sourceSubscription) {
      model.sourceSubscription.unsubscribe();
      model.sourceSubscription = null;
    }
  }, publicAPI.delete);
 
  // Fast getter for rendering
  const nestedProps = [];
  const bbox = [...vtkBoundingBox.INIT_BOUNDS];
 
  function handleProp(prop) {
    if (prop) {
      vtkBoundingBox.addBounds(bbox, prop.getBounds());
      nestedProps.push(prop);
    }
  }
 
  publicAPI.getNestedProps = () => nestedProps;
 
  publicAPI.getBounds = () => {
    if (model.boundMTime < model.mtime) {
      model.boundMTime = model.mtime;
      vtkBoundingBox.reset(bbox);
      nestedProps.length = 0;
      model.actors.forEach(handleProp);
      model.volumes.forEach(handleProp);
    }
    return bbox;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  boundMTime: 0,
  actors: [],
  volumes: [],
  sourceDependencies: [],
  rescaleOnColorBy: true,
};
 
// ----------------------------------------------------------------------------
 
function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  vtkProp.extend(publicAPI, model, initialValues);
  macro.setGet(publicAPI, model, ['rescaleOnColorBy']);
  macro.get(publicAPI, model, ['input', 'mapper', 'actors', 'volumes']);
 
  // Object specific methods
  vtkAbstractRepresentationProxy(publicAPI, model);
  macro.proxy(publicAPI, model);
}
 
export default { extend };