UNPKG

@thewtex/vtk.js-esm

Version:

Visualization Toolkit for the Web

158 lines (123 loc) 5.37 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import macro from '../../macro.js'; import vtkBoundingBox from './BoundingBox.js'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } // Global methods // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // Static API // ---------------------------------------------------------------------------- var STATIC = {}; // ---------------------------------------------------------------------------- // vtkBox methods // ---------------------------------------------------------------------------- function vtkBox(publicAPI, model) { // Set our className model.classHierarchy.push('vtkBox'); // TODO: replace with macro.setArray ? publicAPI.setBounds = function () { var boundsArray = []; for (var _len = arguments.length, bounds = new Array(_len), _key = 0; _key < _len; _key++) { bounds[_key] = arguments[_key]; } if (Array.isArray(bounds[0])) { boundsArray = bounds[0]; } else { for (var i = 0; i < bounds.length; i++) { boundsArray.push(bounds[i]); } } if (boundsArray.length !== 6) { console.log('vtkBox.setBounds', boundsArray, bounds); return; } vtkBoundingBox.setBounds(model.bbox, boundsArray); }; publicAPI.getBounds = function () { return model.bbox; }; publicAPI.evaluateFunction = function (x, y, z) { var point = Array.isArray(x) ? x : [x, y, z]; var diff; var dist; var t; var minDistance = -Number.MAX_VALUE; var distance = 0; var minPoint = vtkBoundingBox.getMinPoint(model.bbox); var maxPoint = vtkBoundingBox.getMaxPoint(model.bbox); var inside = 1; for (var i = 0; i < 3; i++) { diff = vtkBoundingBox.getLength(model.bbox, i); if (diff !== 0.0) { t = (point[i] - minPoint[i]) / diff; if (t < 0.0) { inside = 0; dist = minPoint[i] - point[i]; } else if (t > 1.0) { inside = 0; dist = point[i] - maxPoint[i]; } else { // want negative distance, we are inside if (t <= 0.5) { dist = minPoint[i] - point[i]; } else { dist = point[i] - maxPoint[i]; } if (dist > minDistance) { // remember, it's engative minDistance = dist; } } // end if inside } else { dist = Math.abs(point[i] - minPoint[i]); if (dist > 0.0) { inside = 0; } } if (dist > 0.0) { distance += dist * dist; } } // end for i distance = Math.sqrt(distance); if (inside) { return minDistance; } return distance; }; publicAPI.addBounds = function () { var boundsArray = []; if (Array.isArray(arguments.length <= 0 ? undefined : arguments[0])) { boundsArray = arguments.length <= 0 ? undefined : arguments[0]; } else { for (var i = 0; i < arguments.length; i++) { boundsArray.push(i < 0 || arguments.length <= i ? undefined : arguments[i]); } } if (boundsArray.length !== 6) { return; } vtkBoundingBox.addBounds.apply(vtkBoundingBox, [model.bbox].concat(_toConsumableArray(boundsArray))); publicAPI.modified(); }; publicAPI.addBox = function (other) { return publicAPI.addBounds(other.getBounds()); }; } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- var DEFAULT_VALUES = { bbox: _toConsumableArray(vtkBoundingBox.INIT_BOUNDS) }; // ---------------------------------------------------------------------------- function extend(publicAPI, model) { var initialValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; Object.assign(model, DEFAULT_VALUES, initialValues); // Object methods macro.obj(publicAPI, model); vtkBox(publicAPI, model); } // ---------------------------------------------------------------------------- var newInstance = macro.newInstance(extend, 'vtkBox'); // ---------------------------------------------------------------------------- var vtkBox$1 = _objectSpread({ newInstance: newInstance, extend: extend }, STATIC); export default vtkBox$1; export { STATIC, extend, newInstance };