rotationMatrix.js (6961B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createRotationMatrix = void 0; 7 8 var _is = require("../../utils/is.js"); 9 10 var _factory = require("../../utils/factory.js"); 11 12 var name = 'rotationMatrix'; 13 var dependencies = ['typed', 'config', 'multiplyScalar', 'addScalar', 'unaryMinus', 'norm', 'matrix', 'BigNumber', 'DenseMatrix', 'SparseMatrix', 'cos', 'sin']; 14 var createRotationMatrix = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 15 var typed = _ref.typed, 16 config = _ref.config, 17 multiplyScalar = _ref.multiplyScalar, 18 addScalar = _ref.addScalar, 19 unaryMinus = _ref.unaryMinus, 20 norm = _ref.norm, 21 BigNumber = _ref.BigNumber, 22 matrix = _ref.matrix, 23 DenseMatrix = _ref.DenseMatrix, 24 SparseMatrix = _ref.SparseMatrix, 25 cos = _ref.cos, 26 sin = _ref.sin; 27 28 /** 29 * Create a 2-dimensional counter-clockwise rotation matrix (2x2) for a given angle (expressed in radians). 30 * Create a 2-dimensional counter-clockwise rotation matrix (3x3) by a given angle (expressed in radians) around a given axis (1x3). 31 * 32 * Syntax: 33 * 34 * math.rotationMatrix(theta) 35 * math.rotationMatrix(theta, format) 36 * math.rotationMatrix(theta, [v]) 37 * math.rotationMatrix(theta, [v], format) 38 * 39 * Examples: 40 * 41 * math.rotationMatrix(math.pi / 2) // returns [[0, -1], [1, 0]] 42 * math.rotationMatrix(math.bignumber(1)) // returns [[bignumber(cos(1)), bignumber(-sin(1))], [bignumber(sin(1)), bignumber(cos(1))]] 43 * math.rotationMatrix(math.complex(1 + i)) // returns [[cos(1 + i), -sin(1 + i)], [sin(1 + i), cos(1 + i)]] 44 * math.rotationMatrix(math.unit('1rad')) // returns [[cos(1), -sin(1)], [sin(1), cos(1)]] 45 * 46 * math.rotationMatrix(math.pi / 2, [0, 1, 0]) // returns [[0, 0, 1], [0, 1, 0], [-1, 0, 0]] 47 * math.rotationMatrix(math.pi / 2, matrix([0, 1, 0])) // returns matrix([[0, 0, 1], [0, 1, 0], [-1, 0, 0]]) 48 * 49 * 50 * See also: 51 * 52 * matrix, cos, sin 53 * 54 * 55 * @param {number | BigNumber | Complex | Unit} theta Rotation angle 56 * @param {Array | Matrix} [v] Rotation axis 57 * @param {string} [format] Result Matrix storage format 58 * @return {Array | Matrix} Rotation matrix 59 */ 60 return typed(name, { 61 '': function _() { 62 return config.matrix === 'Matrix' ? matrix([]) : []; 63 }, 64 string: function string(format) { 65 return matrix(format); 66 }, 67 'number | BigNumber | Complex | Unit': function numberBigNumberComplexUnit(theta) { 68 return _rotationMatrix2x2(theta, config.matrix === 'Matrix' ? 'dense' : undefined); 69 }, 70 'number | BigNumber | Complex | Unit, string': function numberBigNumberComplexUnitString(theta, format) { 71 return _rotationMatrix2x2(theta, format); 72 }, 73 'number | BigNumber | Complex | Unit, Array': function numberBigNumberComplexUnitArray(theta, v) { 74 var matrixV = matrix(v); 75 76 _validateVector(matrixV); 77 78 return _rotationMatrix3x3(theta, matrixV, undefined); 79 }, 80 'number | BigNumber | Complex | Unit, Matrix': function numberBigNumberComplexUnitMatrix(theta, v) { 81 _validateVector(v); 82 83 var storageType = v.storage() || (config.matrix === 'Matrix' ? 'dense' : undefined); 84 return _rotationMatrix3x3(theta, v, storageType); 85 }, 86 'number | BigNumber | Complex | Unit, Array, string': function numberBigNumberComplexUnitArrayString(theta, v, format) { 87 var matrixV = matrix(v); 88 89 _validateVector(matrixV); 90 91 return _rotationMatrix3x3(theta, matrixV, format); 92 }, 93 'number | BigNumber | Complex | Unit, Matrix, string': function numberBigNumberComplexUnitMatrixString(theta, v, format) { 94 _validateVector(v); 95 96 return _rotationMatrix3x3(theta, v, format); 97 } 98 }); 99 /** 100 * Returns 2x2 matrix of 2D rotation of angle theta 101 * 102 * @param {number | BigNumber | Complex | Unit} theta The rotation angle 103 * @param {string} format The result Matrix storage format 104 * @returns {Matrix} 105 * @private 106 */ 107 108 function _rotationMatrix2x2(theta, format) { 109 var Big = (0, _is.isBigNumber)(theta); 110 var minusOne = Big ? new BigNumber(-1) : -1; 111 var cosTheta = cos(theta); 112 var sinTheta = sin(theta); 113 var data = [[cosTheta, multiplyScalar(minusOne, sinTheta)], [sinTheta, cosTheta]]; 114 return _convertToFormat(data, format); 115 } 116 117 function _validateVector(v) { 118 var size = v.size(); 119 120 if (size.length < 1 || size[0] !== 3) { 121 throw new RangeError('Vector must be of dimensions 1x3'); 122 } 123 } 124 125 function _mul(array) { 126 return array.reduce(function (p, curr) { 127 return multiplyScalar(p, curr); 128 }); 129 } 130 131 function _convertToFormat(data, format) { 132 if (format) { 133 if (format === 'sparse') { 134 return new SparseMatrix(data); 135 } 136 137 if (format === 'dense') { 138 return new DenseMatrix(data); 139 } 140 141 throw new TypeError("Unknown matrix type \"".concat(format, "\"")); 142 } 143 144 return data; 145 } 146 /** 147 * Returns a 3x3 matrix of rotation of angle theta around vector v 148 * 149 * @param {number | BigNumber | Complex | Unit} theta The rotation angle 150 * @param {Matrix} v The rotation axis vector 151 * @param {string} format The storage format of the resulting matrix 152 * @returns {Matrix} 153 * @private 154 */ 155 156 157 function _rotationMatrix3x3(theta, v, format) { 158 var normV = norm(v); 159 160 if (normV === 0) { 161 throw new RangeError('Rotation around zero vector'); 162 } 163 164 var Big = (0, _is.isBigNumber)(theta) ? BigNumber : null; 165 var one = Big ? new Big(1) : 1; 166 var minusOne = Big ? new Big(-1) : -1; 167 var vx = Big ? new Big(v.get([0]) / normV) : v.get([0]) / normV; 168 var vy = Big ? new Big(v.get([1]) / normV) : v.get([1]) / normV; 169 var vz = Big ? new Big(v.get([2]) / normV) : v.get([2]) / normV; 170 var c = cos(theta); 171 var oneMinusC = addScalar(one, unaryMinus(c)); 172 var s = sin(theta); 173 var r11 = addScalar(c, _mul([vx, vx, oneMinusC])); 174 var r12 = addScalar(_mul([vx, vy, oneMinusC]), _mul([minusOne, vz, s])); 175 var r13 = addScalar(_mul([vx, vz, oneMinusC]), _mul([vy, s])); 176 var r21 = addScalar(_mul([vx, vy, oneMinusC]), _mul([vz, s])); 177 var r22 = addScalar(c, _mul([vy, vy, oneMinusC])); 178 var r23 = addScalar(_mul([vy, vz, oneMinusC]), _mul([minusOne, vx, s])); 179 var r31 = addScalar(_mul([vx, vz, oneMinusC]), _mul([minusOne, vy, s])); 180 var r32 = addScalar(_mul([vy, vz, oneMinusC]), _mul([vx, s])); 181 var r33 = addScalar(c, _mul([vz, vz, oneMinusC])); 182 var data = [[r11, r12, r13], [r21, r22, r23], [r31, r32, r33]]; 183 return _convertToFormat(data, format); 184 } 185 }); 186 exports.createRotationMatrix = createRotationMatrix;