UNPKG

@kitware/vtk.js

Version:

Visualization Toolkit for the Web

121 lines (91 loc) 5.01 kB
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { mat4, vec3 } from 'gl-matrix'; import macro from '../../macros.js'; import vtkActor from './Actor.js'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } // vtkFollower methods // ---------------------------------------------------------------------------- function vtkFollower(publicAPI, model) { // Set our className model.classHierarchy.push('vtkFollower'); // Capture 'parentClass' api for internal use var superClass = _objectSpread({}, publicAPI); publicAPI.getMTime = function () { var mt = superClass.getMTime(); if (model.camera !== null) { var time = model.camera.getMTime(); mt = time > mt ? time : mt; } return mt; }; publicAPI.computeMatrix = function () { // check whether or not need to rebuild the matrix if (publicAPI.getMTime() > model.matrixMTime.getMTime()) { mat4.identity(model.matrix); if (model.userMatrix) { mat4.multiply(model.matrix, model.matrix, model.userMatrix); } mat4.translate(model.matrix, model.matrix, model.origin); mat4.translate(model.matrix, model.matrix, model.position); mat4.multiply(model.matrix, model.matrix, model.rotation); mat4.scale(model.matrix, model.matrix, model.scale); if (model.camera) { // first compute our target viewUp var vup = new Float64Array(model.viewUp); if (!model.useViewUp) { vec3.set.apply(vec3, [vup].concat(_toConsumableArray(model.camera.getViewUp()))); } // compute a vpn var vpn = new Float64Array(3); if (model.camera.getParallelProjection()) { vec3.set(vpn, model.camera.getViewPlaneNormal()); } else { vec3.set.apply(vec3, [vpn].concat(_toConsumableArray(model.position))); vec3.subtract(vpn, model.camera.getPosition(), vpn); vec3.normalize(vpn, vpn); } // compute vright var vright = new Float64Array(3); vec3.cross(vright, vup, vpn); vec3.normalize(vright, vright); // now recompute the vpn so that it is orthogonal to vup vec3.cross(vpn, vright, vup); vec3.normalize(vpn, vpn); model.followerMatrix[0] = vright[0]; model.followerMatrix[1] = vright[1]; model.followerMatrix[2] = vright[2]; model.followerMatrix[4] = vup[0]; model.followerMatrix[5] = vup[1]; model.followerMatrix[6] = vup[2]; model.followerMatrix[8] = vpn[0]; model.followerMatrix[9] = vpn[1]; model.followerMatrix[10] = vpn[2]; mat4.multiply(model.matrix, model.matrix, model.followerMatrix); } mat4.translate(model.matrix, model.matrix, [-model.origin[0], -model.origin[1], -model.origin[2]]); mat4.transpose(model.matrix, model.matrix); // check for identity model.isIdentity = false; model.matrixMTime.modified(); } }; } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- var DEFAULT_VALUES = { viewUp: [0, 1, 0], useViewUp: false, camera: null }; // ---------------------------------------------------------------------------- function extend(publicAPI, model) { var initialValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; Object.assign(model, DEFAULT_VALUES, initialValues); // Inheritance vtkActor.extend(publicAPI, model, initialValues); model.followerMatrix = mat4.identity(new Float64Array(16)); // Build VTK API macro.setGet(publicAPI, model, ['useViewUp', 'camera']); macro.setGetArray(publicAPI, model, ['viewUp'], 3); // Object methods vtkFollower(publicAPI, model); } // ---------------------------------------------------------------------------- var newInstance = macro.newInstance(extend, 'vtkFollower'); // ---------------------------------------------------------------------------- var vtkFollower$1 = { newInstance: newInstance, extend: extend }; export { vtkFollower$1 as default, extend, newInstance };