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';
import 'vtk.js/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper';
function vtkElevationReader(publicAPI, model) { model.classHierarchy.push('vtkElevationReader');
if (!model.dataAccessHelper) { model.dataAccessHelper = DataAccessHelper.get('http'); }
function fetchCSV(url, options) { return model.dataAccessHelper.fetchText(publicAPI, url, options); }
publicAPI.setUrl = (url, options) => { model.url = url;
return publicAPI.loadData(options); };
publicAPI.loadData = (options) => fetchCSV(model.url, options).then((csv) => { publicAPI.parseAsText(csv); return true; });
publicAPI.parseAsText = (csv) => { model.csv = csv; model.elevation = [];
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;
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;
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;
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;
tcData[offsetIdx * 2] = i / (iSize - 1.0); tcData[offsetIdx * 2 + 1] = 1.0 - j / (jSize - 1.0);
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; };
publicAPI.isBusy = () => !!model.requestCount; }
const DEFAULT_VALUES = { origin: [0, 0, 0], xSpacing: 1, ySpacing: 1, zScaling: 1, xDirection: 1, yDirection: -1, requestCount: 0, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
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');
vtkElevationReader(publicAPI, model); }
export const newInstance = macro.newInstance(extend, 'vtkElevationReader');
export default { newInstance, extend };
|