vec4.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @fileoverview Supplies 4 element vectors that are compatible with WebGL.
  8. * Each element is a float32 since that is typically the desired size of a
  9. * 4-vector in the GPU. The API is structured to avoid unnecessary memory
  10. * allocations. The last parameter will typically be the output vector and
  11. * an object can be both an input and output parameter to all methods except
  12. * where noted.
  13. */
  14. goog.provide('goog.vec.Vec4');
  15. /** @suppress {extraRequire} */
  16. goog.require('goog.vec');
  17. /** @typedef {!goog.vec.Float32} */ goog.vec.Vec4.Float32;
  18. /** @typedef {!goog.vec.Float64} */ goog.vec.Vec4.Float64;
  19. /** @typedef {!goog.vec.Number} */ goog.vec.Vec4.Number;
  20. /** @typedef {!goog.vec.AnyType} */ goog.vec.Vec4.AnyType;
  21. // The following two types are deprecated - use the above types instead.
  22. /** @typedef {!Float32Array} */ goog.vec.Vec4.Type;
  23. /** @typedef {!goog.vec.ArrayType} */ goog.vec.Vec4.Vec4Like;
  24. /**
  25. * Creates a 4 element vector of Float32. The array is initialized to zero.
  26. *
  27. * @return {!goog.vec.Vec4.Float32} The new 3 element array.
  28. */
  29. goog.vec.Vec4.createFloat32 = function() {
  30. return new Float32Array(4);
  31. };
  32. /**
  33. * Creates a 4 element vector of Float64. The array is initialized to zero.
  34. *
  35. * @return {!goog.vec.Vec4.Float64} The new 4 element array.
  36. */
  37. goog.vec.Vec4.createFloat64 = function() {
  38. return new Float64Array(4);
  39. };
  40. /**
  41. * Creates a 4 element vector of Number. The array is initialized to zero.
  42. *
  43. * @return {!goog.vec.Vec4.Number} The new 4 element array.
  44. */
  45. goog.vec.Vec4.createNumber = function() {
  46. var v = new Array(4);
  47. goog.vec.Vec4.setFromValues(v, 0, 0, 0, 0);
  48. return v;
  49. };
  50. /**
  51. * Creates a 4 element vector of Float32Array. The array is initialized to zero.
  52. *
  53. * @deprecated Use createFloat32.
  54. * @return {!goog.vec.Vec4.Type} The new 4 element array.
  55. */
  56. goog.vec.Vec4.create = function() {
  57. return new Float32Array(4);
  58. };
  59. /**
  60. * Creates a new 4 element vector initialized with the value from the given
  61. * array.
  62. *
  63. * @deprecated Use createFloat32FromArray.
  64. * @param {goog.vec.Vec4.Vec4Like} vec The source 4 element array.
  65. * @return {!goog.vec.Vec4.Type} The new 4 element array.
  66. */
  67. goog.vec.Vec4.createFromArray = function(vec) {
  68. var newVec = goog.vec.Vec4.create();
  69. goog.vec.Vec4.setFromArray(newVec, vec);
  70. return newVec;
  71. };
  72. /**
  73. * Creates a new 4 element FLoat32 vector initialized with the value from the
  74. * given array.
  75. *
  76. * @param {goog.vec.Vec4.AnyType} vec The source 3 element array.
  77. * @return {!goog.vec.Vec4.Float32} The new 3 element array.
  78. */
  79. goog.vec.Vec4.createFloat32FromArray = function(vec) {
  80. var newVec = goog.vec.Vec4.createFloat32();
  81. goog.vec.Vec4.setFromArray(newVec, vec);
  82. return newVec;
  83. };
  84. /**
  85. * Creates a new 4 element Float32 vector initialized with the supplied values.
  86. *
  87. * @param {number} v0 The value for element at index 0.
  88. * @param {number} v1 The value for element at index 1.
  89. * @param {number} v2 The value for element at index 2.
  90. * @param {number} v3 The value for element at index 3.
  91. * @return {!goog.vec.Vec4.Float32} The new vector.
  92. */
  93. goog.vec.Vec4.createFloat32FromValues = function(v0, v1, v2, v3) {
  94. var vec = goog.vec.Vec4.createFloat32();
  95. goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
  96. return vec;
  97. };
  98. /**
  99. * Creates a clone of the given 4 element Float32 vector.
  100. *
  101. * @param {goog.vec.Vec4.Float32} vec The source 3 element vector.
  102. * @return {!goog.vec.Vec4.Float32} The new cloned vector.
  103. */
  104. goog.vec.Vec4.cloneFloat32 = goog.vec.Vec4.createFloat32FromArray;
  105. /**
  106. * Creates a new 4 element Float64 vector initialized with the value from the
  107. * given array.
  108. *
  109. * @param {goog.vec.Vec4.AnyType} vec The source 4 element array.
  110. * @return {!goog.vec.Vec4.Float64} The new 4 element array.
  111. */
  112. goog.vec.Vec4.createFloat64FromArray = function(vec) {
  113. var newVec = goog.vec.Vec4.createFloat64();
  114. goog.vec.Vec4.setFromArray(newVec, vec);
  115. return newVec;
  116. };
  117. /**
  118. * Creates a new 4 element Float64 vector initialized with the supplied values.
  119. *
  120. * @param {number} v0 The value for element at index 0.
  121. * @param {number} v1 The value for element at index 1.
  122. * @param {number} v2 The value for element at index 2.
  123. * @param {number} v3 The value for element at index 3.
  124. * @return {!goog.vec.Vec4.Float64} The new vector.
  125. */
  126. goog.vec.Vec4.createFloat64FromValues = function(v0, v1, v2, v3) {
  127. var vec = goog.vec.Vec4.createFloat64();
  128. goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
  129. return vec;
  130. };
  131. /**
  132. * Creates a clone of the given 4 element vector.
  133. *
  134. * @param {goog.vec.Vec4.Float64} vec The source 4 element vector.
  135. * @return {!goog.vec.Vec4.Float64} The new cloned vector.
  136. */
  137. goog.vec.Vec4.cloneFloat64 = goog.vec.Vec4.createFloat64FromArray;
  138. /**
  139. * Creates a new 4 element vector initialized with the supplied values.
  140. *
  141. * @deprecated Use createFloat32FromValues.
  142. * @param {number} v0 The value for element at index 0.
  143. * @param {number} v1 The value for element at index 1.
  144. * @param {number} v2 The value for element at index 2.
  145. * @param {number} v3 The value for element at index 3.
  146. * @return {!goog.vec.Vec4.Type} The new vector.
  147. */
  148. goog.vec.Vec4.createFromValues = function(v0, v1, v2, v3) {
  149. var vec = goog.vec.Vec4.create();
  150. goog.vec.Vec4.setFromValues(vec, v0, v1, v2, v3);
  151. return vec;
  152. };
  153. /**
  154. * Creates a clone of the given 4 element vector.
  155. *
  156. * @deprecated Use cloneFloat32.
  157. * @param {goog.vec.Vec4.Vec4Like} vec The source 4 element vector.
  158. * @return {!goog.vec.Vec4.Type} The new cloned vector.
  159. */
  160. goog.vec.Vec4.clone = goog.vec.Vec4.createFromArray;
  161. /**
  162. * Initializes the vector with the given values.
  163. *
  164. * @param {goog.vec.Vec4.AnyType} vec The vector to receive the values.
  165. * @param {number} v0 The value for element at index 0.
  166. * @param {number} v1 The value for element at index 1.
  167. * @param {number} v2 The value for element at index 2.
  168. * @param {number} v3 The value for element at index 3.
  169. * @return {!goog.vec.Vec4.AnyType} Return vec so that operations can be
  170. * chained together.
  171. */
  172. goog.vec.Vec4.setFromValues = function(vec, v0, v1, v2, v3) {
  173. vec[0] = v0;
  174. vec[1] = v1;
  175. vec[2] = v2;
  176. vec[3] = v3;
  177. return vec;
  178. };
  179. /**
  180. * Initializes the vector with the given array of values.
  181. *
  182. * @param {goog.vec.Vec4.AnyType} vec The vector to receive the
  183. * values.
  184. * @param {goog.vec.Vec4.AnyType} values The array of values.
  185. * @return {!goog.vec.Vec4.AnyType} Return vec so that operations can be
  186. * chained together.
  187. */
  188. goog.vec.Vec4.setFromArray = function(vec, values) {
  189. vec[0] = values[0];
  190. vec[1] = values[1];
  191. vec[2] = values[2];
  192. vec[3] = values[3];
  193. return vec;
  194. };
  195. /**
  196. * Performs a component-wise addition of vec0 and vec1 together storing the
  197. * result into resultVec.
  198. *
  199. * @param {goog.vec.Vec4.AnyType} vec0 The first addend.
  200. * @param {goog.vec.Vec4.AnyType} vec1 The second addend.
  201. * @param {goog.vec.Vec4.AnyType} resultVec The vector to
  202. * receive the result. May be vec0 or vec1.
  203. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  204. * chained together.
  205. */
  206. goog.vec.Vec4.add = function(vec0, vec1, resultVec) {
  207. resultVec[0] = vec0[0] + vec1[0];
  208. resultVec[1] = vec0[1] + vec1[1];
  209. resultVec[2] = vec0[2] + vec1[2];
  210. resultVec[3] = vec0[3] + vec1[3];
  211. return resultVec;
  212. };
  213. /**
  214. * Performs a component-wise subtraction of vec1 from vec0 storing the
  215. * result into resultVec.
  216. *
  217. * @param {goog.vec.Vec4.AnyType} vec0 The minuend.
  218. * @param {goog.vec.Vec4.AnyType} vec1 The subtrahend.
  219. * @param {goog.vec.Vec4.AnyType} resultVec The vector to
  220. * receive the result. May be vec0 or vec1.
  221. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  222. * chained together.
  223. */
  224. goog.vec.Vec4.subtract = function(vec0, vec1, resultVec) {
  225. resultVec[0] = vec0[0] - vec1[0];
  226. resultVec[1] = vec0[1] - vec1[1];
  227. resultVec[2] = vec0[2] - vec1[2];
  228. resultVec[3] = vec0[3] - vec1[3];
  229. return resultVec;
  230. };
  231. /**
  232. * Negates vec0, storing the result into resultVec.
  233. *
  234. * @param {goog.vec.Vec4.AnyType} vec0 The vector to negate.
  235. * @param {goog.vec.Vec4.AnyType} resultVec The vector to
  236. * receive the result. May be vec0.
  237. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  238. * chained together.
  239. */
  240. goog.vec.Vec4.negate = function(vec0, resultVec) {
  241. resultVec[0] = -vec0[0];
  242. resultVec[1] = -vec0[1];
  243. resultVec[2] = -vec0[2];
  244. resultVec[3] = -vec0[3];
  245. return resultVec;
  246. };
  247. /**
  248. * Takes the absolute value of each component of vec0 storing the result in
  249. * resultVec.
  250. *
  251. * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
  252. * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the result.
  253. * May be vec0.
  254. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  255. * chained together.
  256. */
  257. goog.vec.Vec4.abs = function(vec0, resultVec) {
  258. resultVec[0] = Math.abs(vec0[0]);
  259. resultVec[1] = Math.abs(vec0[1]);
  260. resultVec[2] = Math.abs(vec0[2]);
  261. resultVec[3] = Math.abs(vec0[3]);
  262. return resultVec;
  263. };
  264. /**
  265. * Multiplies each component of vec0 with scalar storing the product into
  266. * resultVec.
  267. *
  268. * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
  269. * @param {number} scalar The value to multiply with each component of vec0.
  270. * @param {goog.vec.Vec4.AnyType} resultVec The vector to
  271. * receive the result. May be vec0.
  272. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  273. * chained together.
  274. */
  275. goog.vec.Vec4.scale = function(vec0, scalar, resultVec) {
  276. resultVec[0] = vec0[0] * scalar;
  277. resultVec[1] = vec0[1] * scalar;
  278. resultVec[2] = vec0[2] * scalar;
  279. resultVec[3] = vec0[3] * scalar;
  280. return resultVec;
  281. };
  282. /**
  283. * Returns the magnitudeSquared of the given vector.
  284. *
  285. * @param {goog.vec.Vec4.AnyType} vec0 The vector.
  286. * @return {number} The magnitude of the vector.
  287. */
  288. goog.vec.Vec4.magnitudeSquared = function(vec0) {
  289. var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
  290. return x * x + y * y + z * z + w * w;
  291. };
  292. /**
  293. * Returns the magnitude of the given vector.
  294. *
  295. * @param {goog.vec.Vec4.AnyType} vec0 The vector.
  296. * @return {number} The magnitude of the vector.
  297. */
  298. goog.vec.Vec4.magnitude = function(vec0) {
  299. var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
  300. return Math.sqrt(x * x + y * y + z * z + w * w);
  301. };
  302. /**
  303. * Normalizes the given vector storing the result into resultVec.
  304. *
  305. * @param {goog.vec.Vec4.AnyType} vec0 The vector to normalize.
  306. * @param {goog.vec.Vec4.AnyType} resultVec The vector to
  307. * receive the result. May be vec0.
  308. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  309. * chained together.
  310. */
  311. goog.vec.Vec4.normalize = function(vec0, resultVec) {
  312. var ilen = 1 / goog.vec.Vec4.magnitude(vec0);
  313. resultVec[0] = vec0[0] * ilen;
  314. resultVec[1] = vec0[1] * ilen;
  315. resultVec[2] = vec0[2] * ilen;
  316. resultVec[3] = vec0[3] * ilen;
  317. return resultVec;
  318. };
  319. /**
  320. * Returns the scalar product of vectors v0 and v1.
  321. *
  322. * @param {goog.vec.Vec4.AnyType} v0 The first vector.
  323. * @param {goog.vec.Vec4.AnyType} v1 The second vector.
  324. * @return {number} The scalar product.
  325. */
  326. goog.vec.Vec4.dot = function(v0, v1) {
  327. return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2] + v0[3] * v1[3];
  328. };
  329. /**
  330. * Linearly interpolate from v0 to v1 according to f. The value of f should be
  331. * in the range [0..1] otherwise the results are undefined.
  332. *
  333. * @param {goog.vec.Vec4.AnyType} v0 The first vector.
  334. * @param {goog.vec.Vec4.AnyType} v1 The second vector.
  335. * @param {number} f The interpolation factor.
  336. * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the
  337. * results (may be v0 or v1).
  338. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  339. * chained together.
  340. */
  341. goog.vec.Vec4.lerp = function(v0, v1, f, resultVec) {
  342. var x = v0[0], y = v0[1], z = v0[2], w = v0[3];
  343. resultVec[0] = (v1[0] - x) * f + x;
  344. resultVec[1] = (v1[1] - y) * f + y;
  345. resultVec[2] = (v1[2] - z) * f + z;
  346. resultVec[3] = (v1[3] - w) * f + w;
  347. return resultVec;
  348. };
  349. /**
  350. * Compares the components of vec0 with the components of another vector or
  351. * scalar, storing the larger values in resultVec.
  352. *
  353. * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
  354. * @param {goog.vec.Vec4.AnyType|number} limit The limit vector or scalar.
  355. * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the
  356. * results (may be vec0 or limit).
  357. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  358. * chained together.
  359. */
  360. goog.vec.Vec4.max = function(vec0, limit, resultVec) {
  361. if (typeof limit === 'number') {
  362. resultVec[0] = Math.max(vec0[0], limit);
  363. resultVec[1] = Math.max(vec0[1], limit);
  364. resultVec[2] = Math.max(vec0[2], limit);
  365. resultVec[3] = Math.max(vec0[3], limit);
  366. } else {
  367. resultVec[0] = Math.max(vec0[0], limit[0]);
  368. resultVec[1] = Math.max(vec0[1], limit[1]);
  369. resultVec[2] = Math.max(vec0[2], limit[2]);
  370. resultVec[3] = Math.max(vec0[3], limit[3]);
  371. }
  372. return resultVec;
  373. };
  374. /**
  375. * Compares the components of vec0 with the components of another vector or
  376. * scalar, storing the smaller values in resultVec.
  377. *
  378. * @param {goog.vec.Vec4.AnyType} vec0 The source vector.
  379. * @param {goog.vec.Vec4.AnyType|number} limit The limit vector or scalar.
  380. * @param {goog.vec.Vec4.AnyType} resultVec The vector to receive the
  381. * results (may be vec0 or limit).
  382. * @return {!goog.vec.Vec4.AnyType} Return resultVec so that operations can be
  383. * chained together.
  384. */
  385. goog.vec.Vec4.min = function(vec0, limit, resultVec) {
  386. if (typeof limit === 'number') {
  387. resultVec[0] = Math.min(vec0[0], limit);
  388. resultVec[1] = Math.min(vec0[1], limit);
  389. resultVec[2] = Math.min(vec0[2], limit);
  390. resultVec[3] = Math.min(vec0[3], limit);
  391. } else {
  392. resultVec[0] = Math.min(vec0[0], limit[0]);
  393. resultVec[1] = Math.min(vec0[1], limit[1]);
  394. resultVec[2] = Math.min(vec0[2], limit[2]);
  395. resultVec[3] = Math.min(vec0[3], limit[3]);
  396. }
  397. return resultVec;
  398. };
  399. /**
  400. * Returns true if the components of v0 are equal to the components of v1.
  401. *
  402. * @param {goog.vec.Vec4.AnyType} v0 The first vector.
  403. * @param {goog.vec.Vec4.AnyType} v1 The second vector.
  404. * @return {boolean} True if the vectors are equal, false otherwise.
  405. */
  406. goog.vec.Vec4.equals = function(v0, v1) {
  407. return v0.length == v1.length && v0[0] == v1[0] && v0[1] == v1[1] &&
  408. v0[2] == v1[2] && v0[3] == v1[3];
  409. };