import { vec3, mat4 } from 'gl-matrix'; import macro from 'vtk.js/Sources/macro'; import vtkProp3D from 'vtk.js/Sources/Rendering/Core/Prop3D'; import vtkImageProperty from 'vtk.js/Sources/Rendering/Core/ImageProperty';
const { vtkDebugMacro } = macro;
function vtkImageSlice(publicAPI, model) { model.classHierarchy.push('vtkImageSlice');
publicAPI.getActors = () => publicAPI; publicAPI.getImages = () => publicAPI;
publicAPI.getIsOpaque = () => { if (model.forceOpaque) { return true; } if (model.forceTranslucent) { return false; } if (!model.property) { publicAPI.getProperty(); }
let isOpaque = model.property.getOpacity() >= 1.0;
isOpaque = isOpaque && (!model.mapper || model.mapper.getIsOpaque());
return isOpaque; };
publicAPI.hasTranslucentPolygonalGeometry = () => false;
publicAPI.makeProperty = vtkImageProperty.newInstance;
publicAPI.getProperty = () => { if (model.property === null) { model.property = publicAPI.makeProperty(); } return model.property; };
publicAPI.getBounds = () => { if (model.mapper === null) { return model.bounds; }
const bds = model.mapper.getBounds(); if (!bds || bds.length !== 6) { return bds; }
if (bds[0] > bds[1]) { model.mapperBounds = bds.concat(); model.bounds = [1, -1, 1, -1, 1, -1]; model.boundsMTime.modified(); return bds; }
const zip = (rows) => rows[0].map((_, c) => rows.map((row) => row[c])); if ( !model.mapperBounds || !zip([bds, model.mapperBounds]).reduce( (a, b) => a && b[0] === b[1], true ) || publicAPI.getMTime() > model.boundsMTime.getMTime() ) { vtkDebugMacro('Recomputing bounds...'); model.mapperBounds = bds.map((x) => x); const bbox = [ vec3.fromValues(bds[1], bds[3], bds[5]), vec3.fromValues(bds[1], bds[2], bds[5]), vec3.fromValues(bds[0], bds[2], bds[5]), vec3.fromValues(bds[0], bds[3], bds[5]), vec3.fromValues(bds[1], bds[3], bds[4]), vec3.fromValues(bds[1], bds[2], bds[4]), vec3.fromValues(bds[0], bds[2], bds[4]), vec3.fromValues(bds[0], bds[3], bds[4]), ];
publicAPI.computeMatrix(); const tmp4 = mat4.create(); mat4.transpose(tmp4, model.matrix); bbox.forEach((pt) => vec3.transformMat4(pt, pt, tmp4));
model.bounds[0] = model.bounds[2] = model.bounds[4] = Number.MAX_VALUE; model.bounds[1] = model.bounds[3] = model.bounds[5] = -Number.MAX_VALUE; model.bounds = model.bounds.map((d, i) => i % 2 === 0 ? bbox.reduce((a, b) => (a > b[i / 2] ? b[i / 2] : a), d) : bbox.reduce((a, b) => (a < b[(i - 1) / 2] ? b[(i - 1) / 2] : a), d) ); model.boundsMTime.modified(); } return model.bounds; };
publicAPI.getBoundsForSlice = (slice, thickness = 0) => { const bds = model.mapper.getBoundsForSlice(slice, thickness); if (!bds || bds.length !== 6) { return bds; }
if (bds[0] > bds[1]) { return bds; }
const bbox = [ vec3.fromValues(bds[1], bds[3], bds[5]), vec3.fromValues(bds[1], bds[2], bds[5]), vec3.fromValues(bds[0], bds[2], bds[5]), vec3.fromValues(bds[0], bds[3], bds[5]), vec3.fromValues(bds[1], bds[3], bds[4]), vec3.fromValues(bds[1], bds[2], bds[4]), vec3.fromValues(bds[0], bds[2], bds[4]), vec3.fromValues(bds[0], bds[3], bds[4]), ];
publicAPI.computeMatrix(); const tmp4 = mat4.create(); mat4.transpose(tmp4, model.matrix); bbox.forEach((pt) => vec3.transformMat4(pt, pt, tmp4));
let newBounds = [ Number.MAX_VALUE, -Number.MAX_VALUE, Number.MAX_VALUE, -Number.MAX_VALUE, Number.MAX_VALUE, -Number.MAX_VALUE, ]; newBounds = newBounds.map((d, i) => i % 2 === 0 ? bbox.reduce((a, b) => (a > b[i / 2] ? b[i / 2] : a), d) : bbox.reduce((a, b) => (a < b[(i - 1) / 2] ? b[(i - 1) / 2] : a), d) ); return newBounds; };
publicAPI.getMinXBound = () => { publicAPI.getBounds(); return model.bounds[0]; };
publicAPI.getMaxXBound = () => { publicAPI.getBounds(); return model.bounds[1]; };
publicAPI.getMinYBound = () => { publicAPI.getBounds(); return model.bounds[2]; };
publicAPI.getMaxYBound = () => { publicAPI.getBounds(); return model.bounds[3]; };
publicAPI.getMinZBound = () => { publicAPI.getBounds(); return model.bounds[4]; };
publicAPI.getMaxZBound = () => { publicAPI.getBounds(); return model.bounds[5]; };
publicAPI.getMTime = () => { let mt = model.mtime; if (model.property !== null) { const time = model.property.getMTime(); mt = time > mt ? time : mt; }
return mt; };
publicAPI.getRedrawMTime = () => { let mt = model.mtime; if (model.mapper !== null) { let time = model.mapper.getMTime(); mt = time > mt ? time : mt; if (model.mapper.getInput() !== null) { model.mapper.getInputAlgorithm().update(); time = model.mapper.getInput().getMTime(); mt = time > mt ? time : mt; } } if (model.property !== null) { let time = model.property.getMTime(); mt = time > mt ? time : mt; if (model.property.getRGBTransferFunction() !== null) { time = model.property.getRGBTransferFunction().getMTime(); mt = time > mt ? time : mt; } } return mt; };
publicAPI.getSupportsSelection = () => model.mapper ? model.mapper.getSupportsSelection() : false; }
const DEFAULT_VALUES = { mapper: null, property: null,
bounds: [1, -1, 1, -1, 1, -1], };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
vtkProp3D.extend(publicAPI, model, initialValues);
model.boundsMTime = {}; macro.obj(model.boundsMTime);
macro.set(publicAPI, model, ['property']); macro.setGet(publicAPI, model, ['mapper']); macro.getArray(publicAPI, model, ['bounds'], 6);
vtkImageSlice(publicAPI, model); }
export const newInstance = macro.newInstance(extend, 'vtkImageSlice');
export default { newInstance, extend };
|