Matrix2.js 30 KB

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