From 2b1d11a40c83aef5893f266a1f9116cf422fb819 Mon Sep 17 00:00:00 2001 From: Forrest Li Date: Wed, 13 Dec 2017 13:46:16 -0500 Subject: [PATCH 1/2] fix(XMLReader): vti/vtp readers expose parseArrayBuffer() The VTI/VTP readers changed from having a .parse() method to having a .parseArrayBuffer() method to be explicit about what is an acceptable parameter type. --- Examples/Applications/GeometryViewer/index.js | 15 +++++--------- Examples/Applications/VolumeViewer/index.js | 15 +++++--------- Sources/IO/XML/XMLImageDataReader/index.js | 2 +- Sources/IO/XML/XMLPolyDataReader/index.js | 2 +- Sources/IO/XML/XMLReader/index.js | 20 +++++++++++++------ 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Examples/Applications/GeometryViewer/index.js b/Examples/Applications/GeometryViewer/index.js index 9fd557465ac..bc98ff98b27 100644 --- a/Examples/Applications/GeometryViewer/index.js +++ b/Examples/Applications/GeometryViewer/index.js @@ -14,8 +14,6 @@ import vtkURLExtract from 'vtk.js/Sources/Common/Core/URLExtract'; import vtkXMLPolyDataReader from 'vtk.js/Sources/IO/XML/XMLPolyDataReader'; import { ColorMode, ScalarMode } from 'vtk.js/Sources/Rendering/Core/Mapper/Constants'; -import BinaryHelper from 'vtk.js/Sources/IO/Core/BinaryHelper'; - import style from './GeometryViewer.mcss'; import icon from '../../../Documentation/content/icon/favicon-96x96.png'; @@ -113,7 +111,7 @@ function createViewer(container) { // ---------------------------------------------------------------------------- -function createPipeline(fileName, parsedFileContents) { +function createPipeline(fileName, fileContents) { // Create UI const presetSelector = document.createElement('select'); presetSelector.setAttribute('class', selectorClass); @@ -158,7 +156,7 @@ function createPipeline(fileName, parsedFileContents) { // VTK pipeline const vtpReader = vtkXMLPolyDataReader.newInstance(); - vtpReader.parse(parsedFileContents.text, parsedFileContents.binaryBuffer); + vtpReader.parseArrayBuffer(fileContents); const lookupTable = vtkColorTransferFunction.newInstance(); const source = vtpReader.getOutputData(0); @@ -304,10 +302,7 @@ function createPipeline(fileName, parsedFileContents) { function loadFile(file) { const reader = new FileReader(); reader.onload = function onLoad(e) { - const prefixRegex = /^\s*\s*_/m; - const suffixRegex = /\n\s*<\/AppendedData>/m; - const result = BinaryHelper.extractBinary(reader.result, prefixRegex, suffixRegex); - createPipeline(file.name, result); + createPipeline(file.name, reader.result); }; reader.readAsArrayBuffer(file); } @@ -335,10 +330,10 @@ export function load(container, options) { progressContainer.innerHTML = `Loading ${percent}%`; }; - HttpDataAccessHelper.fetchText({}, options.fileURL, { progressCallback }).then((txt) => { + HttpDataAccessHelper.fetchBinary(options.fileURL, { progressCallback }).then((binary) => { container.removeChild(progressContainer); createViewer(container); - createPipeline(defaultName, { text: txt }); + createPipeline(defaultName, binary); updateCamera(renderer.getActiveCamera()); }); } diff --git a/Examples/Applications/VolumeViewer/index.js b/Examples/Applications/VolumeViewer/index.js index 2f4d402d608..b6e64df215f 100644 --- a/Examples/Applications/VolumeViewer/index.js +++ b/Examples/Applications/VolumeViewer/index.js @@ -15,8 +15,6 @@ import vtkVolume from 'vtk.js/Sources/Rendering/Core/Volume'; import vtkVolumeMapper from 'vtk.js/Sources/Rendering/Core/VolumeMapper'; import vtkXMLImageDataReader from 'vtk.js/Sources/IO/XML/XMLImageDataReader'; -import BinaryHelper from 'vtk.js/Sources/IO/Core/BinaryHelper'; - import style from './VolumeViewer.mcss'; let autoInit = true; @@ -49,7 +47,7 @@ function preventDefaults(e) { // ---------------------------------------------------------------------------- -function createViewer(rootContainer, parsedFileContents, options) { +function createViewer(rootContainer, fileContents, options) { const background = options.background ? options.background.split(',').map(s => Number(s)) : [0, 0, 0]; const containerStyle = options.containerStyle; const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ background, rootContainer, containerStyle }); @@ -58,7 +56,7 @@ function createViewer(rootContainer, parsedFileContents, options) { renderWindow.getInteractor().setDesiredUpdateRate(15); const vtiReader = vtkXMLImageDataReader.newInstance(); - vtiReader.parse(parsedFileContents.text, parsedFileContents.binaryBuffer); + vtiReader.parseArrayBuffer(fileContents); const source = vtiReader.getOutputData(0); const mapper = vtkVolumeMapper.newInstance(); @@ -146,10 +144,7 @@ export function load(container, options) { if (options.ext === 'vti') { const reader = new FileReader(); reader.onload = function onLoad(e) { - const prefixRegex = /^\s*\s*_/m; - const suffixRegex = /\n\s*<\/AppendedData>/m; - const result = BinaryHelper.extractBinary(reader.result, prefixRegex, suffixRegex); - createViewer(container, result, options); + createViewer(container, reader.result, options); }; reader.readAsArrayBuffer(options.file); } else { @@ -165,9 +160,9 @@ export function load(container, options) { progressContainer.innerHTML = `Loading ${percent}%`; }; - HttpDataAccessHelper.fetchText({}, options.fileURL, { progressCallback }).then((txt) => { + HttpDataAccessHelper.fetchBinary(options.fileURL, { progressCallback }).then((binary) => { container.removeChild(progressContainer); - createViewer(container, { text: txt }, options); + createViewer(container, binary, options); }); } } diff --git a/Sources/IO/XML/XMLImageDataReader/index.js b/Sources/IO/XML/XMLImageDataReader/index.js index 4d9a42341fd..9c53457e7c6 100644 --- a/Sources/IO/XML/XMLImageDataReader/index.js +++ b/Sources/IO/XML/XMLImageDataReader/index.js @@ -44,7 +44,7 @@ function vtkXMLImageDataReader(publicAPI, model) { }; publicAPI.requestData = (inData, outData) => { - publicAPI.parse(model.parseData); + publicAPI.parseArrayBuffer(model.rawDataBuffer); }; } diff --git a/Sources/IO/XML/XMLPolyDataReader/index.js b/Sources/IO/XML/XMLPolyDataReader/index.js index 3a3441c6fba..b3e20329837 100644 --- a/Sources/IO/XML/XMLPolyDataReader/index.js +++ b/Sources/IO/XML/XMLPolyDataReader/index.js @@ -66,7 +66,7 @@ function vtkXMLPolyDataReader(publicAPI, model) { }; publicAPI.requestData = (inData, outData) => { - publicAPI.parse(model.parseData); + publicAPI.parseArrayBuffer(model.rawDataBuffer); }; } diff --git a/Sources/IO/XML/XMLReader/index.js b/Sources/IO/XML/XMLReader/index.js index eef00d63f0b..89d8c4464dc 100644 --- a/Sources/IO/XML/XMLReader/index.js +++ b/Sources/IO/XML/XMLReader/index.js @@ -4,6 +4,7 @@ import { toByteArray } from 'base64-js'; import DataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper'; import macro from 'vtk.js/Sources/macro'; import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; +import BinaryHelper from 'vtk.js/Sources/IO/Core/BinaryHelper'; // ---------------------------------------------------------------------------- // Global methods @@ -18,6 +19,13 @@ function stringToXML(xmlStr) { return (new DOMParser()).parseFromString(xmlStr, 'application/xml'); } +function parseArrayBuffer(buffer) { + // search for appended data tag + const prefixRegex = /^\s*\s*_/m; + const suffixRegex = /\n\s*<\/AppendedData>/m; + return BinaryHelper.extractBinary(buffer, prefixRegex, suffixRegex); +} + // ---------------------------------------------------------------------------- const TYPED_ARRAY = { @@ -282,18 +290,18 @@ function vtkXMLReader(publicAPI, model) { return promise; }; - publicAPI.parse = (content, binaryBuffer) => { - if (!content) { + publicAPI.parseArrayBuffer = (arrayBuffer) => { + if (!arrayBuffer) { return; } - if (content !== model.parseData) { + if (arrayBuffer !== model.rawDataBuffer) { publicAPI.modified(); } else { return; } - model.parseData = content; - // TODO maybe name as "appendDataBuffer" + const { text: content, binaryBuffer } = parseArrayBuffer(arrayBuffer); + model.rawDataBuffer = arrayBuffer; model.binaryBuffer = binaryBuffer; // Parse data here... @@ -423,7 +431,7 @@ function vtkXMLReader(publicAPI, model) { }; publicAPI.requestData = (inData, outData) => { - publicAPI.parse(model.parseData, model.binaryBuffer); + publicAPI.parseArrayBuffer(model.rawDataBuffer); }; } From 7917bee4988dcb012bf3e2f203e3ca4f5e0a00d9 Mon Sep 17 00:00:00 2001 From: Forrest Li Date: Wed, 13 Dec 2017 14:07:36 -0500 Subject: [PATCH 2/2] fix(XMLReader): rename parseArrayBuffer to extractAppendedData --- Sources/IO/XML/XMLReader/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/IO/XML/XMLReader/index.js b/Sources/IO/XML/XMLReader/index.js index 89d8c4464dc..7ad4a6ad22c 100644 --- a/Sources/IO/XML/XMLReader/index.js +++ b/Sources/IO/XML/XMLReader/index.js @@ -19,7 +19,7 @@ function stringToXML(xmlStr) { return (new DOMParser()).parseFromString(xmlStr, 'application/xml'); } -function parseArrayBuffer(buffer) { +function extractAppendedData(buffer) { // search for appended data tag const prefixRegex = /^\s*\s*_/m; const suffixRegex = /\n\s*<\/AppendedData>/m; @@ -300,7 +300,7 @@ function vtkXMLReader(publicAPI, model) { return; } - const { text: content, binaryBuffer } = parseArrayBuffer(arrayBuffer); + const { text: content, binaryBuffer } = extractAppendedData(arrayBuffer); model.rawDataBuffer = arrayBuffer; model.binaryBuffer = binaryBuffer;