Matrix4.js 86 KB

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