Matrix2.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. import Cartesian2 from "./Cartesian2.js";
  2. import Check from "./Check.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. /**
  6. * A 2x2 matrix, indexable as a column-major order array.
  7. * Constructor parameters are in row-major order for code readability.
  8. * @alias Matrix2
  9. * @constructor
  10. * @implements {ArrayLike<number>}
  11. *
  12. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  13. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  14. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  15. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  16. *
  17. * @see Matrix2.fromColumnMajorArray
  18. * @see Matrix2.fromRowMajorArray
  19. * @see Matrix2.fromScale
  20. * @see Matrix2.fromUniformScale
  21. * @see Matrix3
  22. * @see Matrix4
  23. */
  24. function Matrix2(column0Row0, column1Row0, column0Row1, column1Row1) {
  25. this[0] = defaultValue(column0Row0, 0.0);
  26. this[1] = defaultValue(column0Row1, 0.0);
  27. this[2] = defaultValue(column1Row0, 0.0);
  28. this[3] = defaultValue(column1Row1, 0.0);
  29. }
  30. /**
  31. * The number of elements used to pack the object into an array.
  32. * @type {Number}
  33. */
  34. Matrix2.packedLength = 4;
  35. /**
  36. * Stores the provided instance into the provided array.
  37. *
  38. * @param {Matrix2} value The value to pack.
  39. * @param {Number[]} array The array to pack into.
  40. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  41. *
  42. * @returns {Number[]} The array that was packed into
  43. */
  44. Matrix2.pack = function (value, array, startingIndex) {
  45. //>>includeStart('debug', pragmas.debug);
  46. Check.typeOf.object("value", value);
  47. Check.defined("array", array);
  48. //>>includeEnd('debug');
  49. startingIndex = defaultValue(startingIndex, 0);
  50. array[startingIndex++] = value[0];
  51. array[startingIndex++] = value[1];
  52. array[startingIndex++] = value[2];
  53. array[startingIndex++] = value[3];
  54. return array;
  55. };
  56. /**
  57. * Retrieves an instance from a packed array.
  58. *
  59. * @param {Number[]} array The packed array.
  60. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  61. * @param {Matrix2} [result] The object into which to store the result.
  62. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  63. */
  64. Matrix2.unpack = function (array, startingIndex, result) {
  65. //>>includeStart('debug', pragmas.debug);
  66. Check.defined("array", array);
  67. //>>includeEnd('debug');
  68. startingIndex = defaultValue(startingIndex, 0);
  69. if (!defined(result)) {
  70. result = new Matrix2();
  71. }
  72. result[0] = array[startingIndex++];
  73. result[1] = array[startingIndex++];
  74. result[2] = array[startingIndex++];
  75. result[3] = array[startingIndex++];
  76. return result;
  77. };
  78. /**
  79. * Duplicates a Matrix2 instance.
  80. *
  81. * @param {Matrix2} matrix The matrix to duplicate.
  82. * @param {Matrix2} [result] The object onto which to store the result.
  83. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided. (Returns undefined if matrix is undefined)
  84. */
  85. Matrix2.clone = function (matrix, result) {
  86. if (!defined(matrix)) {
  87. return undefined;
  88. }
  89. if (!defined(result)) {
  90. return new Matrix2(matrix[0], matrix[2], matrix[1], matrix[3]);
  91. }
  92. result[0] = matrix[0];
  93. result[1] = matrix[1];
  94. result[2] = matrix[2];
  95. result[3] = matrix[3];
  96. return result;
  97. };
  98. /**
  99. * Creates a Matrix2 from 4 consecutive elements in an array.
  100. *
  101. * @param {Number[]} array The array whose 4 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  102. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  103. * @param {Matrix2} [result] The object onto which to store the result.
  104. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  105. *
  106. * @example
  107. * // Create the Matrix2:
  108. * // [1.0, 2.0]
  109. * // [1.0, 2.0]
  110. *
  111. * var v = [1.0, 1.0, 2.0, 2.0];
  112. * var m = Cesium.Matrix2.fromArray(v);
  113. *
  114. * // Create same Matrix2 with using an offset into an array
  115. * var v2 = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0];
  116. * var m2 = Cesium.Matrix2.fromArray(v2, 2);
  117. */
  118. Matrix2.fromArray = function (array, startingIndex, result) {
  119. //>>includeStart('debug', pragmas.debug);
  120. Check.defined("array", array);
  121. //>>includeEnd('debug');
  122. startingIndex = defaultValue(startingIndex, 0);
  123. if (!defined(result)) {
  124. result = new Matrix2();
  125. }
  126. result[0] = array[startingIndex];
  127. result[1] = array[startingIndex + 1];
  128. result[2] = array[startingIndex + 2];
  129. result[3] = array[startingIndex + 3];
  130. return result;
  131. };
  132. /**
  133. * Creates a Matrix2 instance from a column-major order array.
  134. *
  135. * @param {Number[]} values The column-major order array.
  136. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  137. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  138. */
  139. Matrix2.fromColumnMajorArray = function (values, result) {
  140. //>>includeStart('debug', pragmas.debug);
  141. Check.defined("values", values);
  142. //>>includeEnd('debug');
  143. return Matrix2.clone(values, result);
  144. };
  145. /**
  146. * Creates a Matrix2 instance from a row-major order array.
  147. * The resulting matrix will be in column-major order.
  148. *
  149. * @param {Number[]} values The row-major order array.
  150. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  151. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  152. */
  153. Matrix2.fromRowMajorArray = function (values, result) {
  154. //>>includeStart('debug', pragmas.debug);
  155. Check.defined("values", values);
  156. //>>includeEnd('debug');
  157. if (!defined(result)) {
  158. return new Matrix2(values[0], values[1], values[2], values[3]);
  159. }
  160. result[0] = values[0];
  161. result[1] = values[2];
  162. result[2] = values[1];
  163. result[3] = values[3];
  164. return result;
  165. };
  166. /**
  167. * Computes a Matrix2 instance representing a non-uniform scale.
  168. *
  169. * @param {Cartesian2} scale The x and y scale factors.
  170. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  171. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  172. *
  173. * @example
  174. * // Creates
  175. * // [7.0, 0.0]
  176. * // [0.0, 8.0]
  177. * var m = Cesium.Matrix2.fromScale(new Cesium.Cartesian2(7.0, 8.0));
  178. */
  179. Matrix2.fromScale = function (scale, result) {
  180. //>>includeStart('debug', pragmas.debug);
  181. Check.typeOf.object("scale", scale);
  182. //>>includeEnd('debug');
  183. if (!defined(result)) {
  184. return new Matrix2(scale.x, 0.0, 0.0, scale.y);
  185. }
  186. result[0] = scale.x;
  187. result[1] = 0.0;
  188. result[2] = 0.0;
  189. result[3] = scale.y;
  190. return result;
  191. };
  192. /**
  193. * Computes a Matrix2 instance representing a uniform scale.
  194. *
  195. * @param {Number} scale The uniform scale factor.
  196. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  197. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  198. *
  199. * @example
  200. * // Creates
  201. * // [2.0, 0.0]
  202. * // [0.0, 2.0]
  203. * var m = Cesium.Matrix2.fromUniformScale(2.0);
  204. */
  205. Matrix2.fromUniformScale = function (scale, result) {
  206. //>>includeStart('debug', pragmas.debug);
  207. Check.typeOf.number("scale", scale);
  208. //>>includeEnd('debug');
  209. if (!defined(result)) {
  210. return new Matrix2(scale, 0.0, 0.0, scale);
  211. }
  212. result[0] = scale;
  213. result[1] = 0.0;
  214. result[2] = 0.0;
  215. result[3] = scale;
  216. return result;
  217. };
  218. /**
  219. * Creates a rotation matrix.
  220. *
  221. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  222. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  223. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  224. *
  225. * @example
  226. * // Rotate a point 45 degrees counterclockwise.
  227. * var p = new Cesium.Cartesian2(5, 6);
  228. * var m = Cesium.Matrix2.fromRotation(Cesium.Math.toRadians(45.0));
  229. * var rotated = Cesium.Matrix2.multiplyByVector(m, p, new Cesium.Cartesian2());
  230. */
  231. Matrix2.fromRotation = function (angle, result) {
  232. //>>includeStart('debug', pragmas.debug);
  233. Check.typeOf.number("angle", angle);
  234. //>>includeEnd('debug');
  235. var cosAngle = Math.cos(angle);
  236. var sinAngle = Math.sin(angle);
  237. if (!defined(result)) {
  238. return new Matrix2(cosAngle, -sinAngle, sinAngle, cosAngle);
  239. }
  240. result[0] = cosAngle;
  241. result[1] = sinAngle;
  242. result[2] = -sinAngle;
  243. result[3] = cosAngle;
  244. return result;
  245. };
  246. /**
  247. * Creates an Array from the provided Matrix2 instance.
  248. * The array will be in column-major order.
  249. *
  250. * @param {Matrix2} matrix The matrix to use..
  251. * @param {Number[]} [result] The Array onto which to store the result.
  252. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  253. */
  254. Matrix2.toArray = function (matrix, result) {
  255. //>>includeStart('debug', pragmas.debug);
  256. Check.typeOf.object("matrix", matrix);
  257. //>>includeEnd('debug');
  258. if (!defined(result)) {
  259. return [matrix[0], matrix[1], matrix[2], matrix[3]];
  260. }
  261. result[0] = matrix[0];
  262. result[1] = matrix[1];
  263. result[2] = matrix[2];
  264. result[3] = matrix[3];
  265. return result;
  266. };
  267. /**
  268. * Computes the array index of the element at the provided row and column.
  269. *
  270. * @param {Number} row The zero-based index of the row.
  271. * @param {Number} column The zero-based index of the column.
  272. * @returns {Number} The index of the element at the provided row and column.
  273. *
  274. * @exception {DeveloperError} row must be 0 or 1.
  275. * @exception {DeveloperError} column must be 0 or 1.
  276. *
  277. * @example
  278. * var myMatrix = new Cesium.Matrix2();
  279. * var column1Row0Index = Cesium.Matrix2.getElementIndex(1, 0);
  280. * var column1Row0 = myMatrix[column1Row0Index]
  281. * myMatrix[column1Row0Index] = 10.0;
  282. */
  283. Matrix2.getElementIndex = function (column, row) {
  284. //>>includeStart('debug', pragmas.debug);
  285. Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  286. Check.typeOf.number.lessThanOrEquals("row", row, 1);
  287. Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  288. Check.typeOf.number.lessThanOrEquals("column", column, 1);
  289. //>>includeEnd('debug');
  290. return column * 2 + row;
  291. };
  292. /**
  293. * Retrieves a copy of the matrix column at the provided index as a Cartesian2 instance.
  294. *
  295. * @param {Matrix2} matrix The matrix to use.
  296. * @param {Number} index The zero-based index of the column to retrieve.
  297. * @param {Cartesian2} result The object onto which to store the result.
  298. * @returns {Cartesian2} The modified result parameter.
  299. *
  300. * @exception {DeveloperError} index must be 0 or 1.
  301. */
  302. Matrix2.getColumn = function (matrix, index, result) {
  303. //>>includeStart('debug', pragmas.debug);
  304. Check.typeOf.object("matrix", matrix);
  305. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  306. Check.typeOf.number.lessThanOrEquals("index", index, 1);
  307. Check.typeOf.object("result", result);
  308. //>>includeEnd('debug');
  309. var startIndex = index * 2;
  310. var x = matrix[startIndex];
  311. var y = matrix[startIndex + 1];
  312. result.x = x;
  313. result.y = y;
  314. return result;
  315. };
  316. /**
  317. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian2 instance.
  318. *
  319. * @param {Matrix2} matrix The matrix to use.
  320. * @param {Number} index The zero-based index of the column to set.
  321. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified column.
  322. * @param {Cartesian2} result The object onto which to store the result.
  323. * @returns {Matrix2} The modified result parameter.
  324. *
  325. * @exception {DeveloperError} index must be 0 or 1.
  326. */
  327. Matrix2.setColumn = function (matrix, index, cartesian, result) {
  328. //>>includeStart('debug', pragmas.debug);
  329. Check.typeOf.object("matrix", matrix);
  330. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  331. Check.typeOf.number.lessThanOrEquals("index", index, 1);
  332. Check.typeOf.object("cartesian", cartesian);
  333. Check.typeOf.object("result", result);
  334. //>>includeEnd('debug');
  335. result = Matrix2.clone(matrix, result);
  336. var startIndex = index * 2;
  337. result[startIndex] = cartesian.x;
  338. result[startIndex + 1] = cartesian.y;
  339. return result;
  340. };
  341. /**
  342. * Retrieves a copy of the matrix row at the provided index as a Cartesian2 instance.
  343. *
  344. * @param {Matrix2} matrix The matrix to use.
  345. * @param {Number} index The zero-based index of the row to retrieve.
  346. * @param {Cartesian2} result The object onto which to store the result.
  347. * @returns {Cartesian2} The modified result parameter.
  348. *
  349. * @exception {DeveloperError} index must be 0 or 1.
  350. */
  351. Matrix2.getRow = function (matrix, index, result) {
  352. //>>includeStart('debug', pragmas.debug);
  353. Check.typeOf.object("matrix", matrix);
  354. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  355. Check.typeOf.number.lessThanOrEquals("index", index, 1);
  356. Check.typeOf.object("result", result);
  357. //>>includeEnd('debug');
  358. var x = matrix[index];
  359. var y = matrix[index + 2];
  360. result.x = x;
  361. result.y = y;
  362. return result;
  363. };
  364. /**
  365. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian2 instance.
  366. *
  367. * @param {Matrix2} matrix The matrix to use.
  368. * @param {Number} index The zero-based index of the row to set.
  369. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified row.
  370. * @param {Matrix2} result The object onto which to store the result.
  371. * @returns {Matrix2} The modified result parameter.
  372. *
  373. * @exception {DeveloperError} index must be 0 or 1.
  374. */
  375. Matrix2.setRow = function (matrix, index, cartesian, result) {
  376. //>>includeStart('debug', pragmas.debug);
  377. Check.typeOf.object("matrix", matrix);
  378. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  379. Check.typeOf.number.lessThanOrEquals("index", index, 1);
  380. Check.typeOf.object("cartesian", cartesian);
  381. Check.typeOf.object("result", result);
  382. //>>includeEnd('debug');
  383. result = Matrix2.clone(matrix, result);
  384. result[index] = cartesian.x;
  385. result[index + 2] = cartesian.y;
  386. return result;
  387. };
  388. var scratchColumn = new Cartesian2();
  389. /**
  390. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  391. *
  392. * @param {Matrix2} matrix The matrix.
  393. * @param {Cartesian2} result The object onto which to store the result.
  394. * @returns {Cartesian2} The modified result parameter.
  395. */
  396. Matrix2.getScale = function (matrix, result) {
  397. //>>includeStart('debug', pragmas.debug);
  398. Check.typeOf.object("matrix", matrix);
  399. Check.typeOf.object("result", result);
  400. //>>includeEnd('debug');
  401. result.x = Cartesian2.magnitude(
  402. Cartesian2.fromElements(matrix[0], matrix[1], scratchColumn)
  403. );
  404. result.y = Cartesian2.magnitude(
  405. Cartesian2.fromElements(matrix[2], matrix[3], scratchColumn)
  406. );
  407. return result;
  408. };
  409. var scratchScale = new Cartesian2();
  410. /**
  411. * Computes the maximum scale assuming the matrix is an affine transformation.
  412. * The maximum scale is the maximum length of the column vectors.
  413. *
  414. * @param {Matrix2} matrix The matrix.
  415. * @returns {Number} The maximum scale.
  416. */
  417. Matrix2.getMaximumScale = function (matrix) {
  418. Matrix2.getScale(matrix, scratchScale);
  419. return Cartesian2.maximumComponent(scratchScale);
  420. };
  421. /**
  422. * Computes the product of two matrices.
  423. *
  424. * @param {Matrix2} left The first matrix.
  425. * @param {Matrix2} right The second matrix.
  426. * @param {Matrix2} result The object onto which to store the result.
  427. * @returns {Matrix2} The modified result parameter.
  428. */
  429. Matrix2.multiply = function (left, right, result) {
  430. //>>includeStart('debug', pragmas.debug);
  431. Check.typeOf.object("left", left);
  432. Check.typeOf.object("right", right);
  433. Check.typeOf.object("result", result);
  434. //>>includeEnd('debug');
  435. var column0Row0 = left[0] * right[0] + left[2] * right[1];
  436. var column1Row0 = left[0] * right[2] + left[2] * right[3];
  437. var column0Row1 = left[1] * right[0] + left[3] * right[1];
  438. var column1Row1 = left[1] * right[2] + left[3] * right[3];
  439. result[0] = column0Row0;
  440. result[1] = column0Row1;
  441. result[2] = column1Row0;
  442. result[3] = column1Row1;
  443. return result;
  444. };
  445. /**
  446. * Computes the sum of two matrices.
  447. *
  448. * @param {Matrix2} left The first matrix.
  449. * @param {Matrix2} right The second matrix.
  450. * @param {Matrix2} result The object onto which to store the result.
  451. * @returns {Matrix2} The modified result parameter.
  452. */
  453. Matrix2.add = function (left, right, result) {
  454. //>>includeStart('debug', pragmas.debug);
  455. Check.typeOf.object("left", left);
  456. Check.typeOf.object("right", right);
  457. Check.typeOf.object("result", result);
  458. //>>includeEnd('debug');
  459. result[0] = left[0] + right[0];
  460. result[1] = left[1] + right[1];
  461. result[2] = left[2] + right[2];
  462. result[3] = left[3] + right[3];
  463. return result;
  464. };
  465. /**
  466. * Computes the difference of two matrices.
  467. *
  468. * @param {Matrix2} left The first matrix.
  469. * @param {Matrix2} right The second matrix.
  470. * @param {Matrix2} result The object onto which to store the result.
  471. * @returns {Matrix2} The modified result parameter.
  472. */
  473. Matrix2.subtract = function (left, right, result) {
  474. //>>includeStart('debug', pragmas.debug);
  475. Check.typeOf.object("left", left);
  476. Check.typeOf.object("right", right);
  477. Check.typeOf.object("result", result);
  478. //>>includeEnd('debug');
  479. result[0] = left[0] - right[0];
  480. result[1] = left[1] - right[1];
  481. result[2] = left[2] - right[2];
  482. result[3] = left[3] - right[3];
  483. return result;
  484. };
  485. /**
  486. * Computes the product of a matrix and a column vector.
  487. *
  488. * @param {Matrix2} matrix The matrix.
  489. * @param {Cartesian2} cartesian The column.
  490. * @param {Cartesian2} result The object onto which to store the result.
  491. * @returns {Cartesian2} The modified result parameter.
  492. */
  493. Matrix2.multiplyByVector = function (matrix, cartesian, result) {
  494. //>>includeStart('debug', pragmas.debug);
  495. Check.typeOf.object("matrix", matrix);
  496. Check.typeOf.object("cartesian", cartesian);
  497. Check.typeOf.object("result", result);
  498. //>>includeEnd('debug');
  499. var x = matrix[0] * cartesian.x + matrix[2] * cartesian.y;
  500. var y = matrix[1] * cartesian.x + matrix[3] * cartesian.y;
  501. result.x = x;
  502. result.y = y;
  503. return result;
  504. };
  505. /**
  506. * Computes the product of a matrix and a scalar.
  507. *
  508. * @param {Matrix2} matrix The matrix.
  509. * @param {Number} scalar The number to multiply by.
  510. * @param {Matrix2} result The object onto which to store the result.
  511. * @returns {Matrix2} The modified result parameter.
  512. */
  513. Matrix2.multiplyByScalar = function (matrix, scalar, result) {
  514. //>>includeStart('debug', pragmas.debug);
  515. Check.typeOf.object("matrix", matrix);
  516. Check.typeOf.number("scalar", scalar);
  517. Check.typeOf.object("result", result);
  518. //>>includeEnd('debug');
  519. result[0] = matrix[0] * scalar;
  520. result[1] = matrix[1] * scalar;
  521. result[2] = matrix[2] * scalar;
  522. result[3] = matrix[3] * scalar;
  523. return result;
  524. };
  525. /**
  526. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  527. *
  528. * @param {Matrix2} matrix The matrix on the left-hand side.
  529. * @param {Cartesian2} scale The non-uniform scale on the right-hand side.
  530. * @param {Matrix2} result The object onto which to store the result.
  531. * @returns {Matrix2} The modified result parameter.
  532. *
  533. *
  534. * @example
  535. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
  536. * Cesium.Matrix2.multiplyByScale(m, scale, m);
  537. *
  538. * @see Matrix2.fromScale
  539. * @see Matrix2.multiplyByUniformScale
  540. */
  541. Matrix2.multiplyByScale = function (matrix, scale, result) {
  542. //>>includeStart('debug', pragmas.debug);
  543. Check.typeOf.object("matrix", matrix);
  544. Check.typeOf.object("scale", scale);
  545. Check.typeOf.object("result", result);
  546. //>>includeEnd('debug');
  547. result[0] = matrix[0] * scale.x;
  548. result[1] = matrix[1] * scale.x;
  549. result[2] = matrix[2] * scale.y;
  550. result[3] = matrix[3] * scale.y;
  551. return result;
  552. };
  553. /**
  554. * Creates a negated copy of the provided matrix.
  555. *
  556. * @param {Matrix2} matrix The matrix to negate.
  557. * @param {Matrix2} result The object onto which to store the result.
  558. * @returns {Matrix2} The modified result parameter.
  559. */
  560. Matrix2.negate = function (matrix, result) {
  561. //>>includeStart('debug', pragmas.debug);
  562. Check.typeOf.object("matrix", matrix);
  563. Check.typeOf.object("result", result);
  564. //>>includeEnd('debug');
  565. result[0] = -matrix[0];
  566. result[1] = -matrix[1];
  567. result[2] = -matrix[2];
  568. result[3] = -matrix[3];
  569. return result;
  570. };
  571. /**
  572. * Computes the transpose of the provided matrix.
  573. *
  574. * @param {Matrix2} matrix The matrix to transpose.
  575. * @param {Matrix2} result The object onto which to store the result.
  576. * @returns {Matrix2} The modified result parameter.
  577. */
  578. Matrix2.transpose = function (matrix, result) {
  579. //>>includeStart('debug', pragmas.debug);
  580. Check.typeOf.object("matrix", matrix);
  581. Check.typeOf.object("result", result);
  582. //>>includeEnd('debug');
  583. var column0Row0 = matrix[0];
  584. var column0Row1 = matrix[2];
  585. var column1Row0 = matrix[1];
  586. var column1Row1 = matrix[3];
  587. result[0] = column0Row0;
  588. result[1] = column0Row1;
  589. result[2] = column1Row0;
  590. result[3] = column1Row1;
  591. return result;
  592. };
  593. /**
  594. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  595. *
  596. * @param {Matrix2} matrix The matrix with signed elements.
  597. * @param {Matrix2} result The object onto which to store the result.
  598. * @returns {Matrix2} The modified result parameter.
  599. */
  600. Matrix2.abs = function (matrix, result) {
  601. //>>includeStart('debug', pragmas.debug);
  602. Check.typeOf.object("matrix", matrix);
  603. Check.typeOf.object("result", result);
  604. //>>includeEnd('debug');
  605. result[0] = Math.abs(matrix[0]);
  606. result[1] = Math.abs(matrix[1]);
  607. result[2] = Math.abs(matrix[2]);
  608. result[3] = Math.abs(matrix[3]);
  609. return result;
  610. };
  611. /**
  612. * Compares the provided matrices componentwise and returns
  613. * <code>true</code> if they are equal, <code>false</code> otherwise.
  614. *
  615. * @param {Matrix2} [left] The first matrix.
  616. * @param {Matrix2} [right] The second matrix.
  617. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  618. */
  619. Matrix2.equals = function (left, right) {
  620. return (
  621. left === right ||
  622. (defined(left) &&
  623. defined(right) &&
  624. left[0] === right[0] &&
  625. left[1] === right[1] &&
  626. left[2] === right[2] &&
  627. left[3] === right[3])
  628. );
  629. };
  630. /**
  631. * @private
  632. */
  633. Matrix2.equalsArray = function (matrix, array, offset) {
  634. return (
  635. matrix[0] === array[offset] &&
  636. matrix[1] === array[offset + 1] &&
  637. matrix[2] === array[offset + 2] &&
  638. matrix[3] === array[offset + 3]
  639. );
  640. };
  641. /**
  642. * Compares the provided matrices componentwise and returns
  643. * <code>true</code> if they are within the provided epsilon,
  644. * <code>false</code> otherwise.
  645. *
  646. * @param {Matrix2} [left] The first matrix.
  647. * @param {Matrix2} [right] The second matrix.
  648. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  649. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  650. */
  651. Matrix2.equalsEpsilon = function (left, right, epsilon) {
  652. epsilon = defaultValue(epsilon, 0);
  653. return (
  654. left === right ||
  655. (defined(left) &&
  656. defined(right) &&
  657. Math.abs(left[0] - right[0]) <= epsilon &&
  658. Math.abs(left[1] - right[1]) <= epsilon &&
  659. Math.abs(left[2] - right[2]) <= epsilon &&
  660. Math.abs(left[3] - right[3]) <= epsilon)
  661. );
  662. };
  663. /**
  664. * An immutable Matrix2 instance initialized to the identity matrix.
  665. *
  666. * @type {Matrix2}
  667. * @constant
  668. */
  669. Matrix2.IDENTITY = Object.freeze(new Matrix2(1.0, 0.0, 0.0, 1.0));
  670. /**
  671. * An immutable Matrix2 instance initialized to the zero matrix.
  672. *
  673. * @type {Matrix2}
  674. * @constant
  675. */
  676. Matrix2.ZERO = Object.freeze(new Matrix2(0.0, 0.0, 0.0, 0.0));
  677. /**
  678. * The index into Matrix2 for column 0, row 0.
  679. *
  680. * @type {Number}
  681. * @constant
  682. *
  683. * @example
  684. * var matrix = new Cesium.Matrix2();
  685. * matrix[Cesium.Matrix2.COLUMN0ROW0] = 5.0; // set column 0, row 0 to 5.0
  686. */
  687. Matrix2.COLUMN0ROW0 = 0;
  688. /**
  689. * The index into Matrix2 for column 0, row 1.
  690. *
  691. * @type {Number}
  692. * @constant
  693. *
  694. * @example
  695. * var matrix = new Cesium.Matrix2();
  696. * matrix[Cesium.Matrix2.COLUMN0ROW1] = 5.0; // set column 0, row 1 to 5.0
  697. */
  698. Matrix2.COLUMN0ROW1 = 1;
  699. /**
  700. * The index into Matrix2 for column 1, row 0.
  701. *
  702. * @type {Number}
  703. * @constant
  704. *
  705. * @example
  706. * var matrix = new Cesium.Matrix2();
  707. * matrix[Cesium.Matrix2.COLUMN1ROW0] = 5.0; // set column 1, row 0 to 5.0
  708. */
  709. Matrix2.COLUMN1ROW0 = 2;
  710. /**
  711. * The index into Matrix2 for column 1, row 1.
  712. *
  713. * @type {Number}
  714. * @constant
  715. *
  716. * @example
  717. * var matrix = new Cesium.Matrix2();
  718. * matrix[Cesium.Matrix2.COLUMN1ROW1] = 5.0; // set column 1, row 1 to 5.0
  719. */
  720. Matrix2.COLUMN1ROW1 = 3;
  721. Object.defineProperties(Matrix2.prototype, {
  722. /**
  723. * Gets the number of items in the collection.
  724. * @memberof Matrix2.prototype
  725. *
  726. * @type {Number}
  727. */
  728. length: {
  729. get: function () {
  730. return Matrix2.packedLength;
  731. },
  732. },
  733. });
  734. /**
  735. * Duplicates the provided Matrix2 instance.
  736. *
  737. * @param {Matrix2} [result] The object onto which to store the result.
  738. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  739. */
  740. Matrix2.prototype.clone = function (result) {
  741. return Matrix2.clone(this, result);
  742. };
  743. /**
  744. * Compares this matrix to the provided matrix componentwise and returns
  745. * <code>true</code> if they are equal, <code>false</code> otherwise.
  746. *
  747. * @param {Matrix2} [right] The right hand side matrix.
  748. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  749. */
  750. Matrix2.prototype.equals = function (right) {
  751. return Matrix2.equals(this, right);
  752. };
  753. /**
  754. * Compares this matrix to the provided matrix componentwise and returns
  755. * <code>true</code> if they are within the provided epsilon,
  756. * <code>false</code> otherwise.
  757. *
  758. * @param {Matrix2} [right] The right hand side matrix.
  759. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  760. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  761. */
  762. Matrix2.prototype.equalsEpsilon = function (right, epsilon) {
  763. return Matrix2.equalsEpsilon(this, right, epsilon);
  764. };
  765. /**
  766. * Creates a string representing this Matrix with each row being
  767. * on a separate line and in the format '(column0, column1)'.
  768. *
  769. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1)'.
  770. */
  771. Matrix2.prototype.toString = function () {
  772. return (
  773. "(" +
  774. this[0] +
  775. ", " +
  776. this[2] +
  777. ")\n" +
  778. "(" +
  779. this[1] +
  780. ", " +
  781. this[3] +
  782. ")"
  783. );
  784. };
  785. export default Matrix2;