All files / Sources/IO/Misc/JSONNucleoReader index.js

38.46% Statements 15/39
20% Branches 1/5
25% Functions 2/8
38.46% Lines 15/39

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                                1x     1x 1x                 1x               1x           1x                                             1x                       1x               1x     1x 1x 1x 1x     1x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import DataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper';
 
// Enable several sources for DataAccessHelper
import 'vtk.js/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper'; // Just need HTTP
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; // HTTP + gz
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; // html + base64 + zip
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; // zip
 
// ----------------------------------------------------------------------------
// vtkElevationReader methods
// ----------------------------------------------------------------------------
 
function vtkJSONNucleoReader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkJSONNucleoReader');
 
  // Create default dataAccessHelper if not available
  if (!model.dataAccessHelper) {
    model.dataAccessHelper = DataAccessHelper.get('http');
  }
 
  // Internal method to fetch Array
  function fetchText(url, options) {
    return model.dataAccessHelper.fetchText(publicAPI, url, options);
  }
 
  // Set DataSet url
  publicAPI.setUrl = (url, options) => {
    model.url = url;
 
    // Fetch metadata
    return publicAPI.loadData(options);
  };
 
  // Fetch the actual data arrays
  publicAPI.loadData = (options) =>
    fetchText(model.url, options).then((csv) => {
      publicAPI.parseAsText(csv);
      return true;
    });
 
  publicAPI.parseAsText = (jsonAsTxt) => {
    const { vertices, indices } = JSON.parse(jsonAsTxt);
    const nbIndices = indices.length;
    const nbTriangles = nbIndices / 3;
    const nbCellsValues = nbTriangles + nbIndices;
 
    model.points = Float32Array.from(vertices);
    model.polys =
      nbCellsValues < 65535
        ? new Uint16Array(nbCellsValues)
        : new Uint32Array(nbCellsValues);
    let srcOffset = 0;
    let destOffset = 0;
    while (destOffset < model.polys.length) {
      model.polys[destOffset++] = 3;
      model.polys[destOffset++] = indices[srcOffset++];
      model.polys[destOffset++] = indices[srcOffset++];
      model.polys[destOffset++] = indices[srcOffset++];
    }
 
    publicAPI.modified();
  };
 
  publicAPI.requestData = (inData, outData) => {
    const polydata = vtkPolyData.newInstance();
    polydata.getPoints().setData(model.points, 3);
    polydata.getPolys().setData(model.polys);
    model.output[0] = polydata;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  // dataAccessHelper: null,
  // url: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, ['url']);
  macro.setGet(publicAPI, model, ['dataAccessHelper']);
  macro.algo(publicAPI, model, 0, 1);
 
  // Object methods
  vtkJSONNucleoReader(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkJSONNucleoReader');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };