rotationMatrix.js 6.4 KB

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