JSONNucleoReader

Introduction

vtkJSONNucleoReader reader.

Methods

extend

Method used to decorate a given object (publicAPI+model) with vtkJSONNucleoReader characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues IJSONNucleoReaderInitialValues No (default: {})

getBaseURL

getDataAccessHelper

getUrl

Get the url of the object to load.

loadData

Load the object data.

Argument Type Required Description
options IJSONNucleoReaderOptions No

newInstance

Method used to create a new instance of vtkJSONNucleoReader

Argument Type Required Description
initialValues IJSONNucleoReaderInitialValues No for pre-setting some of its content

parseAsText

Parse data as text.

Argument Type Required Description
jsonAsTxt String Yes The content to parse.

requestData

Argument Type Required Description
inData Yes
outData Yes

setDataAccessHelper

Argument Type Required Description
dataAccessHelper Yes

setUrl

Set the url of the object to load.

Argument Type Required Description
url String Yes the url of the object to load.
option IJSONNucleoReaderOptions No The JSONNucleo reader options.

Source

index.d.ts
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
import HtmlDataAccessHelper from '../../Core/DataAccessHelper/HtmlDataAccessHelper';
import HttpDataAccessHelper from '../../Core/DataAccessHelper/HttpDataAccessHelper';
import JSZipDataAccessHelper from '../../Core/DataAccessHelper/JSZipDataAccessHelper';
import LiteHttpDataAccessHelper from '../../Core/DataAccessHelper/LiteHttpDataAccessHelper';

interface IJSONNucleoReaderOptions {
binary?: boolean;
compression?: string;
progressCallback?: any;
}

/**
*
*/
export interface IJSONNucleoReaderInitialValues {}

type vtkJSONNucleoReaderBase = vtkObject &
Omit<
vtkAlgorithm,
| 'getInputData'
| 'setInputData'
| 'setInputConnection'
| 'getInputConnection'
| 'addInputConnection'
| 'addInputData'
>;

export interface vtkJSONNucleoReader extends vtkJSONNucleoReaderBase {
/**
*
*/
getBaseURL(): string;

/**
*
*/
getDataAccessHelper():
| HtmlDataAccessHelper
| HttpDataAccessHelper
| JSZipDataAccessHelper
| LiteHttpDataAccessHelper;

/**
* Get the url of the object to load.
*/
getUrl(): string;

/**
* Load the object data.
* @param {IJSONNucleoReaderOptions} [options]
*/
loadData(options?: IJSONNucleoReaderOptions): Promise<any>;

/**
* Parse data as text.
* @param {String} jsonAsTxt The content to parse.
*/
parseAsText(jsonAsTxt: string): void;
/**
*
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
*
* @param dataAccessHelper
*/
setDataAccessHelper(
dataAccessHelper:
| HtmlDataAccessHelper
| HttpDataAccessHelper
| JSZipDataAccessHelper
| LiteHttpDataAccessHelper
): boolean;

/**
* Set the url of the object to load.
* @param {String} url the url of the object to load.
* @param {IJSONNucleoReaderOptions} [option] The JSONNucleo reader options.
*/
setUrl(url: string, option: IJSONNucleoReaderOptions): Promise<string>;
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkJSONNucleoReader characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {IJSONNucleoReaderInitialValues} [initialValues] (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: IJSONNucleoReaderInitialValues
): void;

/**
* Method used to create a new instance of vtkJSONNucleoReader
* @param {IJSONNucleoReaderInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(
initialValues?: IJSONNucleoReaderInitialValues
): vtkJSONNucleoReader;

/**
* vtkJSONNucleoReader reader.
*/
export declare const vtkJSONNucleoReader: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkJSONNucleoReader;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
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 vtkJSONNucleoReader(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkJSONNucleoReader');

// Create default dataAccessHelper if not available
if (!model.dataAccessHelper) {
model.dataAccessHelper = DataAccessHelper.get('http');
}

// Internal method to fetch Array
function fetchText(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) =>
fetchText(model.url, options).then((csv) => {
publicAPI.parseAsText(csv);
return true;
});

publicAPI.parseAsText = (jsonAsTxt) => {
const { vertices, indices } = JSON.parse(jsonAsTxt);
const nbIndices = indices.length;
const nbTriangles = nbIndices / 3;
const nbCellsValues = nbTriangles + nbIndices;

model.points = Float32Array.from(vertices);
model.polys =
nbCellsValues < 65535
? new Uint16Array(nbCellsValues)
: new Uint32Array(nbCellsValues);
let srcOffset = 0;
let destOffset = 0;
while (destOffset < model.polys.length) {
model.polys[destOffset++] = 3;
model.polys[destOffset++] = indices[srcOffset++];
model.polys[destOffset++] = indices[srcOffset++];
model.polys[destOffset++] = indices[srcOffset++];
}

publicAPI.modified();
};

publicAPI.requestData = (inData, outData) => {
const polydata = vtkPolyData.newInstance();
polydata.getPoints().setData(model.points, 3);
polydata.getPolys().setData(model.polys);
model.output[0] = polydata;
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
// 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']);
macro.algo(publicAPI, model, 0, 1);

// Object methods
vtkJSONNucleoReader(publicAPI, model);
}

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

export const newInstance = macro.newInstance(extend, 'vtkJSONNucleoReader');

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

export default { newInstance, extend };