@thewtex/vtk.js-esm
Version:
Visualization Toolkit for the Web
204 lines (162 loc) • 6.09 kB
JavaScript
import macro from '../../macro.js';
import vtkBoundingBox from '../../Common/DataModel/BoundingBox.js';
import { y as degreesFromRadians, r as radiansFromDegrees } from '../../Common/Core/Math/index.js';
import vtkProp from './Prop.js';
import { i as identity, n as getRotation, a as rotateX, b as rotateY, c as rotateZ, k as fromQuat, m as multiply, d as copy, t as translate, s as scale, j as transpose } from '../../vendor/gl-matrix/esm/mat4.js';
import { c as create, g as getAxisAngle, s as setAxisAngle } from '../../vendor/gl-matrix/esm/quat.js';
// vtkProp3D methods
// ----------------------------------------------------------------------------
function vtkProp3D(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkProp3D');
publicAPI.addPosition = function (deltaXYZ) {
model.position = model.position.map(function (value, index) {
return value + deltaXYZ[index];
});
publicAPI.modified();
};
publicAPI.getOrientationWXYZ = function () {
var q = create();
getRotation(q, model.rotation);
var oaxis = new Float64Array(3);
var w = getAxisAngle(oaxis, q);
return [degreesFromRadians(w), oaxis[0], oaxis[1], oaxis[2]];
};
publicAPI.rotateX = function (val) {
if (val === 0.0) {
return;
}
rotateX(model.rotation, model.rotation, radiansFromDegrees(val));
publicAPI.modified();
};
publicAPI.rotateY = function (val) {
if (val === 0.0) {
return;
}
rotateY(model.rotation, model.rotation, radiansFromDegrees(val));
publicAPI.modified();
};
publicAPI.rotateZ = function (val) {
if (val === 0.0) {
return;
}
rotateZ(model.rotation, model.rotation, radiansFromDegrees(val));
publicAPI.modified();
};
publicAPI.rotateWXYZ = function (degrees, x, y, z) {
if (degrees === 0.0 || x === 0.0 && y === 0.0 && z === 0.0) {
return;
} // convert to radians
var angle = radiansFromDegrees(degrees);
var q = create();
setAxisAngle(q, [x, y, z], angle);
var quatMat = new Float64Array(16);
fromQuat(quatMat, q);
multiply(model.rotation, model.rotation, quatMat);
publicAPI.modified();
};
publicAPI.setOrientation = function (x, y, z) {
if (x === model.orientation[0] && y === model.orientation[1] && z === model.orientation[2]) {
return false;
}
model.orientation = [x, y, z];
identity(model.rotation);
publicAPI.rotateZ(z);
publicAPI.rotateX(x);
publicAPI.rotateY(y);
publicAPI.modified();
return true;
};
publicAPI.setUserMatrix = function (matrix) {
copy(model.userMatrix, matrix);
publicAPI.modified();
};
publicAPI.getMatrix = function () {
publicAPI.computeMatrix();
return model.matrix;
};
publicAPI.computeMatrix = function () {
// check whether or not need to rebuild the matrix
if (publicAPI.getMTime() > model.matrixMTime.getMTime()) {
identity(model.matrix);
if (model.userMatrix) {
multiply(model.matrix, model.matrix, model.userMatrix);
}
translate(model.matrix, model.matrix, model.origin);
translate(model.matrix, model.matrix, model.position);
multiply(model.matrix, model.matrix, model.rotation);
scale(model.matrix, model.matrix, model.scale);
translate(model.matrix, model.matrix, [-model.origin[0], -model.origin[1], -model.origin[2]]);
transpose(model.matrix, model.matrix); // check for identity
model.isIdentity = true;
for (var i = 0; i < 4; ++i) {
for (var j = 0; j < 4; ++j) {
if ((i === j ? 1.0 : 0.0) !== model.matrix[i + j * 4]) {
model.isIdentity = false;
}
}
}
model.matrixMTime.modified();
}
};
publicAPI.getCenter = function () {
return vtkBoundingBox.getCenter(model.bounds);
};
publicAPI.getLength = function () {
return vtkBoundingBox.getLength(model.bounds);
};
publicAPI.getXRange = function () {
return vtkBoundingBox.getXRange(model.bounds);
};
publicAPI.getYRange = function () {
return vtkBoundingBox.getYRange(model.bounds);
};
publicAPI.getZRange = function () {
return vtkBoundingBox.getZRange(model.bounds);
};
publicAPI.getUserMatrix = function () {
return model.userMatrix;
};
function updateIdentityFlag() {
publicAPI.computeMatrix();
}
publicAPI.onModified(updateIdentityFlag);
} // ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
var DEFAULT_VALUES = {
origin: [0, 0, 0],
position: [0, 0, 0],
orientation: [0, 0, 0],
rotation: null,
scale: [1, 1, 1],
bounds: [1, -1, 1, -1, 1, -1],
userMatrix: null,
userMatrixMTime: null,
cachedProp3D: null,
isIdentity: true,
matrixMTime: null
}; // ----------------------------------------------------------------------------
function extend(publicAPI, model) {
var initialValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
Object.assign(model, DEFAULT_VALUES, initialValues); // Inheritance
vtkProp.extend(publicAPI, model, initialValues);
model.matrixMTime = {};
macro.obj(model.matrixMTime); // Build VTK API
macro.get(publicAPI, model, ['bounds', 'isIdentity']);
macro.getArray(publicAPI, model, ['orientation']);
macro.setGetArray(publicAPI, model, ['origin', 'position', 'scale'], 3); // Object internal instance
model.matrix = identity(new Float64Array(16));
model.rotation = identity(new Float64Array(16));
model.userMatrix = identity(new Float64Array(16));
model.transform = null; // FIXME
// Object methods
vtkProp3D(publicAPI, model);
} // ----------------------------------------------------------------------------
var newInstance = macro.newInstance(extend, 'vtkProp3D'); // ----------------------------------------------------------------------------
var vtkProp3D$1 = {
newInstance: newInstance,
extend: extend
};
export default vtkProp3D$1;
export { extend, newInstance };