Matrix3.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Check from "./Check.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. import DeveloperError from "./DeveloperError.js";
  6. import CesiumMath from "./Math.js";
  7. /**
  8. * A 3x3 matrix, indexable as a column-major order array.
  9. * Constructor parameters are in row-major order for code readability.
  10. * @alias Matrix3
  11. * @constructor
  12. * @implements {ArrayLike<number>}
  13. *
  14. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  15. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  16. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  17. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  18. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  19. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  20. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  21. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  22. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  23. *
  24. * @see Matrix3.fromColumnMajorArray
  25. * @see Matrix3.fromRowMajorArray
  26. * @see Matrix3.fromQuaternion
  27. * @see Matrix3.fromScale
  28. * @see Matrix3.fromUniformScale
  29. * @see Matrix2
  30. * @see Matrix4
  31. */
  32. function Matrix3(
  33. column0Row0,
  34. column1Row0,
  35. column2Row0,
  36. column0Row1,
  37. column1Row1,
  38. column2Row1,
  39. column0Row2,
  40. column1Row2,
  41. column2Row2
  42. ) {
  43. this[0] = defaultValue(column0Row0, 0.0);
  44. this[1] = defaultValue(column0Row1, 0.0);
  45. this[2] = defaultValue(column0Row2, 0.0);
  46. this[3] = defaultValue(column1Row0, 0.0);
  47. this[4] = defaultValue(column1Row1, 0.0);
  48. this[5] = defaultValue(column1Row2, 0.0);
  49. this[6] = defaultValue(column2Row0, 0.0);
  50. this[7] = defaultValue(column2Row1, 0.0);
  51. this[8] = defaultValue(column2Row2, 0.0);
  52. }
  53. /**
  54. * The number of elements used to pack the object into an array.
  55. * @type {Number}
  56. */
  57. Matrix3.packedLength = 9;
  58. /**
  59. * Stores the provided instance into the provided array.
  60. *
  61. * @param {Matrix3} value The value to pack.
  62. * @param {Number[]} array The array to pack into.
  63. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  64. *
  65. * @returns {Number[]} The array that was packed into
  66. */
  67. Matrix3.pack = function (value, array, startingIndex) {
  68. //>>includeStart('debug', pragmas.debug);
  69. Check.typeOf.object("value", value);
  70. Check.defined("array", array);
  71. //>>includeEnd('debug');
  72. startingIndex = defaultValue(startingIndex, 0);
  73. array[startingIndex++] = value[0];
  74. array[startingIndex++] = value[1];
  75. array[startingIndex++] = value[2];
  76. array[startingIndex++] = value[3];
  77. array[startingIndex++] = value[4];
  78. array[startingIndex++] = value[5];
  79. array[startingIndex++] = value[6];
  80. array[startingIndex++] = value[7];
  81. array[startingIndex++] = value[8];
  82. return array;
  83. };
  84. /**
  85. * Retrieves an instance from a packed array.
  86. *
  87. * @param {Number[]} array The packed array.
  88. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  89. * @param {Matrix3} [result] The object into which to store the result.
  90. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  91. */
  92. Matrix3.unpack = function (array, startingIndex, result) {
  93. //>>includeStart('debug', pragmas.debug);
  94. Check.defined("array", array);
  95. //>>includeEnd('debug');
  96. startingIndex = defaultValue(startingIndex, 0);
  97. if (!defined(result)) {
  98. result = new Matrix3();
  99. }
  100. result[0] = array[startingIndex++];
  101. result[1] = array[startingIndex++];
  102. result[2] = array[startingIndex++];
  103. result[3] = array[startingIndex++];
  104. result[4] = array[startingIndex++];
  105. result[5] = array[startingIndex++];
  106. result[6] = array[startingIndex++];
  107. result[7] = array[startingIndex++];
  108. result[8] = array[startingIndex++];
  109. return result;
  110. };
  111. /**
  112. * Duplicates a Matrix3 instance.
  113. *
  114. * @param {Matrix3} matrix The matrix to duplicate.
  115. * @param {Matrix3} [result] The object onto which to store the result.
  116. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  117. */
  118. Matrix3.clone = function (matrix, result) {
  119. if (!defined(matrix)) {
  120. return undefined;
  121. }
  122. if (!defined(result)) {
  123. return new Matrix3(
  124. matrix[0],
  125. matrix[3],
  126. matrix[6],
  127. matrix[1],
  128. matrix[4],
  129. matrix[7],
  130. matrix[2],
  131. matrix[5],
  132. matrix[8]
  133. );
  134. }
  135. result[0] = matrix[0];
  136. result[1] = matrix[1];
  137. result[2] = matrix[2];
  138. result[3] = matrix[3];
  139. result[4] = matrix[4];
  140. result[5] = matrix[5];
  141. result[6] = matrix[6];
  142. result[7] = matrix[7];
  143. result[8] = matrix[8];
  144. return result;
  145. };
  146. /**
  147. * Creates a Matrix3 from 9 consecutive elements in an array.
  148. *
  149. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  150. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  151. * @param {Matrix3} [result] The object onto which to store the result.
  152. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  153. *
  154. * @example
  155. * // Create the Matrix3:
  156. * // [1.0, 2.0, 3.0]
  157. * // [1.0, 2.0, 3.0]
  158. * // [1.0, 2.0, 3.0]
  159. *
  160. * var v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  161. * var m = Cesium.Matrix3.fromArray(v);
  162. *
  163. * // Create same Matrix3 with using an offset into an array
  164. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  165. * var m2 = Cesium.Matrix3.fromArray(v2, 2);
  166. */
  167. Matrix3.fromArray = function (array, startingIndex, result) {
  168. //>>includeStart('debug', pragmas.debug);
  169. Check.defined("array", array);
  170. //>>includeEnd('debug');
  171. startingIndex = defaultValue(startingIndex, 0);
  172. if (!defined(result)) {
  173. result = new Matrix3();
  174. }
  175. result[0] = array[startingIndex];
  176. result[1] = array[startingIndex + 1];
  177. result[2] = array[startingIndex + 2];
  178. result[3] = array[startingIndex + 3];
  179. result[4] = array[startingIndex + 4];
  180. result[5] = array[startingIndex + 5];
  181. result[6] = array[startingIndex + 6];
  182. result[7] = array[startingIndex + 7];
  183. result[8] = array[startingIndex + 8];
  184. return result;
  185. };
  186. /**
  187. * Creates a Matrix3 instance from a column-major order array.
  188. *
  189. * @param {Number[]} values The column-major order array.
  190. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  191. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  192. */
  193. Matrix3.fromColumnMajorArray = function (values, result) {
  194. //>>includeStart('debug', pragmas.debug);
  195. Check.defined("values", values);
  196. //>>includeEnd('debug');
  197. return Matrix3.clone(values, result);
  198. };
  199. /**
  200. * Creates a Matrix3 instance from a row-major order array.
  201. * The resulting matrix will be in column-major order.
  202. *
  203. * @param {Number[]} values The row-major order array.
  204. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  205. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  206. */
  207. Matrix3.fromRowMajorArray = function (values, result) {
  208. //>>includeStart('debug', pragmas.debug);
  209. Check.defined("values", values);
  210. //>>includeEnd('debug');
  211. if (!defined(result)) {
  212. return new Matrix3(
  213. values[0],
  214. values[1],
  215. values[2],
  216. values[3],
  217. values[4],
  218. values[5],
  219. values[6],
  220. values[7],
  221. values[8]
  222. );
  223. }
  224. result[0] = values[0];
  225. result[1] = values[3];
  226. result[2] = values[6];
  227. result[3] = values[1];
  228. result[4] = values[4];
  229. result[5] = values[7];
  230. result[6] = values[2];
  231. result[7] = values[5];
  232. result[8] = values[8];
  233. return result;
  234. };
  235. /**
  236. * Computes a 3x3 rotation matrix from the provided quaternion.
  237. *
  238. * @param {Quaternion} quaternion the quaternion to use.
  239. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  240. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  241. */
  242. Matrix3.fromQuaternion = function (quaternion, result) {
  243. //>>includeStart('debug', pragmas.debug);
  244. Check.typeOf.object("quaternion", quaternion);
  245. //>>includeEnd('debug');
  246. var x2 = quaternion.x * quaternion.x;
  247. var xy = quaternion.x * quaternion.y;
  248. var xz = quaternion.x * quaternion.z;
  249. var xw = quaternion.x * quaternion.w;
  250. var y2 = quaternion.y * quaternion.y;
  251. var yz = quaternion.y * quaternion.z;
  252. var yw = quaternion.y * quaternion.w;
  253. var z2 = quaternion.z * quaternion.z;
  254. var zw = quaternion.z * quaternion.w;
  255. var w2 = quaternion.w * quaternion.w;
  256. var m00 = x2 - y2 - z2 + w2;
  257. var m01 = 2.0 * (xy - zw);
  258. var m02 = 2.0 * (xz + yw);
  259. var m10 = 2.0 * (xy + zw);
  260. var m11 = -x2 + y2 - z2 + w2;
  261. var m12 = 2.0 * (yz - xw);
  262. var m20 = 2.0 * (xz - yw);
  263. var m21 = 2.0 * (yz + xw);
  264. var m22 = -x2 - y2 + z2 + w2;
  265. if (!defined(result)) {
  266. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  267. }
  268. result[0] = m00;
  269. result[1] = m10;
  270. result[2] = m20;
  271. result[3] = m01;
  272. result[4] = m11;
  273. result[5] = m21;
  274. result[6] = m02;
  275. result[7] = m12;
  276. result[8] = m22;
  277. return result;
  278. };
  279. /**
  280. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  281. *
  282. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  283. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  284. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  285. */
  286. Matrix3.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  287. //>>includeStart('debug', pragmas.debug);
  288. Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  289. //>>includeEnd('debug');
  290. var cosTheta = Math.cos(-headingPitchRoll.pitch);
  291. var cosPsi = Math.cos(-headingPitchRoll.heading);
  292. var cosPhi = Math.cos(headingPitchRoll.roll);
  293. var sinTheta = Math.sin(-headingPitchRoll.pitch);
  294. var sinPsi = Math.sin(-headingPitchRoll.heading);
  295. var sinPhi = Math.sin(headingPitchRoll.roll);
  296. var m00 = cosTheta * cosPsi;
  297. var m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  298. var m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  299. var m10 = cosTheta * sinPsi;
  300. var m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  301. var m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  302. var m20 = -sinTheta;
  303. var m21 = sinPhi * cosTheta;
  304. var m22 = cosPhi * cosTheta;
  305. if (!defined(result)) {
  306. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  307. }
  308. result[0] = m00;
  309. result[1] = m10;
  310. result[2] = m20;
  311. result[3] = m01;
  312. result[4] = m11;
  313. result[5] = m21;
  314. result[6] = m02;
  315. result[7] = m12;
  316. result[8] = m22;
  317. return result;
  318. };
  319. /**
  320. * Computes a Matrix3 instance representing a non-uniform scale.
  321. *
  322. * @param {Cartesian3} scale The x, y, and z scale factors.
  323. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  324. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  325. *
  326. * @example
  327. * // Creates
  328. * // [7.0, 0.0, 0.0]
  329. * // [0.0, 8.0, 0.0]
  330. * // [0.0, 0.0, 9.0]
  331. * var m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  332. */
  333. Matrix3.fromScale = function (scale, result) {
  334. //>>includeStart('debug', pragmas.debug);
  335. Check.typeOf.object("scale", scale);
  336. //>>includeEnd('debug');
  337. if (!defined(result)) {
  338. return new Matrix3(scale.x, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, scale.z);
  339. }
  340. result[0] = scale.x;
  341. result[1] = 0.0;
  342. result[2] = 0.0;
  343. result[3] = 0.0;
  344. result[4] = scale.y;
  345. result[5] = 0.0;
  346. result[6] = 0.0;
  347. result[7] = 0.0;
  348. result[8] = scale.z;
  349. return result;
  350. };
  351. /**
  352. * Computes a Matrix3 instance representing a uniform scale.
  353. *
  354. * @param {Number} scale The uniform scale factor.
  355. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  356. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  357. *
  358. * @example
  359. * // Creates
  360. * // [2.0, 0.0, 0.0]
  361. * // [0.0, 2.0, 0.0]
  362. * // [0.0, 0.0, 2.0]
  363. * var m = Cesium.Matrix3.fromUniformScale(2.0);
  364. */
  365. Matrix3.fromUniformScale = function (scale, result) {
  366. //>>includeStart('debug', pragmas.debug);
  367. Check.typeOf.number("scale", scale);
  368. //>>includeEnd('debug');
  369. if (!defined(result)) {
  370. return new Matrix3(scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, scale);
  371. }
  372. result[0] = scale;
  373. result[1] = 0.0;
  374. result[2] = 0.0;
  375. result[3] = 0.0;
  376. result[4] = scale;
  377. result[5] = 0.0;
  378. result[6] = 0.0;
  379. result[7] = 0.0;
  380. result[8] = scale;
  381. return result;
  382. };
  383. /**
  384. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  385. *
  386. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  387. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  388. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  389. *
  390. * @example
  391. * // Creates
  392. * // [0.0, -9.0, 8.0]
  393. * // [9.0, 0.0, -7.0]
  394. * // [-8.0, 7.0, 0.0]
  395. * var m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  396. */
  397. Matrix3.fromCrossProduct = function (vector, result) {
  398. //>>includeStart('debug', pragmas.debug);
  399. Check.typeOf.object("vector", vector);
  400. //>>includeEnd('debug');
  401. if (!defined(result)) {
  402. return new Matrix3(
  403. 0.0,
  404. -vector.z,
  405. vector.y,
  406. vector.z,
  407. 0.0,
  408. -vector.x,
  409. -vector.y,
  410. vector.x,
  411. 0.0
  412. );
  413. }
  414. result[0] = 0.0;
  415. result[1] = vector.z;
  416. result[2] = -vector.y;
  417. result[3] = -vector.z;
  418. result[4] = 0.0;
  419. result[5] = vector.x;
  420. result[6] = vector.y;
  421. result[7] = -vector.x;
  422. result[8] = 0.0;
  423. return result;
  424. };
  425. /**
  426. * Creates a rotation matrix around the x-axis.
  427. *
  428. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  429. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  430. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  431. *
  432. * @example
  433. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  434. * var p = new Cesium.Cartesian3(5, 6, 7);
  435. * var m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  436. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  437. */
  438. Matrix3.fromRotationX = function (angle, result) {
  439. //>>includeStart('debug', pragmas.debug);
  440. Check.typeOf.number("angle", angle);
  441. //>>includeEnd('debug');
  442. var cosAngle = Math.cos(angle);
  443. var sinAngle = Math.sin(angle);
  444. if (!defined(result)) {
  445. return new Matrix3(
  446. 1.0,
  447. 0.0,
  448. 0.0,
  449. 0.0,
  450. cosAngle,
  451. -sinAngle,
  452. 0.0,
  453. sinAngle,
  454. cosAngle
  455. );
  456. }
  457. result[0] = 1.0;
  458. result[1] = 0.0;
  459. result[2] = 0.0;
  460. result[3] = 0.0;
  461. result[4] = cosAngle;
  462. result[5] = sinAngle;
  463. result[6] = 0.0;
  464. result[7] = -sinAngle;
  465. result[8] = cosAngle;
  466. return result;
  467. };
  468. /**
  469. * Creates a rotation matrix around the y-axis.
  470. *
  471. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  472. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  473. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  474. *
  475. * @example
  476. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  477. * var p = new Cesium.Cartesian3(5, 6, 7);
  478. * var m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  479. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  480. */
  481. Matrix3.fromRotationY = function (angle, result) {
  482. //>>includeStart('debug', pragmas.debug);
  483. Check.typeOf.number("angle", angle);
  484. //>>includeEnd('debug');
  485. var cosAngle = Math.cos(angle);
  486. var sinAngle = Math.sin(angle);
  487. if (!defined(result)) {
  488. return new Matrix3(
  489. cosAngle,
  490. 0.0,
  491. sinAngle,
  492. 0.0,
  493. 1.0,
  494. 0.0,
  495. -sinAngle,
  496. 0.0,
  497. cosAngle
  498. );
  499. }
  500. result[0] = cosAngle;
  501. result[1] = 0.0;
  502. result[2] = -sinAngle;
  503. result[3] = 0.0;
  504. result[4] = 1.0;
  505. result[5] = 0.0;
  506. result[6] = sinAngle;
  507. result[7] = 0.0;
  508. result[8] = cosAngle;
  509. return result;
  510. };
  511. /**
  512. * Creates a rotation matrix around the z-axis.
  513. *
  514. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  515. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  516. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  517. *
  518. * @example
  519. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  520. * var p = new Cesium.Cartesian3(5, 6, 7);
  521. * var m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  522. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  523. */
  524. Matrix3.fromRotationZ = function (angle, result) {
  525. //>>includeStart('debug', pragmas.debug);
  526. Check.typeOf.number("angle", angle);
  527. //>>includeEnd('debug');
  528. var cosAngle = Math.cos(angle);
  529. var sinAngle = Math.sin(angle);
  530. if (!defined(result)) {
  531. return new Matrix3(
  532. cosAngle,
  533. -sinAngle,
  534. 0.0,
  535. sinAngle,
  536. cosAngle,
  537. 0.0,
  538. 0.0,
  539. 0.0,
  540. 1.0
  541. );
  542. }
  543. result[0] = cosAngle;
  544. result[1] = sinAngle;
  545. result[2] = 0.0;
  546. result[3] = -sinAngle;
  547. result[4] = cosAngle;
  548. result[5] = 0.0;
  549. result[6] = 0.0;
  550. result[7] = 0.0;
  551. result[8] = 1.0;
  552. return result;
  553. };
  554. /**
  555. * Creates an Array from the provided Matrix3 instance.
  556. * The array will be in column-major order.
  557. *
  558. * @param {Matrix3} matrix The matrix to use..
  559. * @param {Number[]} [result] The Array onto which to store the result.
  560. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  561. */
  562. Matrix3.toArray = function (matrix, result) {
  563. //>>includeStart('debug', pragmas.debug);
  564. Check.typeOf.object("matrix", matrix);
  565. //>>includeEnd('debug');
  566. if (!defined(result)) {
  567. return [
  568. matrix[0],
  569. matrix[1],
  570. matrix[2],
  571. matrix[3],
  572. matrix[4],
  573. matrix[5],
  574. matrix[6],
  575. matrix[7],
  576. matrix[8],
  577. ];
  578. }
  579. result[0] = matrix[0];
  580. result[1] = matrix[1];
  581. result[2] = matrix[2];
  582. result[3] = matrix[3];
  583. result[4] = matrix[4];
  584. result[5] = matrix[5];
  585. result[6] = matrix[6];
  586. result[7] = matrix[7];
  587. result[8] = matrix[8];
  588. return result;
  589. };
  590. /**
  591. * Computes the array index of the element at the provided row and column.
  592. *
  593. * @param {Number} row The zero-based index of the row.
  594. * @param {Number} column The zero-based index of the column.
  595. * @returns {Number} The index of the element at the provided row and column.
  596. *
  597. * @exception {DeveloperError} row must be 0, 1, or 2.
  598. * @exception {DeveloperError} column must be 0, 1, or 2.
  599. *
  600. * @example
  601. * var myMatrix = new Cesium.Matrix3();
  602. * var column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  603. * var column1Row0 = myMatrix[column1Row0Index]
  604. * myMatrix[column1Row0Index] = 10.0;
  605. */
  606. Matrix3.getElementIndex = function (column, row) {
  607. //>>includeStart('debug', pragmas.debug);
  608. Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  609. Check.typeOf.number.lessThanOrEquals("row", row, 2);
  610. Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  611. Check.typeOf.number.lessThanOrEquals("column", column, 2);
  612. //>>includeEnd('debug');
  613. return column * 3 + row;
  614. };
  615. /**
  616. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  617. *
  618. * @param {Matrix3} matrix The matrix to use.
  619. * @param {Number} index The zero-based index of the column to retrieve.
  620. * @param {Cartesian3} result The object onto which to store the result.
  621. * @returns {Cartesian3} The modified result parameter.
  622. *
  623. * @exception {DeveloperError} index must be 0, 1, or 2.
  624. */
  625. Matrix3.getColumn = function (matrix, index, result) {
  626. //>>includeStart('debug', pragmas.debug);
  627. Check.typeOf.object("matrix", matrix);
  628. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  629. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  630. Check.typeOf.object("result", result);
  631. //>>includeEnd('debug');
  632. var startIndex = index * 3;
  633. var x = matrix[startIndex];
  634. var y = matrix[startIndex + 1];
  635. var z = matrix[startIndex + 2];
  636. result.x = x;
  637. result.y = y;
  638. result.z = z;
  639. return result;
  640. };
  641. /**
  642. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  643. *
  644. * @param {Matrix3} matrix The matrix to use.
  645. * @param {Number} index The zero-based index of the column to set.
  646. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  647. * @param {Matrix3} result The object onto which to store the result.
  648. * @returns {Matrix3} The modified result parameter.
  649. *
  650. * @exception {DeveloperError} index must be 0, 1, or 2.
  651. */
  652. Matrix3.setColumn = function (matrix, index, cartesian, result) {
  653. //>>includeStart('debug', pragmas.debug);
  654. Check.typeOf.object("matrix", matrix);
  655. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  656. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  657. Check.typeOf.object("cartesian", cartesian);
  658. Check.typeOf.object("result", result);
  659. //>>includeEnd('debug');
  660. result = Matrix3.clone(matrix, result);
  661. var startIndex = index * 3;
  662. result[startIndex] = cartesian.x;
  663. result[startIndex + 1] = cartesian.y;
  664. result[startIndex + 2] = cartesian.z;
  665. return result;
  666. };
  667. /**
  668. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  669. *
  670. * @param {Matrix3} matrix The matrix to use.
  671. * @param {Number} index The zero-based index of the row to retrieve.
  672. * @param {Cartesian3} result The object onto which to store the result.
  673. * @returns {Cartesian3} The modified result parameter.
  674. *
  675. * @exception {DeveloperError} index must be 0, 1, or 2.
  676. */
  677. Matrix3.getRow = function (matrix, index, result) {
  678. //>>includeStart('debug', pragmas.debug);
  679. Check.typeOf.object("matrix", matrix);
  680. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  681. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  682. Check.typeOf.object("result", result);
  683. //>>includeEnd('debug');
  684. var x = matrix[index];
  685. var y = matrix[index + 3];
  686. var z = matrix[index + 6];
  687. result.x = x;
  688. result.y = y;
  689. result.z = z;
  690. return result;
  691. };
  692. /**
  693. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  694. *
  695. * @param {Matrix3} matrix The matrix to use.
  696. * @param {Number} index The zero-based index of the row to set.
  697. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  698. * @param {Matrix3} result The object onto which to store the result.
  699. * @returns {Matrix3} The modified result parameter.
  700. *
  701. * @exception {DeveloperError} index must be 0, 1, or 2.
  702. */
  703. Matrix3.setRow = function (matrix, index, cartesian, result) {
  704. //>>includeStart('debug', pragmas.debug);
  705. Check.typeOf.object("matrix", matrix);
  706. Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  707. Check.typeOf.number.lessThanOrEquals("index", index, 2);
  708. Check.typeOf.object("cartesian", cartesian);
  709. Check.typeOf.object("result", result);
  710. //>>includeEnd('debug');
  711. result = Matrix3.clone(matrix, result);
  712. result[index] = cartesian.x;
  713. result[index + 3] = cartesian.y;
  714. result[index + 6] = cartesian.z;
  715. return result;
  716. };
  717. var scratchColumn = new Cartesian3();
  718. /**
  719. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  720. *
  721. * @param {Matrix3} matrix The matrix.
  722. * @param {Cartesian3} result The object onto which to store the result.
  723. * @returns {Cartesian3} The modified result parameter.
  724. */
  725. Matrix3.getScale = function (matrix, result) {
  726. //>>includeStart('debug', pragmas.debug);
  727. Check.typeOf.object("matrix", matrix);
  728. Check.typeOf.object("result", result);
  729. //>>includeEnd('debug');
  730. result.x = Cartesian3.magnitude(
  731. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn)
  732. );
  733. result.y = Cartesian3.magnitude(
  734. Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn)
  735. );
  736. result.z = Cartesian3.magnitude(
  737. Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn)
  738. );
  739. return result;
  740. };
  741. var scratchScale = new Cartesian3();
  742. /**
  743. * Computes the maximum scale assuming the matrix is an affine transformation.
  744. * The maximum scale is the maximum length of the column vectors.
  745. *
  746. * @param {Matrix3} matrix The matrix.
  747. * @returns {Number} The maximum scale.
  748. */
  749. Matrix3.getMaximumScale = function (matrix) {
  750. Matrix3.getScale(matrix, scratchScale);
  751. return Cartesian3.maximumComponent(scratchScale);
  752. };
  753. /**
  754. * Computes the product of two matrices.
  755. *
  756. * @param {Matrix3} left The first matrix.
  757. * @param {Matrix3} right The second matrix.
  758. * @param {Matrix3} result The object onto which to store the result.
  759. * @returns {Matrix3} The modified result parameter.
  760. */
  761. Matrix3.multiply = function (left, right, result) {
  762. //>>includeStart('debug', pragmas.debug);
  763. Check.typeOf.object("left", left);
  764. Check.typeOf.object("right", right);
  765. Check.typeOf.object("result", result);
  766. //>>includeEnd('debug');
  767. var column0Row0 =
  768. left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  769. var column0Row1 =
  770. left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  771. var column0Row2 =
  772. left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  773. var column1Row0 =
  774. left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  775. var column1Row1 =
  776. left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  777. var column1Row2 =
  778. left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  779. var column2Row0 =
  780. left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  781. var column2Row1 =
  782. left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  783. var column2Row2 =
  784. left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  785. result[0] = column0Row0;
  786. result[1] = column0Row1;
  787. result[2] = column0Row2;
  788. result[3] = column1Row0;
  789. result[4] = column1Row1;
  790. result[5] = column1Row2;
  791. result[6] = column2Row0;
  792. result[7] = column2Row1;
  793. result[8] = column2Row2;
  794. return result;
  795. };
  796. /**
  797. * Computes the sum of two matrices.
  798. *
  799. * @param {Matrix3} left The first matrix.
  800. * @param {Matrix3} right The second matrix.
  801. * @param {Matrix3} result The object onto which to store the result.
  802. * @returns {Matrix3} The modified result parameter.
  803. */
  804. Matrix3.add = function (left, right, result) {
  805. //>>includeStart('debug', pragmas.debug);
  806. Check.typeOf.object("left", left);
  807. Check.typeOf.object("right", right);
  808. Check.typeOf.object("result", result);
  809. //>>includeEnd('debug');
  810. result[0] = left[0] + right[0];
  811. result[1] = left[1] + right[1];
  812. result[2] = left[2] + right[2];
  813. result[3] = left[3] + right[3];
  814. result[4] = left[4] + right[4];
  815. result[5] = left[5] + right[5];
  816. result[6] = left[6] + right[6];
  817. result[7] = left[7] + right[7];
  818. result[8] = left[8] + right[8];
  819. return result;
  820. };
  821. /**
  822. * Computes the difference of two matrices.
  823. *
  824. * @param {Matrix3} left The first matrix.
  825. * @param {Matrix3} right The second matrix.
  826. * @param {Matrix3} result The object onto which to store the result.
  827. * @returns {Matrix3} The modified result parameter.
  828. */
  829. Matrix3.subtract = function (left, right, result) {
  830. //>>includeStart('debug', pragmas.debug);
  831. Check.typeOf.object("left", left);
  832. Check.typeOf.object("right", right);
  833. Check.typeOf.object("result", result);
  834. //>>includeEnd('debug');
  835. result[0] = left[0] - right[0];
  836. result[1] = left[1] - right[1];
  837. result[2] = left[2] - right[2];
  838. result[3] = left[3] - right[3];
  839. result[4] = left[4] - right[4];
  840. result[5] = left[5] - right[5];
  841. result[6] = left[6] - right[6];
  842. result[7] = left[7] - right[7];
  843. result[8] = left[8] - right[8];
  844. return result;
  845. };
  846. /**
  847. * Computes the product of a matrix and a column vector.
  848. *
  849. * @param {Matrix3} matrix The matrix.
  850. * @param {Cartesian3} cartesian The column.
  851. * @param {Cartesian3} result The object onto which to store the result.
  852. * @returns {Cartesian3} The modified result parameter.
  853. */
  854. Matrix3.multiplyByVector = function (matrix, cartesian, result) {
  855. //>>includeStart('debug', pragmas.debug);
  856. Check.typeOf.object("matrix", matrix);
  857. Check.typeOf.object("cartesian", cartesian);
  858. Check.typeOf.object("result", result);
  859. //>>includeEnd('debug');
  860. var vX = cartesian.x;
  861. var vY = cartesian.y;
  862. var vZ = cartesian.z;
  863. var x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  864. var y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  865. var z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  866. result.x = x;
  867. result.y = y;
  868. result.z = z;
  869. return result;
  870. };
  871. /**
  872. * Computes the product of a matrix and a scalar.
  873. *
  874. * @param {Matrix3} matrix The matrix.
  875. * @param {Number} scalar The number to multiply by.
  876. * @param {Matrix3} result The object onto which to store the result.
  877. * @returns {Matrix3} The modified result parameter.
  878. */
  879. Matrix3.multiplyByScalar = function (matrix, scalar, result) {
  880. //>>includeStart('debug', pragmas.debug);
  881. Check.typeOf.object("matrix", matrix);
  882. Check.typeOf.number("scalar", scalar);
  883. Check.typeOf.object("result", result);
  884. //>>includeEnd('debug');
  885. result[0] = matrix[0] * scalar;
  886. result[1] = matrix[1] * scalar;
  887. result[2] = matrix[2] * scalar;
  888. result[3] = matrix[3] * scalar;
  889. result[4] = matrix[4] * scalar;
  890. result[5] = matrix[5] * scalar;
  891. result[6] = matrix[6] * scalar;
  892. result[7] = matrix[7] * scalar;
  893. result[8] = matrix[8] * scalar;
  894. return result;
  895. };
  896. /**
  897. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  898. *
  899. * @param {Matrix3} matrix The matrix on the left-hand side.
  900. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  901. * @param {Matrix3} result The object onto which to store the result.
  902. * @returns {Matrix3} The modified result parameter.
  903. *
  904. *
  905. * @example
  906. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  907. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  908. *
  909. * @see Matrix3.fromScale
  910. * @see Matrix3.multiplyByUniformScale
  911. */
  912. Matrix3.multiplyByScale = function (matrix, scale, result) {
  913. //>>includeStart('debug', pragmas.debug);
  914. Check.typeOf.object("matrix", matrix);
  915. Check.typeOf.object("scale", scale);
  916. Check.typeOf.object("result", result);
  917. //>>includeEnd('debug');
  918. result[0] = matrix[0] * scale.x;
  919. result[1] = matrix[1] * scale.x;
  920. result[2] = matrix[2] * scale.x;
  921. result[3] = matrix[3] * scale.y;
  922. result[4] = matrix[4] * scale.y;
  923. result[5] = matrix[5] * scale.y;
  924. result[6] = matrix[6] * scale.z;
  925. result[7] = matrix[7] * scale.z;
  926. result[8] = matrix[8] * scale.z;
  927. return result;
  928. };
  929. /**
  930. * Creates a negated copy of the provided matrix.
  931. *
  932. * @param {Matrix3} matrix The matrix to negate.
  933. * @param {Matrix3} result The object onto which to store the result.
  934. * @returns {Matrix3} The modified result parameter.
  935. */
  936. Matrix3.negate = function (matrix, result) {
  937. //>>includeStart('debug', pragmas.debug);
  938. Check.typeOf.object("matrix", matrix);
  939. Check.typeOf.object("result", result);
  940. //>>includeEnd('debug');
  941. result[0] = -matrix[0];
  942. result[1] = -matrix[1];
  943. result[2] = -matrix[2];
  944. result[3] = -matrix[3];
  945. result[4] = -matrix[4];
  946. result[5] = -matrix[5];
  947. result[6] = -matrix[6];
  948. result[7] = -matrix[7];
  949. result[8] = -matrix[8];
  950. return result;
  951. };
  952. /**
  953. * Computes the transpose of the provided matrix.
  954. *
  955. * @param {Matrix3} matrix The matrix to transpose.
  956. * @param {Matrix3} result The object onto which to store the result.
  957. * @returns {Matrix3} The modified result parameter.
  958. */
  959. Matrix3.transpose = function (matrix, result) {
  960. //>>includeStart('debug', pragmas.debug);
  961. Check.typeOf.object("matrix", matrix);
  962. Check.typeOf.object("result", result);
  963. //>>includeEnd('debug');
  964. var column0Row0 = matrix[0];
  965. var column0Row1 = matrix[3];
  966. var column0Row2 = matrix[6];
  967. var column1Row0 = matrix[1];
  968. var column1Row1 = matrix[4];
  969. var column1Row2 = matrix[7];
  970. var column2Row0 = matrix[2];
  971. var column2Row1 = matrix[5];
  972. var column2Row2 = matrix[8];
  973. result[0] = column0Row0;
  974. result[1] = column0Row1;
  975. result[2] = column0Row2;
  976. result[3] = column1Row0;
  977. result[4] = column1Row1;
  978. result[5] = column1Row2;
  979. result[6] = column2Row0;
  980. result[7] = column2Row1;
  981. result[8] = column2Row2;
  982. return result;
  983. };
  984. var UNIT = new Cartesian3(1, 1, 1);
  985. /**
  986. * Extracts the rotation assuming the matrix is an affine transformation.
  987. *
  988. * @param {Matrix3} matrix The matrix.
  989. * @param {Matrix3} result The object onto which to store the result.
  990. * @returns {Matrix3} The modified result parameter
  991. */
  992. Matrix3.getRotation = function (matrix, result) {
  993. //>>includeStart('debug', pragmas.debug);
  994. Check.typeOf.object("matrix", matrix);
  995. Check.typeOf.object("result", result);
  996. //>>includeEnd('debug');
  997. var inverseScale = Cartesian3.divideComponents(
  998. UNIT,
  999. Matrix3.getScale(matrix, scratchScale),
  1000. scratchScale
  1001. );
  1002. result = Matrix3.multiplyByScale(matrix, inverseScale, result);
  1003. return result;
  1004. };
  1005. function computeFrobeniusNorm(matrix) {
  1006. var norm = 0.0;
  1007. for (var i = 0; i < 9; ++i) {
  1008. var temp = matrix[i];
  1009. norm += temp * temp;
  1010. }
  1011. return Math.sqrt(norm);
  1012. }
  1013. var rowVal = [1, 0, 0];
  1014. var colVal = [2, 2, 1];
  1015. function offDiagonalFrobeniusNorm(matrix) {
  1016. // Computes the "off-diagonal" Frobenius norm.
  1017. // Assumes matrix is symmetric.
  1018. var norm = 0.0;
  1019. for (var i = 0; i < 3; ++i) {
  1020. var temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  1021. norm += 2.0 * temp * temp;
  1022. }
  1023. return Math.sqrt(norm);
  1024. }
  1025. function shurDecomposition(matrix, result) {
  1026. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1027. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  1028. //
  1029. // The routine takes a matrix, which is assumed to be symmetric, and
  1030. // finds the largest off-diagonal term, and then creates
  1031. // a matrix (result) which can be used to help reduce it
  1032. var tolerance = CesiumMath.EPSILON15;
  1033. var maxDiagonal = 0.0;
  1034. var rotAxis = 1;
  1035. // find pivot (rotAxis) based on max diagonal of matrix
  1036. for (var i = 0; i < 3; ++i) {
  1037. var temp = Math.abs(matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]);
  1038. if (temp > maxDiagonal) {
  1039. rotAxis = i;
  1040. maxDiagonal = temp;
  1041. }
  1042. }
  1043. var c = 1.0;
  1044. var s = 0.0;
  1045. var p = rowVal[rotAxis];
  1046. var q = colVal[rotAxis];
  1047. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  1048. var qq = matrix[Matrix3.getElementIndex(q, q)];
  1049. var pp = matrix[Matrix3.getElementIndex(p, p)];
  1050. var qp = matrix[Matrix3.getElementIndex(q, p)];
  1051. var tau = (qq - pp) / 2.0 / qp;
  1052. var t;
  1053. if (tau < 0.0) {
  1054. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  1055. } else {
  1056. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  1057. }
  1058. c = 1.0 / Math.sqrt(1.0 + t * t);
  1059. s = t * c;
  1060. }
  1061. result = Matrix3.clone(Matrix3.IDENTITY, result);
  1062. result[Matrix3.getElementIndex(p, p)] = result[
  1063. Matrix3.getElementIndex(q, q)
  1064. ] = c;
  1065. result[Matrix3.getElementIndex(q, p)] = s;
  1066. result[Matrix3.getElementIndex(p, q)] = -s;
  1067. return result;
  1068. }
  1069. var jMatrix = new Matrix3();
  1070. var jMatrixTranspose = new Matrix3();
  1071. /**
  1072. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  1073. * <p>
  1074. * Returns a diagonal matrix and unitary matrix such that:
  1075. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  1076. * </p>
  1077. * <p>
  1078. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  1079. * of the unitary matrix are the corresponding eigenvectors.
  1080. * </p>
  1081. *
  1082. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  1083. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  1084. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  1085. *
  1086. * @example
  1087. * var a = //... symetric matrix
  1088. * var result = {
  1089. * unitary : new Cesium.Matrix3(),
  1090. * diagonal : new Cesium.Matrix3()
  1091. * };
  1092. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  1093. *
  1094. * var unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  1095. * var b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  1096. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  1097. *
  1098. * var lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  1099. * var v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  1100. * var c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  1101. */
  1102. Matrix3.computeEigenDecomposition = function (matrix, result) {
  1103. //>>includeStart('debug', pragmas.debug);
  1104. Check.typeOf.object("matrix", matrix);
  1105. //>>includeEnd('debug');
  1106. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1107. // section 8.4.3 The Classical Jacobi Algorithm
  1108. var tolerance = CesiumMath.EPSILON20;
  1109. var maxSweeps = 10;
  1110. var count = 0;
  1111. var sweep = 0;
  1112. if (!defined(result)) {
  1113. result = {};
  1114. }
  1115. var unitaryMatrix = (result.unitary = Matrix3.clone(
  1116. Matrix3.IDENTITY,
  1117. result.unitary
  1118. ));
  1119. var diagMatrix = (result.diagonal = Matrix3.clone(matrix, result.diagonal));
  1120. var epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  1121. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  1122. shurDecomposition(diagMatrix, jMatrix);
  1123. Matrix3.transpose(jMatrix, jMatrixTranspose);
  1124. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  1125. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  1126. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  1127. if (++count > 2) {
  1128. ++sweep;
  1129. count = 0;
  1130. }
  1131. }
  1132. return result;
  1133. };
  1134. /**
  1135. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  1136. *
  1137. * @param {Matrix3} matrix The matrix with signed elements.
  1138. * @param {Matrix3} result The object onto which to store the result.
  1139. * @returns {Matrix3} The modified result parameter.
  1140. */
  1141. Matrix3.abs = function (matrix, result) {
  1142. //>>includeStart('debug', pragmas.debug);
  1143. Check.typeOf.object("matrix", matrix);
  1144. Check.typeOf.object("result", result);
  1145. //>>includeEnd('debug');
  1146. result[0] = Math.abs(matrix[0]);
  1147. result[1] = Math.abs(matrix[1]);
  1148. result[2] = Math.abs(matrix[2]);
  1149. result[3] = Math.abs(matrix[3]);
  1150. result[4] = Math.abs(matrix[4]);
  1151. result[5] = Math.abs(matrix[5]);
  1152. result[6] = Math.abs(matrix[6]);
  1153. result[7] = Math.abs(matrix[7]);
  1154. result[8] = Math.abs(matrix[8]);
  1155. return result;
  1156. };
  1157. /**
  1158. * Computes the determinant of the provided matrix.
  1159. *
  1160. * @param {Matrix3} matrix The matrix to use.
  1161. * @returns {Number} The value of the determinant of the matrix.
  1162. */
  1163. Matrix3.determinant = function (matrix) {
  1164. //>>includeStart('debug', pragmas.debug);
  1165. Check.typeOf.object("matrix", matrix);
  1166. //>>includeEnd('debug');
  1167. var m11 = matrix[0];
  1168. var m21 = matrix[3];
  1169. var m31 = matrix[6];
  1170. var m12 = matrix[1];
  1171. var m22 = matrix[4];
  1172. var m32 = matrix[7];
  1173. var m13 = matrix[2];
  1174. var m23 = matrix[5];
  1175. var m33 = matrix[8];
  1176. return (
  1177. m11 * (m22 * m33 - m23 * m32) +
  1178. m12 * (m23 * m31 - m21 * m33) +
  1179. m13 * (m21 * m32 - m22 * m31)
  1180. );
  1181. };
  1182. /**
  1183. * Computes the inverse of the provided matrix.
  1184. *
  1185. * @param {Matrix3} matrix The matrix to invert.
  1186. * @param {Matrix3} result The object onto which to store the result.
  1187. * @returns {Matrix3} The modified result parameter.
  1188. *
  1189. * @exception {DeveloperError} matrix is not invertible.
  1190. */
  1191. Matrix3.inverse = function (matrix, result) {
  1192. //>>includeStart('debug', pragmas.debug);
  1193. Check.typeOf.object("matrix", matrix);
  1194. Check.typeOf.object("result", result);
  1195. //>>includeEnd('debug');
  1196. var m11 = matrix[0];
  1197. var m21 = matrix[1];
  1198. var m31 = matrix[2];
  1199. var m12 = matrix[3];
  1200. var m22 = matrix[4];
  1201. var m32 = matrix[5];
  1202. var m13 = matrix[6];
  1203. var m23 = matrix[7];
  1204. var m33 = matrix[8];
  1205. var determinant = Matrix3.determinant(matrix);
  1206. //>>includeStart('debug', pragmas.debug);
  1207. if (Math.abs(determinant) <= CesiumMath.EPSILON15) {
  1208. throw new DeveloperError("matrix is not invertible");
  1209. }
  1210. //>>includeEnd('debug');
  1211. result[0] = m22 * m33 - m23 * m32;
  1212. result[1] = m23 * m31 - m21 * m33;
  1213. result[2] = m21 * m32 - m22 * m31;
  1214. result[3] = m13 * m32 - m12 * m33;
  1215. result[4] = m11 * m33 - m13 * m31;
  1216. result[5] = m12 * m31 - m11 * m32;
  1217. result[6] = m12 * m23 - m13 * m22;
  1218. result[7] = m13 * m21 - m11 * m23;
  1219. result[8] = m11 * m22 - m12 * m21;
  1220. var scale = 1.0 / determinant;
  1221. return Matrix3.multiplyByScalar(result, scale, result);
  1222. };
  1223. /**
  1224. * Compares the provided matrices componentwise and returns
  1225. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1226. *
  1227. * @param {Matrix3} [left] The first matrix.
  1228. * @param {Matrix3} [right] The second matrix.
  1229. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1230. */
  1231. Matrix3.equals = function (left, right) {
  1232. return (
  1233. left === right ||
  1234. (defined(left) &&
  1235. defined(right) &&
  1236. left[0] === right[0] &&
  1237. left[1] === right[1] &&
  1238. left[2] === right[2] &&
  1239. left[3] === right[3] &&
  1240. left[4] === right[4] &&
  1241. left[5] === right[5] &&
  1242. left[6] === right[6] &&
  1243. left[7] === right[7] &&
  1244. left[8] === right[8])
  1245. );
  1246. };
  1247. /**
  1248. * Compares the provided matrices componentwise and returns
  1249. * <code>true</code> if they are within the provided epsilon,
  1250. * <code>false</code> otherwise.
  1251. *
  1252. * @param {Matrix3} [left] The first matrix.
  1253. * @param {Matrix3} [right] The second matrix.
  1254. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1255. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1256. */
  1257. Matrix3.equalsEpsilon = function (left, right, epsilon) {
  1258. epsilon = defaultValue(epsilon, 0);
  1259. return (
  1260. left === right ||
  1261. (defined(left) &&
  1262. defined(right) &&
  1263. Math.abs(left[0] - right[0]) <= epsilon &&
  1264. Math.abs(left[1] - right[1]) <= epsilon &&
  1265. Math.abs(left[2] - right[2]) <= epsilon &&
  1266. Math.abs(left[3] - right[3]) <= epsilon &&
  1267. Math.abs(left[4] - right[4]) <= epsilon &&
  1268. Math.abs(left[5] - right[5]) <= epsilon &&
  1269. Math.abs(left[6] - right[6]) <= epsilon &&
  1270. Math.abs(left[7] - right[7]) <= epsilon &&
  1271. Math.abs(left[8] - right[8]) <= epsilon)
  1272. );
  1273. };
  1274. /**
  1275. * An immutable Matrix3 instance initialized to the identity matrix.
  1276. *
  1277. * @type {Matrix3}
  1278. * @constant
  1279. */
  1280. Matrix3.IDENTITY = Object.freeze(
  1281. new Matrix3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  1282. );
  1283. /**
  1284. * An immutable Matrix3 instance initialized to the zero matrix.
  1285. *
  1286. * @type {Matrix3}
  1287. * @constant
  1288. */
  1289. Matrix3.ZERO = Object.freeze(
  1290. new Matrix3(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  1291. );
  1292. /**
  1293. * The index into Matrix3 for column 0, row 0.
  1294. *
  1295. * @type {Number}
  1296. * @constant
  1297. */
  1298. Matrix3.COLUMN0ROW0 = 0;
  1299. /**
  1300. * The index into Matrix3 for column 0, row 1.
  1301. *
  1302. * @type {Number}
  1303. * @constant
  1304. */
  1305. Matrix3.COLUMN0ROW1 = 1;
  1306. /**
  1307. * The index into Matrix3 for column 0, row 2.
  1308. *
  1309. * @type {Number}
  1310. * @constant
  1311. */
  1312. Matrix3.COLUMN0ROW2 = 2;
  1313. /**
  1314. * The index into Matrix3 for column 1, row 0.
  1315. *
  1316. * @type {Number}
  1317. * @constant
  1318. */
  1319. Matrix3.COLUMN1ROW0 = 3;
  1320. /**
  1321. * The index into Matrix3 for column 1, row 1.
  1322. *
  1323. * @type {Number}
  1324. * @constant
  1325. */
  1326. Matrix3.COLUMN1ROW1 = 4;
  1327. /**
  1328. * The index into Matrix3 for column 1, row 2.
  1329. *
  1330. * @type {Number}
  1331. * @constant
  1332. */
  1333. Matrix3.COLUMN1ROW2 = 5;
  1334. /**
  1335. * The index into Matrix3 for column 2, row 0.
  1336. *
  1337. * @type {Number}
  1338. * @constant
  1339. */
  1340. Matrix3.COLUMN2ROW0 = 6;
  1341. /**
  1342. * The index into Matrix3 for column 2, row 1.
  1343. *
  1344. * @type {Number}
  1345. * @constant
  1346. */
  1347. Matrix3.COLUMN2ROW1 = 7;
  1348. /**
  1349. * The index into Matrix3 for column 2, row 2.
  1350. *
  1351. * @type {Number}
  1352. * @constant
  1353. */
  1354. Matrix3.COLUMN2ROW2 = 8;
  1355. Object.defineProperties(Matrix3.prototype, {
  1356. /**
  1357. * Gets the number of items in the collection.
  1358. * @memberof Matrix3.prototype
  1359. *
  1360. * @type {Number}
  1361. */
  1362. length: {
  1363. get: function () {
  1364. return Matrix3.packedLength;
  1365. },
  1366. },
  1367. });
  1368. /**
  1369. * Duplicates the provided Matrix3 instance.
  1370. *
  1371. * @param {Matrix3} [result] The object onto which to store the result.
  1372. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  1373. */
  1374. Matrix3.prototype.clone = function (result) {
  1375. return Matrix3.clone(this, result);
  1376. };
  1377. /**
  1378. * Compares this matrix to the provided matrix componentwise and returns
  1379. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1380. *
  1381. * @param {Matrix3} [right] The right hand side matrix.
  1382. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1383. */
  1384. Matrix3.prototype.equals = function (right) {
  1385. return Matrix3.equals(this, right);
  1386. };
  1387. /**
  1388. * @private
  1389. */
  1390. Matrix3.equalsArray = function (matrix, array, offset) {
  1391. return (
  1392. matrix[0] === array[offset] &&
  1393. matrix[1] === array[offset + 1] &&
  1394. matrix[2] === array[offset + 2] &&
  1395. matrix[3] === array[offset + 3] &&
  1396. matrix[4] === array[offset + 4] &&
  1397. matrix[5] === array[offset + 5] &&
  1398. matrix[6] === array[offset + 6] &&
  1399. matrix[7] === array[offset + 7] &&
  1400. matrix[8] === array[offset + 8]
  1401. );
  1402. };
  1403. /**
  1404. * Compares this matrix to the provided matrix componentwise and returns
  1405. * <code>true</code> if they are within the provided epsilon,
  1406. * <code>false</code> otherwise.
  1407. *
  1408. * @param {Matrix3} [right] The right hand side matrix.
  1409. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1410. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1411. */
  1412. Matrix3.prototype.equalsEpsilon = function (right, epsilon) {
  1413. return Matrix3.equalsEpsilon(this, right, epsilon);
  1414. };
  1415. /**
  1416. * Creates a string representing this Matrix with each row being
  1417. * on a separate line and in the format '(column0, column1, column2)'.
  1418. *
  1419. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  1420. */
  1421. Matrix3.prototype.toString = function () {
  1422. return (
  1423. "(" +
  1424. this[0] +
  1425. ", " +
  1426. this[3] +
  1427. ", " +
  1428. this[6] +
  1429. ")\n" +
  1430. "(" +
  1431. this[1] +
  1432. ", " +
  1433. this[4] +
  1434. ", " +
  1435. this[7] +
  1436. ")\n" +
  1437. "(" +
  1438. this[2] +
  1439. ", " +
  1440. this[5] +
  1441. ", " +
  1442. this[8] +
  1443. ")"
  1444. );
  1445. };
  1446. export default Matrix3;