All files / Sources/IO/Legacy/LegacyAsciiParser index.js

4.31% Statements 5/116
0% Branches 0/31
0% Functions 0/30
4.31% Lines 5/116

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      1x                     1x                           1x                                                         1x                                 1x                                                                                                                                                                                                                                                                                                                                                                                                                                          
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
 
const METHOD_MAPPING = {
  POINTS: 'getPoints',
  VERTICES: 'getVerts',
  LINES: 'getLines',
  TRIANGLE_STRIPS: 'getStrips',
  POLYGONS: 'getPolys',
  POINT_DATA: 'getPointData',
  CELL_DATA: 'getCellData',
  FIELD: 'getFieldData',
};
 
const DATATYPES = {
  bit: Uint8Array,
  unsigned_char: Uint8Array,
  char: Int8Array,
  unsigned_short: Uint16Array,
  short: Int16Array,
  unsigned_int: Uint32Array,
  int: Int32Array,
  unsigned_long: Uint32Array,
  long: Int32Array,
  float: Float32Array,
  double: Float64Array,
};
 
const REGISTER_MAPPING = {
  SCALARS: 'addArray',
  COLOR_SCALARS: 'addArray',
  VECTORS: 'setVectors',
  NORMALS: 'setNormals',
  TEXTURE_COORDINATES: 'setTCoords',
  TENSORS: 'setTensors',
  FIELD: 'addArray',
};
 
function createArrayHandler(array, setData, nbComponents) {
  let offset = 0;
 
  function fillWith(line) {
    line.split(' ').forEach((token) => {
      if (token.length) {
        array[offset++] = Number(token);
      }
    });
    if (offset < array.length) {
      return true;
    }
    setData(array, nbComponents);
    return false;
  }
 
  return fillWith;
}
 
const GENERIC_CELL_HANDLER = {
  init(line, dataModel) {
    const [name, nbCells, nbValues] = line.split(' ');
    const cellArray = dataModel.dataset[METHOD_MAPPING[name]]();
    cellArray.set({ numberOfCells: Number(nbCells) }, true); // Force numberOfCells update
    dataModel.arrayHandler = createArrayHandler(
      new Uint32Array(Number(nbValues)),
      cellArray.setData,
      1
    );
    return true;
  },
  parse(line, dataModel) {
    return dataModel.arrayHandler(line);
  },
};
 
const TYPE_PARSER = {
  DATASET: {
    init(line, datamodel) {
      const type = line.split(' ')[1];
      switch (type) {
        case 'POLYDATA':
          datamodel.dataset = vtkPolyData.newInstance();
          break;
        default:
          console.error(`Dataset of type ${type} not supported`);
      }
      return false;
    },
    parse(line, datamodel) {
      return false;
    },
  },
  POINTS: {
    init(line, dataModel) {
      const [name, size, type] = line.split(' ');
      const array =
        type === 'float'
          ? new Float32Array(3 * Number(size))
          : new Float64Array(3 * Number(size));
      const dataArray = dataModel.dataset.getPoints();
      dataArray.setName(name);
      dataModel.arrayHandler = createArrayHandler(array, dataArray.setData, 3);
      return true;
    },
    parse(line, dataModel) {
      return dataModel.arrayHandler(line);
    },
  },
  METADATA: {
    init(line, dataModel) {
      return true;
    },
    parse(line, dataModel) {
      return !!line.length;
    },
  },
  VERTICES: GENERIC_CELL_HANDLER,
  LINES: GENERIC_CELL_HANDLER,
  TRIANGLE_STRIPS: GENERIC_CELL_HANDLER,
  POLYGONS: GENERIC_CELL_HANDLER,
  POINT_DATA: {
    init(line, dataModel) {
      dataModel.POINT_DATA = Number(line.split(' ')[1]);
      dataModel.activeFieldLocation = 'POINT_DATA';
      return false;
    },
    parse(line, dataModel) {
      return false;
    },
  },
  CELL_DATA: {
    init(line, dataModel) {
      dataModel.CELL_DATA = Number(line.split(' ')[1]);
      dataModel.activeFieldLocation = 'CELL_DATA';
      return false;
    },
    parse(line, dataModel) {
      return false;
    },
  },
  SCALARS: {
    init(line, dataModel) {
      const [type, name, dataType, numComp] = line.split(' ');
      const numOfComp = Number(numComp) > 0 ? Number(numComp) : 1;
      const size = dataModel[dataModel.activeFieldLocation] * numOfComp;
      const array = new DATATYPES[dataType](size);
      const dataArray = vtkDataArray.newInstance({ name, empty: true });
      dataModel.dataset[METHOD_MAPPING[dataModel.activeFieldLocation]]()[
        REGISTER_MAPPING[type]
      ](dataArray);
      dataModel.arrayHandler = createArrayHandler(
        array,
        dataArray.setData,
        numOfComp
      );
      return true;
    },
    parse(line, dataModel) {
      if (line.split(' ')[0] === 'LOOKUP_TABLE') {
        return true;
      }
      return dataModel.arrayHandler(line);
    },
  },
  COLOR_SCALARS: {
    init(line, dataModel) {
      const [type, name, numComp] = line.split(' ');
      const numOfComp = Number(numComp) > 0 ? Number(numComp) : 1;
      const size = dataModel[dataModel.activeFieldLocation] * numOfComp;
      const array = new Uint8Array(size);
      const dataArray = vtkDataArray.newInstance({ name, empty: true });
      dataModel.dataset[METHOD_MAPPING[dataModel.activeFieldLocation]]()[
        REGISTER_MAPPING[type]
      ](dataArray);
      dataModel.arrayHandler = createArrayHandler(
        array,
        dataArray.setData,
        numOfComp
      );
      return true;
    },
    parse(line, dataModel) {
      if (line.split(' ')[0] === 'LOOKUP_TABLE') {
        return true;
      }
      return dataModel.arrayHandler(line);
    },
  },
  VECTORS: {
    init(line, dataModel) {
      const [type, name, dataType] = line.split(' ');
      const size = dataModel[dataModel.activeFieldLocation] * 3;
      const array = new DATATYPES[dataType](size);
      const dataArray = vtkDataArray.newInstance({ name, empty: true });
      dataModel.dataset[METHOD_MAPPING[dataModel.activeFieldLocation]]()[
        REGISTER_MAPPING[type]
      ](dataArray);
      dataModel.arrayHandler = createArrayHandler(array, dataArray.setData, 3);
      return true;
    },
    parse(line, dataModel) {
      return dataModel.arrayHandler(line);
    },
  },
  NORMALS: {
    init(line, dataModel) {
      const [type, name] = line.split(' ');
      const size = dataModel[dataModel.activeFieldLocation] * 3;
      const array = new Float32Array(size);
      const dataArray = vtkDataArray.newInstance({ name, empty: true });
      dataModel.dataset[METHOD_MAPPING[dataModel.activeFieldLocation]]()[
        REGISTER_MAPPING[type]
      ](dataArray);
      dataModel.arrayHandler = createArrayHandler(array, dataArray.setData, 3);
      return true;
    },
    parse(line, dataModel) {
      return dataModel.arrayHandler(line);
    },
  },
  TEXTURE_COORDINATES: {
    init(line, dataModel) {
      const [type, name, numberOfComponents, dataType] = line.split(' ');
      const size =
        dataModel[dataModel.activeFieldLocation] * Number(numberOfComponents);
      const array = new DATATYPES[dataType](size);
      const dataArray = vtkDataArray.newInstance({ name, empty: true });
      dataModel.dataset[METHOD_MAPPING[dataModel.activeFieldLocation]]()[
        REGISTER_MAPPING[type]
      ](dataArray);
      dataModel.arrayHandler = createArrayHandler(array, dataArray.setData, 3);
      return true;
    },
    parse(line, dataModel) {
      return dataModel.arrayHandler(line);
    },
  },
  TENSORS: {
    init(line, dataModel) {
      const [type, name, dataType] = line.split(' ');
      const size = dataModel[dataModel.activeFieldLocation] * 9;
      const array = new DATATYPES[dataType](size);
      const dataArray = vtkDataArray.newInstance({ name, empty: true });
      dataModel.dataset[METHOD_MAPPING[dataModel.activeFieldLocation]]()[
        REGISTER_MAPPING[type]
      ](dataArray);
      dataModel.arrayHandler = createArrayHandler(array, dataArray.setData, 9);
      return true;
    },
    parse(line, dataModel) {
      return dataModel.arrayHandler(line);
    },
  },
};
 
function getParser(line, dataModel) {
  const tokens = line.split(' ');
  return TYPE_PARSER[tokens[0]];
}
 
function parseLegacyASCII(content, dataModel = {}) {
  let parser = null;
  const separatorRegExp = /\r?\n/;
  const separatorRes = separatorRegExp.exec(content);
  const separator = separatorRes !== null ? separatorRes[0] : null;
  content.split(separator).forEach((line, index) => {
    if (index < 2) {
      return;
    }
    if (!parser) {
      parser = getParser(line, dataModel);
      if (!parser) {
        return;
      }
      parser = parser.init(line, dataModel) ? parser : null;
      return;
    }
 
    if (parser && !parser.parse(line, dataModel)) {
      parser = null;
    }
  });
  return dataModel;
}
 
export default {
  parseLegacyASCII,
};