You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Matrix4.js 97KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617
  1. import Cartesian3 from './Cartesian3.js';
  2. import Cartesian4 from './Cartesian4.js';
  3. import Check from './Check.js';
  4. import defaultValue from './defaultValue.js';
  5. import defined from './defined.js';
  6. import defineProperties from './defineProperties.js';
  7. import freezeObject from './freezeObject.js';
  8. import CesiumMath from './Math.js';
  9. import Matrix3 from './Matrix3.js';
  10. import RuntimeError from './RuntimeError.js';
  11. /**
  12. * A 4x4 matrix, indexable as a column-major order array.
  13. * Constructor parameters are in row-major order for code readability.
  14. * @alias Matrix4
  15. * @constructor
  16. *
  17. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  18. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  19. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  20. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  21. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  22. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  23. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  24. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  25. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  26. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  27. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  28. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  29. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  30. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  31. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  32. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  33. *
  34. * @see Matrix4.fromColumnMajorArray
  35. * @see Matrix4.fromRowMajorArray
  36. * @see Matrix4.fromRotationTranslation
  37. * @see Matrix4.fromTranslationRotationScale
  38. * @see Matrix4.fromTranslationQuaternionRotationScale
  39. * @see Matrix4.fromTranslation
  40. * @see Matrix4.fromScale
  41. * @see Matrix4.fromUniformScale
  42. * @see Matrix4.fromCamera
  43. * @see Matrix4.computePerspectiveFieldOfView
  44. * @see Matrix4.computeOrthographicOffCenter
  45. * @see Matrix4.computePerspectiveOffCenter
  46. * @see Matrix4.computeInfinitePerspectiveOffCenter
  47. * @see Matrix4.computeViewportTransformation
  48. * @see Matrix4.computeView
  49. * @see Matrix2
  50. * @see Matrix3
  51. * @see Packable
  52. */
  53. function Matrix4(column0Row0, column1Row0, column2Row0, column3Row0,
  54. column0Row1, column1Row1, column2Row1, column3Row1,
  55. column0Row2, column1Row2, column2Row2, column3Row2,
  56. column0Row3, column1Row3, column2Row3, column3Row3) {
  57. this[0] = defaultValue(column0Row0, 0.0);
  58. this[1] = defaultValue(column0Row1, 0.0);
  59. this[2] = defaultValue(column0Row2, 0.0);
  60. this[3] = defaultValue(column0Row3, 0.0);
  61. this[4] = defaultValue(column1Row0, 0.0);
  62. this[5] = defaultValue(column1Row1, 0.0);
  63. this[6] = defaultValue(column1Row2, 0.0);
  64. this[7] = defaultValue(column1Row3, 0.0);
  65. this[8] = defaultValue(column2Row0, 0.0);
  66. this[9] = defaultValue(column2Row1, 0.0);
  67. this[10] = defaultValue(column2Row2, 0.0);
  68. this[11] = defaultValue(column2Row3, 0.0);
  69. this[12] = defaultValue(column3Row0, 0.0);
  70. this[13] = defaultValue(column3Row1, 0.0);
  71. this[14] = defaultValue(column3Row2, 0.0);
  72. this[15] = defaultValue(column3Row3, 0.0);
  73. }
  74. /**
  75. * The number of elements used to pack the object into an array.
  76. * @type {Number}
  77. */
  78. Matrix4.packedLength = 16;
  79. /**
  80. * Stores the provided instance into the provided array.
  81. *
  82. * @param {Matrix4} value The value to pack.
  83. * @param {Number[]} array The array to pack into.
  84. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  85. *
  86. * @returns {Number[]} The array that was packed into
  87. */
  88. Matrix4.pack = function(value, array, startingIndex) {
  89. //>>includeStart('debug', pragmas.debug);
  90. Check.typeOf.object('value', value);
  91. Check.defined('array', array);
  92. //>>includeEnd('debug');
  93. startingIndex = defaultValue(startingIndex, 0);
  94. array[startingIndex++] = value[0];
  95. array[startingIndex++] = value[1];
  96. array[startingIndex++] = value[2];
  97. array[startingIndex++] = value[3];
  98. array[startingIndex++] = value[4];
  99. array[startingIndex++] = value[5];
  100. array[startingIndex++] = value[6];
  101. array[startingIndex++] = value[7];
  102. array[startingIndex++] = value[8];
  103. array[startingIndex++] = value[9];
  104. array[startingIndex++] = value[10];
  105. array[startingIndex++] = value[11];
  106. array[startingIndex++] = value[12];
  107. array[startingIndex++] = value[13];
  108. array[startingIndex++] = value[14];
  109. array[startingIndex] = value[15];
  110. return array;
  111. };
  112. /**
  113. * Retrieves an instance from a packed array.
  114. *
  115. * @param {Number[]} array The packed array.
  116. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  117. * @param {Matrix4} [result] The object into which to store the result.
  118. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  119. */
  120. Matrix4.unpack = function(array, startingIndex, result) {
  121. //>>includeStart('debug', pragmas.debug);
  122. Check.defined('array', array);
  123. //>>includeEnd('debug');
  124. startingIndex = defaultValue(startingIndex, 0);
  125. if (!defined(result)) {
  126. result = new Matrix4();
  127. }
  128. result[0] = array[startingIndex++];
  129. result[1] = array[startingIndex++];
  130. result[2] = array[startingIndex++];
  131. result[3] = array[startingIndex++];
  132. result[4] = array[startingIndex++];
  133. result[5] = array[startingIndex++];
  134. result[6] = array[startingIndex++];
  135. result[7] = array[startingIndex++];
  136. result[8] = array[startingIndex++];
  137. result[9] = array[startingIndex++];
  138. result[10] = array[startingIndex++];
  139. result[11] = array[startingIndex++];
  140. result[12] = array[startingIndex++];
  141. result[13] = array[startingIndex++];
  142. result[14] = array[startingIndex++];
  143. result[15] = array[startingIndex];
  144. return result;
  145. };
  146. /**
  147. * Duplicates a Matrix4 instance.
  148. *
  149. * @param {Matrix4} matrix The matrix to duplicate.
  150. * @param {Matrix4} [result] The object onto which to store the result.
  151. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  152. */
  153. Matrix4.clone = function(matrix, result) {
  154. if (!defined(matrix)) {
  155. return undefined;
  156. }
  157. if (!defined(result)) {
  158. return new Matrix4(matrix[0], matrix[4], matrix[8], matrix[12],
  159. matrix[1], matrix[5], matrix[9], matrix[13],
  160. matrix[2], matrix[6], matrix[10], matrix[14],
  161. matrix[3], matrix[7], matrix[11], matrix[15]);
  162. }
  163. result[0] = matrix[0];
  164. result[1] = matrix[1];
  165. result[2] = matrix[2];
  166. result[3] = matrix[3];
  167. result[4] = matrix[4];
  168. result[5] = matrix[5];
  169. result[6] = matrix[6];
  170. result[7] = matrix[7];
  171. result[8] = matrix[8];
  172. result[9] = matrix[9];
  173. result[10] = matrix[10];
  174. result[11] = matrix[11];
  175. result[12] = matrix[12];
  176. result[13] = matrix[13];
  177. result[14] = matrix[14];
  178. result[15] = matrix[15];
  179. return result;
  180. };
  181. /**
  182. * Creates a Matrix4 from 16 consecutive elements in an array.
  183. * @function
  184. *
  185. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  186. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  187. * @param {Matrix4} [result] The object onto which to store the result.
  188. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  189. *
  190. * @example
  191. * // Create the Matrix4:
  192. * // [1.0, 2.0, 3.0, 4.0]
  193. * // [1.0, 2.0, 3.0, 4.0]
  194. * // [1.0, 2.0, 3.0, 4.0]
  195. * // [1.0, 2.0, 3.0, 4.0]
  196. *
  197. * var v = [1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  198. * var m = Cesium.Matrix4.fromArray(v);
  199. *
  200. * // Create same Matrix4 with using an offset into an array
  201. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  202. * var m2 = Cesium.Matrix4.fromArray(v2, 2);
  203. */
  204. Matrix4.fromArray = Matrix4.unpack;
  205. /**
  206. * Computes a Matrix4 instance from a column-major order array.
  207. *
  208. * @param {Number[]} values The column-major order array.
  209. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  210. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  211. */
  212. Matrix4.fromColumnMajorArray = function(values, result) {
  213. //>>includeStart('debug', pragmas.debug);
  214. Check.defined('values', values);
  215. //>>includeEnd('debug');
  216. return Matrix4.clone(values, result);
  217. };
  218. /**
  219. * Computes a Matrix4 instance from a row-major order array.
  220. * The resulting matrix will be in column-major order.
  221. *
  222. * @param {Number[]} values The row-major order array.
  223. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  224. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  225. */
  226. Matrix4.fromRowMajorArray = function(values, result) {
  227. //>>includeStart('debug', pragmas.debug);
  228. Check.defined('values', values);
  229. //>>includeEnd('debug');
  230. if (!defined(result)) {
  231. return new Matrix4(values[0], values[1], values[2], values[3],
  232. values[4], values[5], values[6], values[7],
  233. values[8], values[9], values[10], values[11],
  234. values[12], values[13], values[14], values[15]);
  235. }
  236. result[0] = values[0];
  237. result[1] = values[4];
  238. result[2] = values[8];
  239. result[3] = values[12];
  240. result[4] = values[1];
  241. result[5] = values[5];
  242. result[6] = values[9];
  243. result[7] = values[13];
  244. result[8] = values[2];
  245. result[9] = values[6];
  246. result[10] = values[10];
  247. result[11] = values[14];
  248. result[12] = values[3];
  249. result[13] = values[7];
  250. result[14] = values[11];
  251. result[15] = values[15];
  252. return result;
  253. };
  254. /**
  255. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  256. * and a Cartesian3 representing the translation.
  257. *
  258. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  259. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  260. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  261. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  262. */
  263. Matrix4.fromRotationTranslation = function(rotation, translation, result) {
  264. //>>includeStart('debug', pragmas.debug);
  265. Check.typeOf.object('rotation', rotation);
  266. //>>includeEnd('debug');
  267. translation = defaultValue(translation, Cartesian3.ZERO);
  268. if (!defined(result)) {
  269. return new Matrix4(rotation[0], rotation[3], rotation[6], translation.x,
  270. rotation[1], rotation[4], rotation[7], translation.y,
  271. rotation[2], rotation[5], rotation[8], translation.z,
  272. 0.0, 0.0, 0.0, 1.0);
  273. }
  274. result[0] = rotation[0];
  275. result[1] = rotation[1];
  276. result[2] = rotation[2];
  277. result[3] = 0.0;
  278. result[4] = rotation[3];
  279. result[5] = rotation[4];
  280. result[6] = rotation[5];
  281. result[7] = 0.0;
  282. result[8] = rotation[6];
  283. result[9] = rotation[7];
  284. result[10] = rotation[8];
  285. result[11] = 0.0;
  286. result[12] = translation.x;
  287. result[13] = translation.y;
  288. result[14] = translation.z;
  289. result[15] = 1.0;
  290. return result;
  291. };
  292. /**
  293. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  294. * representation with the rotation represented as a quaternion.
  295. *
  296. * @param {Cartesian3} translation The translation transformation.
  297. * @param {Quaternion} rotation The rotation transformation.
  298. * @param {Cartesian3} scale The non-uniform scale transformation.
  299. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  300. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  301. *
  302. * @example
  303. * var result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  304. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  305. * Cesium.Quaternion.IDENTITY, // rotation
  306. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  307. * result);
  308. */
  309. Matrix4.fromTranslationQuaternionRotationScale = function(translation, rotation, scale, result) {
  310. //>>includeStart('debug', pragmas.debug);
  311. Check.typeOf.object('translation', translation);
  312. Check.typeOf.object('rotation', rotation);
  313. Check.typeOf.object('scale', scale);
  314. //>>includeEnd('debug');
  315. if (!defined(result)) {
  316. result = new Matrix4();
  317. }
  318. var scaleX = scale.x;
  319. var scaleY = scale.y;
  320. var scaleZ = scale.z;
  321. var x2 = rotation.x * rotation.x;
  322. var xy = rotation.x * rotation.y;
  323. var xz = rotation.x * rotation.z;
  324. var xw = rotation.x * rotation.w;
  325. var y2 = rotation.y * rotation.y;
  326. var yz = rotation.y * rotation.z;
  327. var yw = rotation.y * rotation.w;
  328. var z2 = rotation.z * rotation.z;
  329. var zw = rotation.z * rotation.w;
  330. var w2 = rotation.w * rotation.w;
  331. var m00 = x2 - y2 - z2 + w2;
  332. var m01 = 2.0 * (xy - zw);
  333. var m02 = 2.0 * (xz + yw);
  334. var m10 = 2.0 * (xy + zw);
  335. var m11 = -x2 + y2 - z2 + w2;
  336. var m12 = 2.0 * (yz - xw);
  337. var m20 = 2.0 * (xz - yw);
  338. var m21 = 2.0 * (yz + xw);
  339. var m22 = -x2 - y2 + z2 + w2;
  340. result[0] = m00 * scaleX;
  341. result[1] = m10 * scaleX;
  342. result[2] = m20 * scaleX;
  343. result[3] = 0.0;
  344. result[4] = m01 * scaleY;
  345. result[5] = m11 * scaleY;
  346. result[6] = m21 * scaleY;
  347. result[7] = 0.0;
  348. result[8] = m02 * scaleZ;
  349. result[9] = m12 * scaleZ;
  350. result[10] = m22 * scaleZ;
  351. result[11] = 0.0;
  352. result[12] = translation.x;
  353. result[13] = translation.y;
  354. result[14] = translation.z;
  355. result[15] = 1.0;
  356. return result;
  357. };
  358. /**
  359. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  360. *
  361. * @param {TranslationRotationScale} translationRotationScale The instance.
  362. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  363. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  364. */
  365. Matrix4.fromTranslationRotationScale = function(translationRotationScale, result) {
  366. //>>includeStart('debug', pragmas.debug);
  367. Check.typeOf.object('translationRotationScale', translationRotationScale);
  368. //>>includeEnd('debug');
  369. return Matrix4.fromTranslationQuaternionRotationScale(translationRotationScale.translation, translationRotationScale.rotation, translationRotationScale.scale, result);
  370. };
  371. /**
  372. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  373. *
  374. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  375. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  376. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  377. *
  378. * @see Matrix4.multiplyByTranslation
  379. */
  380. Matrix4.fromTranslation = function(translation, result) {
  381. //>>includeStart('debug', pragmas.debug);
  382. Check.typeOf.object('translation', translation);
  383. //>>includeEnd('debug');
  384. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  385. };
  386. /**
  387. * Computes a Matrix4 instance representing a non-uniform scale.
  388. *
  389. * @param {Cartesian3} scale The x, y, and z scale factors.
  390. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  391. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  392. *
  393. * @example
  394. * // Creates
  395. * // [7.0, 0.0, 0.0, 0.0]
  396. * // [0.0, 8.0, 0.0, 0.0]
  397. * // [0.0, 0.0, 9.0, 0.0]
  398. * // [0.0, 0.0, 0.0, 1.0]
  399. * var m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  400. */
  401. Matrix4.fromScale = function(scale, result) {
  402. //>>includeStart('debug', pragmas.debug);
  403. Check.typeOf.object('scale', scale);
  404. //>>includeEnd('debug');
  405. if (!defined(result)) {
  406. return new Matrix4(
  407. scale.x, 0.0, 0.0, 0.0,
  408. 0.0, scale.y, 0.0, 0.0,
  409. 0.0, 0.0, scale.z, 0.0,
  410. 0.0, 0.0, 0.0, 1.0);
  411. }
  412. result[0] = scale.x;
  413. result[1] = 0.0;
  414. result[2] = 0.0;
  415. result[3] = 0.0;
  416. result[4] = 0.0;
  417. result[5] = scale.y;
  418. result[6] = 0.0;
  419. result[7] = 0.0;
  420. result[8] = 0.0;
  421. result[9] = 0.0;
  422. result[10] = scale.z;
  423. result[11] = 0.0;
  424. result[12] = 0.0;
  425. result[13] = 0.0;
  426. result[14] = 0.0;
  427. result[15] = 1.0;
  428. return result;
  429. };
  430. /**
  431. * Computes a Matrix4 instance representing a uniform scale.
  432. *
  433. * @param {Number} scale The uniform scale factor.
  434. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  435. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  436. *
  437. * @example
  438. * // Creates
  439. * // [2.0, 0.0, 0.0, 0.0]
  440. * // [0.0, 2.0, 0.0, 0.0]
  441. * // [0.0, 0.0, 2.0, 0.0]
  442. * // [0.0, 0.0, 0.0, 1.0]
  443. * var m = Cesium.Matrix4.fromUniformScale(2.0);
  444. */
  445. Matrix4.fromUniformScale = function(scale, result) {
  446. //>>includeStart('debug', pragmas.debug);
  447. Check.typeOf.number('scale', scale);
  448. //>>includeEnd('debug');
  449. if (!defined(result)) {
  450. return new Matrix4(scale, 0.0, 0.0, 0.0,
  451. 0.0, scale, 0.0, 0.0,
  452. 0.0, 0.0, scale, 0.0,
  453. 0.0, 0.0, 0.0, 1.0);
  454. }
  455. result[0] = scale;
  456. result[1] = 0.0;
  457. result[2] = 0.0;
  458. result[3] = 0.0;
  459. result[4] = 0.0;
  460. result[5] = scale;
  461. result[6] = 0.0;
  462. result[7] = 0.0;
  463. result[8] = 0.0;
  464. result[9] = 0.0;
  465. result[10] = scale;
  466. result[11] = 0.0;
  467. result[12] = 0.0;
  468. result[13] = 0.0;
  469. result[14] = 0.0;
  470. result[15] = 1.0;
  471. return result;
  472. };
  473. var fromCameraF = new Cartesian3();
  474. var fromCameraR = new Cartesian3();
  475. var fromCameraU = new Cartesian3();
  476. /**
  477. * Computes a Matrix4 instance from a Camera.
  478. *
  479. * @param {Camera} camera The camera to use.
  480. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  481. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  482. */
  483. Matrix4.fromCamera = function(camera, result) {
  484. //>>includeStart('debug', pragmas.debug);
  485. Check.typeOf.object('camera', camera);
  486. //>>includeEnd('debug');
  487. var position = camera.position;
  488. var direction = camera.direction;
  489. var up = camera.up;
  490. //>>includeStart('debug', pragmas.debug);
  491. Check.typeOf.object('camera.position', position);
  492. Check.typeOf.object('camera.direction', direction);
  493. Check.typeOf.object('camera.up', up);
  494. //>>includeEnd('debug');
  495. Cartesian3.normalize(direction, fromCameraF);
  496. Cartesian3.normalize(Cartesian3.cross(fromCameraF, up, fromCameraR), fromCameraR);
  497. Cartesian3.normalize(Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU), fromCameraU);
  498. var sX = fromCameraR.x;
  499. var sY = fromCameraR.y;
  500. var sZ = fromCameraR.z;
  501. var fX = fromCameraF.x;
  502. var fY = fromCameraF.y;
  503. var fZ = fromCameraF.z;
  504. var uX = fromCameraU.x;
  505. var uY = fromCameraU.y;
  506. var uZ = fromCameraU.z;
  507. var positionX = position.x;
  508. var positionY = position.y;
  509. var positionZ = position.z;
  510. var t0 = sX * -positionX + sY * -positionY + sZ * -positionZ;
  511. var t1 = uX * -positionX + uY * -positionY + uZ * -positionZ;
  512. var t2 = fX * positionX + fY * positionY + fZ * positionZ;
  513. // The code below this comment is an optimized
  514. // version of the commented lines.
  515. // Rather that create two matrices and then multiply,
  516. // we just bake in the multiplcation as part of creation.
  517. // var rotation = new Matrix4(
  518. // sX, sY, sZ, 0.0,
  519. // uX, uY, uZ, 0.0,
  520. // -fX, -fY, -fZ, 0.0,
  521. // 0.0, 0.0, 0.0, 1.0);
  522. // var translation = new Matrix4(
  523. // 1.0, 0.0, 0.0, -position.x,
  524. // 0.0, 1.0, 0.0, -position.y,
  525. // 0.0, 0.0, 1.0, -position.z,
  526. // 0.0, 0.0, 0.0, 1.0);
  527. // return rotation.multiply(translation);
  528. if (!defined(result)) {
  529. return new Matrix4(
  530. sX, sY, sZ, t0,
  531. uX, uY, uZ, t1,
  532. -fX, -fY, -fZ, t2,
  533. 0.0, 0.0, 0.0, 1.0);
  534. }
  535. result[0] = sX;
  536. result[1] = uX;
  537. result[2] = -fX;
  538. result[3] = 0.0;
  539. result[4] = sY;
  540. result[5] = uY;
  541. result[6] = -fY;
  542. result[7] = 0.0;
  543. result[8] = sZ;
  544. result[9] = uZ;
  545. result[10] = -fZ;
  546. result[11] = 0.0;
  547. result[12] = t0;
  548. result[13] = t1;
  549. result[14] = t2;
  550. result[15] = 1.0;
  551. return result;
  552. };
  553. /**
  554. * Computes a Matrix4 instance representing a perspective transformation matrix.
  555. *
  556. * @param {Number} fovY The field of view along the Y axis in radians.
  557. * @param {Number} aspectRatio The aspect ratio.
  558. * @param {Number} near The distance to the near plane in meters.
  559. * @param {Number} far The distance to the far plane in meters.
  560. * @param {Matrix4} result The object in which the result will be stored.
  561. * @returns {Matrix4} The modified result parameter.
  562. *
  563. * @exception {DeveloperError} fovY must be in (0, PI].
  564. * @exception {DeveloperError} aspectRatio must be greater than zero.
  565. * @exception {DeveloperError} near must be greater than zero.
  566. * @exception {DeveloperError} far must be greater than zero.
  567. */
  568. Matrix4.computePerspectiveFieldOfView = function(fovY, aspectRatio, near, far, result) {
  569. //>>includeStart('debug', pragmas.debug);
  570. Check.typeOf.number.greaterThan('fovY', fovY, 0.0);
  571. Check.typeOf.number.lessThan('fovY', fovY, Math.PI);
  572. Check.typeOf.number.greaterThan('near', near, 0.0);
  573. Check.typeOf.number.greaterThan('far', far, 0.0);
  574. Check.typeOf.object('result', result);
  575. //>>includeEnd('debug');
  576. var bottom = Math.tan(fovY * 0.5);
  577. var column1Row1 = 1.0 / bottom;
  578. var column0Row0 = column1Row1 / aspectRatio;
  579. var column2Row2 = (far + near) / (near - far);
  580. var column3Row2 = (2.0 * far * near) / (near - far);
  581. result[0] = column0Row0;
  582. result[1] = 0.0;
  583. result[2] = 0.0;
  584. result[3] = 0.0;
  585. result[4] = 0.0;
  586. result[5] = column1Row1;
  587. result[6] = 0.0;
  588. result[7] = 0.0;
  589. result[8] = 0.0;
  590. result[9] = 0.0;
  591. result[10] = column2Row2;
  592. result[11] = -1.0;
  593. result[12] = 0.0;
  594. result[13] = 0.0;
  595. result[14] = column3Row2;
  596. result[15] = 0.0;
  597. return result;
  598. };
  599. /**
  600. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  601. *
  602. * @param {Number} left The number of meters to the left of the camera that will be in view.
  603. * @param {Number} right The number of meters to the right of the camera that will be in view.
  604. * @param {Number} bottom The number of meters below of the camera that will be in view.
  605. * @param {Number} top The number of meters above of the camera that will be in view.
  606. * @param {Number} near The distance to the near plane in meters.
  607. * @param {Number} far The distance to the far plane in meters.
  608. * @param {Matrix4} result The object in which the result will be stored.
  609. * @returns {Matrix4} The modified result parameter.
  610. */
  611. Matrix4.computeOrthographicOffCenter = function(left, right, bottom, top, near, far, result) {
  612. //>>includeStart('debug', pragmas.debug);
  613. Check.typeOf.number('left', left);
  614. Check.typeOf.number('right', right);
  615. Check.typeOf.number('bottom', bottom);
  616. Check.typeOf.number('top', top);
  617. Check.typeOf.number('near', near);
  618. Check.typeOf.number('far', far);
  619. Check.typeOf.object('result', result);
  620. //>>includeEnd('debug');
  621. var a = 1.0 / (right - left);
  622. var b = 1.0 / (top - bottom);
  623. var c = 1.0 / (far - near);
  624. var tx = -(right + left) * a;
  625. var ty = -(top + bottom) * b;
  626. var tz = -(far + near) * c;
  627. a *= 2.0;
  628. b *= 2.0;
  629. c *= -2.0;
  630. result[0] = a;
  631. result[1] = 0.0;
  632. result[2] = 0.0;
  633. result[3] = 0.0;
  634. result[4] = 0.0;
  635. result[5] = b;
  636. result[6] = 0.0;
  637. result[7] = 0.0;
  638. result[8] = 0.0;
  639. result[9] = 0.0;
  640. result[10] = c;
  641. result[11] = 0.0;
  642. result[12] = tx;
  643. result[13] = ty;
  644. result[14] = tz;
  645. result[15] = 1.0;
  646. return result;
  647. };
  648. /**
  649. * Computes a Matrix4 instance representing an off center perspective transformation.
  650. *
  651. * @param {Number} left The number of meters to the left of the camera that will be in view.
  652. * @param {Number} right The number of meters to the right of the camera that will be in view.
  653. * @param {Number} bottom The number of meters below of the camera that will be in view.
  654. * @param {Number} top The number of meters above of the camera that will be in view.
  655. * @param {Number} near The distance to the near plane in meters.
  656. * @param {Number} far The distance to the far plane in meters.
  657. * @param {Matrix4} result The object in which the result will be stored.
  658. * @returns {Matrix4} The modified result parameter.
  659. */
  660. Matrix4.computePerspectiveOffCenter = function(left, right, bottom, top, near, far, result) {
  661. //>>includeStart('debug', pragmas.debug);
  662. Check.typeOf.number('left', left);
  663. Check.typeOf.number('right', right);
  664. Check.typeOf.number('bottom', bottom);
  665. Check.typeOf.number('top', top);
  666. Check.typeOf.number('near', near);
  667. Check.typeOf.number('far', far);
  668. Check.typeOf.object('result', result);
  669. //>>includeEnd('debug');
  670. var column0Row0 = 2.0 * near / (right - left);
  671. var column1Row1 = 2.0 * near / (top - bottom);
  672. var column2Row0 = (right + left) / (right - left);
  673. var column2Row1 = (top + bottom) / (top - bottom);
  674. var column2Row2 = -(far + near) / (far - near);
  675. var column2Row3 = -1.0;
  676. var column3Row2 = -2.0 * far * near / (far - near);
  677. result[0] = column0Row0;
  678. result[1] = 0.0;
  679. result[2] = 0.0;
  680. result[3] = 0.0;
  681. result[4] = 0.0;
  682. result[5] = column1Row1;
  683. result[6] = 0.0;
  684. result[7] = 0.0;
  685. result[8] = column2Row0;
  686. result[9] = column2Row1;
  687. result[10] = column2Row2;
  688. result[11] = column2Row3;
  689. result[12] = 0.0;
  690. result[13] = 0.0;
  691. result[14] = column3Row2;
  692. result[15] = 0.0;
  693. return result;
  694. };
  695. /**
  696. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  697. *
  698. * @param {Number} left The number of meters to the left of the camera that will be in view.
  699. * @param {Number} right The number of meters to the right of the camera that will be in view.
  700. * @param {Number} bottom The number of meters below of the camera that will be in view.
  701. * @param {Number} top The number of meters above of the camera that will be in view.
  702. * @param {Number} near The distance to the near plane in meters.
  703. * @param {Matrix4} result The object in which the result will be stored.
  704. * @returns {Matrix4} The modified result parameter.
  705. */
  706. Matrix4.computeInfinitePerspectiveOffCenter = function(left, right, bottom, top, near, result) {
  707. //>>includeStart('debug', pragmas.debug);
  708. Check.typeOf.number('left', left);
  709. Check.typeOf.number('right', right);
  710. Check.typeOf.number('bottom', bottom);
  711. Check.typeOf.number('top', top);
  712. Check.typeOf.number('near', near);
  713. Check.typeOf.object('result', result);
  714. //>>includeEnd('debug');
  715. var column0Row0 = 2.0 * near / (right - left);
  716. var column1Row1 = 2.0 * near / (top - bottom);
  717. var column2Row0 = (right + left) / (right - left);
  718. var column2Row1 = (top + bottom) / (top - bottom);
  719. var column2Row2 = -1.0;
  720. var column2Row3 = -1.0;
  721. var column3Row2 = -2.0 * near;
  722. result[0] = column0Row0;
  723. result[1] = 0.0;
  724. result[2] = 0.0;
  725. result[3] = 0.0;
  726. result[4] = 0.0;
  727. result[5] = column1Row1;
  728. result[6] = 0.0;
  729. result[7] = 0.0;
  730. result[8] = column2Row0;
  731. result[9] = column2Row1;
  732. result[10] = column2Row2;
  733. result[11] = column2Row3;
  734. result[12] = 0.0;
  735. result[13] = 0.0;
  736. result[14] = column3Row2;
  737. result[15] = 0.0;
  738. return result;
  739. };
  740. /**
  741. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  742. *
  743. * @param {Object}[viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  744. * @param {Number}[nearDepthRange=0.0] The near plane distance in window coordinates.
  745. * @param {Number}[farDepthRange=1.0] The far plane distance in window coordinates.
  746. * @param {Matrix4} result The object in which the result will be stored.
  747. * @returns {Matrix4} The modified result parameter.
  748. *
  749. * @example
  750. * // Create viewport transformation using an explicit viewport and depth range.
  751. * var m = Cesium.Matrix4.computeViewportTransformation({
  752. * x : 0.0,
  753. * y : 0.0,
  754. * width : 1024.0,
  755. * height : 768.0
  756. * }, 0.0, 1.0, new Cesium.Matrix4());
  757. */
  758. Matrix4.computeViewportTransformation = function(viewport, nearDepthRange, farDepthRange, result) {
  759. //>>includeStart('debug', pragmas.debug);
  760. Check.typeOf.object('result', result);
  761. //>>includeEnd('debug');
  762. viewport = defaultValue(viewport, defaultValue.EMPTY_OBJECT);
  763. var x = defaultValue(viewport.x, 0.0);
  764. var y = defaultValue(viewport.y, 0.0);
  765. var width = defaultValue(viewport.width, 0.0);
  766. var height = defaultValue(viewport.height, 0.0);
  767. nearDepthRange = defaultValue(nearDepthRange, 0.0);
  768. farDepthRange = defaultValue(farDepthRange, 1.0);
  769. var halfWidth = width * 0.5;
  770. var halfHeight = height * 0.5;
  771. var halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  772. var column0Row0 = halfWidth;
  773. var column1Row1 = halfHeight;
  774. var column2Row2 = halfDepth;
  775. var column3Row0 = x + halfWidth;
  776. var column3Row1 = y + halfHeight;
  777. var column3Row2 = nearDepthRange + halfDepth;
  778. var column3Row3 = 1.0;
  779. result[0] = column0Row0;
  780. result[1] = 0.0;
  781. result[2] = 0.0;
  782. result[3] = 0.0;
  783. result[4] = 0.0;
  784. result[5] = column1Row1;
  785. result[6] = 0.0;
  786. result[7] = 0.0;
  787. result[8] = 0.0;
  788. result[9] = 0.0;
  789. result[10] = column2Row2;
  790. result[11] = 0.0;
  791. result[12] = column3Row0;
  792. result[13] = column3Row1;
  793. result[14] = column3Row2;
  794. result[15] = column3Row3;
  795. return result;
  796. };
  797. /**
  798. * Computes a Matrix4 instance that transforms from world space to view space.
  799. *
  800. * @param {Cartesian3} position The position of the camera.
  801. * @param {Cartesian3} direction The forward direction.
  802. * @param {Cartesian3} up The up direction.
  803. * @param {Cartesian3} right The right direction.
  804. * @param {Matrix4} result The object in which the result will be stored.
  805. * @returns {Matrix4} The modified result parameter.
  806. */
  807. Matrix4.computeView = function(position, direction, up, right, result) {
  808. //>>includeStart('debug', pragmas.debug);
  809. Check.typeOf.object('position', position);
  810. Check.typeOf.object('direction', direction);
  811. Check.typeOf.object('up', up);
  812. Check.typeOf.object('right', right);
  813. Check.typeOf.object('result', result);
  814. //>>includeEnd('debug');
  815. result[0] = right.x;
  816. result[1] = up.x;
  817. result[2] = -direction.x;
  818. result[3] = 0.0;
  819. result[4] = right.y;
  820. result[5] = up.y;
  821. result[6] = -direction.y;
  822. result[7] = 0.0;
  823. result[8] = right.z;
  824. result[9] = up.z;
  825. result[10] = -direction.z;
  826. result[11] = 0.0;
  827. result[12] = -Cartesian3.dot(right, position);
  828. result[13] = -Cartesian3.dot(up, position);
  829. result[14] = Cartesian3.dot(direction, position);
  830. result[15] = 1.0;
  831. return result;
  832. };
  833. /**
  834. * Computes an Array from the provided Matrix4 instance.
  835. * The array will be in column-major order.
  836. *
  837. * @param {Matrix4} matrix The matrix to use..
  838. * @param {Number[]} [result] The Array onto which to store the result.
  839. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  840. *
  841. * @example
  842. * //create an array from an instance of Matrix4
  843. * // m = [10.0, 14.0, 18.0, 22.0]
  844. * // [11.0, 15.0, 19.0, 23.0]
  845. * // [12.0, 16.0, 20.0, 24.0]
  846. * // [13.0, 17.0, 21.0, 25.0]
  847. * var a = Cesium.Matrix4.toArray(m);
  848. *
  849. * // m remains the same
  850. * //creates a = [10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0]
  851. */
  852. Matrix4.toArray = function(matrix, result) {
  853. //>>includeStart('debug', pragmas.debug);
  854. Check.typeOf.object('matrix', matrix);
  855. //>>includeEnd('debug');
  856. if (!defined(result)) {
  857. return [matrix[0], matrix[1], matrix[2], matrix[3],
  858. matrix[4], matrix[5], matrix[6], matrix[7],
  859. matrix[8], matrix[9], matrix[10], matrix[11],
  860. matrix[12], matrix[13], matrix[14], matrix[15]];
  861. }
  862. result[0] = matrix[0];
  863. result[1] = matrix[1];
  864. result[2] = matrix[2];
  865. result[3] = matrix[3];
  866. result[4] = matrix[4];
  867. result[5] = matrix[5];
  868. result[6] = matrix[6];
  869. result[7] = matrix[7];
  870. result[8] = matrix[8];
  871. result[9] = matrix[9];
  872. result[10] = matrix[10];
  873. result[11] = matrix[11];
  874. result[12] = matrix[12];
  875. result[13] = matrix[13];
  876. result[14] = matrix[14];
  877. result[15] = matrix[15];
  878. return result;
  879. };
  880. /**
  881. * Computes the array index of the element at the provided row and column.
  882. *
  883. * @param {Number} row The zero-based index of the row.
  884. * @param {Number} column The zero-based index of the column.
  885. * @returns {Number} The index of the element at the provided row and column.
  886. *
  887. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  888. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  889. *
  890. * @example
  891. * var myMatrix = new Cesium.Matrix4();
  892. * var column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  893. * var column1Row0 = myMatrix[column1Row0Index];
  894. * myMatrix[column1Row0Index] = 10.0;
  895. */
  896. Matrix4.getElementIndex = function(column, row) {
  897. //>>includeStart('debug', pragmas.debug);
  898. Check.typeOf.number.greaterThanOrEquals('row', row, 0);
  899. Check.typeOf.number.lessThanOrEquals('row', row, 3);
  900. Check.typeOf.number.greaterThanOrEquals('column', column, 0);
  901. Check.typeOf.number.lessThanOrEquals('column', column, 3);
  902. //>>includeEnd('debug');
  903. return column * 4 + row;
  904. };
  905. /**
  906. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  907. *
  908. * @param {Matrix4} matrix The matrix to use.
  909. * @param {Number} index The zero-based index of the column to retrieve.
  910. * @param {Cartesian4} result The object onto which to store the result.
  911. * @returns {Cartesian4} The modified result parameter.
  912. *
  913. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  914. *
  915. * @example
  916. * //returns a Cartesian4 instance with values from the specified column
  917. * // m = [10.0, 11.0, 12.0, 13.0]
  918. * // [14.0, 15.0, 16.0, 17.0]
  919. * // [18.0, 19.0, 20.0, 21.0]
  920. * // [22.0, 23.0, 24.0, 25.0]
  921. *
  922. * //Example 1: Creates an instance of Cartesian
  923. * var a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  924. *
  925. * @example
  926. * //Example 2: Sets values for Cartesian instance
  927. * var a = new Cesium.Cartesian4();
  928. * Cesium.Matrix4.getColumn(m, 2, a);
  929. *
  930. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  931. */
  932. Matrix4.getColumn = function(matrix, index, result) {
  933. //>>includeStart('debug', pragmas.debug);
  934. Check.typeOf.object('matrix', matrix);
  935. Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  936. Check.typeOf.number.lessThanOrEquals('index', index, 3);
  937. Check.typeOf.object('result', result);
  938. //>>includeEnd('debug');
  939. var startIndex = index * 4;
  940. var x = matrix[startIndex];
  941. var y = matrix[startIndex + 1];
  942. var z = matrix[startIndex + 2];
  943. var w = matrix[startIndex + 3];
  944. result.x = x;
  945. result.y = y;
  946. result.z = z;
  947. result.w = w;
  948. return result;
  949. };
  950. /**
  951. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  952. *
  953. * @param {Matrix4} matrix The matrix to use.
  954. * @param {Number} index The zero-based index of the column to set.
  955. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  956. * @param {Matrix4} result The object onto which to store the result.
  957. * @returns {Matrix4} The modified result parameter.
  958. *
  959. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  960. *
  961. * @example
  962. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  963. * // m = [10.0, 11.0, 12.0, 13.0]
  964. * // [14.0, 15.0, 16.0, 17.0]
  965. * // [18.0, 19.0, 20.0, 21.0]
  966. * // [22.0, 23.0, 24.0, 25.0]
  967. *
  968. * var a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  969. *
  970. * // m remains the same
  971. * // a = [10.0, 11.0, 99.0, 13.0]
  972. * // [14.0, 15.0, 98.0, 17.0]
  973. * // [18.0, 19.0, 97.0, 21.0]
  974. * // [22.0, 23.0, 96.0, 25.0]
  975. */
  976. Matrix4.setColumn = function(matrix, index, cartesian, result) {
  977. //>>includeStart('debug', pragmas.debug);
  978. Check.typeOf.object('matrix', matrix);
  979. Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  980. Check.typeOf.number.lessThanOrEquals('index', index, 3);
  981. Check.typeOf.object('cartesian', cartesian);
  982. Check.typeOf.object('result', result);
  983. //>>includeEnd('debug');
  984. result = Matrix4.clone(matrix, result);
  985. var startIndex = index * 4;
  986. result[startIndex] = cartesian.x;
  987. result[startIndex + 1] = cartesian.y;
  988. result[startIndex + 2] = cartesian.z;
  989. result[startIndex + 3] = cartesian.w;
  990. return result;
  991. };
  992. /**
  993. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  994. * matrix with the provided translation. This assumes the matrix is an affine transformation
  995. *
  996. * @param {Matrix4} matrix The matrix to use.
  997. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  998. * @param {Matrix4} result The object onto which to store the result.
  999. * @returns {Matrix4} The modified result parameter.
  1000. */
  1001. Matrix4.setTranslation = function(matrix, translation, result) {
  1002. //>>includeStart('debug', pragmas.debug);
  1003. Check.typeOf.object('matrix', matrix);
  1004. Check.typeOf.object('translation', translation);
  1005. Check.typeOf.object('result', result);
  1006. //>>includeEnd('debug');
  1007. result[0] = matrix[0];
  1008. result[1] = matrix[1];
  1009. result[2] = matrix[2];
  1010. result[3] = matrix[3];
  1011. result[4] = matrix[4];
  1012. result[5] = matrix[5];
  1013. result[6] = matrix[6];
  1014. result[7] = matrix[7];
  1015. result[8] = matrix[8];
  1016. result[9] = matrix[9];
  1017. result[10] = matrix[10];
  1018. result[11] = matrix[11];
  1019. result[12] = translation.x;
  1020. result[13] = translation.y;
  1021. result[14] = translation.z;
  1022. result[15] = matrix[15];
  1023. return result;
  1024. };
  1025. var scaleScratch = new Cartesian3();
  1026. /**
  1027. * Computes a new matrix that replaces the scale with the provided scale. This assumes the matrix is an affine transformation
  1028. *
  1029. * @param {Matrix4} matrix The matrix to use.
  1030. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  1031. * @param {Matrix4} result The object onto which to store the result.
  1032. * @returns {Matrix4} The modified result parameter.
  1033. */
  1034. Matrix4.setScale = function(matrix, scale, result) {
  1035. //>>includeStart('debug', pragmas.debug);
  1036. Check.typeOf.object('matrix', matrix);
  1037. Check.typeOf.object('scale', scale);
  1038. Check.typeOf.object('result', result);
  1039. //>>includeEnd('debug');
  1040. var existingScale = Matrix4.getScale(matrix, scaleScratch);
  1041. var newScale = Cartesian3.divideComponents(scale, existingScale, scaleScratch);
  1042. return Matrix4.multiplyByScale(matrix, newScale, result);
  1043. };
  1044. /**
  1045. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  1046. *
  1047. * @param {Matrix4} matrix The matrix to use.
  1048. * @param {Number} index The zero-based index of the row to retrieve.
  1049. * @param {Cartesian4} result The object onto which to store the result.
  1050. * @returns {Cartesian4} The modified result parameter.
  1051. *
  1052. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  1053. *
  1054. * @example
  1055. * //returns a Cartesian4 instance with values from the specified column
  1056. * // m = [10.0, 11.0, 12.0, 13.0]
  1057. * // [14.0, 15.0, 16.0, 17.0]
  1058. * // [18.0, 19.0, 20.0, 21.0]
  1059. * // [22.0, 23.0, 24.0, 25.0]
  1060. *
  1061. * //Example 1: Returns an instance of Cartesian
  1062. * var a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  1063. *
  1064. * @example
  1065. * //Example 2: Sets values for a Cartesian instance
  1066. * var a = new Cesium.Cartesian4();
  1067. * Cesium.Matrix4.getRow(m, 2, a);
  1068. *
  1069. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  1070. */
  1071. Matrix4.getRow = function(matrix, index, result) {
  1072. //>>includeStart('debug', pragmas.debug);
  1073. Check.typeOf.object('matrix', matrix);
  1074. Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  1075. Check.typeOf.number.lessThanOrEquals('index', index, 3);
  1076. Check.typeOf.object('result', result);
  1077. //>>includeEnd('debug');
  1078. var x = matrix[index];
  1079. var y = matrix[index + 4];
  1080. var z = matrix[index + 8];
  1081. var w = matrix[index + 12];
  1082. result.x = x;
  1083. result.y = y;
  1084. result.z = z;
  1085. result.w = w;
  1086. return result;
  1087. };
  1088. /**
  1089. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  1090. *
  1091. * @param {Matrix4} matrix The matrix to use.
  1092. * @param {Number} index The zero-based index of the row to set.
  1093. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  1094. * @param {Matrix4} result The object onto which to store the result.
  1095. * @returns {Matrix4} The modified result parameter.
  1096. *
  1097. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  1098. *
  1099. * @example
  1100. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  1101. * // m = [10.0, 11.0, 12.0, 13.0]
  1102. * // [14.0, 15.0, 16.0, 17.0]
  1103. * // [18.0, 19.0, 20.0, 21.0]
  1104. * // [22.0, 23.0, 24.0, 25.0]
  1105. *
  1106. * var a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  1107. *
  1108. * // m remains the same
  1109. * // a = [10.0, 11.0, 12.0, 13.0]
  1110. * // [14.0, 15.0, 16.0, 17.0]
  1111. * // [99.0, 98.0, 97.0, 96.0]
  1112. * // [22.0, 23.0, 24.0, 25.0]
  1113. */
  1114. Matrix4.setRow = function(matrix, index, cartesian, result) {
  1115. //>>includeStart('debug', pragmas.debug);
  1116. Check.typeOf.object('matrix', matrix);
  1117. Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  1118. Check.typeOf.number.lessThanOrEquals('index', index, 3);
  1119. Check.typeOf.object('cartesian', cartesian);
  1120. Check.typeOf.object('result', result);
  1121. //>>includeEnd('debug');
  1122. result = Matrix4.clone(matrix, result);
  1123. result[index] = cartesian.x;
  1124. result[index + 4] = cartesian.y;
  1125. result[index + 8] = cartesian.z;
  1126. result[index + 12] = cartesian.w;
  1127. return result;
  1128. };
  1129. var scratchColumn = new Cartesian3();
  1130. /**
  1131. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  1132. *
  1133. * @param {Matrix4} matrix The matrix.
  1134. * @param {Cartesian3} result The object onto which to store the result.
  1135. * @returns {Cartesian3} The modified result parameter
  1136. */
  1137. Matrix4.getScale = function(matrix, result) {
  1138. //>>includeStart('debug', pragmas.debug);
  1139. Check.typeOf.object('matrix', matrix);
  1140. Check.typeOf.object('result', result);
  1141. //>>includeEnd('debug');
  1142. result.x = Cartesian3.magnitude(Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  1143. result.y = Cartesian3.magnitude(Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn));
  1144. result.z = Cartesian3.magnitude(Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn));
  1145. return result;
  1146. };
  1147. var scratchScale = new Cartesian3();
  1148. /**
  1149. * Computes the maximum scale assuming the matrix is an affine transformation.
  1150. * The maximum scale is the maximum length of the column vectors in the upper-left
  1151. * 3x3 matrix.
  1152. *
  1153. * @param {Matrix4} matrix The matrix.
  1154. * @returns {Number} The maximum scale.
  1155. */
  1156. Matrix4.getMaximumScale = function(matrix) {
  1157. Matrix4.getScale(matrix, scratchScale);
  1158. return Cartesian3.maximumComponent(scratchScale);
  1159. };
  1160. /**
  1161. * Computes the product of two matrices.
  1162. *
  1163. * @param {Matrix4} left The first matrix.
  1164. * @param {Matrix4} right The second matrix.
  1165. * @param {Matrix4} result The object onto which to store the result.
  1166. * @returns {Matrix4} The modified result parameter.
  1167. */
  1168. Matrix4.multiply = function(left, right, result) {
  1169. //>>includeStart('debug', pragmas.debug);
  1170. Check.typeOf.object('left', left);
  1171. Check.typeOf.object('right', right);
  1172. Check.typeOf.object('result', result);
  1173. //>>includeEnd('debug');
  1174. var left0 = left[0];
  1175. var left1 = left[1];
  1176. var left2 = left[2];
  1177. var left3 = left[3];
  1178. var left4 = left[4];
  1179. var left5 = left[5];
  1180. var left6 = left[6];
  1181. var left7 = left[7];
  1182. var left8 = left[8];
  1183. var left9 = left[9];
  1184. var left10 = left[10];
  1185. var left11 = left[11];
  1186. var left12 = left[12];
  1187. var left13 = left[13];
  1188. var left14 = left[14];
  1189. var left15 = left[15];
  1190. var right0 = right[0];
  1191. var right1 = right[1];
  1192. var right2 = right[2];
  1193. var right3 = right[3];
  1194. var right4 = right[4];
  1195. var right5 = right[5];
  1196. var right6 = right[6];
  1197. var right7 = right[7];
  1198. var right8 = right[8];
  1199. var right9 = right[9];
  1200. var right10 = right[10];
  1201. var right11 = right[11];
  1202. var right12 = right[12];
  1203. var right13 = right[13];
  1204. var right14 = right[14];
  1205. var right15 = right[15];
  1206. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  1207. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  1208. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  1209. var column0Row3 = left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  1210. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  1211. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  1212. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  1213. var column1Row3 = left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  1214. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  1215. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  1216. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  1217. var column2Row3 = left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  1218. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  1219. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  1220. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  1221. var column3Row3 = left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  1222. result[0] = column0Row0;
  1223. result[1] = column0Row1;
  1224. result[2] = column0Row2;
  1225. result[3] = column0Row3;
  1226. result[4] = column1Row0;
  1227. result[5] = column1Row1;
  1228. result[6] = column1Row2;
  1229. result[7] = column1Row3;
  1230. result[8] = column2Row0;
  1231. result[9] = column2Row1;
  1232. result[10] = column2Row2;
  1233. result[11] = column2Row3;
  1234. result[12] = column3Row0;
  1235. result[13] = column3Row1;
  1236. result[14] = column3Row2;
  1237. result[15] = column3Row3;
  1238. return result;
  1239. };
  1240. /**
  1241. * Computes the sum of two matrices.
  1242. *
  1243. * @param {Matrix4} left The first matrix.
  1244. * @param {Matrix4} right The second matrix.
  1245. * @param {Matrix4} result The object onto which to store the result.
  1246. * @returns {Matrix4} The modified result parameter.
  1247. */
  1248. Matrix4.add = function(left, right, result) {
  1249. //>>includeStart('debug', pragmas.debug);
  1250. Check.typeOf.object('left', left);
  1251. Check.typeOf.object('right', right);
  1252. Check.typeOf.object('result', result);
  1253. //>>includeEnd('debug');
  1254. result[0] = left[0] + right[0];
  1255. result[1] = left[1] + right[1];
  1256. result[2] = left[2] + right[2];
  1257. result[3] = left[3] + right[3];
  1258. result[4] = left[4] + right[4];
  1259. result[5] = left[5] + right[5];
  1260. result[6] = left[6] + right[6];
  1261. result[7] = left[7] + right[7];
  1262. result[8] = left[8] + right[8];
  1263. result[9] = left[9] + right[9];
  1264. result[10] = left[10] + right[10];
  1265. result[11] = left[11] + right[11];
  1266. result[12] = left[12] + right[12];
  1267. result[13] = left[13] + right[13];
  1268. result[14] = left[14] + right[14];
  1269. result[15] = left[15] + right[15];
  1270. return result;
  1271. };
  1272. /**
  1273. * Computes the difference of two matrices.
  1274. *
  1275. * @param {Matrix4} left The first matrix.
  1276. * @param {Matrix4} right The second matrix.
  1277. * @param {Matrix4} result The object onto which to store the result.
  1278. * @returns {Matrix4} The modified result parameter.
  1279. */
  1280. Matrix4.subtract = function(left, right, result) {
  1281. //>>includeStart('debug', pragmas.debug);
  1282. Check.typeOf.object('left', left);
  1283. Check.typeOf.object('right', right);
  1284. Check.typeOf.object('result', result);
  1285. //>>includeEnd('debug');
  1286. result[0] = left[0] - right[0];
  1287. result[1] = left[1] - right[1];
  1288. result[2] = left[2] - right[2];
  1289. result[3] = left[3] - right[3];
  1290. result[4] = left[4] - right[4];
  1291. result[5] = left[5] - right[5];
  1292. result[6] = left[6] - right[6];
  1293. result[7] = left[7] - right[7];
  1294. result[8] = left[8] - right[8];
  1295. result[9] = left[9] - right[9];
  1296. result[10] = left[10] - right[10];
  1297. result[11] = left[11] - right[11];
  1298. result[12] = left[12] - right[12];
  1299. result[13] = left[13] - right[13];
  1300. result[14] = left[14] - right[14];
  1301. result[15] = left[15] - right[15];
  1302. return result;
  1303. };
  1304. /**
  1305. * Computes the product of two matrices assuming the matrices are
  1306. * affine transformation matrices, where the upper left 3x3 elements
  1307. * are a rotation matrix, and the upper three elements in the fourth
  1308. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  1309. * The matrix is not verified to be in the proper form.
  1310. * This method is faster than computing the product for general 4x4
  1311. * matrices using {@link Matrix4.multiply}.
  1312. *
  1313. * @param {Matrix4} left The first matrix.
  1314. * @param {Matrix4} right The second matrix.
  1315. * @param {Matrix4} result The object onto which to store the result.
  1316. * @returns {Matrix4} The modified result parameter.
  1317. *
  1318. * @example
  1319. * var m1 = new Cesium.Matrix4(1.0, 6.0, 7.0, 0.0, 2.0, 5.0, 8.0, 0.0, 3.0, 4.0, 9.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  1320. * var m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  1321. * var m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  1322. */
  1323. Matrix4.multiplyTransformation = function(left, right, result) {
  1324. //>>includeStart('debug', pragmas.debug);
  1325. Check.typeOf.object('left', left);
  1326. Check.typeOf.object('right', right);
  1327. Check.typeOf.object('result', result);
  1328. //>>includeEnd('debug');
  1329. var left0 = left[0];
  1330. var left1 = left[1];
  1331. var left2 = left[2];
  1332. var left4 = left[4];
  1333. var left5 = left[5];
  1334. var left6 = left[6];
  1335. var left8 = left[8];
  1336. var left9 = left[9];
  1337. var left10 = left[10];
  1338. var left12 = left[12];
  1339. var left13 = left[13];
  1340. var left14 = left[14];
  1341. var right0 = right[0];
  1342. var right1 = right[1];
  1343. var right2 = right[2];
  1344. var right4 = right[4];
  1345. var right5 = right[5];
  1346. var right6 = right[6];
  1347. var right8 = right[8];
  1348. var right9 = right[9];
  1349. var right10 = right[10];
  1350. var right12 = right[12];
  1351. var right13 = right[13];
  1352. var right14 = right[14];
  1353. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  1354. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  1355. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  1356. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  1357. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  1358. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  1359. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  1360. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  1361. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  1362. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12;
  1363. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13;
  1364. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14;
  1365. result[0] = column0Row0;
  1366. result[1] = column0Row1;
  1367. result[2] = column0Row2;
  1368. result[3] = 0.0;
  1369. result[4] = column1Row0;
  1370. result[5] = column1Row1;
  1371. result[6] = column1Row2;
  1372. result[7] = 0.0;
  1373. result[8] = column2Row0;
  1374. result[9] = column2Row1;
  1375. result[10] = column2Row2;
  1376. result[11] = 0.0;
  1377. result[12] = column3Row0;
  1378. result[13] = column3Row1;
  1379. result[14] = column3Row2;
  1380. result[15] = 1.0;
  1381. return result;
  1382. };
  1383. /**
  1384. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  1385. * by a 3x3 rotation matrix. This is an optimization
  1386. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  1387. *
  1388. * @param {Matrix4} matrix The matrix on the left-hand side.
  1389. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  1390. * @param {Matrix4} result The object onto which to store the result.
  1391. * @returns {Matrix4} The modified result parameter.
  1392. *
  1393. * @example
  1394. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  1395. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  1396. */
  1397. Matrix4.multiplyByMatrix3 = function(matrix, rotation, result) {
  1398. //>>includeStart('debug', pragmas.debug);
  1399. Check.typeOf.object('matrix', matrix);
  1400. Check.typeOf.object('rotation', rotation);
  1401. Check.typeOf.object('result', result);
  1402. //>>includeEnd('debug');
  1403. var left0 = matrix[0];
  1404. var left1 = matrix[1];
  1405. var left2 = matrix[2];
  1406. var left4 = matrix[4];
  1407. var left5 = matrix[5];
  1408. var left6 = matrix[6];
  1409. var left8 = matrix[8];
  1410. var left9 = matrix[9];
  1411. var left10 = matrix[10];
  1412. var right0 = rotation[0];
  1413. var right1 = rotation[1];
  1414. var right2 = rotation[2];
  1415. var right4 = rotation[3];
  1416. var right5 = rotation[4];
  1417. var right6 = rotation[5];
  1418. var right8 = rotation[6];
  1419. var right9 = rotation[7];
  1420. var right10 = rotation[8];
  1421. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  1422. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  1423. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  1424. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  1425. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  1426. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  1427. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  1428. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  1429. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  1430. result[0] = column0Row0;
  1431. result[1] = column0Row1;
  1432. result[2] = column0Row2;
  1433. result[3] = 0.0;
  1434. result[4] = column1Row0;
  1435. result[5] = column1Row1;
  1436. result[6] = column1Row2;
  1437. result[7] = 0.0;
  1438. result[8] = column2Row0;
  1439. result[9] = column2Row1;
  1440. result[10] = column2Row2;
  1441. result[11] = 0.0;
  1442. result[12] = matrix[12];
  1443. result[13] = matrix[13];
  1444. result[14] = matrix[14];
  1445. result[15] = matrix[15];
  1446. return result;
  1447. };
  1448. /**
  1449. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  1450. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  1451. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  1452. *
  1453. * @param {Matrix4} matrix The matrix on the left-hand side.
  1454. * @param {Cartesian3} translation The translation on the right-hand side.
  1455. * @param {Matrix4} result The object onto which to store the result.
  1456. * @returns {Matrix4} The modified result parameter.
  1457. *
  1458. * @example
  1459. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  1460. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  1461. */
  1462. Matrix4.multiplyByTranslation = function(matrix, translation, result) {
  1463. //>>includeStart('debug', pragmas.debug);
  1464. Check.typeOf.object('matrix', matrix);
  1465. Check.typeOf.object('translation', translation);
  1466. Check.typeOf.object('result', result);
  1467. //>>includeEnd('debug');
  1468. var x = translation.x;
  1469. var y = translation.y;
  1470. var z = translation.z;
  1471. var tx = (x * matrix[0]) + (y * matrix[4]) + (z * matrix[8]) + matrix[12];
  1472. var ty = (x * matrix[1]) + (y * matrix[5]) + (z * matrix[9]) + matrix[13];
  1473. var tz = (x * matrix[2]) + (y * matrix[6]) + (z * matrix[10]) + matrix[14];
  1474. result[0] = matrix[0];
  1475. result[1] = matrix[1];
  1476. result[2] = matrix[2];
  1477. result[3] = matrix[3];
  1478. result[4] = matrix[4];
  1479. result[5] = matrix[5];
  1480. result[6] = matrix[6];
  1481. result[7] = matrix[7];
  1482. result[8] = matrix[8];
  1483. result[9] = matrix[9];
  1484. result[10] = matrix[10];
  1485. result[11] = matrix[11];
  1486. result[12] = tx;
  1487. result[13] = ty;
  1488. result[14] = tz;
  1489. result[15] = matrix[15];
  1490. return result;
  1491. };
  1492. var uniformScaleScratch = new Cartesian3();
  1493. /**
  1494. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  1495. * by an implicit uniform scale matrix. This is an optimization
  1496. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  1497. * <code>m</code> must be an affine matrix.
  1498. * This function performs fewer allocations and arithmetic operations.
  1499. *
  1500. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  1501. * @param {Number} scale The uniform scale on the right-hand side.
  1502. * @param {Matrix4} result The object onto which to store the result.
  1503. * @returns {Matrix4} The modified result parameter.
  1504. *
  1505. *
  1506. * @example
  1507. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  1508. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  1509. *
  1510. * @see Matrix4.fromUniformScale
  1511. * @see Matrix4.multiplyByScale
  1512. */
  1513. Matrix4.multiplyByUniformScale = function(matrix, scale, result) {
  1514. //>>includeStart('debug', pragmas.debug);
  1515. Check.typeOf.object('matrix', matrix);
  1516. Check.typeOf.number('scale', scale);
  1517. Check.typeOf.object('result', result);
  1518. //>>includeEnd('debug');
  1519. uniformScaleScratch.x = scale;
  1520. uniformScaleScratch.y = scale;
  1521. uniformScaleScratch.z = scale;
  1522. return Matrix4.multiplyByScale(matrix, uniformScaleScratch, result);
  1523. };
  1524. /**
  1525. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  1526. * by an implicit non-uniform scale matrix. This is an optimization
  1527. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  1528. * <code>m</code> must be an affine matrix.
  1529. * This function performs fewer allocations and arithmetic operations.
  1530. *
  1531. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  1532. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  1533. * @param {Matrix4} result The object onto which to store the result.
  1534. * @returns {Matrix4} The modified result parameter.
  1535. *
  1536. *
  1537. * @example
  1538. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  1539. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  1540. *
  1541. * @see Matrix4.fromScale
  1542. * @see Matrix4.multiplyByUniformScale
  1543. */
  1544. Matrix4.multiplyByScale = function(matrix, scale, result) {
  1545. //>>includeStart('debug', pragmas.debug);
  1546. Check.typeOf.object('matrix', matrix);
  1547. Check.typeOf.object('scale', scale);
  1548. Check.typeOf.object('result', result);
  1549. //>>includeEnd('debug');
  1550. var scaleX = scale.x;
  1551. var scaleY = scale.y;
  1552. var scaleZ = scale.z;
  1553. // Faster than Cartesian3.equals
  1554. if ((scaleX === 1.0) && (scaleY === 1.0) && (scaleZ === 1.0)) {
  1555. return Matrix4.clone(matrix, result);
  1556. }
  1557. result[0] = scaleX * matrix[0];
  1558. result[1] = scaleX * matrix[1];
  1559. result[2] = scaleX * matrix[2];
  1560. result[3] = 0.0;
  1561. result[4] = scaleY * matrix[4];
  1562. result[5] = scaleY * matrix[5];
  1563. result[6] = scaleY * matrix[6];
  1564. result[7] = 0.0;
  1565. result[8] = scaleZ * matrix[8];
  1566. result[9] = scaleZ * matrix[9];
  1567. result[10] = scaleZ * matrix[10];
  1568. result[11] = 0.0;
  1569. result[12] = matrix[12];
  1570. result[13] = matrix[13];
  1571. result[14] = matrix[14];
  1572. result[15] = 1.0;
  1573. return result;
  1574. };
  1575. /**
  1576. * Computes the product of a matrix and a column vector.
  1577. *
  1578. * @param {Matrix4} matrix The matrix.
  1579. * @param {Cartesian4} cartesian The vector.
  1580. * @param {Cartesian4} result The object onto which to store the result.
  1581. * @returns {Cartesian4} The modified result parameter.
  1582. */
  1583. Matrix4.multiplyByVector = function(matrix, cartesian, result) {
  1584. //>>includeStart('debug', pragmas.debug);
  1585. Check.typeOf.object('matrix', matrix);
  1586. Check.typeOf.object('cartesian', cartesian);
  1587. Check.typeOf.object('result', result);
  1588. //>>includeEnd('debug');
  1589. var vX = cartesian.x;
  1590. var vY = cartesian.y;
  1591. var vZ = cartesian.z;
  1592. var vW = cartesian.w;
  1593. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  1594. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  1595. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  1596. var w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  1597. result.x = x;
  1598. result.y = y;
  1599. result.z = z;
  1600. result.w = w;
  1601. return result;
  1602. };
  1603. /**
  1604. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  1605. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  1606. *
  1607. * @param {Matrix4} matrix The matrix.
  1608. * @param {Cartesian3} cartesian The point.
  1609. * @param {Cartesian3} result The object onto which to store the result.
  1610. * @returns {Cartesian3} The modified result parameter.
  1611. *
  1612. * @example
  1613. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  1614. * var result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  1615. * // A shortcut for
  1616. * // Cartesian3 p = ...
  1617. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  1618. */
  1619. Matrix4.multiplyByPointAsVector = function(matrix, cartesian, result) {
  1620. //>>includeStart('debug', pragmas.debug);
  1621. Check.typeOf.object('matrix', matrix);
  1622. Check.typeOf.object('cartesian', cartesian);
  1623. Check.typeOf.object('result', result);
  1624. //>>includeEnd('debug');
  1625. var vX = cartesian.x;
  1626. var vY = cartesian.y;
  1627. var vZ = cartesian.z;
  1628. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  1629. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  1630. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  1631. result.x = x;
  1632. result.y = y;
  1633. result.z = z;
  1634. return result;
  1635. };
  1636. /**
  1637. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  1638. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  1639. *
  1640. * @param {Matrix4} matrix The matrix.
  1641. * @param {Cartesian3} cartesian The point.
  1642. * @param {Cartesian3} result The object onto which to store the result.
  1643. * @returns {Cartesian3} The modified result parameter.
  1644. *
  1645. * @example
  1646. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  1647. * var result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  1648. */
  1649. Matrix4.multiplyByPoint = function(matrix, cartesian, result) {
  1650. //>>includeStart('debug', pragmas.debug);
  1651. Check.typeOf.object('matrix', matrix);
  1652. Check.typeOf.object('cartesian', cartesian);
  1653. Check.typeOf.object('result', result);
  1654. //>>includeEnd('debug');
  1655. var vX = cartesian.x;
  1656. var vY = cartesian.y;
  1657. var vZ = cartesian.z;
  1658. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  1659. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  1660. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  1661. result.x = x;
  1662. result.y = y;
  1663. result.z = z;
  1664. return result;
  1665. };
  1666. /**
  1667. * Computes the product of a matrix and a scalar.
  1668. *
  1669. * @param {Matrix4} matrix The matrix.
  1670. * @param {Number} scalar The number to multiply by.
  1671. * @param {Matrix4} result The object onto which to store the result.
  1672. * @returns {Matrix4} The modified result parameter.
  1673. *
  1674. * @example
  1675. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  1676. * // m = [10.0, 11.0, 12.0, 13.0]
  1677. * // [14.0, 15.0, 16.0, 17.0]
  1678. * // [18.0, 19.0, 20.0, 21.0]
  1679. * // [22.0, 23.0, 24.0, 25.0]
  1680. *
  1681. * var a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  1682. *
  1683. * // m remains the same
  1684. * // a = [-20.0, -22.0, -24.0, -26.0]
  1685. * // [-28.0, -30.0, -32.0, -34.0]
  1686. * // [-36.0, -38.0, -40.0, -42.0]
  1687. * // [-44.0, -46.0, -48.0, -50.0]
  1688. */
  1689. Matrix4.multiplyByScalar = function(matrix, scalar, result) {
  1690. //>>includeStart('debug', pragmas.debug);
  1691. Check.typeOf.object('matrix', matrix);
  1692. Check.typeOf.number('scalar', scalar);
  1693. Check.typeOf.object('result', result);
  1694. //>>includeEnd('debug');
  1695. result[0] = matrix[0] * scalar;
  1696. result[1] = matrix[1] * scalar;
  1697. result[2] = matrix[2] * scalar;
  1698. result[3] = matrix[3] * scalar;
  1699. result[4] = matrix[4] * scalar;
  1700. result[5] = matrix[5] * scalar;
  1701. result[6] = matrix[6] * scalar;
  1702. result[7] = matrix[7] * scalar;
  1703. result[8] = matrix[8] * scalar;
  1704. result[9] = matrix[9] * scalar;
  1705. result[10] = matrix[10] * scalar;
  1706. result[11] = matrix[11] * scalar;
  1707. result[12] = matrix[12] * scalar;
  1708. result[13] = matrix[13] * scalar;
  1709. result[14] = matrix[14] * scalar;
  1710. result[15] = matrix[15] * scalar;
  1711. return result;
  1712. };
  1713. /**
  1714. * Computes a negated copy of the provided matrix.
  1715. *
  1716. * @param {Matrix4} matrix The matrix to negate.
  1717. * @param {Matrix4} result The object onto which to store the result.
  1718. * @returns {Matrix4} The modified result parameter.
  1719. *
  1720. * @example
  1721. * //create a new Matrix4 instance which is a negation of a Matrix4
  1722. * // m = [10.0, 11.0, 12.0, 13.0]
  1723. * // [14.0, 15.0, 16.0, 17.0]
  1724. * // [18.0, 19.0, 20.0, 21.0]
  1725. * // [22.0, 23.0, 24.0, 25.0]
  1726. *
  1727. * var a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  1728. *
  1729. * // m remains the same
  1730. * // a = [-10.0, -11.0, -12.0, -13.0]
  1731. * // [-14.0, -15.0, -16.0, -17.0]
  1732. * // [-18.0, -19.0, -20.0, -21.0]
  1733. * // [-22.0, -23.0, -24.0, -25.0]
  1734. */
  1735. Matrix4.negate = function(matrix, result) {
  1736. //>>includeStart('debug', pragmas.debug);
  1737. Check.typeOf.object('matrix', matrix);
  1738. Check.typeOf.object('result', result);
  1739. //>>includeEnd('debug');
  1740. result[0] = -matrix[0];
  1741. result[1] = -matrix[1];
  1742. result[2] = -matrix[2];
  1743. result[3] = -matrix[3];
  1744. result[4] = -matrix[4];
  1745. result[5] = -matrix[5];
  1746. result[6] = -matrix[6];
  1747. result[7] = -matrix[7];
  1748. result[8] = -matrix[8];
  1749. result[9] = -matrix[9];
  1750. result[10] = -matrix[10];
  1751. result[11] = -matrix[11];
  1752. result[12] = -matrix[12];
  1753. result[13] = -matrix[13];
  1754. result[14] = -matrix[14];
  1755. result[15] = -matrix[15];
  1756. return result;
  1757. };
  1758. /**
  1759. * Computes the transpose of the provided matrix.
  1760. *
  1761. * @param {Matrix4} matrix The matrix to transpose.
  1762. * @param {Matrix4} result The object onto which to store the result.
  1763. * @returns {Matrix4} The modified result parameter.
  1764. *
  1765. * @example
  1766. * //returns transpose of a Matrix4
  1767. * // m = [10.0, 11.0, 12.0, 13.0]
  1768. * // [14.0, 15.0, 16.0, 17.0]
  1769. * // [18.0, 19.0, 20.0, 21.0]
  1770. * // [22.0, 23.0, 24.0, 25.0]
  1771. *
  1772. * var a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  1773. *
  1774. * // m remains the same
  1775. * // a = [10.0, 14.0, 18.0, 22.0]
  1776. * // [11.0, 15.0, 19.0, 23.0]
  1777. * // [12.0, 16.0, 20.0, 24.0]
  1778. * // [13.0, 17.0, 21.0, 25.0]
  1779. */
  1780. Matrix4.transpose = function(matrix, result) {
  1781. //>>includeStart('debug', pragmas.debug);
  1782. Check.typeOf.object('matrix', matrix);
  1783. Check.typeOf.object('result', result);
  1784. //>>includeEnd('debug');
  1785. var matrix1 = matrix[1];
  1786. var matrix2 = matrix[2];
  1787. var matrix3 = matrix[3];
  1788. var matrix6 = matrix[6];
  1789. var matrix7 = matrix[7];
  1790. var matrix11 = matrix[11];
  1791. result[0] = matrix[0];
  1792. result[1] = matrix[4];
  1793. result[2] = matrix[8];
  1794. result[3] = matrix[12];
  1795. result[4] = matrix1;
  1796. result[5] = matrix[5];
  1797. result[6] = matrix[9];
  1798. result[7] = matrix[13];
  1799. result[8] = matrix2;
  1800. result[9] = matrix6;
  1801. result[10] = matrix[10];
  1802. result[11] = matrix[14];
  1803. result[12] = matrix3;
  1804. result[13] = matrix7;
  1805. result[14] = matrix11;
  1806. result[15] = matrix[15];
  1807. return result;
  1808. };
  1809. /**
  1810. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  1811. *
  1812. * @param {Matrix4} matrix The matrix with signed elements.
  1813. * @param {Matrix4} result The object onto which to store the result.
  1814. * @returns {Matrix4} The modified result parameter.
  1815. */
  1816. Matrix4.abs = function(matrix, result) {
  1817. //>>includeStart('debug', pragmas.debug);
  1818. Check.typeOf.object('matrix', matrix);
  1819. Check.typeOf.object('result', result);
  1820. //>>includeEnd('debug');
  1821. result[0] = Math.abs(matrix[0]);
  1822. result[1] = Math.abs(matrix[1]);
  1823. result[2] = Math.abs(matrix[2]);
  1824. result[3] = Math.abs(matrix[3]);
  1825. result[4] = Math.abs(matrix[4]);
  1826. result[5] = Math.abs(matrix[5]);
  1827. result[6] = Math.abs(matrix[6]);
  1828. result[7] = Math.abs(matrix[7]);
  1829. result[8] = Math.abs(matrix[8]);
  1830. result[9] = Math.abs(matrix[9]);
  1831. result[10] = Math.abs(matrix[10]);
  1832. result[11] = Math.abs(matrix[11]);
  1833. result[12] = Math.abs(matrix[12]);
  1834. result[13] = Math.abs(matrix[13]);
  1835. result[14] = Math.abs(matrix[14]);
  1836. result[15] = Math.abs(matrix[15]);
  1837. return result;
  1838. };
  1839. /**
  1840. * Compares the provided matrices componentwise and returns
  1841. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1842. *
  1843. * @param {Matrix4} [left] The first matrix.
  1844. * @param {Matrix4} [right] The second matrix.
  1845. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1846. *
  1847. * @example
  1848. * //compares two Matrix4 instances
  1849. *
  1850. * // a = [10.0, 14.0, 18.0, 22.0]
  1851. * // [11.0, 15.0, 19.0, 23.0]
  1852. * // [12.0, 16.0, 20.0, 24.0]
  1853. * // [13.0, 17.0, 21.0, 25.0]
  1854. *
  1855. * // b = [10.0, 14.0, 18.0, 22.0]
  1856. * // [11.0, 15.0, 19.0, 23.0]
  1857. * // [12.0, 16.0, 20.0, 24.0]
  1858. * // [13.0, 17.0, 21.0, 25.0]
  1859. *
  1860. * if(Cesium.Matrix4.equals(a,b)) {
  1861. * console.log("Both matrices are equal");
  1862. * } else {
  1863. * console.log("They are not equal");
  1864. * }
  1865. *
  1866. * //Prints "Both matrices are equal" on the console
  1867. */
  1868. Matrix4.equals = function(left, right) {
  1869. // Given that most matrices will be transformation matrices, the elements
  1870. // are tested in order such that the test is likely to fail as early
  1871. // as possible. I _think_ this is just as friendly to the L1 cache
  1872. // as testing in index order. It is certainty faster in practice.
  1873. return (left === right) ||
  1874. (defined(left) &&
  1875. defined(right) &&
  1876. // Translation
  1877. left[12] === right[12] &&
  1878. left[13] === right[13] &&
  1879. left[14] === right[14] &&
  1880. // Rotation/scale
  1881. left[0] === right[0] &&
  1882. left[1] === right[1] &&
  1883. left[2] === right[2] &&
  1884. left[4] === right[4] &&
  1885. left[5] === right[5] &&
  1886. left[6] === right[6] &&
  1887. left[8] === right[8] &&
  1888. left[9] === right[9] &&
  1889. left[10] === right[10] &&
  1890. // Bottom row
  1891. left[3] === right[3] &&
  1892. left[7] === right[7] &&
  1893. left[11] === right[11] &&
  1894. left[15] === right[15]);
  1895. };
  1896. /**
  1897. * Compares the provided matrices componentwise and returns
  1898. * <code>true</code> if they are within the provided epsilon,
  1899. * <code>false</code> otherwise.
  1900. *
  1901. * @param {Matrix4} [left] The first matrix.
  1902. * @param {Matrix4} [right] The second matrix.
  1903. * @param {Number} epsilon The epsilon to use for equality testing.
  1904. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1905. *
  1906. * @example
  1907. * //compares two Matrix4 instances
  1908. *
  1909. * // a = [10.5, 14.5, 18.5, 22.5]
  1910. * // [11.5, 15.5, 19.5, 23.5]
  1911. * // [12.5, 16.5, 20.5, 24.5]
  1912. * // [13.5, 17.5, 21.5, 25.5]
  1913. *
  1914. * // b = [10.0, 14.0, 18.0, 22.0]
  1915. * // [11.0, 15.0, 19.0, 23.0]
  1916. * // [12.0, 16.0, 20.0, 24.0]
  1917. * // [13.0, 17.0, 21.0, 25.0]
  1918. *
  1919. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  1920. * console.log("Difference between both the matrices is less than 0.1");
  1921. * } else {
  1922. * console.log("Difference between both the matrices is not less than 0.1");
  1923. * }
  1924. *
  1925. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  1926. */
  1927. Matrix4.equalsEpsilon = function(left, right, epsilon) {
  1928. //>>includeStart('debug', pragmas.debug);
  1929. Check.typeOf.number('epsilon', epsilon);
  1930. //>>includeEnd('debug');
  1931. return (left === right) ||
  1932. (defined(left) &&
  1933. defined(right) &&
  1934. Math.abs(left[0] - right[0]) <= epsilon &&
  1935. Math.abs(left[1] - right[1]) <= epsilon &&
  1936. Math.abs(left[2] - right[2]) <= epsilon &&
  1937. Math.abs(left[3] - right[3]) <= epsilon &&
  1938. Math.abs(left[4] - right[4]) <= epsilon &&
  1939. Math.abs(left[5] - right[5]) <= epsilon &&
  1940. Math.abs(left[6] - right[6]) <= epsilon &&
  1941. Math.abs(left[7] - right[7]) <= epsilon &&
  1942. Math.abs(left[8] - right[8]) <= epsilon &&
  1943. Math.abs(left[9] - right[9]) <= epsilon &&
  1944. Math.abs(left[10] - right[10]) <= epsilon &&
  1945. Math.abs(left[11] - right[11]) <= epsilon &&
  1946. Math.abs(left[12] - right[12]) <= epsilon &&
  1947. Math.abs(left[13] - right[13]) <= epsilon &&
  1948. Math.abs(left[14] - right[14]) <= epsilon &&
  1949. Math.abs(left[15] - right[15]) <= epsilon);
  1950. };
  1951. /**
  1952. * Gets the translation portion of the provided matrix, assuming the matrix is a affine transformation matrix.
  1953. *
  1954. * @param {Matrix4} matrix The matrix to use.
  1955. * @param {Cartesian3} result The object onto which to store the result.
  1956. * @returns {Cartesian3} The modified result parameter.
  1957. */
  1958. Matrix4.getTranslation = function(matrix, result) {
  1959. //>>includeStart('debug', pragmas.debug);
  1960. Check.typeOf.object('matrix', matrix);
  1961. Check.typeOf.object('result', result);
  1962. //>>includeEnd('debug');
  1963. result.x = matrix[12];
  1964. result.y = matrix[13];
  1965. result.z = matrix[14];
  1966. return result;
  1967. };
  1968. /**
  1969. * Gets the upper left 3x3 rotation matrix of the provided matrix, assuming the matrix is an affine transformation matrix.
  1970. *
  1971. * @param {Matrix4} matrix The matrix to use.
  1972. * @param {Matrix3} result The object onto which to store the result.
  1973. * @returns {Matrix3} The modified result parameter.
  1974. *
  1975. * @example
  1976. * // returns a Matrix3 instance from a Matrix4 instance
  1977. *
  1978. * // m = [10.0, 14.0, 18.0, 22.0]
  1979. * // [11.0, 15.0, 19.0, 23.0]
  1980. * // [12.0, 16.0, 20.0, 24.0]
  1981. * // [13.0, 17.0, 21.0, 25.0]
  1982. *
  1983. * var b = new Cesium.Matrix3();
  1984. * Cesium.Matrix4.getMatrix3(m,b);
  1985. *
  1986. * // b = [10.0, 14.0, 18.0]
  1987. * // [11.0, 15.0, 19.0]
  1988. * // [12.0, 16.0, 20.0]
  1989. */
  1990. Matrix4.getMatrix3 = function(matrix, result) {
  1991. //>>includeStart('debug', pragmas.debug);
  1992. Check.typeOf.object('matrix', matrix);
  1993. Check.typeOf.object('result', result);
  1994. //>>includeEnd('debug');
  1995. result[0] = matrix[0];
  1996. result[1] = matrix[1];
  1997. result[2] = matrix[2];
  1998. result[3] = matrix[4];
  1999. result[4] = matrix[5];
  2000. result[5] = matrix[6];
  2001. result[6] = matrix[8];
  2002. result[7] = matrix[9];
  2003. result[8] = matrix[10];
  2004. return result;
  2005. };
  2006. var scratchInverseRotation = new Matrix3();
  2007. var scratchMatrix3Zero = new Matrix3();
  2008. var scratchBottomRow = new Cartesian4();
  2009. var scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  2010. /**
  2011. * Computes the inverse of the provided matrix using Cramers Rule.
  2012. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  2013. * If the matrix is an affine transformation matrix, it is more efficient
  2014. * to invert it with {@link Matrix4.inverseTransformation}.
  2015. *
  2016. * @param {Matrix4} matrix The matrix to invert.
  2017. * @param {Matrix4} result The object onto which to store the result.
  2018. * @returns {Matrix4} The modified result parameter.
  2019. *
  2020. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  2021. */
  2022. Matrix4.inverse = function(matrix, result) {
  2023. //>>includeStart('debug', pragmas.debug);
  2024. Check.typeOf.object('matrix', matrix);
  2025. Check.typeOf.object('result', result);
  2026. //>>includeEnd('debug');
  2027. //
  2028. // Ported from:
  2029. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  2030. //
  2031. var src0 = matrix[0];
  2032. var src1 = matrix[4];
  2033. var src2 = matrix[8];
  2034. var src3 = matrix[12];
  2035. var src4 = matrix[1];
  2036. var src5 = matrix[5];
  2037. var src6 = matrix[9];
  2038. var src7 = matrix[13];
  2039. var src8 = matrix[2];
  2040. var src9 = matrix[6];
  2041. var src10 = matrix[10];
  2042. var src11 = matrix[14];
  2043. var src12 = matrix[3];
  2044. var src13 = matrix[7];
  2045. var src14 = matrix[11];
  2046. var src15 = matrix[15];
  2047. // calculate pairs for first 8 elements (cofactors)
  2048. var tmp0 = src10 * src15;
  2049. var tmp1 = src11 * src14;
  2050. var tmp2 = src9 * src15;
  2051. var tmp3 = src11 * src13;
  2052. var tmp4 = src9 * src14;
  2053. var tmp5 = src10 * src13;
  2054. var tmp6 = src8 * src15;
  2055. var tmp7 = src11 * src12;
  2056. var tmp8 = src8 * src14;
  2057. var tmp9 = src10 * src12;
  2058. var tmp10 = src8 * src13;
  2059. var tmp11 = src9 * src12;
  2060. // calculate first 8 elements (cofactors)
  2061. var dst0 = (tmp0 * src5 + tmp3 * src6 + tmp4 * src7) - (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  2062. var dst1 = (tmp1 * src4 + tmp6 * src6 + tmp9 * src7) - (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  2063. var dst2 = (tmp2 * src4 + tmp7 * src5 + tmp10 * src7) - (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  2064. var dst3 = (tmp5 * src4 + tmp8 * src5 + tmp11 * src6) - (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  2065. var dst4 = (tmp1 * src1 + tmp2 * src2 + tmp5 * src3) - (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  2066. var dst5 = (tmp0 * src0 + tmp7 * src2 + tmp8 * src3) - (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  2067. var dst6 = (tmp3 * src0 + tmp6 * src1 + tmp11 * src3) - (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  2068. var dst7 = (tmp4 * src0 + tmp9 * src1 + tmp10 * src2) - (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  2069. // calculate pairs for second 8 elements (cofactors)
  2070. tmp0 = src2 * src7;
  2071. tmp1 = src3 * src6;
  2072. tmp2 = src1 * src7;
  2073. tmp3 = src3 * src5;
  2074. tmp4 = src1 * src6;
  2075. tmp5 = src2 * src5;
  2076. tmp6 = src0 * src7;
  2077. tmp7 = src3 * src4;
  2078. tmp8 = src0 * src6;
  2079. tmp9 = src2 * src4;
  2080. tmp10 = src0 * src5;
  2081. tmp11 = src1 * src4;
  2082. // calculate second 8 elements (cofactors)
  2083. var dst8 = (tmp0 * src13 + tmp3 * src14 + tmp4 * src15) - (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  2084. var dst9 = (tmp1 * src12 + tmp6 * src14 + tmp9 * src15) - (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  2085. var dst10 = (tmp2 * src12 + tmp7 * src13 + tmp10 * src15) - (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  2086. var dst11 = (tmp5 * src12 + tmp8 * src13 + tmp11 * src14) - (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  2087. var dst12 = (tmp2 * src10 + tmp5 * src11 + tmp1 * src9) - (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  2088. var dst13 = (tmp8 * src11 + tmp0 * src8 + tmp7 * src10) - (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  2089. var dst14 = (tmp6 * src9 + tmp11 * src11 + tmp3 * src8) - (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  2090. var dst15 = (tmp10 * src10 + tmp4 * src8 + tmp9 * src9) - (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  2091. // calculate determinant
  2092. var det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  2093. if (Math.abs(det) < CesiumMath.EPSILON21) {
  2094. // Special case for a zero scale matrix that can occur, for example,
  2095. // when a model's node has a [0, 0, 0] scale.
  2096. if (Matrix3.equalsEpsilon(Matrix4.getMatrix3(matrix, scratchInverseRotation), scratchMatrix3Zero, CesiumMath.EPSILON7) &&
  2097. Cartesian4.equals(Matrix4.getRow(matrix, 3, scratchBottomRow), scratchExpectedBottomRow)) {
  2098. result[0] = 0.0;
  2099. result[1] = 0.0;
  2100. result[2] = 0.0;
  2101. result[3] = 0.0;
  2102. result[4] = 0.0;
  2103. result[5] = 0.0;
  2104. result[6] = 0.0;
  2105. result[7] = 0.0;
  2106. result[8] = 0.0;
  2107. result[9] = 0.0;
  2108. result[10] = 0.0;
  2109. result[11] = 0.0;
  2110. result[12] = -matrix[12];
  2111. result[13] = -matrix[13];
  2112. result[14] = -matrix[14];
  2113. result[15] = 1.0;
  2114. return result;
  2115. }
  2116. throw new RuntimeError('matrix is not invertible because its determinate is zero.');
  2117. }
  2118. // calculate matrix inverse
  2119. det = 1.0 / det;
  2120. result[0] = dst0 * det;
  2121. result[1] = dst1 * det;
  2122. result[2] = dst2 * det;
  2123. result[3] = dst3 * det;
  2124. result[4] = dst4 * det;
  2125. result[5] = dst5 * det;
  2126. result[6] = dst6 * det;
  2127. result[7] = dst7 * det;
  2128. result[8] = dst8 * det;
  2129. result[9] = dst9 * det;
  2130. result[10] = dst10 * det;
  2131. result[11] = dst11 * det;
  2132. result[12] = dst12 * det;
  2133. result[13] = dst13 * det;
  2134. result[14] = dst14 * det;
  2135. result[15] = dst15 * det;
  2136. return result;
  2137. };
  2138. /**
  2139. * Computes the inverse of the provided matrix assuming it is
  2140. * an affine transformation matrix, where the upper left 3x3 elements
  2141. * are a rotation matrix, and the upper three elements in the fourth
  2142. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  2143. * The matrix is not verified to be in the proper form.
  2144. * This method is faster than computing the inverse for a general 4x4
  2145. * matrix using {@link Matrix4.inverse}.
  2146. *
  2147. * @param {Matrix4} matrix The matrix to invert.
  2148. * @param {Matrix4} result The object onto which to store the result.
  2149. * @returns {Matrix4} The modified result parameter.
  2150. */
  2151. Matrix4.inverseTransformation = function(matrix, result) {
  2152. //>>includeStart('debug', pragmas.debug);
  2153. Check.typeOf.object('matrix', matrix);
  2154. Check.typeOf.object('result', result);
  2155. //>>includeEnd('debug');
  2156. //This function is an optimized version of the below 4 lines.
  2157. //var rT = Matrix3.transpose(Matrix4.getMatrix3(matrix));
  2158. //var rTN = Matrix3.negate(rT);
  2159. //var rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  2160. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  2161. var matrix0 = matrix[0];
  2162. var matrix1 = matrix[1];
  2163. var matrix2 = matrix[2];
  2164. var matrix4 = matrix[4];
  2165. var matrix5 = matrix[5];
  2166. var matrix6 = matrix[6];
  2167. var matrix8 = matrix[8];
  2168. var matrix9 = matrix[9];
  2169. var matrix10 = matrix[10];
  2170. var vX = matrix[12];
  2171. var vY = matrix[13];
  2172. var vZ = matrix[14];
  2173. var x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  2174. var y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  2175. var z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  2176. result[0] = matrix0;
  2177. result[1] = matrix4;
  2178. result[2] = matrix8;
  2179. result[3] = 0.0;
  2180. result[4] = matrix1;
  2181. result[5] = matrix5;
  2182. result[6] = matrix9;
  2183. result[7] = 0.0;
  2184. result[8] = matrix2;
  2185. result[9] = matrix6;
  2186. result[10] = matrix10;
  2187. result[11] = 0.0;
  2188. result[12] = x;
  2189. result[13] = y;
  2190. result[14] = z;
  2191. result[15] = 1.0;
  2192. return result;
  2193. };
  2194. /**
  2195. * An immutable Matrix4 instance initialized to the identity matrix.
  2196. *
  2197. * @type {Matrix4}
  2198. * @constant
  2199. */
  2200. Matrix4.IDENTITY = freezeObject(new Matrix4(1.0, 0.0, 0.0, 0.0,
  2201. 0.0, 1.0, 0.0, 0.0,
  2202. 0.0, 0.0, 1.0, 0.0,
  2203. 0.0, 0.0, 0.0, 1.0));
  2204. /**
  2205. * An immutable Matrix4 instance initialized to the zero matrix.
  2206. *
  2207. * @type {Matrix4}
  2208. * @constant
  2209. */
  2210. Matrix4.ZERO = freezeObject(new Matrix4(0.0, 0.0, 0.0, 0.0,
  2211. 0.0, 0.0, 0.0, 0.0,
  2212. 0.0, 0.0, 0.0, 0.0,
  2213. 0.0, 0.0, 0.0, 0.0));
  2214. /**
  2215. * The index into Matrix4 for column 0, row 0.
  2216. *
  2217. * @type {Number}
  2218. * @constant
  2219. */
  2220. Matrix4.COLUMN0ROW0 = 0;
  2221. /**
  2222. * The index into Matrix4 for column 0, row 1.
  2223. *
  2224. * @type {Number}
  2225. * @constant
  2226. */
  2227. Matrix4.COLUMN0ROW1 = 1;
  2228. /**
  2229. * The index into Matrix4 for column 0, row 2.
  2230. *
  2231. * @type {Number}
  2232. * @constant
  2233. */
  2234. Matrix4.COLUMN0ROW2 = 2;
  2235. /**
  2236. * The index into Matrix4 for column 0, row 3.
  2237. *
  2238. * @type {Number}
  2239. * @constant
  2240. */
  2241. Matrix4.COLUMN0ROW3 = 3;
  2242. /**
  2243. * The index into Matrix4 for column 1, row 0.
  2244. *
  2245. * @type {Number}
  2246. * @constant
  2247. */
  2248. Matrix4.COLUMN1ROW0 = 4;
  2249. /**
  2250. * The index into Matrix4 for column 1, row 1.
  2251. *
  2252. * @type {Number}
  2253. * @constant
  2254. */
  2255. Matrix4.COLUMN1ROW1 = 5;
  2256. /**
  2257. * The index into Matrix4 for column 1, row 2.
  2258. *
  2259. * @type {Number}
  2260. * @constant
  2261. */
  2262. Matrix4.COLUMN1ROW2 = 6;
  2263. /**
  2264. * The index into Matrix4 for column 1, row 3.
  2265. *
  2266. * @type {Number}
  2267. * @constant
  2268. */
  2269. Matrix4.COLUMN1ROW3 = 7;
  2270. /**
  2271. * The index into Matrix4 for column 2, row 0.
  2272. *
  2273. * @type {Number}
  2274. * @constant
  2275. */
  2276. Matrix4.COLUMN2ROW0 = 8;
  2277. /**
  2278. * The index into Matrix4 for column 2, row 1.
  2279. *
  2280. * @type {Number}
  2281. * @constant
  2282. */
  2283. Matrix4.COLUMN2ROW1 = 9;
  2284. /**
  2285. * The index into Matrix4 for column 2, row 2.
  2286. *
  2287. * @type {Number}
  2288. * @constant
  2289. */
  2290. Matrix4.COLUMN2ROW2 = 10;
  2291. /**
  2292. * The index into Matrix4 for column 2, row 3.
  2293. *
  2294. * @type {Number}
  2295. * @constant
  2296. */
  2297. Matrix4.COLUMN2ROW3 = 11;
  2298. /**
  2299. * The index into Matrix4 for column 3, row 0.
  2300. *
  2301. * @type {Number}
  2302. * @constant
  2303. */
  2304. Matrix4.COLUMN3ROW0 = 12;
  2305. /**
  2306. * The index into Matrix4 for column 3, row 1.
  2307. *
  2308. * @type {Number}
  2309. * @constant
  2310. */
  2311. Matrix4.COLUMN3ROW1 = 13;
  2312. /**
  2313. * The index into Matrix4 for column 3, row 2.
  2314. *
  2315. * @type {Number}
  2316. * @constant
  2317. */
  2318. Matrix4.COLUMN3ROW2 = 14;
  2319. /**
  2320. * The index into Matrix4 for column 3, row 3.
  2321. *
  2322. * @type {Number}
  2323. * @constant
  2324. */
  2325. Matrix4.COLUMN3ROW3 = 15;
  2326. defineProperties(Matrix4.prototype, {
  2327. /**
  2328. * Gets the number of items in the collection.
  2329. * @memberof Matrix4.prototype
  2330. *
  2331. * @type {Number}
  2332. */
  2333. length : {
  2334. get : function() {
  2335. return Matrix4.packedLength;
  2336. }
  2337. }
  2338. });
  2339. /**
  2340. * Duplicates the provided Matrix4 instance.
  2341. *
  2342. * @param {Matrix4} [result] The object onto which to store the result.
  2343. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  2344. */
  2345. Matrix4.prototype.clone = function(result) {
  2346. return Matrix4.clone(this, result);
  2347. };
  2348. /**
  2349. * Compares this matrix to the provided matrix componentwise and returns
  2350. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2351. *
  2352. * @param {Matrix4} [right] The right hand side matrix.
  2353. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2354. */
  2355. Matrix4.prototype.equals = function(right) {
  2356. return Matrix4.equals(this, right);
  2357. };
  2358. /**
  2359. * @private
  2360. */
  2361. Matrix4.equalsArray = function(matrix, array, offset) {
  2362. return matrix[0] === array[offset] &&
  2363. matrix[1] === array[offset + 1] &&
  2364. matrix[2] === array[offset + 2] &&
  2365. matrix[3] === array[offset + 3] &&
  2366. matrix[4] === array[offset + 4] &&
  2367. matrix[5] === array[offset + 5] &&
  2368. matrix[6] === array[offset + 6] &&
  2369. matrix[7] === array[offset + 7] &&
  2370. matrix[8] === array[offset + 8] &&
  2371. matrix[9] === array[offset + 9] &&
  2372. matrix[10] === array[offset + 10] &&
  2373. matrix[11] === array[offset + 11] &&
  2374. matrix[12] === array[offset + 12] &&
  2375. matrix[13] === array[offset + 13] &&
  2376. matrix[14] === array[offset + 14] &&
  2377. matrix[15] === array[offset + 15];
  2378. };
  2379. /**
  2380. * Compares this matrix to the provided matrix componentwise and returns
  2381. * <code>true</code> if they are within the provided epsilon,
  2382. * <code>false</code> otherwise.
  2383. *
  2384. * @param {Matrix4} [right] The right hand side matrix.
  2385. * @param {Number} epsilon The epsilon to use for equality testing.
  2386. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  2387. */
  2388. Matrix4.prototype.equalsEpsilon = function(right, epsilon) {
  2389. return Matrix4.equalsEpsilon(this, right, epsilon);
  2390. };
  2391. /**
  2392. * Computes a string representing this Matrix with each row being
  2393. * on a separate line and in the format '(column0, column1, column2, column3)'.
  2394. *
  2395. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  2396. */
  2397. Matrix4.prototype.toString = function() {
  2398. return '(' + this[0] + ', ' + this[4] + ', ' + this[8] + ', ' + this[12] + ')\n' +
  2399. '(' + this[1] + ', ' + this[5] + ', ' + this[9] + ', ' + this[13] + ')\n' +
  2400. '(' + this[2] + ', ' + this[6] + ', ' + this[10] + ', ' + this[14] + ')\n' +
  2401. '(' + this[3] + ', ' + this[7] + ', ' + this[11] + ', ' + this[15] + ')';
  2402. };
  2403. export default Matrix4;