All files / Sources/IO/XML/XMLPolyDataReader index.js

21.87% Statements 7/32
0% Branches 0/5
33.33% Functions 2/6
22.58% Lines 7/31

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                                                                                                                                      1x   1x                                                                                                                                 1x             1x 1x 1x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkXMLReader from 'vtk.js/Sources/IO/XML/XMLReader';
 
// ----------------------------------------------------------------------------
// Global method
// ----------------------------------------------------------------------------
 
function handleArray(
  polydata,
  cellType,
  piece,
  compressor,
  byteOrder,
  headerType,
  binaryBuffer
) {
  const size = Number(piece.getAttribute(`NumberOf${cellType}`));
  if (size > 0) {
    const dataArrayElem = piece
      .getElementsByTagName(cellType)[0]
      .getElementsByTagName('DataArray')[0];
    const { values, numberOfComponents } = vtkXMLReader.processDataArray(
      size,
      dataArrayElem,
      compressor,
      byteOrder,
      headerType,
      binaryBuffer
    );
    polydata[`get${cellType}`]().setData(values, numberOfComponents);
  }
  return size;
}
 
// ----------------------------------------------------------------------------
 
function handleCells(
  polydata,
  cellType,
  piece,
  compressor,
  byteOrder,
  headerType,
  binaryBuffer
) {
  const size = Number(piece.getAttribute(`NumberOf${cellType}`));
  if (size > 0) {
    const values = vtkXMLReader.processCells(
      size,
      piece.getElementsByTagName(cellType)[0],
      compressor,
      byteOrder,
      headerType,
      binaryBuffer
    );
    polydata[`get${cellType}`]().setData(values);
  }
  return size;
}
 
// ----------------------------------------------------------------------------
// vtkXMLPolyDataReader methods
// ----------------------------------------------------------------------------
 
function vtkXMLPolyDataReader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkXMLPolyDataReader');
 
  publicAPI.parseXML = (rootElem, type, compressor, byteOrder, headerType) => {
    const datasetElem = rootElem.getElementsByTagName(model.dataType)[0];
    const pieces = datasetElem.getElementsByTagName('Piece');
    const nbPieces = pieces.length;
 
    for (let outputIndex = 0; outputIndex < nbPieces; outputIndex++) {
      // Create dataset
      const polydata = vtkPolyData.newInstance();
      const piece = pieces[outputIndex];
 
      // Points
      const nbPoints = handleArray(
        polydata,
        'Points',
        piece,
        compressor,
        byteOrder,
        headerType,
        model.binaryBuffer
      );
 
      // Cells
      let nbCells = 0;
      ['Verts', 'Lines', 'Strips', 'Polys'].forEach((cellType) => {
        nbCells += handleCells(
          polydata,
          cellType,
          piece,
          compressor,
          byteOrder,
          headerType,
          model.binaryBuffer
        );
      });
 
      // Fill data
      vtkXMLReader.processFieldData(
        nbPoints,
        piece.getElementsByTagName('PointData')[0],
        polydata.getPointData(),
        compressor,
        byteOrder,
        headerType,
        model.binaryBuffer
      );
      vtkXMLReader.processFieldData(
        nbCells,
        piece.getElementsByTagName('CellData')[0],
        polydata.getCellData(),
        compressor,
        byteOrder,
        headerType,
        model.binaryBuffer
      );
 
      // Add new output
      model.output[outputIndex] = polydata;
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  dataType: 'PolyData',
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
  vtkXMLReader.extend(publicAPI, model, initialValues);
  vtkXMLPolyDataReader(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkXMLPolyDataReader');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };