Matrix4.js 98 KB

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