Matrix4.js 95 KB

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