UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

125 lines (116 loc) 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.remapClamp = exports.remap = exports.default = void 0; var _Node = _interopRequireDefault(require("../core/Node.js")); var _TSLCore = require("../tsl/TSLCore.js"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * This node allows to remap a node value from one range into another. E.g a value of * `0.4` in the range `[ 0.3, 0.5 ]` should be remapped into the normalized range `[ 0, 1 ]`. * `RemapNode` takes care of that and converts the original value of `0.4` to `0.5`. * * @augments Node */ class RemapNode extends _Node.default { static get type() { return 'RemapNode'; } /** * Constructs a new remap node. * * @param {Node} node - The node that should be remapped. * @param {Node} inLowNode - The source or current lower bound of the range. * @param {Node} inHighNode - The source or current upper bound of the range. * @param {Node} [outLowNode=float(0)] - The target lower bound of the range. * @param {Node} [outHighNode=float(1)] - The target upper bound of the range. */ constructor(node, inLowNode, inHighNode, outLowNode = (0, _TSLCore.float)(0), outHighNode = (0, _TSLCore.float)(1)) { super(); /** * The node that should be remapped. * * @type {Node} */ this.node = node; /** * The source or current lower bound of the range. * * @type {Node} */ this.inLowNode = inLowNode; /** * The source or current upper bound of the range. * * @type {Node} */ this.inHighNode = inHighNode; /** * The target lower bound of the range. * * @type {Node} * @default float(0) */ this.outLowNode = outLowNode; /** * The target upper bound of the range. * * @type {Node} * @default float(1) */ this.outHighNode = outHighNode; /** * Whether the node value should be clamped before * remapping it to the target range. * * @type {boolean} * @default true */ this.doClamp = true; } setup() { const { node, inLowNode, inHighNode, outLowNode, outHighNode, doClamp } = this; let t = node.sub(inLowNode).div(inHighNode.sub(inLowNode)); if (doClamp === true) t = t.clamp(); return t.mul(outHighNode.sub(outLowNode)).add(outLowNode); } } var _default = exports.default = RemapNode; /** * TSL function for creating a remap node. * * @tsl * @function * @param {Node} node - The node that should be remapped. * @param {Node} inLowNode - The source or current lower bound of the range. * @param {Node} inHighNode - The source or current upper bound of the range. * @param {?Node} [outLowNode=float(0)] - The target lower bound of the range. * @param {?Node} [outHighNode=float(1)] - The target upper bound of the range. * @returns {RemapNode} */ const remap = exports.remap = /*@__PURE__*/(0, _TSLCore.nodeProxy)(RemapNode, null, null, { doClamp: false }).setParameterLength(3, 5); /** * TSL function for creating a remap node, but with enabled clamping. * * @tsl * @function * @param {Node} node - The node that should be remapped. * @param {Node} inLowNode - The source or current lower bound of the range. * @param {Node} inHighNode - The source or current upper bound of the range. * @param {?Node} [outLowNode=float(0)] - The target lower bound of the range. * @param {?Node} [outHighNode=float(1)] - The target upper bound of the range. * @returns {RemapNode} */ const remapClamp = exports.remapClamp = /*@__PURE__*/(0, _TSLCore.nodeProxy)(RemapNode).setParameterLength(3, 5); (0, _TSLCore.addMethodChaining)('remap', remap); (0, _TSLCore.addMethodChaining)('remapClamp', remapClamp);