VariantArray

Introduction

An array holding vtkVariants.

Methods

extend

Method used to decorate a given object (publicAPI+model) with vtkVariantArray 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 IVariantArrayInitialValues Yes (default: {})

getComponent

Get the data component at the location specified by tupleIdx and compIdx.

Argument Type Required Description
tupleIdx Number Yes
compIdx Number No

getData

getDataType

getName

getNumberOfComponents

getNumberOfTuples

getNumberOfValues

getTuple

Argument Type Required Description
idx Number Yes
tupleToFill Array. No

getTupleLocation

Argument Type Required Description
idx Number No

newClone

newInstance

Method used to create a new instance of vtkVariantArray

Argument Type Required Description
initialValues IVariantArrayInitialValues Yes for pre-setting some of its content

setComponent

Set the data component at the location specified by tupleIdx and compIdx
to value.
Note that i is less than NumberOfTuples and j is less than
NumberOfComponents. Make sure enough memory has been allocated
(use SetNumberOfTuples() and SetNumberOfComponents()).

Argument Type Required Description
tupleIdx Number Yes
compIdx Number Yes
value String Yes

setData

Argument Type Required Description
array Array. Yes
numberOfComponents Number Yes

setName

Source

index.d.ts
import { vtkObject } from "../../../interfaces" ;

/**
*
*/
export interface IVariantArrayInitialValues {
name?: string;
numberOfComponents?: number;
size: number;
dataType?: string;
}

export interface vtkVariantArray extends vtkObject {

/**
* Get the data component at the location specified by tupleIdx and compIdx.
* @param {Number} tupleIdx
* @param {Number} [compIdx]
*/
getComponent(tupleIdx: number, compIdx?: number): void;

/**
*
*/
getData(): Array<any>;

/**
*
*/
getDataType(): string;

/**
*
*/
getName(): string;

/**
*
*/
getNumberOfComponents(): number;

/**
*
*/
getNumberOfValues(): number;

/**
*
*/
getNumberOfTuples(): number;

/**
*
* @param {Number} idx
* @param {Array<any>} [tupleToFill]
*/
getTuple(idx: number, tupleToFill?: Array<any>): Array<any>;

/**
*
* @param {Number} [idx]
*/
getTupleLocation(idx?: number): number;

/**
*
*/
newClone(): void;

/**
* Set the data component at the location specified by tupleIdx and compIdx
* to value.
* Note that i is less than NumberOfTuples and j is less than
* NumberOfComponents. Make sure enough memory has been allocated
* (use SetNumberOfTuples() and SetNumberOfComponents()).
* @param {Number} tupleIdx
* @param {Number} compIdx
* @param {String} value
*/
setComponent(tupleIdx: number, compIdx: number, value: string): void;

/**
*
* @param {Array<any>} array
* @param {Number} numberOfComponents
*/
setData(array: Array<any>, numberOfComponents: number): void;

/**
*
*/
setName(name: string): boolean;
}

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

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

/**
* An array holding vtkVariants.
*/
export declare const vtkVariantArray: {
newInstance: typeof newInstance;
extend: typeof extend;
}
export default vtkVariantArray;
index.js
import macro from 'vtk.js/Sources/macros';

// ----------------------------------------------------------------------------
// vtkVariantArray methods
// ----------------------------------------------------------------------------

function vtkVariantArray(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkVariantArray');

// Description:
// Return the data component at the location specified by tupleIdx and
// compIdx.
publicAPI.getComponent = (tupleIdx, compIdx = 0) =>
model.values[tupleIdx * model.numberOfComponents + compIdx];

// Description:
// Set the data component at the location specified by tupleIdx and compIdx
// to value.
// Note that i is less than NumberOfTuples and j is less than
// NumberOfComponents. Make sure enough memory has been allocated
// (use SetNumberOfTuples() and SetNumberOfComponents()).
publicAPI.setComponent = (tupleIdx, compIdx, value) => {
if (value !== model.values[tupleIdx * model.numberOfComponents + compIdx]) {
model.values[tupleIdx * model.numberOfComponents + compIdx] = value;
publicAPI.modified();
}
};

publicAPI.getData = () => model.values;

publicAPI.getTuple = (idx, tupleToFill = []) => {
const numberOfComponents = model.numberOfComponents || 1;
if (tupleToFill.length) {
tupleToFill.length = numberOfComponents;
}
const offset = idx * numberOfComponents;
for (let i = 0; i < numberOfComponents; i++) {
tupleToFill[i] = model.values[offset + i];
}
return tupleToFill;
};

publicAPI.getTupleLocation = (idx = 1) => idx * model.numberOfComponents;
publicAPI.getNumberOfComponents = () => model.numberOfComponents;
publicAPI.getNumberOfValues = () => model.values.length;
publicAPI.getNumberOfTuples = () =>
model.values.length / model.numberOfComponents;
publicAPI.getDataType = () => model.dataType;
/* eslint-disable no-use-before-define */
publicAPI.newClone = () =>
newInstance({
name: model.name,
numberOfComponents: model.numberOfComponents,
});
/* eslint-enable no-use-before-define */

publicAPI.getName = () => {
if (!model.name) {
publicAPI.modified();
model.name = `vtkVariantArray${publicAPI.getMTime()}`;
}
return model.name;
};

publicAPI.setData = (array, numberOfComponents) => {
model.values = array;
model.size = array.length;
if (numberOfComponents) {
model.numberOfComponents = numberOfComponents;
}
if (model.size % model.numberOfComponents !== 0) {
model.numberOfComponents = 1;
}
publicAPI.modified();
};
}

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

const DEFAULT_VALUES = {
name: '',
numberOfComponents: 1,
size: 0,
// values: null,
dataType: 'JSON',
};

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

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

if (!model.empty && !model.values && !model.size) {
throw new TypeError(
'Cannot create vtkVariantArray object without: size > 0, values'
);
}

if (!model.values) {
model.values = [];
} else if (Array.isArray(model.values)) {
model.values = [...model.values];
}

if (model.values) {
model.size = model.values.length;
}

// Object methods
macro.obj(publicAPI, model);
macro.set(publicAPI, model, ['name']);

// Object specific methods
vtkVariantArray(publicAPI, model);
}

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

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

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

export default { newInstance, extend };