Skip to content

Commit

Permalink
Merge pull request #461 from floryst/vti_vtp_new_api
Browse files Browse the repository at this point in the history
XMLReader new API
  • Loading branch information
jourdain authored Dec 13, 2017
2 parents bed0aa0 + 7917bee commit b51cce7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
15 changes: 5 additions & 10 deletions Examples/Applications/GeometryViewer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -304,10 +302,7 @@ function createPipeline(fileName, parsedFileContents) {
function loadFile(file) {
const reader = new FileReader();
reader.onload = function onLoad(e) {
const prefixRegex = /^\s*<AppendedData\s+encoding="raw">\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);
}
Expand Down Expand Up @@ -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());
});
}
Expand Down
15 changes: 5 additions & 10 deletions Examples/Applications/VolumeViewer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
Expand All @@ -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();
Expand Down Expand Up @@ -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*<AppendedData\s+encoding="raw">\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 {
Expand All @@ -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);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/IO/XML/XMLImageDataReader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function vtkXMLImageDataReader(publicAPI, model) {
};

publicAPI.requestData = (inData, outData) => {
publicAPI.parse(model.parseData);
publicAPI.parseArrayBuffer(model.rawDataBuffer);
};
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/IO/XML/XMLPolyDataReader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function vtkXMLPolyDataReader(publicAPI, model) {
};

publicAPI.requestData = (inData, outData) => {
publicAPI.parse(model.parseData);
publicAPI.parseArrayBuffer(model.rawDataBuffer);
};
}

Expand Down
20 changes: 14 additions & 6 deletions Sources/IO/XML/XMLReader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,13 @@ function stringToXML(xmlStr) {
return (new DOMParser()).parseFromString(xmlStr, 'application/xml');
}

function extractAppendedData(buffer) {
// search for appended data tag
const prefixRegex = /^\s*<AppendedData\s+encoding="raw">\s*_/m;
const suffixRegex = /\n\s*<\/AppendedData>/m;
return BinaryHelper.extractBinary(buffer, prefixRegex, suffixRegex);
}

// ----------------------------------------------------------------------------

const TYPED_ARRAY = {
Expand Down Expand Up @@ -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 } = extractAppendedData(arrayBuffer);
model.rawDataBuffer = arrayBuffer;
model.binaryBuffer = binaryBuffer;

// Parse data here...
Expand Down Expand Up @@ -423,7 +431,7 @@ function vtkXMLReader(publicAPI, model) {
};

publicAPI.requestData = (inData, outData) => {
publicAPI.parse(model.parseData, model.binaryBuffer);
publicAPI.parseArrayBuffer(model.rawDataBuffer);
};
}

Expand Down

0 comments on commit b51cce7

Please sign in to comment.