Matrix3.js 53 KB

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