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

26.56% Statements 17/64
11.11% Branches 1/9
18.18% Functions 2/11
28.33% Lines 17/60

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                                    1x     1x 1x                 1x               1x           1x                       1x                                                                                                                           1x             1x                             1x     1x 1x 1x               1x 1x     1x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
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 vtkElevationReader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkElevationReader');
 
  // Create default dataAccessHelper if not available
  if (!model.dataAccessHelper) {
    model.dataAccessHelper = DataAccessHelper.get('http');
  }
 
  // Internal method to fetch Array
  function fetchCSV(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) =>
    fetchCSV(model.url, options).then((csv) => {
      publicAPI.parseAsText(csv);
      return true;
    });
 
  publicAPI.parseAsText = (csv) => {
    model.csv = csv;
    model.elevation = [];
 
    // Parse data
    const lines = model.csv.split('\n');
    lines.forEach((line, lineIdx) => {
      model.elevation.push(line.split(',').map((str) => Number(str)));
    });
    publicAPI.modified();
  };
 
  publicAPI.requestData = (inData, outData) => {
    const polydata = vtkPolyData.newInstance();
    polydata.getPoints().setData(new Float32Array(0, 0, 0, 1, 1, 1), 3);
 
    if (model.elevation) {
      const jSize = model.elevation.length;
      const iSize = model.elevation[0].length;
 
      // Handle points and polys
      const points = polydata.getPoints();
      points.setNumberOfPoints(iSize * jSize, 3);
      const pointValues = points.getData();
 
      const polys = vtkCellArray.newInstance({
        size: 5 * (iSize - 1) * (jSize - 1),
      });
      polydata.setPolys(polys);
      const polysValues = polys.getData();
      let cellOffset = 0;
 
      // Texture coords
      const tcData = new Float32Array(iSize * jSize * 2);
      const tcoords = vtkDataArray.newInstance({
        numberOfComponents: 2,
        values: tcData,
        name: 'TextureCoordinates',
      });
      polydata.getPointData().setTCoords(tcoords);
 
      for (let j = 0; j < jSize; j++) {
        for (let i = 0; i < iSize; i++) {
          const offsetIdx = j * iSize + i;
          const offsetPt = 3 * offsetIdx;
 
          // Fill points coordinates
          pointValues[offsetPt + 0] =
            model.origin[0] + i * model.xSpacing * model.xDirection;
          pointValues[offsetPt + 1] =
            model.origin[1] + j * model.ySpacing * model.yDirection;
          pointValues[offsetPt + 2] =
            model.origin[2] + model.elevation[j][i] * model.zScaling;
 
          // fill in tcoords
          tcData[offsetIdx * 2] = i / (iSize - 1.0);
          tcData[offsetIdx * 2 + 1] = 1.0 - j / (jSize - 1.0);
 
          // Fill polys
          if (i > 0 && j > 0) {
            polysValues[cellOffset++] = 4;
            polysValues[cellOffset++] = offsetIdx;
            polysValues[cellOffset++] = offsetIdx - 1;
            polysValues[cellOffset++] = offsetIdx - 1 - iSize;
            polysValues[cellOffset++] = offsetIdx - iSize;
          }
        }
      }
    }
 
    model.output[0] = polydata;
  };
 
  // return Busy state
  publicAPI.isBusy = () => !!model.requestCount;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  origin: [0, 0, 0],
  xSpacing: 1,
  ySpacing: 1,
  zScaling: 1,
  xDirection: 1,
  yDirection: -1,
  requestCount: 0,
  // 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',
    'xSpacing',
    'ySpacing',
    'zScaling',
    'xDirection',
    'yDirection',
  ]);
  macro.algo(publicAPI, model, 0, 1);
  macro.event(publicAPI, model, 'busy');
 
  // Object methods
  vtkElevationReader(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkElevationReader');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };