import 'vtk.js/Sources/Common/DataModel/ImageData'; import 'vtk.js/Sources/Common/DataModel/PolyData';
import vtk from 'vtk.js/Sources/vtk'; import macro from 'vtk.js/Sources/macros'; import DataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper'; import HttpDataSetReader from '../HttpDataSetReader';
import 'vtk.js/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper';
const HTTP_DATA_ACCESS = DataAccessHelper.get('http');
function processDataSet(publicAPI, model, dataset, resolve, reject, loadData) { model.readers = [];
const pendingPromises = []; dataset.series.forEach((timeStep) => { const newReader = HttpDataSetReader.newInstance({ fetchGzip: model.fetchGzip, dataAccessHelper: model.dataAccessHelper, }); pendingPromises.push( newReader.setUrl(`${model.baseURL}/${timeStep.url}`, { loadData }) ); model.readers.push({ timeStep: timeStep.timeStep, reader: newReader, }); });
return Promise.all(pendingPromises).then( () => { const range = publicAPI.getTimeRange(); if (range && range.length !== 0) { publicAPI.setUpdateTimeStep(range[0]); } resolve(publicAPI); }, (error) => { reject(error); } ); }
function vtkHttpDataSetSeriesReader(publicAPI, model) { model.classHierarchy.push('vtkHttpDataSetSeriesReader');
model.output[0] = vtk({ vtkClass: 'vtkPolyData' });
if (!model.dataAccessHelper) { model.dataAccessHelper = HTTP_DATA_ACCESS; }
model.currentReader = null;
publicAPI.updateMetaData = (loadData = false) => { if (model.compression === 'zip') { return new Promise((resolve, reject) => { HTTP_DATA_ACCESS.fetchBinary(model.baseURL).then((zipContent) => { model.dataAccessHelper = DataAccessHelper.get('zip', { zipContent, callback: (zip) => { model.baseURL = ''; model.dataAccessHelper.fetchJSON(publicAPI, 'index.json').then( (dataset) => { processDataSet( publicAPI, model, dataset, resolve, reject, loadData ); }, (error) => { reject(error); } ); }, }); }); }); }
return new Promise((resolve, reject) => { model.dataAccessHelper.fetchJSON(publicAPI, model.url).then( (dataset) => processDataSet(publicAPI, model, dataset, resolve, reject, loadData), (error) => { reject(error); } ); }); };
publicAPI.setUrl = (url, options = {}) => { if (!url.endsWith('index.json') && !options.fullpath) { model.baseURL = url; model.url = `${url}/index.json`; } else { model.url = url;
const path = url.split('/'); path.pop(); model.baseURL = path.join('/'); }
model.compression = options.compression;
return publicAPI.updateMetaData(options.loadData); };
publicAPI.getTimeSteps = () => model.readers.map((reader) => reader.timeStep).sort();
publicAPI.getTimeRange = () => { const timeSteps = publicAPI.getTimeSteps(); const length = timeSteps.length; if (length >= 1) { return [timeSteps[0], timeSteps[length - 1]]; } return []; };
publicAPI.setUpdateTimeStep = (timeStep) => { const reader = model.readers.reduce((newReader, currentReader) => { if (currentReader.timeStep <= timeStep) { return currentReader; } return newReader; });
if (reader === undefined) { return; }
model.currentReader = reader.reader; model.output[0] = model.currentReader.getOutputData(); model.output[0].modified(); publicAPI.modified(); };
publicAPI.requestData = (inData, outData) => { if (model.currentReader) { outData[0] = model.currentReader.getOutputData(); } };
publicAPI.enableArray = (location, name, enable = true) => { if (model.currentReader) { model.currentReader.reader.enableArray(location, name, enable); } };
publicAPI.loadData = () => { if (model.currentReader) { model.currentReader.reader.loadData(); } }; }
const DEFAULT_VALUES = { fetchGzip: false, url: null, baseURL: null, dataAccessHelper: null, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
macro.obj(publicAPI, model); macro.get(publicAPI, model, ['url', 'baseURL']); macro.set(publicAPI, model, ['dataAccessHelper']); macro.algo(publicAPI, model, 0, 1);
vtkHttpDataSetSeriesReader(publicAPI, model); }
export const newInstance = macro.newInstance( extend, 'vtkHttpDataSetSeriesReader' );
export default { newInstance, extend };
|