linear-algebra-3d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. /*
  7. * This code has been modified from https://github.com/toji/gl-matrix/,
  8. * copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. */
  17. export type Mat4 = number[]
  18. export type Vec3 = number[]
  19. export type Vec4 = number[]
  20. const enum EPSILON { Value = 0.000001 }
  21. export function Mat4() {
  22. return Mat4.zero();
  23. }
  24. /**
  25. * Stores a 4x4 matrix in a column major (j * 4 + i indexing) format.
  26. */
  27. export namespace Mat4 {
  28. export function zero(): number[] {
  29. // force double backing array by 0.1.
  30. const ret = [0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  31. ret[0] = 0.0;
  32. return ret;
  33. }
  34. export function identity(): number[] {
  35. let out = zero();
  36. out[0] = 1;
  37. out[1] = 0;
  38. out[2] = 0;
  39. out[3] = 0;
  40. out[4] = 0;
  41. out[5] = 1;
  42. out[6] = 0;
  43. out[7] = 0;
  44. out[8] = 0;
  45. out[9] = 0;
  46. out[10] = 1;
  47. out[11] = 0;
  48. out[12] = 0;
  49. out[13] = 0;
  50. out[14] = 0;
  51. out[15] = 1;
  52. return out;
  53. }
  54. export function fromIdentity(mat: number[]): number[] {
  55. mat[0] = 1;
  56. mat[1] = 0;
  57. mat[2] = 0;
  58. mat[3] = 0;
  59. mat[4] = 0;
  60. mat[5] = 1;
  61. mat[6] = 0;
  62. mat[7] = 0;
  63. mat[8] = 0;
  64. mat[9] = 0;
  65. mat[10] = 1;
  66. mat[11] = 0;
  67. mat[12] = 0;
  68. mat[13] = 0;
  69. mat[14] = 0;
  70. mat[15] = 1;
  71. return mat;
  72. }
  73. export function ofRows(rows: number[][]): number[] {
  74. let out = zero(), i: number, j: number, r: number[];
  75. for (i = 0; i < 4; i++) {
  76. r = rows[i];
  77. for (j = 0; j < 4; j++) {
  78. out[4 * j + i] = r[j];
  79. }
  80. }
  81. return out;
  82. }
  83. export function areEqual(a: number[], b: number[], eps: number) {
  84. for (let i = 0; i < 16; i++) {
  85. if (Math.abs(a[i] - b[i]) > eps) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. export function setValue(a: number[], i: number, j: number, value: number) {
  92. a[4 * j + i] = value;
  93. }
  94. export function copy(out: number[], a: number[]) {
  95. out[0] = a[0];
  96. out[1] = a[1];
  97. out[2] = a[2];
  98. out[3] = a[3];
  99. out[4] = a[4];
  100. out[5] = a[5];
  101. out[6] = a[6];
  102. out[7] = a[7];
  103. out[8] = a[8];
  104. out[9] = a[9];
  105. out[10] = a[10];
  106. out[11] = a[11];
  107. out[12] = a[12];
  108. out[13] = a[13];
  109. out[14] = a[14];
  110. out[15] = a[15];
  111. return out;
  112. }
  113. export function clone(a: number[]) {
  114. return Mat4.copy(Mat4.zero(), a);
  115. }
  116. export function invert(out: number[], a: number[]) {
  117. let a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  118. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  119. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  120. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  121. b00 = a00 * a11 - a01 * a10,
  122. b01 = a00 * a12 - a02 * a10,
  123. b02 = a00 * a13 - a03 * a10,
  124. b03 = a01 * a12 - a02 * a11,
  125. b04 = a01 * a13 - a03 * a11,
  126. b05 = a02 * a13 - a03 * a12,
  127. b06 = a20 * a31 - a21 * a30,
  128. b07 = a20 * a32 - a22 * a30,
  129. b08 = a20 * a33 - a23 * a30,
  130. b09 = a21 * a32 - a22 * a31,
  131. b10 = a21 * a33 - a23 * a31,
  132. b11 = a22 * a33 - a23 * a32,
  133. // Calculate the determinant
  134. det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  135. if (!det) {
  136. return null;
  137. }
  138. det = 1.0 / det;
  139. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  140. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  141. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  142. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  143. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  144. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  145. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  146. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  147. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  148. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  149. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  150. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  151. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  152. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  153. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  154. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  155. return out;
  156. }
  157. export function mul(out: number[], a: number[], b: number[]) {
  158. let a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  159. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  160. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  161. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  162. // Cache only the current line of the second matrix
  163. let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
  164. out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  165. out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  166. out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  167. out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  168. b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
  169. out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  170. out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  171. out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  172. out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  173. b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
  174. out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  175. out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  176. out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  177. out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  178. b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
  179. out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  180. out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  181. out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  182. out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  183. return out;
  184. }
  185. export function mul3(out: number[], a: number[], b: number[], c: number[]) {
  186. return mul(out, mul(out, a, b), c);
  187. }
  188. export function translate(out: number[], a: number[], v: number[]) {
  189. let x = v[0], y = v[1], z = v[2],
  190. a00: number, a01: number, a02: number, a03: number,
  191. a10: number, a11: number, a12: number, a13: number,
  192. a20: number, a21: number, a22: number, a23: number;
  193. if (a === out) {
  194. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  195. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  196. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  197. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  198. } else {
  199. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  200. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  201. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  202. out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
  203. out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
  204. out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;
  205. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  206. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  207. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  208. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  209. }
  210. return out;
  211. }
  212. export function fromTranslation(out: number[], v: number[]) {
  213. out[0] = 1;
  214. out[1] = 0;
  215. out[2] = 0;
  216. out[3] = 0;
  217. out[4] = 0;
  218. out[5] = 1;
  219. out[6] = 0;
  220. out[7] = 0;
  221. out[8] = 0;
  222. out[9] = 0;
  223. out[10] = 1;
  224. out[11] = 0;
  225. out[12] = v[0];
  226. out[13] = v[1];
  227. out[14] = v[2];
  228. out[15] = 1;
  229. return out;
  230. }
  231. export function rotate(out: number[], a: number[], rad: number, axis: number[]) {
  232. let x = axis[0], y = axis[1], z = axis[2],
  233. len = Math.sqrt(x * x + y * y + z * z),
  234. s, c, t,
  235. a00, a01, a02, a03,
  236. a10, a11, a12, a13,
  237. a20, a21, a22, a23,
  238. b00, b01, b02,
  239. b10, b11, b12,
  240. b20, b21, b22;
  241. if (Math.abs(len) < EPSILON.Value) { return null; }
  242. len = 1 / len;
  243. x *= len;
  244. y *= len;
  245. z *= len;
  246. s = Math.sin(rad);
  247. c = Math.cos(rad);
  248. t = 1 - c;
  249. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  250. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  251. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  252. // Construct the elements of the rotation matrix
  253. b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
  254. b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
  255. b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
  256. // Perform rotation-specific matrix multiplication
  257. out[0] = a00 * b00 + a10 * b01 + a20 * b02;
  258. out[1] = a01 * b00 + a11 * b01 + a21 * b02;
  259. out[2] = a02 * b00 + a12 * b01 + a22 * b02;
  260. out[3] = a03 * b00 + a13 * b01 + a23 * b02;
  261. out[4] = a00 * b10 + a10 * b11 + a20 * b12;
  262. out[5] = a01 * b10 + a11 * b11 + a21 * b12;
  263. out[6] = a02 * b10 + a12 * b11 + a22 * b12;
  264. out[7] = a03 * b10 + a13 * b11 + a23 * b12;
  265. out[8] = a00 * b20 + a10 * b21 + a20 * b22;
  266. out[9] = a01 * b20 + a11 * b21 + a21 * b22;
  267. out[10] = a02 * b20 + a12 * b21 + a22 * b22;
  268. out[11] = a03 * b20 + a13 * b21 + a23 * b22;
  269. if (a !== out) { // If the source and destination differ, copy the unchanged last row
  270. out[12] = a[12];
  271. out[13] = a[13];
  272. out[14] = a[14];
  273. out[15] = a[15];
  274. }
  275. return out;
  276. }
  277. export function fromRotation(out: number[], rad: number, axis: number[]) {
  278. let x = axis[0], y = axis[1], z = axis[2],
  279. len = Math.sqrt(x * x + y * y + z * z),
  280. s, c, t;
  281. if (Math.abs(len) < EPSILON.Value) { return fromIdentity(out); }
  282. len = 1 / len;
  283. x *= len;
  284. y *= len;
  285. z *= len;
  286. s = Math.sin(rad);
  287. c = Math.cos(rad);
  288. t = 1 - c;
  289. // Perform rotation-specific matrix multiplication
  290. out[0] = x * x * t + c;
  291. out[1] = y * x * t + z * s;
  292. out[2] = z * x * t - y * s;
  293. out[3] = 0;
  294. out[4] = x * y * t - z * s;
  295. out[5] = y * y * t + c;
  296. out[6] = z * y * t + x * s;
  297. out[7] = 0;
  298. out[8] = x * z * t + y * s;
  299. out[9] = y * z * t - x * s;
  300. out[10] = z * z * t + c;
  301. out[11] = 0;
  302. out[12] = 0;
  303. out[13] = 0;
  304. out[14] = 0;
  305. out[15] = 1;
  306. return out;
  307. }
  308. export function scale(out: number[], a: number[], v: number[]) {
  309. let x = v[0], y = v[1], z = v[2];
  310. out[0] = a[0] * x;
  311. out[1] = a[1] * x;
  312. out[2] = a[2] * x;
  313. out[3] = a[3] * x;
  314. out[4] = a[4] * y;
  315. out[5] = a[5] * y;
  316. out[6] = a[6] * y;
  317. out[7] = a[7] * y;
  318. out[8] = a[8] * z;
  319. out[9] = a[9] * z;
  320. out[10] = a[10] * z;
  321. out[11] = a[11] * z;
  322. out[12] = a[12];
  323. out[13] = a[13];
  324. out[14] = a[14];
  325. out[15] = a[15];
  326. return out;
  327. }
  328. export function fromScaling(out: number[], v: number[]) {
  329. out[0] = v[0];
  330. out[1] = 0;
  331. out[2] = 0;
  332. out[3] = 0;
  333. out[4] = 0;
  334. out[5] = v[1];
  335. out[6] = 0;
  336. out[7] = 0;
  337. out[8] = 0;
  338. out[9] = 0;
  339. out[10] = v[2];
  340. out[11] = 0;
  341. out[12] = 0;
  342. out[13] = 0;
  343. out[14] = 0;
  344. out[15] = 1;
  345. return out;
  346. }
  347. export function makeTable(m: number[]) {
  348. let ret = '';
  349. for (let i = 0; i < 4; i++) {
  350. for (let j = 0; j < 4; j++) {
  351. ret += m[4 * j + i].toString();
  352. if (j < 3) ret += ' ';
  353. }
  354. if (i < 3) ret += '\n';
  355. }
  356. return ret;
  357. }
  358. export function determinant(a: number[]) {
  359. let a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  360. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  361. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  362. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  363. b00 = a00 * a11 - a01 * a10,
  364. b01 = a00 * a12 - a02 * a10,
  365. b02 = a00 * a13 - a03 * a10,
  366. b03 = a01 * a12 - a02 * a11,
  367. b04 = a01 * a13 - a03 * a11,
  368. b05 = a02 * a13 - a03 * a12,
  369. b06 = a20 * a31 - a21 * a30,
  370. b07 = a20 * a32 - a22 * a30,
  371. b08 = a20 * a33 - a23 * a30,
  372. b09 = a21 * a32 - a22 * a31,
  373. b10 = a21 * a33 - a23 * a31,
  374. b11 = a22 * a33 - a23 * a32;
  375. // Calculate the determinant
  376. return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  377. }
  378. }
  379. export function Vec3(x?: number, y?: number, z?: number) {
  380. return Vec3.fromValues(x || 0, y || 0, z || 0);
  381. }
  382. export namespace Vec3 {
  383. export function zero() {
  384. let out = [0.1, 0.0, 0.0];
  385. out[0] = 0;
  386. return out;
  387. }
  388. export function clone(a: number[]) {
  389. let out = zero();
  390. out[0] = a[0];
  391. out[1] = a[1];
  392. out[2] = a[2];
  393. return out;
  394. }
  395. export function fromObj(v: { x: number, y: number, z: number }) {
  396. return fromValues(v.x, v.y, v.z);
  397. }
  398. export function toObj(v: number[]) {
  399. return { x: v[0], y: v[1], z: v[2] };
  400. }
  401. export function fromValues(x: number, y: number, z: number) {
  402. let out = zero();
  403. out[0] = x;
  404. out[1] = y;
  405. out[2] = z;
  406. return out;
  407. }
  408. export function set(out: number[], x: number, y: number, z: number) {
  409. out[0] = x;
  410. out[1] = y;
  411. out[2] = z;
  412. return out;
  413. }
  414. export function copy(out: number[], a: number[]) {
  415. out[0] = a[0];
  416. out[1] = a[1];
  417. out[2] = a[2];
  418. return out;
  419. }
  420. export function add(out: number[], a: number[], b: number[]) {
  421. out[0] = a[0] + b[0];
  422. out[1] = a[1] + b[1];
  423. out[2] = a[2] + b[2];
  424. return out;
  425. }
  426. export function sub(out: number[], a: number[], b: number[]) {
  427. out[0] = a[0] - b[0];
  428. out[1] = a[1] - b[1];
  429. out[2] = a[2] - b[2];
  430. return out;
  431. }
  432. export function scale(out: number[], a: number[], b: number) {
  433. out[0] = a[0] * b;
  434. out[1] = a[1] * b;
  435. out[2] = a[2] * b;
  436. return out;
  437. }
  438. export function scaleAndAdd(out: number[], a: number[], b: number[], scale: number) {
  439. out[0] = a[0] + (b[0] * scale);
  440. out[1] = a[1] + (b[1] * scale);
  441. out[2] = a[2] + (b[2] * scale);
  442. return out;
  443. }
  444. export function distance(a: number[], b: number[]) {
  445. let x = b[0] - a[0],
  446. y = b[1] - a[1],
  447. z = b[2] - a[2];
  448. return Math.sqrt(x * x + y * y + z * z);
  449. }
  450. export function squaredDistance(a: number[], b: number[]) {
  451. let x = b[0] - a[0],
  452. y = b[1] - a[1],
  453. z = b[2] - a[2];
  454. return x * x + y * y + z * z;
  455. }
  456. export function magnitude(a: number[]) {
  457. let x = a[0],
  458. y = a[1],
  459. z = a[2];
  460. return Math.sqrt(x * x + y * y + z * z);
  461. }
  462. export function squaredMagnitude(a: number[]) {
  463. let x = a[0],
  464. y = a[1],
  465. z = a[2];
  466. return x * x + y * y + z * z;
  467. }
  468. export function normalize(out: number[], a: number[]) {
  469. let x = a[0],
  470. y = a[1],
  471. z = a[2];
  472. let len = x * x + y * y + z * z;
  473. if (len > 0) {
  474. len = 1 / Math.sqrt(len);
  475. out[0] = a[0] * len;
  476. out[1] = a[1] * len;
  477. out[2] = a[2] * len;
  478. }
  479. return out;
  480. }
  481. export function dot(a: number[], b: number[]) {
  482. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  483. }
  484. export function cross(out: number[], a: number[], b: number[]) {
  485. let ax = a[0], ay = a[1], az = a[2],
  486. bx = b[0], by = b[1], bz = b[2];
  487. out[0] = ay * bz - az * by;
  488. out[1] = az * bx - ax * bz;
  489. out[2] = ax * by - ay * bx;
  490. return out;
  491. }
  492. export function lerp(out: number[], a: number[], b: number[], t: number) {
  493. let ax = a[0],
  494. ay = a[1],
  495. az = a[2];
  496. out[0] = ax + t * (b[0] - ax);
  497. out[1] = ay + t * (b[1] - ay);
  498. out[2] = az + t * (b[2] - az);
  499. return out;
  500. }
  501. export function transformMat4(out: number[], a: number[], m: number[]) {
  502. let x = a[0], y = a[1], z = a[2],
  503. w = (m[3] * x + m[7] * y + m[11] * z + m[15]) || 1.0;
  504. out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
  505. out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
  506. out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
  507. return out;
  508. }
  509. const angleTempA = zero(), angleTempB = zero();
  510. export function angle(a: number[], b: number[]) {
  511. copy(angleTempA, a);
  512. copy(angleTempB, b);
  513. normalize(angleTempA, angleTempA);
  514. normalize(angleTempB, angleTempB);
  515. let cosine = dot(angleTempA, angleTempB);
  516. if (cosine > 1.0) {
  517. return 0;
  518. }
  519. else if (cosine < -1.0) {
  520. return Math.PI;
  521. } else {
  522. return Math.acos(cosine);
  523. }
  524. }
  525. const rotTemp = zero();
  526. export function makeRotation(mat: Mat4, a: Vec3, b: Vec3): Mat4 {
  527. const by = angle(a, b);
  528. if (Math.abs(by) < 0.0001) return Mat4.fromIdentity(mat);
  529. const axis = cross(rotTemp, a, b);
  530. return Mat4.fromRotation(mat, by, axis);
  531. }
  532. }
  533. export function Vec4(x?: number, y?: number, z?: number, w?: number) {
  534. return Vec4.fromValues(x || 0, y || 0, z || 0, w || 0);
  535. }
  536. export namespace Vec4 {
  537. export function zero(): number[] {
  538. // force double backing array by 0.1.
  539. const ret = [0.1, 0, 0, 0];
  540. ret[0] = 0.0;
  541. return ret;
  542. }
  543. export function clone(a: number[]) {
  544. let out = zero();
  545. out[0] = a[0];
  546. out[1] = a[1];
  547. out[2] = a[2];
  548. out[3] = a[3];
  549. return out;
  550. }
  551. export function fromValues(x: number, y: number, z: number, w: number) {
  552. let out = zero();
  553. out[0] = x;
  554. out[1] = y;
  555. out[2] = z;
  556. out[3] = w;
  557. return out;
  558. }
  559. export function set(out: number[], x: number, y: number, z: number, w: number) {
  560. out[0] = x;
  561. out[1] = y;
  562. out[2] = z;
  563. out[3] = w;
  564. return out;
  565. }
  566. export function distance(a: number[], b: number[]) {
  567. let x = b[0] - a[0],
  568. y = b[1] - a[1],
  569. z = b[2] - a[2],
  570. w = b[3] - a[3];
  571. return Math.sqrt(x * x + y * y + z * z + w * w);
  572. }
  573. export function squaredDistance(a: number[], b: number[]) {
  574. let x = b[0] - a[0],
  575. y = b[1] - a[1],
  576. z = b[2] - a[2],
  577. w = b[3] - a[3];
  578. return x * x + y * y + z * z + w * w;
  579. }
  580. export function norm(a: number[]) {
  581. let x = a[0],
  582. y = a[1],
  583. z = a[2],
  584. w = a[3];
  585. return Math.sqrt(x * x + y * y + z * z + w * w);
  586. }
  587. export function squaredNorm(a: number[]) {
  588. let x = a[0],
  589. y = a[1],
  590. z = a[2],
  591. w = a[3];
  592. return x * x + y * y + z * z + w * w;
  593. }
  594. export function transform(out: number[], a: number[], m: number[]) {
  595. let x = a[0], y = a[1], z = a[2], w = a[3];
  596. out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
  597. out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
  598. out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
  599. out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
  600. return out;
  601. }
  602. }