mat4.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Implements 4x4 matrices and their related functions which are
  16. * compatible with WebGL. The API is structured to avoid unnecessary memory
  17. * allocations. The last parameter will typically be the output vector and
  18. * an object can be both an input and output parameter to all methods except
  19. * where noted. Matrix operations follow the mathematical form when multiplying
  20. * vectors as follows: resultVec = matrix * vec.
  21. *
  22. * The matrices are stored in column-major order.
  23. *
  24. */
  25. goog.provide('goog.vec.Mat4');
  26. goog.require('goog.vec');
  27. goog.require('goog.vec.Vec3');
  28. goog.require('goog.vec.Vec4');
  29. /** @typedef {goog.vec.Float32} */ goog.vec.Mat4.Float32;
  30. /** @typedef {goog.vec.Float64} */ goog.vec.Mat4.Float64;
  31. /** @typedef {goog.vec.Number} */ goog.vec.Mat4.Number;
  32. /** @typedef {goog.vec.AnyType} */ goog.vec.Mat4.AnyType;
  33. // The following two types are deprecated - use the above types instead.
  34. /** @typedef {!Float32Array} */ goog.vec.Mat4.Type;
  35. /** @typedef {goog.vec.ArrayType} */ goog.vec.Mat4.Mat4Like;
  36. /**
  37. * Creates the array representation of a 4x4 matrix of Float32.
  38. * The use of the array directly instead of a class reduces overhead.
  39. * The returned matrix is cleared to all zeros.
  40. *
  41. * @return {!goog.vec.Mat4.Float32} The new matrix.
  42. */
  43. goog.vec.Mat4.createFloat32 = function() {
  44. return new Float32Array(16);
  45. };
  46. /**
  47. * Creates the array representation of a 4x4 matrix of Float64.
  48. * The returned matrix is cleared to all zeros.
  49. *
  50. * @return {!goog.vec.Mat4.Float64} The new matrix.
  51. */
  52. goog.vec.Mat4.createFloat64 = function() {
  53. return new Float64Array(16);
  54. };
  55. /**
  56. * Creates the array representation of a 4x4 matrix of Number.
  57. * The returned matrix is cleared to all zeros.
  58. *
  59. * @return {!goog.vec.Mat4.Number} The new matrix.
  60. */
  61. goog.vec.Mat4.createNumber = function() {
  62. var a = new Array(16);
  63. goog.vec.Mat4.setFromValues(
  64. a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  65. return a;
  66. };
  67. /**
  68. * Creates the array representation of a 4x4 matrix of Float32.
  69. * The returned matrix is cleared to all zeros.
  70. *
  71. * @deprecated Use createFloat32.
  72. * @return {!goog.vec.Mat4.Type} The new matrix.
  73. */
  74. goog.vec.Mat4.create = function() {
  75. return goog.vec.Mat4.createFloat32();
  76. };
  77. /**
  78. * Creates a 4x4 identity matrix of Float32.
  79. *
  80. * @return {!goog.vec.Mat4.Float32} The new 16 element array.
  81. */
  82. goog.vec.Mat4.createFloat32Identity = function() {
  83. var mat = goog.vec.Mat4.createFloat32();
  84. mat[0] = mat[5] = mat[10] = mat[15] = 1;
  85. return mat;
  86. };
  87. /**
  88. * Creates a 4x4 identity matrix of Float64.
  89. *
  90. * @return {!goog.vec.Mat4.Float64} The new 16 element array.
  91. */
  92. goog.vec.Mat4.createFloat64Identity = function() {
  93. var mat = goog.vec.Mat4.createFloat64();
  94. mat[0] = mat[5] = mat[10] = mat[15] = 1;
  95. return mat;
  96. };
  97. /**
  98. * Creates a 4x4 identity matrix of Number.
  99. * The returned matrix is cleared to all zeros.
  100. *
  101. * @return {!goog.vec.Mat4.Number} The new 16 element array.
  102. */
  103. goog.vec.Mat4.createNumberIdentity = function() {
  104. var a = new Array(16);
  105. goog.vec.Mat4.setFromValues(
  106. a, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
  107. return a;
  108. };
  109. /**
  110. * Creates the array representation of a 4x4 matrix of Float32.
  111. * The returned matrix is cleared to all zeros.
  112. *
  113. * @deprecated Use createFloat32Identity.
  114. * @return {!goog.vec.Mat4.Type} The new 16 element array.
  115. */
  116. goog.vec.Mat4.createIdentity = function() {
  117. return goog.vec.Mat4.createFloat32Identity();
  118. };
  119. /**
  120. * Creates a 4x4 matrix of Float32 initialized from the given array.
  121. *
  122. * @param {goog.vec.Mat4.AnyType} matrix The array containing the
  123. * matrix values in column major order.
  124. * @return {!goog.vec.Mat4.Float32} The new, 16 element array.
  125. */
  126. goog.vec.Mat4.createFloat32FromArray = function(matrix) {
  127. var newMatrix = goog.vec.Mat4.createFloat32();
  128. goog.vec.Mat4.setFromArray(newMatrix, matrix);
  129. return newMatrix;
  130. };
  131. /**
  132. * Creates a 4x4 matrix of Float32 initialized from the given values.
  133. *
  134. * @param {number} v00 The values at (0, 0).
  135. * @param {number} v10 The values at (1, 0).
  136. * @param {number} v20 The values at (2, 0).
  137. * @param {number} v30 The values at (3, 0).
  138. * @param {number} v01 The values at (0, 1).
  139. * @param {number} v11 The values at (1, 1).
  140. * @param {number} v21 The values at (2, 1).
  141. * @param {number} v31 The values at (3, 1).
  142. * @param {number} v02 The values at (0, 2).
  143. * @param {number} v12 The values at (1, 2).
  144. * @param {number} v22 The values at (2, 2).
  145. * @param {number} v32 The values at (3, 2).
  146. * @param {number} v03 The values at (0, 3).
  147. * @param {number} v13 The values at (1, 3).
  148. * @param {number} v23 The values at (2, 3).
  149. * @param {number} v33 The values at (3, 3).
  150. * @return {!goog.vec.Mat4.Float32} The new, 16 element array.
  151. */
  152. goog.vec.Mat4.createFloat32FromValues = function(
  153. v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
  154. v33) {
  155. var newMatrix = goog.vec.Mat4.createFloat32();
  156. goog.vec.Mat4.setFromValues(
  157. newMatrix, v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32,
  158. v03, v13, v23, v33);
  159. return newMatrix;
  160. };
  161. /**
  162. * Creates a clone of a 4x4 matrix of Float32.
  163. *
  164. * @param {goog.vec.Mat4.Float32} matrix The source 4x4 matrix.
  165. * @return {!goog.vec.Mat4.Float32} The new 4x4 element matrix.
  166. */
  167. goog.vec.Mat4.cloneFloat32 = goog.vec.Mat4.createFloat32FromArray;
  168. /**
  169. * Creates a 4x4 matrix of Float64 initialized from the given array.
  170. *
  171. * @param {goog.vec.Mat4.AnyType} matrix The array containing the
  172. * matrix values in column major order.
  173. * @return {!goog.vec.Mat4.Float64} The new, nine element array.
  174. */
  175. goog.vec.Mat4.createFloat64FromArray = function(matrix) {
  176. var newMatrix = goog.vec.Mat4.createFloat64();
  177. goog.vec.Mat4.setFromArray(newMatrix, matrix);
  178. return newMatrix;
  179. };
  180. /**
  181. * Creates a 4x4 matrix of Float64 initialized from the given values.
  182. *
  183. * @param {number} v00 The values at (0, 0).
  184. * @param {number} v10 The values at (1, 0).
  185. * @param {number} v20 The values at (2, 0).
  186. * @param {number} v30 The values at (3, 0).
  187. * @param {number} v01 The values at (0, 1).
  188. * @param {number} v11 The values at (1, 1).
  189. * @param {number} v21 The values at (2, 1).
  190. * @param {number} v31 The values at (3, 1).
  191. * @param {number} v02 The values at (0, 2).
  192. * @param {number} v12 The values at (1, 2).
  193. * @param {number} v22 The values at (2, 2).
  194. * @param {number} v32 The values at (3, 2).
  195. * @param {number} v03 The values at (0, 3).
  196. * @param {number} v13 The values at (1, 3).
  197. * @param {number} v23 The values at (2, 3).
  198. * @param {number} v33 The values at (3, 3).
  199. * @return {!goog.vec.Mat4.Float64} The new, 16 element array.
  200. */
  201. goog.vec.Mat4.createFloat64FromValues = function(
  202. v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
  203. v33) {
  204. var newMatrix = goog.vec.Mat4.createFloat64();
  205. goog.vec.Mat4.setFromValues(
  206. newMatrix, v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32,
  207. v03, v13, v23, v33);
  208. return newMatrix;
  209. };
  210. /**
  211. * Creates a clone of a 4x4 matrix of Float64.
  212. *
  213. * @param {goog.vec.Mat4.Float64} matrix The source 4x4 matrix.
  214. * @return {!goog.vec.Mat4.Float64} The new 4x4 element matrix.
  215. */
  216. goog.vec.Mat4.cloneFloat64 = goog.vec.Mat4.createFloat64FromArray;
  217. /**
  218. * Creates a 4x4 matrix of Float32 initialized from the given array.
  219. *
  220. * @deprecated Use createFloat32FromArray.
  221. * @param {goog.vec.Mat4.Mat4Like} matrix The array containing the
  222. * matrix values in column major order.
  223. * @return {!goog.vec.Mat4.Type} The new, nine element array.
  224. */
  225. goog.vec.Mat4.createFromArray = function(matrix) {
  226. var newMatrix = goog.vec.Mat4.createFloat32();
  227. goog.vec.Mat4.setFromArray(newMatrix, matrix);
  228. return newMatrix;
  229. };
  230. /**
  231. * Creates a 4x4 matrix of Float32 initialized from the given values.
  232. *
  233. * @deprecated Use createFloat32FromValues.
  234. * @param {number} v00 The values at (0, 0).
  235. * @param {number} v10 The values at (1, 0).
  236. * @param {number} v20 The values at (2, 0).
  237. * @param {number} v30 The values at (3, 0).
  238. * @param {number} v01 The values at (0, 1).
  239. * @param {number} v11 The values at (1, 1).
  240. * @param {number} v21 The values at (2, 1).
  241. * @param {number} v31 The values at (3, 1).
  242. * @param {number} v02 The values at (0, 2).
  243. * @param {number} v12 The values at (1, 2).
  244. * @param {number} v22 The values at (2, 2).
  245. * @param {number} v32 The values at (3, 2).
  246. * @param {number} v03 The values at (0, 3).
  247. * @param {number} v13 The values at (1, 3).
  248. * @param {number} v23 The values at (2, 3).
  249. * @param {number} v33 The values at (3, 3).
  250. * @return {!goog.vec.Mat4.Type} The new, 16 element array.
  251. */
  252. goog.vec.Mat4.createFromValues = function(
  253. v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
  254. v33) {
  255. return goog.vec.Mat4.createFloat32FromValues(
  256. v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13, v23,
  257. v33);
  258. };
  259. /**
  260. * Creates a clone of a 4x4 matrix of Float32.
  261. *
  262. * @deprecated Use cloneFloat32.
  263. * @param {goog.vec.Mat4.Mat4Like} matrix The source 4x4 matrix.
  264. * @return {!goog.vec.Mat4.Type} The new 4x4 element matrix.
  265. */
  266. goog.vec.Mat4.clone = goog.vec.Mat4.createFromArray;
  267. /**
  268. * Retrieves the element at the requested row and column.
  269. *
  270. * @param {goog.vec.Mat4.AnyType} mat The matrix containing the
  271. * value to retrieve.
  272. * @param {number} row The row index.
  273. * @param {number} column The column index.
  274. * @return {number} The element value at the requested row, column indices.
  275. */
  276. goog.vec.Mat4.getElement = function(mat, row, column) {
  277. return mat[row + column * 4];
  278. };
  279. /**
  280. * Sets the element at the requested row and column.
  281. *
  282. * @param {goog.vec.Mat4.AnyType} mat The matrix to set the value on.
  283. * @param {number} row The row index.
  284. * @param {number} column The column index.
  285. * @param {number} value The value to set at the requested row, column.
  286. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  287. * chained together.
  288. */
  289. goog.vec.Mat4.setElement = function(mat, row, column, value) {
  290. mat[row + column * 4] = value;
  291. return mat;
  292. };
  293. /**
  294. * Initializes the matrix from the set of values. Note the values supplied are
  295. * in column major order.
  296. *
  297. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the
  298. * values.
  299. * @param {number} v00 The values at (0, 0).
  300. * @param {number} v10 The values at (1, 0).
  301. * @param {number} v20 The values at (2, 0).
  302. * @param {number} v30 The values at (3, 0).
  303. * @param {number} v01 The values at (0, 1).
  304. * @param {number} v11 The values at (1, 1).
  305. * @param {number} v21 The values at (2, 1).
  306. * @param {number} v31 The values at (3, 1).
  307. * @param {number} v02 The values at (0, 2).
  308. * @param {number} v12 The values at (1, 2).
  309. * @param {number} v22 The values at (2, 2).
  310. * @param {number} v32 The values at (3, 2).
  311. * @param {number} v03 The values at (0, 3).
  312. * @param {number} v13 The values at (1, 3).
  313. * @param {number} v23 The values at (2, 3).
  314. * @param {number} v33 The values at (3, 3).
  315. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  316. * chained together.
  317. */
  318. goog.vec.Mat4.setFromValues = function(
  319. mat, v00, v10, v20, v30, v01, v11, v21, v31, v02, v12, v22, v32, v03, v13,
  320. v23, v33) {
  321. mat[0] = v00;
  322. mat[1] = v10;
  323. mat[2] = v20;
  324. mat[3] = v30;
  325. mat[4] = v01;
  326. mat[5] = v11;
  327. mat[6] = v21;
  328. mat[7] = v31;
  329. mat[8] = v02;
  330. mat[9] = v12;
  331. mat[10] = v22;
  332. mat[11] = v32;
  333. mat[12] = v03;
  334. mat[13] = v13;
  335. mat[14] = v23;
  336. mat[15] = v33;
  337. return mat;
  338. };
  339. /**
  340. * Sets the matrix from the array of values stored in column major order.
  341. *
  342. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  343. * @param {goog.vec.Mat4.AnyType} values The column major ordered
  344. * array of values to store in the matrix.
  345. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  346. * chained together.
  347. */
  348. goog.vec.Mat4.setFromArray = function(mat, values) {
  349. mat[0] = values[0];
  350. mat[1] = values[1];
  351. mat[2] = values[2];
  352. mat[3] = values[3];
  353. mat[4] = values[4];
  354. mat[5] = values[5];
  355. mat[6] = values[6];
  356. mat[7] = values[7];
  357. mat[8] = values[8];
  358. mat[9] = values[9];
  359. mat[10] = values[10];
  360. mat[11] = values[11];
  361. mat[12] = values[12];
  362. mat[13] = values[13];
  363. mat[14] = values[14];
  364. mat[15] = values[15];
  365. return mat;
  366. };
  367. /**
  368. * Sets the matrix from the array of values stored in row major order.
  369. *
  370. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  371. * @param {goog.vec.Mat4.AnyType} values The row major ordered array of
  372. * values to store in the matrix.
  373. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  374. * chained together.
  375. */
  376. goog.vec.Mat4.setFromRowMajorArray = function(mat, values) {
  377. mat[0] = values[0];
  378. mat[1] = values[4];
  379. mat[2] = values[8];
  380. mat[3] = values[12];
  381. mat[4] = values[1];
  382. mat[5] = values[5];
  383. mat[6] = values[9];
  384. mat[7] = values[13];
  385. mat[8] = values[2];
  386. mat[9] = values[6];
  387. mat[10] = values[10];
  388. mat[11] = values[14];
  389. mat[12] = values[3];
  390. mat[13] = values[7];
  391. mat[14] = values[11];
  392. mat[15] = values[15];
  393. return mat;
  394. };
  395. /**
  396. * Sets the diagonal values of the matrix from the given values.
  397. *
  398. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  399. * @param {number} v00 The values for (0, 0).
  400. * @param {number} v11 The values for (1, 1).
  401. * @param {number} v22 The values for (2, 2).
  402. * @param {number} v33 The values for (3, 3).
  403. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  404. * chained together.
  405. */
  406. goog.vec.Mat4.setDiagonalValues = function(mat, v00, v11, v22, v33) {
  407. mat[0] = v00;
  408. mat[5] = v11;
  409. mat[10] = v22;
  410. mat[15] = v33;
  411. return mat;
  412. };
  413. /**
  414. * Sets the diagonal values of the matrix from the given vector.
  415. *
  416. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  417. * @param {goog.vec.Vec4.AnyType} vec The vector containing the values.
  418. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  419. * chained together.
  420. */
  421. goog.vec.Mat4.setDiagonal = function(mat, vec) {
  422. mat[0] = vec[0];
  423. mat[5] = vec[1];
  424. mat[10] = vec[2];
  425. mat[15] = vec[3];
  426. return mat;
  427. };
  428. /**
  429. * Gets the diagonal values of the matrix into the given vector.
  430. *
  431. * @param {goog.vec.Mat4.AnyType} mat The matrix containing the values.
  432. * @param {goog.vec.Vec4.AnyType} vec The vector to receive the values.
  433. * @param {number=} opt_diagonal Which diagonal to get. A value of 0 selects the
  434. * main diagonal, a positive number selects a super diagonal and a negative
  435. * number selects a sub diagonal.
  436. * @return {goog.vec.Vec4.AnyType} return vec so that operations can be
  437. * chained together.
  438. */
  439. goog.vec.Mat4.getDiagonal = function(mat, vec, opt_diagonal) {
  440. if (!opt_diagonal) {
  441. // This is the most common case, so we avoid the for loop.
  442. vec[0] = mat[0];
  443. vec[1] = mat[5];
  444. vec[2] = mat[10];
  445. vec[3] = mat[15];
  446. } else {
  447. var offset = opt_diagonal > 0 ? 4 * opt_diagonal : -opt_diagonal;
  448. for (var i = 0; i < 4 - Math.abs(opt_diagonal); i++) {
  449. vec[i] = mat[offset + 5 * i];
  450. }
  451. }
  452. return vec;
  453. };
  454. /**
  455. * Sets the specified column with the supplied values.
  456. *
  457. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  458. * @param {number} column The column index to set the values on.
  459. * @param {number} v0 The value for row 0.
  460. * @param {number} v1 The value for row 1.
  461. * @param {number} v2 The value for row 2.
  462. * @param {number} v3 The value for row 3.
  463. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  464. * chained together.
  465. */
  466. goog.vec.Mat4.setColumnValues = function(mat, column, v0, v1, v2, v3) {
  467. var i = column * 4;
  468. mat[i] = v0;
  469. mat[i + 1] = v1;
  470. mat[i + 2] = v2;
  471. mat[i + 3] = v3;
  472. return mat;
  473. };
  474. /**
  475. * Sets the specified column with the value from the supplied vector.
  476. *
  477. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  478. * @param {number} column The column index to set the values on.
  479. * @param {goog.vec.Vec4.AnyType} vec The vector of elements for the column.
  480. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  481. * chained together.
  482. */
  483. goog.vec.Mat4.setColumn = function(mat, column, vec) {
  484. var i = column * 4;
  485. mat[i] = vec[0];
  486. mat[i + 1] = vec[1];
  487. mat[i + 2] = vec[2];
  488. mat[i + 3] = vec[3];
  489. return mat;
  490. };
  491. /**
  492. * Retrieves the specified column from the matrix into the given vector.
  493. *
  494. * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the values.
  495. * @param {number} column The column to get the values from.
  496. * @param {goog.vec.Vec4.AnyType} vec The vector of elements to
  497. * receive the column.
  498. * @return {goog.vec.Vec4.AnyType} return vec so that operations can be
  499. * chained together.
  500. */
  501. goog.vec.Mat4.getColumn = function(mat, column, vec) {
  502. var i = column * 4;
  503. vec[0] = mat[i];
  504. vec[1] = mat[i + 1];
  505. vec[2] = mat[i + 2];
  506. vec[3] = mat[i + 3];
  507. return vec;
  508. };
  509. /**
  510. * Sets the columns of the matrix from the given vectors.
  511. *
  512. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  513. * @param {goog.vec.Vec4.AnyType} vec0 The values for column 0.
  514. * @param {goog.vec.Vec4.AnyType} vec1 The values for column 1.
  515. * @param {goog.vec.Vec4.AnyType} vec2 The values for column 2.
  516. * @param {goog.vec.Vec4.AnyType} vec3 The values for column 3.
  517. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  518. * chained together.
  519. */
  520. goog.vec.Mat4.setColumns = function(mat, vec0, vec1, vec2, vec3) {
  521. goog.vec.Mat4.setColumn(mat, 0, vec0);
  522. goog.vec.Mat4.setColumn(mat, 1, vec1);
  523. goog.vec.Mat4.setColumn(mat, 2, vec2);
  524. goog.vec.Mat4.setColumn(mat, 3, vec3);
  525. return mat;
  526. };
  527. /**
  528. * Retrieves the column values from the given matrix into the given vectors.
  529. *
  530. * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the columns.
  531. * @param {goog.vec.Vec4.AnyType} vec0 The vector to receive column 0.
  532. * @param {goog.vec.Vec4.AnyType} vec1 The vector to receive column 1.
  533. * @param {goog.vec.Vec4.AnyType} vec2 The vector to receive column 2.
  534. * @param {goog.vec.Vec4.AnyType} vec3 The vector to receive column 3.
  535. */
  536. goog.vec.Mat4.getColumns = function(mat, vec0, vec1, vec2, vec3) {
  537. goog.vec.Mat4.getColumn(mat, 0, vec0);
  538. goog.vec.Mat4.getColumn(mat, 1, vec1);
  539. goog.vec.Mat4.getColumn(mat, 2, vec2);
  540. goog.vec.Mat4.getColumn(mat, 3, vec3);
  541. };
  542. /**
  543. * Sets the row values from the supplied values.
  544. *
  545. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  546. * @param {number} row The index of the row to receive the values.
  547. * @param {number} v0 The value for column 0.
  548. * @param {number} v1 The value for column 1.
  549. * @param {number} v2 The value for column 2.
  550. * @param {number} v3 The value for column 3.
  551. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  552. * chained together.
  553. */
  554. goog.vec.Mat4.setRowValues = function(mat, row, v0, v1, v2, v3) {
  555. mat[row] = v0;
  556. mat[row + 4] = v1;
  557. mat[row + 8] = v2;
  558. mat[row + 12] = v3;
  559. return mat;
  560. };
  561. /**
  562. * Sets the row values from the supplied vector.
  563. *
  564. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the row values.
  565. * @param {number} row The index of the row.
  566. * @param {goog.vec.Vec4.AnyType} vec The vector containing the values.
  567. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  568. * chained together.
  569. */
  570. goog.vec.Mat4.setRow = function(mat, row, vec) {
  571. mat[row] = vec[0];
  572. mat[row + 4] = vec[1];
  573. mat[row + 8] = vec[2];
  574. mat[row + 12] = vec[3];
  575. return mat;
  576. };
  577. /**
  578. * Retrieves the row values into the given vector.
  579. *
  580. * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the values.
  581. * @param {number} row The index of the row supplying the values.
  582. * @param {goog.vec.Vec4.AnyType} vec The vector to receive the row.
  583. * @return {goog.vec.Vec4.AnyType} return vec so that operations can be
  584. * chained together.
  585. */
  586. goog.vec.Mat4.getRow = function(mat, row, vec) {
  587. vec[0] = mat[row];
  588. vec[1] = mat[row + 4];
  589. vec[2] = mat[row + 8];
  590. vec[3] = mat[row + 12];
  591. return vec;
  592. };
  593. /**
  594. * Sets the rows of the matrix from the supplied vectors.
  595. *
  596. * @param {goog.vec.Mat4.AnyType} mat The matrix to receive the values.
  597. * @param {goog.vec.Vec4.AnyType} vec0 The values for row 0.
  598. * @param {goog.vec.Vec4.AnyType} vec1 The values for row 1.
  599. * @param {goog.vec.Vec4.AnyType} vec2 The values for row 2.
  600. * @param {goog.vec.Vec4.AnyType} vec3 The values for row 3.
  601. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  602. * chained together.
  603. */
  604. goog.vec.Mat4.setRows = function(mat, vec0, vec1, vec2, vec3) {
  605. goog.vec.Mat4.setRow(mat, 0, vec0);
  606. goog.vec.Mat4.setRow(mat, 1, vec1);
  607. goog.vec.Mat4.setRow(mat, 2, vec2);
  608. goog.vec.Mat4.setRow(mat, 3, vec3);
  609. return mat;
  610. };
  611. /**
  612. * Retrieves the rows of the matrix into the supplied vectors.
  613. *
  614. * @param {goog.vec.Mat4.AnyType} mat The matrix to supply the values.
  615. * @param {goog.vec.Vec4.AnyType} vec0 The vector to receive row 0.
  616. * @param {goog.vec.Vec4.AnyType} vec1 The vector to receive row 1.
  617. * @param {goog.vec.Vec4.AnyType} vec2 The vector to receive row 2.
  618. * @param {goog.vec.Vec4.AnyType} vec3 The vector to receive row 3.
  619. */
  620. goog.vec.Mat4.getRows = function(mat, vec0, vec1, vec2, vec3) {
  621. goog.vec.Mat4.getRow(mat, 0, vec0);
  622. goog.vec.Mat4.getRow(mat, 1, vec1);
  623. goog.vec.Mat4.getRow(mat, 2, vec2);
  624. goog.vec.Mat4.getRow(mat, 3, vec3);
  625. };
  626. /**
  627. * Makes the given 4x4 matrix the zero matrix.
  628. *
  629. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  630. * @return {!goog.vec.Mat4.AnyType} return mat so operations can be chained.
  631. */
  632. goog.vec.Mat4.makeZero = function(mat) {
  633. mat[0] = 0;
  634. mat[1] = 0;
  635. mat[2] = 0;
  636. mat[3] = 0;
  637. mat[4] = 0;
  638. mat[5] = 0;
  639. mat[6] = 0;
  640. mat[7] = 0;
  641. mat[8] = 0;
  642. mat[9] = 0;
  643. mat[10] = 0;
  644. mat[11] = 0;
  645. mat[12] = 0;
  646. mat[13] = 0;
  647. mat[14] = 0;
  648. mat[15] = 0;
  649. return mat;
  650. };
  651. /**
  652. * Makes the given 4x4 matrix the identity matrix.
  653. *
  654. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  655. * @return {goog.vec.Mat4.AnyType} return mat so operations can be chained.
  656. */
  657. goog.vec.Mat4.makeIdentity = function(mat) {
  658. mat[0] = 1;
  659. mat[1] = 0;
  660. mat[2] = 0;
  661. mat[3] = 0;
  662. mat[4] = 0;
  663. mat[5] = 1;
  664. mat[6] = 0;
  665. mat[7] = 0;
  666. mat[8] = 0;
  667. mat[9] = 0;
  668. mat[10] = 1;
  669. mat[11] = 0;
  670. mat[12] = 0;
  671. mat[13] = 0;
  672. mat[14] = 0;
  673. mat[15] = 1;
  674. return mat;
  675. };
  676. /**
  677. * Performs a per-component addition of the matrix mat0 and mat1, storing
  678. * the result into resultMat.
  679. *
  680. * @param {goog.vec.Mat4.AnyType} mat0 The first addend.
  681. * @param {goog.vec.Mat4.AnyType} mat1 The second addend.
  682. * @param {goog.vec.Mat4.AnyType} resultMat The matrix to
  683. * receive the results (may be either mat0 or mat1).
  684. * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
  685. * chained together.
  686. */
  687. goog.vec.Mat4.addMat = function(mat0, mat1, resultMat) {
  688. resultMat[0] = mat0[0] + mat1[0];
  689. resultMat[1] = mat0[1] + mat1[1];
  690. resultMat[2] = mat0[2] + mat1[2];
  691. resultMat[3] = mat0[3] + mat1[3];
  692. resultMat[4] = mat0[4] + mat1[4];
  693. resultMat[5] = mat0[5] + mat1[5];
  694. resultMat[6] = mat0[6] + mat1[6];
  695. resultMat[7] = mat0[7] + mat1[7];
  696. resultMat[8] = mat0[8] + mat1[8];
  697. resultMat[9] = mat0[9] + mat1[9];
  698. resultMat[10] = mat0[10] + mat1[10];
  699. resultMat[11] = mat0[11] + mat1[11];
  700. resultMat[12] = mat0[12] + mat1[12];
  701. resultMat[13] = mat0[13] + mat1[13];
  702. resultMat[14] = mat0[14] + mat1[14];
  703. resultMat[15] = mat0[15] + mat1[15];
  704. return resultMat;
  705. };
  706. /**
  707. * Performs a per-component subtraction of the matrix mat0 and mat1,
  708. * storing the result into resultMat.
  709. *
  710. * @param {goog.vec.Mat4.AnyType} mat0 The minuend.
  711. * @param {goog.vec.Mat4.AnyType} mat1 The subtrahend.
  712. * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
  713. * the results (may be either mat0 or mat1).
  714. * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
  715. * chained together.
  716. */
  717. goog.vec.Mat4.subMat = function(mat0, mat1, resultMat) {
  718. resultMat[0] = mat0[0] - mat1[0];
  719. resultMat[1] = mat0[1] - mat1[1];
  720. resultMat[2] = mat0[2] - mat1[2];
  721. resultMat[3] = mat0[3] - mat1[3];
  722. resultMat[4] = mat0[4] - mat1[4];
  723. resultMat[5] = mat0[5] - mat1[5];
  724. resultMat[6] = mat0[6] - mat1[6];
  725. resultMat[7] = mat0[7] - mat1[7];
  726. resultMat[8] = mat0[8] - mat1[8];
  727. resultMat[9] = mat0[9] - mat1[9];
  728. resultMat[10] = mat0[10] - mat1[10];
  729. resultMat[11] = mat0[11] - mat1[11];
  730. resultMat[12] = mat0[12] - mat1[12];
  731. resultMat[13] = mat0[13] - mat1[13];
  732. resultMat[14] = mat0[14] - mat1[14];
  733. resultMat[15] = mat0[15] - mat1[15];
  734. return resultMat;
  735. };
  736. /**
  737. * Multiplies matrix mat with the given scalar, storing the result
  738. * into resultMat.
  739. *
  740. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  741. * @param {number} scalar The scalar value to multiply to each element of mat.
  742. * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
  743. * the results (may be mat).
  744. * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
  745. * chained together.
  746. */
  747. goog.vec.Mat4.multScalar = function(mat, scalar, resultMat) {
  748. resultMat[0] = mat[0] * scalar;
  749. resultMat[1] = mat[1] * scalar;
  750. resultMat[2] = mat[2] * scalar;
  751. resultMat[3] = mat[3] * scalar;
  752. resultMat[4] = mat[4] * scalar;
  753. resultMat[5] = mat[5] * scalar;
  754. resultMat[6] = mat[6] * scalar;
  755. resultMat[7] = mat[7] * scalar;
  756. resultMat[8] = mat[8] * scalar;
  757. resultMat[9] = mat[9] * scalar;
  758. resultMat[10] = mat[10] * scalar;
  759. resultMat[11] = mat[11] * scalar;
  760. resultMat[12] = mat[12] * scalar;
  761. resultMat[13] = mat[13] * scalar;
  762. resultMat[14] = mat[14] * scalar;
  763. resultMat[15] = mat[15] * scalar;
  764. return resultMat;
  765. };
  766. /**
  767. * Multiplies the two matrices mat0 and mat1 using matrix multiplication,
  768. * storing the result into resultMat.
  769. *
  770. * @param {goog.vec.Mat4.AnyType} mat0 The first (left hand) matrix.
  771. * @param {goog.vec.Mat4.AnyType} mat1 The second (right hand) matrix.
  772. * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
  773. * the results (may be either mat0 or mat1).
  774. * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
  775. * chained together.
  776. */
  777. goog.vec.Mat4.multMat = function(mat0, mat1, resultMat) {
  778. var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2], a30 = mat0[3];
  779. var a01 = mat0[4], a11 = mat0[5], a21 = mat0[6], a31 = mat0[7];
  780. var a02 = mat0[8], a12 = mat0[9], a22 = mat0[10], a32 = mat0[11];
  781. var a03 = mat0[12], a13 = mat0[13], a23 = mat0[14], a33 = mat0[15];
  782. var b00 = mat1[0], b10 = mat1[1], b20 = mat1[2], b30 = mat1[3];
  783. var b01 = mat1[4], b11 = mat1[5], b21 = mat1[6], b31 = mat1[7];
  784. var b02 = mat1[8], b12 = mat1[9], b22 = mat1[10], b32 = mat1[11];
  785. var b03 = mat1[12], b13 = mat1[13], b23 = mat1[14], b33 = mat1[15];
  786. resultMat[0] = a00 * b00 + a01 * b10 + a02 * b20 + a03 * b30;
  787. resultMat[1] = a10 * b00 + a11 * b10 + a12 * b20 + a13 * b30;
  788. resultMat[2] = a20 * b00 + a21 * b10 + a22 * b20 + a23 * b30;
  789. resultMat[3] = a30 * b00 + a31 * b10 + a32 * b20 + a33 * b30;
  790. resultMat[4] = a00 * b01 + a01 * b11 + a02 * b21 + a03 * b31;
  791. resultMat[5] = a10 * b01 + a11 * b11 + a12 * b21 + a13 * b31;
  792. resultMat[6] = a20 * b01 + a21 * b11 + a22 * b21 + a23 * b31;
  793. resultMat[7] = a30 * b01 + a31 * b11 + a32 * b21 + a33 * b31;
  794. resultMat[8] = a00 * b02 + a01 * b12 + a02 * b22 + a03 * b32;
  795. resultMat[9] = a10 * b02 + a11 * b12 + a12 * b22 + a13 * b32;
  796. resultMat[10] = a20 * b02 + a21 * b12 + a22 * b22 + a23 * b32;
  797. resultMat[11] = a30 * b02 + a31 * b12 + a32 * b22 + a33 * b32;
  798. resultMat[12] = a00 * b03 + a01 * b13 + a02 * b23 + a03 * b33;
  799. resultMat[13] = a10 * b03 + a11 * b13 + a12 * b23 + a13 * b33;
  800. resultMat[14] = a20 * b03 + a21 * b13 + a22 * b23 + a23 * b33;
  801. resultMat[15] = a30 * b03 + a31 * b13 + a32 * b23 + a33 * b33;
  802. return resultMat;
  803. };
  804. /**
  805. * Transposes the given matrix mat storing the result into resultMat.
  806. *
  807. * @param {goog.vec.Mat4.AnyType} mat The matrix to transpose.
  808. * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
  809. * the results (may be mat).
  810. * @return {goog.vec.Mat4.AnyType} return resultMat so that operations can be
  811. * chained together.
  812. */
  813. goog.vec.Mat4.transpose = function(mat, resultMat) {
  814. if (resultMat == mat) {
  815. var a10 = mat[1], a20 = mat[2], a30 = mat[3];
  816. var a21 = mat[6], a31 = mat[7];
  817. var a32 = mat[11];
  818. resultMat[1] = mat[4];
  819. resultMat[2] = mat[8];
  820. resultMat[3] = mat[12];
  821. resultMat[4] = a10;
  822. resultMat[6] = mat[9];
  823. resultMat[7] = mat[13];
  824. resultMat[8] = a20;
  825. resultMat[9] = a21;
  826. resultMat[11] = mat[14];
  827. resultMat[12] = a30;
  828. resultMat[13] = a31;
  829. resultMat[14] = a32;
  830. } else {
  831. resultMat[0] = mat[0];
  832. resultMat[1] = mat[4];
  833. resultMat[2] = mat[8];
  834. resultMat[3] = mat[12];
  835. resultMat[4] = mat[1];
  836. resultMat[5] = mat[5];
  837. resultMat[6] = mat[9];
  838. resultMat[7] = mat[13];
  839. resultMat[8] = mat[2];
  840. resultMat[9] = mat[6];
  841. resultMat[10] = mat[10];
  842. resultMat[11] = mat[14];
  843. resultMat[12] = mat[3];
  844. resultMat[13] = mat[7];
  845. resultMat[14] = mat[11];
  846. resultMat[15] = mat[15];
  847. }
  848. return resultMat;
  849. };
  850. /**
  851. * Computes the determinant of the matrix.
  852. *
  853. * @param {goog.vec.Mat4.AnyType} mat The matrix to compute the matrix for.
  854. * @return {number} The determinant of the matrix.
  855. */
  856. goog.vec.Mat4.determinant = function(mat) {
  857. var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
  858. var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
  859. var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
  860. var m03 = mat[12], m13 = mat[13], m23 = mat[14], m33 = mat[15];
  861. var a0 = m00 * m11 - m10 * m01;
  862. var a1 = m00 * m21 - m20 * m01;
  863. var a2 = m00 * m31 - m30 * m01;
  864. var a3 = m10 * m21 - m20 * m11;
  865. var a4 = m10 * m31 - m30 * m11;
  866. var a5 = m20 * m31 - m30 * m21;
  867. var b0 = m02 * m13 - m12 * m03;
  868. var b1 = m02 * m23 - m22 * m03;
  869. var b2 = m02 * m33 - m32 * m03;
  870. var b3 = m12 * m23 - m22 * m13;
  871. var b4 = m12 * m33 - m32 * m13;
  872. var b5 = m22 * m33 - m32 * m23;
  873. return a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
  874. };
  875. /**
  876. * Computes the inverse of mat storing the result into resultMat. If the
  877. * inverse is defined, this function returns true, false otherwise.
  878. *
  879. * @param {goog.vec.Mat4.AnyType} mat The matrix to invert.
  880. * @param {goog.vec.Mat4.AnyType} resultMat The matrix to receive
  881. * the result (may be mat).
  882. * @return {boolean} True if the inverse is defined. If false is returned,
  883. * resultMat is not modified.
  884. */
  885. goog.vec.Mat4.invert = function(mat, resultMat) {
  886. var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
  887. var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
  888. var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
  889. var m03 = mat[12], m13 = mat[13], m23 = mat[14], m33 = mat[15];
  890. var a0 = m00 * m11 - m10 * m01;
  891. var a1 = m00 * m21 - m20 * m01;
  892. var a2 = m00 * m31 - m30 * m01;
  893. var a3 = m10 * m21 - m20 * m11;
  894. var a4 = m10 * m31 - m30 * m11;
  895. var a5 = m20 * m31 - m30 * m21;
  896. var b0 = m02 * m13 - m12 * m03;
  897. var b1 = m02 * m23 - m22 * m03;
  898. var b2 = m02 * m33 - m32 * m03;
  899. var b3 = m12 * m23 - m22 * m13;
  900. var b4 = m12 * m33 - m32 * m13;
  901. var b5 = m22 * m33 - m32 * m23;
  902. var det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
  903. if (det == 0) {
  904. return false;
  905. }
  906. var idet = 1.0 / det;
  907. resultMat[0] = (m11 * b5 - m21 * b4 + m31 * b3) * idet;
  908. resultMat[1] = (-m10 * b5 + m20 * b4 - m30 * b3) * idet;
  909. resultMat[2] = (m13 * a5 - m23 * a4 + m33 * a3) * idet;
  910. resultMat[3] = (-m12 * a5 + m22 * a4 - m32 * a3) * idet;
  911. resultMat[4] = (-m01 * b5 + m21 * b2 - m31 * b1) * idet;
  912. resultMat[5] = (m00 * b5 - m20 * b2 + m30 * b1) * idet;
  913. resultMat[6] = (-m03 * a5 + m23 * a2 - m33 * a1) * idet;
  914. resultMat[7] = (m02 * a5 - m22 * a2 + m32 * a1) * idet;
  915. resultMat[8] = (m01 * b4 - m11 * b2 + m31 * b0) * idet;
  916. resultMat[9] = (-m00 * b4 + m10 * b2 - m30 * b0) * idet;
  917. resultMat[10] = (m03 * a4 - m13 * a2 + m33 * a0) * idet;
  918. resultMat[11] = (-m02 * a4 + m12 * a2 - m32 * a0) * idet;
  919. resultMat[12] = (-m01 * b3 + m11 * b1 - m21 * b0) * idet;
  920. resultMat[13] = (m00 * b3 - m10 * b1 + m20 * b0) * idet;
  921. resultMat[14] = (-m03 * a3 + m13 * a1 - m23 * a0) * idet;
  922. resultMat[15] = (m02 * a3 - m12 * a1 + m22 * a0) * idet;
  923. return true;
  924. };
  925. /**
  926. * Returns true if the components of mat0 are equal to the components of mat1.
  927. *
  928. * @param {goog.vec.Mat4.AnyType} mat0 The first matrix.
  929. * @param {goog.vec.Mat4.AnyType} mat1 The second matrix.
  930. * @return {boolean} True if the the two matrices are equivalent.
  931. */
  932. goog.vec.Mat4.equals = function(mat0, mat1) {
  933. return mat0.length == mat1.length && mat0[0] == mat1[0] &&
  934. mat0[1] == mat1[1] && mat0[2] == mat1[2] && mat0[3] == mat1[3] &&
  935. mat0[4] == mat1[4] && mat0[5] == mat1[5] && mat0[6] == mat1[6] &&
  936. mat0[7] == mat1[7] && mat0[8] == mat1[8] && mat0[9] == mat1[9] &&
  937. mat0[10] == mat1[10] && mat0[11] == mat1[11] && mat0[12] == mat1[12] &&
  938. mat0[13] == mat1[13] && mat0[14] == mat1[14] && mat0[15] == mat1[15];
  939. };
  940. /**
  941. * Transforms the given vector with the given matrix storing the resulting,
  942. * transformed vector into resultVec. The input vector is multiplied against the
  943. * upper 3x4 matrix omitting the projective component.
  944. *
  945. * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
  946. * @param {goog.vec.Vec3.AnyType} vec The 3 element vector to transform.
  947. * @param {goog.vec.Vec3.AnyType} resultVec The 3 element vector to
  948. * receive the results (may be vec).
  949. * @return {goog.vec.Vec3.AnyType} return resultVec so that operations can be
  950. * chained together.
  951. */
  952. goog.vec.Mat4.multVec3 = function(mat, vec, resultVec) {
  953. var x = vec[0], y = vec[1], z = vec[2];
  954. resultVec[0] = x * mat[0] + y * mat[4] + z * mat[8] + mat[12];
  955. resultVec[1] = x * mat[1] + y * mat[5] + z * mat[9] + mat[13];
  956. resultVec[2] = x * mat[2] + y * mat[6] + z * mat[10] + mat[14];
  957. return resultVec;
  958. };
  959. /**
  960. * Transforms the given vector with the given matrix storing the resulting,
  961. * transformed vector into resultVec. The input vector is multiplied against the
  962. * upper 3x3 matrix omitting the projective component and translation
  963. * components.
  964. *
  965. * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
  966. * @param {goog.vec.Vec3.AnyType} vec The 3 element vector to transform.
  967. * @param {goog.vec.Vec3.AnyType} resultVec The 3 element vector to
  968. * receive the results (may be vec).
  969. * @return {goog.vec.Vec3.AnyType} return resultVec so that operations can be
  970. * chained together.
  971. */
  972. goog.vec.Mat4.multVec3NoTranslate = function(mat, vec, resultVec) {
  973. var x = vec[0], y = vec[1], z = vec[2];
  974. resultVec[0] = x * mat[0] + y * mat[4] + z * mat[8];
  975. resultVec[1] = x * mat[1] + y * mat[5] + z * mat[9];
  976. resultVec[2] = x * mat[2] + y * mat[6] + z * mat[10];
  977. return resultVec;
  978. };
  979. /**
  980. * Transforms the given vector with the given matrix storing the resulting,
  981. * transformed vector into resultVec. The input vector is multiplied against the
  982. * full 4x4 matrix with the homogeneous divide applied to reduce the 4 element
  983. * vector to a 3 element vector.
  984. *
  985. * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
  986. * @param {goog.vec.Vec3.AnyType} vec The 3 element vector to transform.
  987. * @param {goog.vec.Vec3.AnyType} resultVec The 3 element vector
  988. * to receive the results (may be vec).
  989. * @return {goog.vec.Vec3.AnyType} return resultVec so that operations can be
  990. * chained together.
  991. */
  992. goog.vec.Mat4.multVec3Projective = function(mat, vec, resultVec) {
  993. var x = vec[0], y = vec[1], z = vec[2];
  994. var invw = 1 / (x * mat[3] + y * mat[7] + z * mat[11] + mat[15]);
  995. resultVec[0] = (x * mat[0] + y * mat[4] + z * mat[8] + mat[12]) * invw;
  996. resultVec[1] = (x * mat[1] + y * mat[5] + z * mat[9] + mat[13]) * invw;
  997. resultVec[2] = (x * mat[2] + y * mat[6] + z * mat[10] + mat[14]) * invw;
  998. return resultVec;
  999. };
  1000. /**
  1001. * Transforms the given vector with the given matrix storing the resulting,
  1002. * transformed vector into resultVec.
  1003. *
  1004. * @param {goog.vec.Mat4.AnyType} mat The matrix supplying the transformation.
  1005. * @param {goog.vec.Vec4.AnyType} vec The vector to transform.
  1006. * @param {goog.vec.Vec4.AnyType} resultVec The vector to
  1007. * receive the results (may be vec).
  1008. * @return {goog.vec.Vec4.AnyType} return resultVec so that operations can be
  1009. * chained together.
  1010. */
  1011. goog.vec.Mat4.multVec4 = function(mat, vec, resultVec) {
  1012. var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
  1013. resultVec[0] = x * mat[0] + y * mat[4] + z * mat[8] + w * mat[12];
  1014. resultVec[1] = x * mat[1] + y * mat[5] + z * mat[9] + w * mat[13];
  1015. resultVec[2] = x * mat[2] + y * mat[6] + z * mat[10] + w * mat[14];
  1016. resultVec[3] = x * mat[3] + y * mat[7] + z * mat[11] + w * mat[15];
  1017. return resultVec;
  1018. };
  1019. /**
  1020. * Makes the given 4x4 matrix a translation matrix with x, y and z
  1021. * translation factors.
  1022. *
  1023. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1024. * @param {number} x The translation along the x axis.
  1025. * @param {number} y The translation along the y axis.
  1026. * @param {number} z The translation along the z axis.
  1027. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1028. * chained.
  1029. */
  1030. goog.vec.Mat4.makeTranslate = function(mat, x, y, z) {
  1031. goog.vec.Mat4.makeIdentity(mat);
  1032. return goog.vec.Mat4.setColumnValues(mat, 3, x, y, z, 1);
  1033. };
  1034. /**
  1035. * Makes the given 4x4 matrix as a scale matrix with x, y and z scale factors.
  1036. *
  1037. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1038. * @param {number} x The scale along the x axis.
  1039. * @param {number} y The scale along the y axis.
  1040. * @param {number} z The scale along the z axis.
  1041. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1042. * chained.
  1043. */
  1044. goog.vec.Mat4.makeScale = function(mat, x, y, z) {
  1045. goog.vec.Mat4.makeIdentity(mat);
  1046. return goog.vec.Mat4.setDiagonalValues(mat, x, y, z, 1);
  1047. };
  1048. /**
  1049. * Makes the given 4x4 matrix a rotation matrix with the given rotation
  1050. * angle about the axis defined by the vector (ax, ay, az).
  1051. *
  1052. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1053. * @param {number} angle The rotation angle in radians.
  1054. * @param {number} ax The x component of the rotation axis.
  1055. * @param {number} ay The y component of the rotation axis.
  1056. * @param {number} az The z component of the rotation axis.
  1057. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1058. * chained.
  1059. */
  1060. goog.vec.Mat4.makeRotate = function(mat, angle, ax, ay, az) {
  1061. var c = Math.cos(angle);
  1062. var d = 1 - c;
  1063. var s = Math.sin(angle);
  1064. return goog.vec.Mat4.setFromValues(
  1065. mat, ax * ax * d + c, ax * ay * d + az * s, ax * az * d - ay * s, 0,
  1066. ax * ay * d - az * s, ay * ay * d + c, ay * az * d + ax * s, 0,
  1067. ax * az * d + ay * s, ay * az * d - ax * s, az * az * d + c, 0,
  1068. 0, 0, 0, 1);
  1069. };
  1070. /**
  1071. * Makes the given 4x4 matrix a rotation matrix with the given rotation
  1072. * angle about the X axis.
  1073. *
  1074. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1075. * @param {number} angle The rotation angle in radians.
  1076. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1077. * chained.
  1078. */
  1079. goog.vec.Mat4.makeRotateX = function(mat, angle) {
  1080. var c = Math.cos(angle);
  1081. var s = Math.sin(angle);
  1082. return goog.vec.Mat4.setFromValues(
  1083. mat, 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1);
  1084. };
  1085. /**
  1086. * Makes the given 4x4 matrix a rotation matrix with the given rotation
  1087. * angle about the Y axis.
  1088. *
  1089. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1090. * @param {number} angle The rotation angle in radians.
  1091. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1092. * chained.
  1093. */
  1094. goog.vec.Mat4.makeRotateY = function(mat, angle) {
  1095. var c = Math.cos(angle);
  1096. var s = Math.sin(angle);
  1097. return goog.vec.Mat4.setFromValues(
  1098. mat, c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1);
  1099. };
  1100. /**
  1101. * Makes the given 4x4 matrix a rotation matrix with the given rotation
  1102. * angle about the Z axis.
  1103. *
  1104. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1105. * @param {number} angle The rotation angle in radians.
  1106. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1107. * chained.
  1108. */
  1109. goog.vec.Mat4.makeRotateZ = function(mat, angle) {
  1110. var c = Math.cos(angle);
  1111. var s = Math.sin(angle);
  1112. return goog.vec.Mat4.setFromValues(
  1113. mat, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
  1114. };
  1115. /**
  1116. * Makes the given 4x4 matrix a perspective projection matrix.
  1117. *
  1118. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1119. * @param {number} left The coordinate of the left clipping plane.
  1120. * @param {number} right The coordinate of the right clipping plane.
  1121. * @param {number} bottom The coordinate of the bottom clipping plane.
  1122. * @param {number} top The coordinate of the top clipping plane.
  1123. * @param {number} near The distance to the near clipping plane.
  1124. * @param {number} far The distance to the far clipping plane.
  1125. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1126. * chained.
  1127. */
  1128. goog.vec.Mat4.makeFrustum = function(mat, left, right, bottom, top, near, far) {
  1129. var x = (2 * near) / (right - left);
  1130. var y = (2 * near) / (top - bottom);
  1131. var a = (right + left) / (right - left);
  1132. var b = (top + bottom) / (top - bottom);
  1133. var c = -(far + near) / (far - near);
  1134. var d = -(2 * far * near) / (far - near);
  1135. return goog.vec.Mat4.setFromValues(
  1136. mat, x, 0, 0, 0, 0, y, 0, 0, a, b, c, -1, 0, 0, d, 0);
  1137. };
  1138. /**
  1139. * Makes the given 4x4 matrix perspective projection matrix given a
  1140. * field of view and aspect ratio.
  1141. *
  1142. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1143. * @param {number} fovy The field of view along the y (vertical) axis in
  1144. * radians.
  1145. * @param {number} aspect The x (width) to y (height) aspect ratio.
  1146. * @param {number} near The distance to the near clipping plane.
  1147. * @param {number} far The distance to the far clipping plane.
  1148. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1149. * chained.
  1150. */
  1151. goog.vec.Mat4.makePerspective = function(mat, fovy, aspect, near, far) {
  1152. var angle = fovy / 2;
  1153. var dz = far - near;
  1154. var sinAngle = Math.sin(angle);
  1155. if (dz == 0 || sinAngle == 0 || aspect == 0) {
  1156. return mat;
  1157. }
  1158. var cot = Math.cos(angle) / sinAngle;
  1159. return goog.vec.Mat4.setFromValues(
  1160. mat, cot / aspect, 0, 0, 0, 0, cot, 0, 0, 0, 0, -(far + near) / dz, -1, 0,
  1161. 0, -(2 * near * far) / dz, 0);
  1162. };
  1163. /**
  1164. * Makes the given 4x4 matrix an orthographic projection matrix.
  1165. *
  1166. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1167. * @param {number} left The coordinate of the left clipping plane.
  1168. * @param {number} right The coordinate of the right clipping plane.
  1169. * @param {number} bottom The coordinate of the bottom clipping plane.
  1170. * @param {number} top The coordinate of the top clipping plane.
  1171. * @param {number} near The distance to the near clipping plane.
  1172. * @param {number} far The distance to the far clipping plane.
  1173. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1174. * chained.
  1175. */
  1176. goog.vec.Mat4.makeOrtho = function(mat, left, right, bottom, top, near, far) {
  1177. var x = 2 / (right - left);
  1178. var y = 2 / (top - bottom);
  1179. var z = -2 / (far - near);
  1180. var a = -(right + left) / (right - left);
  1181. var b = -(top + bottom) / (top - bottom);
  1182. var c = -(far + near) / (far - near);
  1183. return goog.vec.Mat4.setFromValues(
  1184. mat, x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, a, b, c, 1);
  1185. };
  1186. /**
  1187. * Makes the given 4x4 matrix a modelview matrix of a camera so that
  1188. * the camera is 'looking at' the given center point.
  1189. *
  1190. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1191. * @param {goog.vec.Vec3.AnyType} eyePt The position of the eye point
  1192. * (camera origin).
  1193. * @param {goog.vec.Vec3.AnyType} centerPt The point to aim the camera at.
  1194. * @param {goog.vec.Vec3.AnyType} worldUpVec The vector that identifies
  1195. * the up direction for the camera.
  1196. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1197. * chained.
  1198. */
  1199. goog.vec.Mat4.makeLookAt = function(mat, eyePt, centerPt, worldUpVec) {
  1200. // Compute the direction vector from the eye point to the center point and
  1201. // normalize.
  1202. var fwdVec = goog.vec.Mat4.tmpVec4_[0];
  1203. goog.vec.Vec3.subtract(centerPt, eyePt, fwdVec);
  1204. goog.vec.Vec3.normalize(fwdVec, fwdVec);
  1205. fwdVec[3] = 0;
  1206. // Compute the side vector from the forward vector and the input up vector.
  1207. var sideVec = goog.vec.Mat4.tmpVec4_[1];
  1208. goog.vec.Vec3.cross(fwdVec, worldUpVec, sideVec);
  1209. goog.vec.Vec3.normalize(sideVec, sideVec);
  1210. sideVec[3] = 0;
  1211. // Now the up vector to form the orthonormal basis.
  1212. var upVec = goog.vec.Mat4.tmpVec4_[2];
  1213. goog.vec.Vec3.cross(sideVec, fwdVec, upVec);
  1214. goog.vec.Vec3.normalize(upVec, upVec);
  1215. upVec[3] = 0;
  1216. // Update the view matrix with the new orthonormal basis and position the
  1217. // camera at the given eye point.
  1218. goog.vec.Vec3.negate(fwdVec, fwdVec);
  1219. goog.vec.Mat4.setRow(mat, 0, sideVec);
  1220. goog.vec.Mat4.setRow(mat, 1, upVec);
  1221. goog.vec.Mat4.setRow(mat, 2, fwdVec);
  1222. goog.vec.Mat4.setRowValues(mat, 3, 0, 0, 0, 1);
  1223. goog.vec.Mat4.translate(mat, -eyePt[0], -eyePt[1], -eyePt[2]);
  1224. return mat;
  1225. };
  1226. /**
  1227. * Decomposes a matrix into the lookAt vectors eyePt, fwdVec and worldUpVec.
  1228. * The matrix represents the modelview matrix of a camera. It is the inverse
  1229. * of lookAt except for the output of the fwdVec instead of centerPt.
  1230. * The centerPt itself cannot be recovered from a modelview matrix.
  1231. *
  1232. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1233. * @param {goog.vec.Vec3.AnyType} eyePt The position of the eye point
  1234. * (camera origin).
  1235. * @param {goog.vec.Vec3.AnyType} fwdVec The vector describing where
  1236. * the camera points to.
  1237. * @param {goog.vec.Vec3.AnyType} worldUpVec The vector that
  1238. * identifies the up direction for the camera.
  1239. * @return {boolean} True if the method succeeds, false otherwise.
  1240. * The method can only fail if the inverse of viewMatrix is not defined.
  1241. */
  1242. goog.vec.Mat4.toLookAt = function(mat, eyePt, fwdVec, worldUpVec) {
  1243. // Get eye of the camera.
  1244. var matInverse = goog.vec.Mat4.tmpMat4_[0];
  1245. if (!goog.vec.Mat4.invert(mat, matInverse)) {
  1246. // The input matrix does not have a valid inverse.
  1247. return false;
  1248. }
  1249. if (eyePt) {
  1250. eyePt[0] = matInverse[12];
  1251. eyePt[1] = matInverse[13];
  1252. eyePt[2] = matInverse[14];
  1253. }
  1254. // Get forward vector from the definition of lookAt.
  1255. if (fwdVec || worldUpVec) {
  1256. if (!fwdVec) {
  1257. fwdVec = goog.vec.Mat4.tmpVec3_[0];
  1258. }
  1259. fwdVec[0] = -mat[2];
  1260. fwdVec[1] = -mat[6];
  1261. fwdVec[2] = -mat[10];
  1262. // Normalize forward vector.
  1263. goog.vec.Vec3.normalize(fwdVec, fwdVec);
  1264. }
  1265. if (worldUpVec) {
  1266. // Get side vector from the definition of gluLookAt.
  1267. var side = goog.vec.Mat4.tmpVec3_[1];
  1268. side[0] = mat[0];
  1269. side[1] = mat[4];
  1270. side[2] = mat[8];
  1271. // Compute up vector as a up = side x forward.
  1272. goog.vec.Vec3.cross(side, fwdVec, worldUpVec);
  1273. // Normalize up vector.
  1274. goog.vec.Vec3.normalize(worldUpVec, worldUpVec);
  1275. }
  1276. return true;
  1277. };
  1278. /**
  1279. * Makes the given 4x4 matrix a rotation matrix given Euler angles using
  1280. * the ZXZ convention.
  1281. * Given the euler angles [theta1, theta2, theta3], the rotation is defined as
  1282. * rotation = rotation_z(theta1) * rotation_x(theta2) * rotation_z(theta3),
  1283. * with theta1 in [0, 2 * pi], theta2 in [0, pi] and theta3 in [0, 2 * pi].
  1284. * rotation_x(theta) means rotation around the X axis of theta radians,
  1285. *
  1286. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1287. * @param {number} theta1 The angle of rotation around the Z axis in radians.
  1288. * @param {number} theta2 The angle of rotation around the X axis in radians.
  1289. * @param {number} theta3 The angle of rotation around the Z axis in radians.
  1290. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1291. * chained.
  1292. */
  1293. goog.vec.Mat4.makeEulerZXZ = function(mat, theta1, theta2, theta3) {
  1294. var c1 = Math.cos(theta1);
  1295. var s1 = Math.sin(theta1);
  1296. var c2 = Math.cos(theta2);
  1297. var s2 = Math.sin(theta2);
  1298. var c3 = Math.cos(theta3);
  1299. var s3 = Math.sin(theta3);
  1300. mat[0] = c1 * c3 - c2 * s1 * s3;
  1301. mat[1] = c2 * c1 * s3 + c3 * s1;
  1302. mat[2] = s3 * s2;
  1303. mat[3] = 0;
  1304. mat[4] = -c1 * s3 - c3 * c2 * s1;
  1305. mat[5] = c1 * c2 * c3 - s1 * s3;
  1306. mat[6] = c3 * s2;
  1307. mat[7] = 0;
  1308. mat[8] = s2 * s1;
  1309. mat[9] = -c1 * s2;
  1310. mat[10] = c2;
  1311. mat[11] = 0;
  1312. mat[12] = 0;
  1313. mat[13] = 0;
  1314. mat[14] = 0;
  1315. mat[15] = 1;
  1316. return mat;
  1317. };
  1318. /**
  1319. * Decomposes a rotation matrix into Euler angles using the ZXZ convention so
  1320. * that rotation = rotation_z(theta1) * rotation_x(theta2) * rotation_z(theta3),
  1321. * with theta1 in [0, 2 * pi], theta2 in [0, pi] and theta3 in [0, 2 * pi].
  1322. * rotation_x(theta) means rotation around the X axis of theta radians.
  1323. *
  1324. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1325. * @param {goog.vec.Vec3.AnyType} euler The ZXZ Euler angles in
  1326. * radians as [theta1, theta2, theta3].
  1327. * @param {boolean=} opt_theta2IsNegative Whether theta2 is in [-pi, 0] instead
  1328. * of the default [0, pi].
  1329. * @return {goog.vec.Vec4.AnyType} return euler so that operations can be
  1330. * chained together.
  1331. */
  1332. goog.vec.Mat4.toEulerZXZ = function(mat, euler, opt_theta2IsNegative) {
  1333. // There is an ambiguity in the sign of sinTheta2 because of the sqrt.
  1334. var sinTheta2 = Math.sqrt(mat[2] * mat[2] + mat[6] * mat[6]);
  1335. // By default we explicitely constrain theta2 to be in [0, pi],
  1336. // so sinTheta2 is always positive. We can change the behavior and specify
  1337. // theta2 to be negative in [-pi, 0] with opt_Theta2IsNegative.
  1338. var signTheta2 = opt_theta2IsNegative ? -1 : 1;
  1339. if (sinTheta2 > goog.vec.EPSILON) {
  1340. euler[2] = Math.atan2(mat[2] * signTheta2, mat[6] * signTheta2);
  1341. euler[1] = Math.atan2(sinTheta2 * signTheta2, mat[10]);
  1342. euler[0] = Math.atan2(mat[8] * signTheta2, -mat[9] * signTheta2);
  1343. } else {
  1344. // There is also an arbitrary choice for theta1 = 0 or theta2 = 0 here.
  1345. // We assume theta1 = 0 as some applications do not allow the camera to roll
  1346. // (i.e. have theta1 != 0).
  1347. euler[0] = 0;
  1348. euler[1] = Math.atan2(sinTheta2 * signTheta2, mat[10]);
  1349. euler[2] = Math.atan2(mat[1], mat[0]);
  1350. }
  1351. // Atan2 outputs angles in [-pi, pi] so we bring them back to [0, 2 * pi].
  1352. euler[0] = (euler[0] + Math.PI * 2) % (Math.PI * 2);
  1353. euler[2] = (euler[2] + Math.PI * 2) % (Math.PI * 2);
  1354. // For theta2 we want the angle to be in [0, pi] or [-pi, 0] depending on
  1355. // signTheta2.
  1356. euler[1] =
  1357. ((euler[1] * signTheta2 + Math.PI * 2) % (Math.PI * 2)) * signTheta2;
  1358. return euler;
  1359. };
  1360. /**
  1361. * Translates the given matrix by x,y,z. Equvialent to:
  1362. * goog.vec.Mat4.multMat(
  1363. * mat,
  1364. * goog.vec.Mat4.makeTranslate(goog.vec.Mat4.create(), x, y, z),
  1365. * mat);
  1366. *
  1367. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1368. * @param {number} x The translation along the x axis.
  1369. * @param {number} y The translation along the y axis.
  1370. * @param {number} z The translation along the z axis.
  1371. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1372. * chained.
  1373. */
  1374. goog.vec.Mat4.translate = function(mat, x, y, z) {
  1375. return goog.vec.Mat4.setColumnValues(
  1376. mat, 3, mat[0] * x + mat[4] * y + mat[8] * z + mat[12],
  1377. mat[1] * x + mat[5] * y + mat[9] * z + mat[13],
  1378. mat[2] * x + mat[6] * y + mat[10] * z + mat[14],
  1379. mat[3] * x + mat[7] * y + mat[11] * z + mat[15]);
  1380. };
  1381. /**
  1382. * Scales the given matrix by x,y,z. Equivalent to:
  1383. * goog.vec.Mat4.multMat(
  1384. * mat,
  1385. * goog.vec.Mat4.makeScale(goog.vec.Mat4.create(), x, y, z),
  1386. * mat);
  1387. *
  1388. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1389. * @param {number} x The x scale factor.
  1390. * @param {number} y The y scale factor.
  1391. * @param {number} z The z scale factor.
  1392. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1393. * chained.
  1394. */
  1395. goog.vec.Mat4.scale = function(mat, x, y, z) {
  1396. return goog.vec.Mat4.setFromValues(
  1397. mat, mat[0] * x, mat[1] * x, mat[2] * x, mat[3] * x, mat[4] * y,
  1398. mat[5] * y, mat[6] * y, mat[7] * y, mat[8] * z, mat[9] * z, mat[10] * z,
  1399. mat[11] * z, mat[12], mat[13], mat[14], mat[15]);
  1400. };
  1401. /**
  1402. * Rotate the given matrix by angle about the x,y,z axis. Equivalent to:
  1403. * goog.vec.Mat4.multMat(
  1404. * mat,
  1405. * goog.vec.Mat4.makeRotate(goog.vec.Mat4.create(), angle, x, y, z),
  1406. * mat);
  1407. *
  1408. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1409. * @param {number} angle The angle in radians.
  1410. * @param {number} x The x component of the rotation axis.
  1411. * @param {number} y The y component of the rotation axis.
  1412. * @param {number} z The z component of the rotation axis.
  1413. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1414. * chained.
  1415. */
  1416. goog.vec.Mat4.rotate = function(mat, angle, x, y, z) {
  1417. var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
  1418. var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
  1419. var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
  1420. var m03 = mat[12], m13 = mat[13], m23 = mat[14], m33 = mat[15];
  1421. var cosAngle = Math.cos(angle);
  1422. var sinAngle = Math.sin(angle);
  1423. var diffCosAngle = 1 - cosAngle;
  1424. var r00 = x * x * diffCosAngle + cosAngle;
  1425. var r10 = x * y * diffCosAngle + z * sinAngle;
  1426. var r20 = x * z * diffCosAngle - y * sinAngle;
  1427. var r01 = x * y * diffCosAngle - z * sinAngle;
  1428. var r11 = y * y * diffCosAngle + cosAngle;
  1429. var r21 = y * z * diffCosAngle + x * sinAngle;
  1430. var r02 = x * z * diffCosAngle + y * sinAngle;
  1431. var r12 = y * z * diffCosAngle - x * sinAngle;
  1432. var r22 = z * z * diffCosAngle + cosAngle;
  1433. return goog.vec.Mat4.setFromValues(
  1434. mat, m00 * r00 + m01 * r10 + m02 * r20, m10 * r00 + m11 * r10 + m12 * r20,
  1435. m20 * r00 + m21 * r10 + m22 * r20, m30 * r00 + m31 * r10 + m32 * r20,
  1436. m00 * r01 + m01 * r11 + m02 * r21, m10 * r01 + m11 * r11 + m12 * r21,
  1437. m20 * r01 + m21 * r11 + m22 * r21, m30 * r01 + m31 * r11 + m32 * r21,
  1438. m00 * r02 + m01 * r12 + m02 * r22, m10 * r02 + m11 * r12 + m12 * r22,
  1439. m20 * r02 + m21 * r12 + m22 * r22, m30 * r02 + m31 * r12 + m32 * r22,
  1440. m03, m13, m23, m33);
  1441. };
  1442. /**
  1443. * Rotate the given matrix by angle about the x axis. Equivalent to:
  1444. * goog.vec.Mat4.multMat(
  1445. * mat,
  1446. * goog.vec.Mat4.makeRotateX(goog.vec.Mat4.create(), angle),
  1447. * mat);
  1448. *
  1449. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1450. * @param {number} angle The angle in radians.
  1451. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1452. * chained.
  1453. */
  1454. goog.vec.Mat4.rotateX = function(mat, angle) {
  1455. var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
  1456. var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
  1457. var c = Math.cos(angle);
  1458. var s = Math.sin(angle);
  1459. mat[4] = m01 * c + m02 * s;
  1460. mat[5] = m11 * c + m12 * s;
  1461. mat[6] = m21 * c + m22 * s;
  1462. mat[7] = m31 * c + m32 * s;
  1463. mat[8] = m01 * -s + m02 * c;
  1464. mat[9] = m11 * -s + m12 * c;
  1465. mat[10] = m21 * -s + m22 * c;
  1466. mat[11] = m31 * -s + m32 * c;
  1467. return mat;
  1468. };
  1469. /**
  1470. * Rotate the given matrix by angle about the y axis. Equivalent to:
  1471. * goog.vec.Mat4.multMat(
  1472. * mat,
  1473. * goog.vec.Mat4.makeRotateY(goog.vec.Mat4.create(), angle),
  1474. * mat);
  1475. *
  1476. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1477. * @param {number} angle The angle in radians.
  1478. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1479. * chained.
  1480. */
  1481. goog.vec.Mat4.rotateY = function(mat, angle) {
  1482. var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
  1483. var m02 = mat[8], m12 = mat[9], m22 = mat[10], m32 = mat[11];
  1484. var c = Math.cos(angle);
  1485. var s = Math.sin(angle);
  1486. mat[0] = m00 * c + m02 * -s;
  1487. mat[1] = m10 * c + m12 * -s;
  1488. mat[2] = m20 * c + m22 * -s;
  1489. mat[3] = m30 * c + m32 * -s;
  1490. mat[8] = m00 * s + m02 * c;
  1491. mat[9] = m10 * s + m12 * c;
  1492. mat[10] = m20 * s + m22 * c;
  1493. mat[11] = m30 * s + m32 * c;
  1494. return mat;
  1495. };
  1496. /**
  1497. * Rotate the given matrix by angle about the z axis. Equivalent to:
  1498. * goog.vec.Mat4.multMat(
  1499. * mat,
  1500. * goog.vec.Mat4.makeRotateZ(goog.vec.Mat4.create(), angle),
  1501. * mat);
  1502. *
  1503. * @param {goog.vec.Mat4.AnyType} mat The matrix.
  1504. * @param {number} angle The angle in radians.
  1505. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1506. * chained.
  1507. */
  1508. goog.vec.Mat4.rotateZ = function(mat, angle) {
  1509. var m00 = mat[0], m10 = mat[1], m20 = mat[2], m30 = mat[3];
  1510. var m01 = mat[4], m11 = mat[5], m21 = mat[6], m31 = mat[7];
  1511. var c = Math.cos(angle);
  1512. var s = Math.sin(angle);
  1513. mat[0] = m00 * c + m01 * s;
  1514. mat[1] = m10 * c + m11 * s;
  1515. mat[2] = m20 * c + m21 * s;
  1516. mat[3] = m30 * c + m31 * s;
  1517. mat[4] = m00 * -s + m01 * c;
  1518. mat[5] = m10 * -s + m11 * c;
  1519. mat[6] = m20 * -s + m21 * c;
  1520. mat[7] = m30 * -s + m31 * c;
  1521. return mat;
  1522. };
  1523. /**
  1524. * Retrieves the translation component of the transformation matrix.
  1525. *
  1526. * @param {goog.vec.Mat4.AnyType} mat The transformation matrix.
  1527. * @param {goog.vec.Vec3.AnyType} translation The vector for storing the
  1528. * result.
  1529. * @return {goog.vec.Mat4.AnyType} return mat so that operations can be
  1530. * chained.
  1531. */
  1532. goog.vec.Mat4.getTranslation = function(mat, translation) {
  1533. translation[0] = mat[12];
  1534. translation[1] = mat[13];
  1535. translation[2] = mat[14];
  1536. return translation;
  1537. };
  1538. /**
  1539. * @type {!Array<!goog.vec.Mat4.Float64>}
  1540. * @private
  1541. */
  1542. goog.vec.Mat4.tmpVec3_ =
  1543. [goog.vec.Vec3.createFloat64(), goog.vec.Vec3.createFloat64()];
  1544. /**
  1545. * @type {!Array<!goog.vec.Mat4.Float64>}
  1546. * @private
  1547. */
  1548. goog.vec.Mat4.tmpVec4_ = [
  1549. goog.vec.Vec4.createFloat64(), goog.vec.Vec4.createFloat64(),
  1550. goog.vec.Vec4.createFloat64()
  1551. ];
  1552. /**
  1553. * @type {!Array<!goog.vec.Mat4.Float64>}
  1554. * @private
  1555. */
  1556. goog.vec.Mat4.tmpMat4_ = [goog.vec.Mat4.createFloat64()];