Serializer

Source

ArraySerializer.js
import macro from 'vtk.js/Sources/macro';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';

const FACTORY = {
vtkDataArray,
vtkCellArray,
vtkPoints,
};

function createDefaultTypedArrayHandler() {
const arrays = [];

function write(array) {
const id = arrays.length;
arrays.push(array);
return id;
}

function read(arrayId) {
return arrays[arrayId];
}

return {
write,
read,
arrays,
};
}

function vtkArraySerializer(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkArraySerializer');
if (!model.typedArrayHandler) {
model.typedArrayHandler = createDefaultTypedArrayHandler();
}

publicAPI.serialize = (obj) => {
const name = obj.getName();
const numberOfTuples = obj.getNumberOfTuples();
const vtkClass = obj.getClassName();
const rawData = obj.getData();
return {
id: model.typedArrayHandler.write(rawData),
name,
numberOfTuples,
vtkClass,
};
};

publicAPI.deserialize = (obj) => {
const values = model.typedArrayHandler.read(obj.id);
const { name, numberOfTuples } = obj;
return FACTORY[obj.vtkClass].newInstance({ name, numberOfTuples, values });
};
}

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

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

macro.obj(publicAPI, model);
macro.setGet(publicAPI, model, ['typedArrayHandler']);

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

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

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

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

export default Object.assign({ newInstance, extend });
FieldDataSerializer.js
import macro from 'vtk.js/Sources/macro';
import vtkDataSetAttributes from 'vtk.js/Sources/Common/DataModel/DataSetAttributes';

const CLASS_NAME = 'vtkDataSetAttributes';
const ARRAYS = [
'Scalars',
'Vectors',
'Normals',
'TCoords',
'Tensors',
'GlobalIds',
'PedigreeIds',
];

function canSerialize(obj) {
return obj && obj.isA && obj.isA(CLASS_NAME);
}

function canDeserialize(obj) {
return obj && obj.vtkClass && obj.vtkClass === CLASS_NAME;
}

function serialize(obj, arrayHandler) {
const output = {
vtkClass: CLASS_NAME,
};
const indexMapping = [];
const arrays = obj.getArrays();
for (let i = 0; i < arrays.length; i++) {
indexMapping.push(arrayHandler.serialize(arrays[i]));
}
ARRAYS.forEach((attrType) => {
const arrayIdx = obj[`getActive${attrType}`]();
if (arrayIdx !== -1) {
output[attrType] = indexMapping[arrayIdx];
}
});
// List all arrays
output.arrays = indexMapping;

return output;
}

function deserialize(obj, arrayHandler) {
const ds = vtkDataSetAttributes.newInstance();
for (let i = 0; i < obj.arrays.length; i++) {
ds.addArray(arrayHandler.deserialize(obj.arrays[i]));
}
ARRAYS.forEach((attrType) => {
ds[`set${attrType}`](arrayHandler.deserialize(obj[attrType]));
});
return ds;
}

export default {
canSerialize,
serialize,
canDeserialize,
deserialize,
};
ImageDataSerializer.js
import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData';
import vtkFieldDataSerializer from 'vtk.js/Sources/IO/Core/Serializer/FieldDataSerializer';

const CLASS_NAME = 'vtkImageData';

function canSerialize(obj) {
return obj && obj.isA && obj.isA(CLASS_NAME);
}

function canDeserialize(obj) {
return obj && obj.vtkClass && obj.vtkClass === CLASS_NAME;
}

function serialize(obj, arrayHandler) {
const output = Object.assign(
obj.get('direction', 'spacing', 'origin', 'extent'),
{
vtkClass: CLASS_NAME,
}
);

// Handle fields
output.pointData = vtkFieldDataSerializer.serialize(
obj.getPointData(),
arrayHandler
);
output.cellData = vtkFieldDataSerializer.serialize(
obj.getCellData(),
arrayHandler
);
output.fieldData = vtkFieldDataSerializer.serialize(
obj.getFieldData(),
arrayHandler
);
return output;
}

function deserialize(obj, arrayHandler) {
const { direction, spacing, origin, extent } = obj;
const ds = vtkImageData.newInstance({ direction, spacing, origin, extent });

// Handle fields
ds.setPointData(
vtkFieldDataSerializer.deserialize(obj.pointData, arrayHandler)
);
ds.setCellData(
vtkFieldDataSerializer.deserialize(obj.cellData, arrayHandler)
);
ds.setFieldData(
vtkFieldDataSerializer.deserialize(obj.fieldData, arrayHandler)
);

return ds;
}

export default {
canSerialize,
serialize,
canDeserialize,
deserialize,
};
PolyDataSerializer.js
import macro from 'vtk.js/Sources/macro';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkFieldDataSerializer from 'vtk.js/Sources/IO/Core/Serializer/FieldDataSerializer';

const CLASS_NAME = 'vtkPolyData';
const ARRAYS = ['points', 'verts', 'lines', 'polys', 'strips'];

function canSerialize(obj) {
return obj && obj.isA && obj.isA(CLASS_NAME);
}

function canDeserialize(obj) {
return obj && obj.vtkClass && obj.vtkClass === CLASS_NAME;
}

function serialize(obj, arrayHandler) {
const output = {
vtkClass: CLASS_NAME,
};
ARRAYS.forEach((arrayName) => {
const array = obj[`get${macro.capitalize(arrayName)}`]();
if (array && array.getNumberOfValues() > 0) {
output[arrayName] = arrayHandler.serialize(array);
}
});

// Handle fields
output.pointData = vtkFieldDataSerializer.serialize(
obj.getPointData(),
arrayHandler
);
output.cellData = vtkFieldDataSerializer.serialize(
obj.getCellData(),
arrayHandler
);
output.fieldData = vtkFieldDataSerializer.serialize(
obj.getFieldData(),
arrayHandler
);
return output;
}

function deserialize(obj, arrayHandler) {
const ds = vtkPolyData.newInstance();
ARRAYS.forEach((arrayName) => {
ds[`set${macro.capitalize(arrayName)}`](
arrayHandler.deserialize(obj[arrayName])
);
});

// Handle fields
ds.setPointData(
vtkFieldDataSerializer.deserialize(obj.pointData, arrayHandler)
);
ds.setCellData(
vtkFieldDataSerializer.deserialize(obj.cellData, arrayHandler)
);
ds.setFieldData(
vtkFieldDataSerializer.deserialize(obj.fieldData, arrayHandler)
);

return ds;
}

export default {
canSerialize,
serialize,
canDeserialize,
deserialize,
};
index.js
import vtkArraySerializer from 'vtk.js/Sources/IO/Core/Serializer/ArraySerializer';
import vtkFieldDataSerializer from 'vtk.js/Sources/IO/Core/Serializer/FieldDataSerializer';
import vtkImageDataSerializer from 'vtk.js/Sources/IO/Core/Serializer/ImageDataSerializer';
import vtkPolyDataSerializer from 'vtk.js/Sources/IO/Core/Serializer/PolyDataSerializer';

const LIST = [
vtkFieldDataSerializer,
vtkImageDataSerializer,
vtkPolyDataSerializer,
];

function getSerializer(obj) {
return LIST.find((s) => s.canSerialize(obj));
}

function getDeserializer(obj) {
return LIST.find((s) => s.canDeserialize(obj));
}

export default {
vtkArraySerializer,
getSerializer,
getDeserializer,
};