@thewtex/vtk.js-esm
Version:
Visualization Toolkit for the Web
428 lines (379 loc) • 15.9 kB
JavaScript
import macro from '../../macro.js';
import vtkPolyData from '../../Common/DataModel/PolyData.js';
import vtkCellArray from '../../Common/Core/CellArray.js';
import vtkPoints from '../../Common/Core/Points.js';
// vtkCursor3D methods
// ----------------------------------------------------------------------------
function vtkCursor3D(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkCursor3D'); // Public API methods
publicAPI.setModelBounds = function (bounds) {
if (!Array.isArray(bounds) || bounds.length < 6) {
return;
}
if (model.modelBounds[0] === bounds[0] && model.modelBounds[1] === bounds[1] && model.modelBounds[2] === bounds[2] && model.modelBounds[3] === bounds[3] && model.modelBounds[4] === bounds[4] && model.modelBounds[5] === bounds[5]) {
return;
}
publicAPI.modified(); // Doing type convert, make sure it is a number array.
// Without correct coversion, the array may contains string which cause
// the wrapping and clampping works incorrectly.
model.modelBounds = bounds.map(function (v) {
return Number(v);
});
for (var i = 0; i < 3; ++i) {
model.modelBounds[2 * i] = Math.min(model.modelBounds[2 * i], model.modelBounds[2 * i + 1]);
}
};
publicAPI.setFocalPoint = function (points) {
if (!Array.isArray(points) || points.length < 3) {
return;
}
if (points[0] === model.focalPoint[0] && points[1] === model.focalPoint[1] && points[2] === model.focalPoint[2]) {
return;
}
publicAPI.modified();
var v = [];
for (var i = 0; i < 3; i++) {
v[i] = points[i] - model.focalPoint[i];
model.focalPoint[i] = Number(points[i]);
if (model.translationMode) {
model.modelBounds[2 * i] += v[i];
model.modelBounds[2 * i + 1] += v[i];
} // wrap
else if (model.wrap) {
model.focalPoint[i] = model.modelBounds[2 * i] + (model.focalPoint[i] - model.modelBounds[2 * i]) * 1.0 % ((model.modelBounds[2 * i + 1] - model.modelBounds[2 * i]) * 1.0);
} // clamp
else {
if (points[i] < model.modelBounds[2 * i]) {
model.focalPoint[i] = model.modelBounds[2 * i];
}
if (points[i] > model.modelBounds[2 * i + 1]) {
model.focalPoint[i] = model.modelBounds[2 * i + 1];
}
}
}
};
publicAPI.setAll = function (flag) {
publicAPI.setOutline(flag);
publicAPI.setAxes(flag);
publicAPI.setXShadows(flag);
publicAPI.setYShadows(flag);
publicAPI.setZShadows(flag);
};
publicAPI.allOn = function () {
publicAPI.setAll(true);
};
publicAPI.allOff = function () {
publicAPI.setAll(false);
};
publicAPI.requestData = function (inData, outData) {
if (model.deleted) {
return;
}
var numPts = 0;
var numLines = 0; // Check bounding box and origin
if (model.wrap) {
for (var i = 0; i < model.focalPoint.length; ++i) {
model.focalPoint[i] = model.modelBounds[2 * i] + (model.focalPoint[i] - model.modelBounds[2 * i]) * 1.0 % (model.modelBounds[2 * i + 1] - model.modelBounds[2 * i]);
}
} else {
for (var _i = 0; _i < model.focalPoint.length; ++_i) {
model.focalPoint[_i] = Math.max(model.focalPoint[_i], model.modelBounds[2 * _i]);
model.focalPoint[_i] = Math.min(model.focalPoint[_i], model.modelBounds[2 * _i + 1]);
}
} // allocate storage
if (model.axes) {
numPts += 6;
numLines += 3;
}
if (model.outline) {
numPts += 8;
numLines += 12;
}
if (model.xShadows) {
numPts += 8;
numLines += 4;
}
if (model.yShadows) {
numPts += 8;
numLines += 4;
}
if (model.zShadows) {
numPts += 8;
numLines += 4;
}
if (numPts === 0) {
return;
}
var polyData = vtkPolyData.newInstance();
var newPts = vtkPoints.newInstance({
size: numPts * 3
}); // vtkCellArray is a supporting object that explicitly represents cell
// connectivity. The cell array structure is a raw integer list
// of the form: (n,id1,id2,...,idn, n,id1,id2,...,idn, ...)
// where n is the number of points in the cell, and id is a zero-offset index
// into an associated point list.
var newLines = vtkCellArray.newInstance({
size: numLines * (2 + 1)
});
var pid = 0;
var cid = 0; // Create axes
if (model.axes) {
newPts.getData()[pid * 3 + 0] = model.modelBounds[0];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[1];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[2];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[3];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.modelBounds[4];
++pid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.modelBounds[5];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
} // create outline
if (model.outline) {
// first traid
newPts.getData()[pid * 3 + 0] = model.modelBounds[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[2];
newPts.getData()[pid * 3 + 2] = model.modelBounds[4];
var corner024 = pid;
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[1];
newPts.getData()[pid * 3 + 1] = model.modelBounds[2];
newPts.getData()[pid * 3 + 2] = model.modelBounds[4];
var corner124 = pid;
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[3];
newPts.getData()[pid * 3 + 2] = model.modelBounds[4];
var corner034 = pid;
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[2];
newPts.getData()[pid * 3 + 2] = model.modelBounds[5];
var corner025 = pid;
++pid;
newLines.getData()[(cid + 0) * 3 + 0] = 2;
newLines.getData()[(cid + 0) * 3 + 1] = corner024;
newLines.getData()[(cid + 0) * 3 + 2] = corner124;
newLines.getData()[(cid + 1) * 3 + 0] = 2;
newLines.getData()[(cid + 1) * 3 + 1] = corner024;
newLines.getData()[(cid + 1) * 3 + 2] = corner034;
newLines.getData()[(cid + 2) * 3 + 0] = 2;
newLines.getData()[(cid + 2) * 3 + 1] = corner024;
newLines.getData()[(cid + 2) * 3 + 2] = corner025;
cid += 3; // second triad
newPts.getData()[pid * 3 + 0] = model.modelBounds[1];
newPts.getData()[pid * 3 + 1] = model.modelBounds[3];
newPts.getData()[pid * 3 + 2] = model.modelBounds[5];
var corner135 = pid;
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[3];
newPts.getData()[pid * 3 + 2] = model.modelBounds[5];
var corner035 = pid;
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[1];
newPts.getData()[pid * 3 + 1] = model.modelBounds[2];
newPts.getData()[pid * 3 + 2] = model.modelBounds[5];
var corner125 = pid;
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[1];
newPts.getData()[pid * 3 + 1] = model.modelBounds[3];
newPts.getData()[pid * 3 + 2] = model.modelBounds[4];
var corner134 = pid;
++pid;
newLines.getData()[(cid + 0) * 3 + 0] = 2;
newLines.getData()[(cid + 0) * 3 + 1] = corner135;
newLines.getData()[(cid + 0) * 3 + 2] = corner035;
newLines.getData()[(cid + 1) * 3 + 0] = 2;
newLines.getData()[(cid + 1) * 3 + 1] = corner135;
newLines.getData()[(cid + 1) * 3 + 2] = corner125;
newLines.getData()[(cid + 2) * 3 + 0] = 2;
newLines.getData()[(cid + 2) * 3 + 1] = corner135;
newLines.getData()[(cid + 2) * 3 + 2] = corner134;
cid += 3; // Fill in remaining lines
// vtk.js do not support checking repeating insertion
newLines.getData()[(cid + 0) * 3 + 0] = 2;
newLines.getData()[(cid + 0) * 3 + 1] = corner124;
newLines.getData()[(cid + 0) * 3 + 2] = corner134;
newLines.getData()[(cid + 1) * 3 + 0] = 2;
newLines.getData()[(cid + 1) * 3 + 1] = corner124;
newLines.getData()[(cid + 1) * 3 + 2] = corner125;
cid += 2;
newLines.getData()[(cid + 0) * 3 + 0] = 2;
newLines.getData()[(cid + 0) * 3 + 1] = corner034;
newLines.getData()[(cid + 0) * 3 + 2] = corner134;
newLines.getData()[(cid + 1) * 3 + 0] = 2;
newLines.getData()[(cid + 1) * 3 + 1] = corner034;
newLines.getData()[(cid + 1) * 3 + 2] = corner035;
cid += 2;
newLines.getData()[(cid + 0) * 3 + 0] = 2;
newLines.getData()[(cid + 0) * 3 + 1] = corner025;
newLines.getData()[(cid + 0) * 3 + 2] = corner125;
newLines.getData()[(cid + 1) * 3 + 0] = 2;
newLines.getData()[(cid + 1) * 3 + 1] = corner025;
newLines.getData()[(cid + 1) * 3 + 2] = corner035;
cid += 2;
} // create x-shadows
if (model.xShadows) {
for (var _i2 = 0; _i2 < 2; ++_i2) {
newPts.getData()[pid * 3 + 0] = model.modelBounds[_i2];
newPts.getData()[pid * 3 + 1] = model.modelBounds[2];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[_i2];
newPts.getData()[pid * 3 + 1] = model.modelBounds[3];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[_i2];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.modelBounds[4];
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[_i2];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.modelBounds[5];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
}
} // create y-shadows
if (model.yShadows) {
for (var _i3 = 0; _i3 < 2; ++_i3) {
newPts.getData()[pid * 3 + 0] = model.modelBounds[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[_i3 + 2];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[1];
newPts.getData()[pid * 3 + 1] = model.modelBounds[_i3 + 2];
newPts.getData()[pid * 3 + 2] = model.focalPoint[2];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[_i3 + 2];
newPts.getData()[pid * 3 + 2] = model.modelBounds[4];
++pid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[_i3 + 2];
newPts.getData()[pid * 3 + 2] = model.modelBounds[5];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
}
} // create z-shadows
if (model.zShadows) {
for (var _i4 = 0; _i4 < 2; ++_i4) {
newPts.getData()[pid * 3 + 0] = model.modelBounds[0];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.modelBounds[_i4 + 4];
++pid;
newPts.getData()[pid * 3 + 0] = model.modelBounds[1];
newPts.getData()[pid * 3 + 1] = model.focalPoint[1];
newPts.getData()[pid * 3 + 2] = model.modelBounds[_i4 + 4];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[2];
newPts.getData()[pid * 3 + 2] = model.modelBounds[_i4 + 4];
++pid;
newPts.getData()[pid * 3 + 0] = model.focalPoint[0];
newPts.getData()[pid * 3 + 1] = model.modelBounds[3];
newPts.getData()[pid * 3 + 2] = model.modelBounds[_i4 + 4];
++pid;
newLines.getData()[cid * 3 + 0] = 2;
newLines.getData()[cid * 3 + 1] = pid - 2;
newLines.getData()[cid * 3 + 2] = pid - 1;
++cid;
}
}
var pts = vtkPoints.newInstance({
size: 3
});
pts.getData()[0] = model.focalPoint[0];
pts.getData()[1] = model.focalPoint[1];
pts.getData()[2] = model.focalPoint[2]; // update ourseleves
model.focus = vtkPolyData.newInstance();
model.focus.setPoints(pts);
polyData.setPoints(newPts);
polyData.setLines(newLines);
outData[0] = polyData;
};
} // ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
var DEFAULT_VALUES = {
focus: null,
modelBounds: [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0],
focalPoint: [0.0, 0.0, 0.0],
outline: true,
axes: true,
xShadows: true,
yShadows: true,
zShadows: true,
wrap: false,
translationMode: false
}; // ----------------------------------------------------------------------------
function extend(publicAPI, model) {
var initialValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
Object.assign(model, DEFAULT_VALUES, initialValues); // Build VTK API
// Cursor3D
macro.obj(publicAPI, model);
macro.get(publicAPI, model, ['focus']);
macro.getArray(publicAPI, model, ['modelBounds'], 6);
macro.getArray(publicAPI, model, ['focalPoint'], 3);
macro.setGet(publicAPI, model, ['outline']);
macro.setGet(publicAPI, model, ['axes']);
macro.setGet(publicAPI, model, ['xShadows']);
macro.setGet(publicAPI, model, ['yShadows']);
macro.setGet(publicAPI, model, ['zShadows']);
macro.setGet(publicAPI, model, ['wrap']);
macro.setGet(publicAPI, model, ['translationMode']);
macro.algo(publicAPI, model, 0, 1);
vtkCursor3D(publicAPI, model);
} // ----------------------------------------------------------------------------
var newInstance = macro.newInstance(extend, 'vtkCursor3D'); // ----------------------------------------------------------------------------
var vtkCursor3D$1 = {
newInstance: newInstance,
extend: extend
};
export default vtkCursor3D$1;
export { extend, newInstance };