vec4f.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  7. // //
  8. // Any edits to this file must be applied to vec4d.js by running: //
  9. // swap_type.sh vec4f.js > vec4d.js //
  10. // //
  11. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  12. /**
  13. * @fileoverview Provides functions for operating on 4 element float (32bit)
  14. * vectors.
  15. *
  16. * The last parameter will typically be the output object and an object
  17. * can be both an input and output parameter to all methods except where
  18. * noted.
  19. *
  20. * See the README for notes about the design and structure of the API
  21. * (especially related to performance).
  22. */
  23. goog.provide('goog.vec.vec4f');
  24. goog.provide('goog.vec.vec4f.Type');
  25. /** @suppress {extraRequire} */
  26. goog.require('goog.vec');
  27. /** @typedef {!goog.vec.Float32} */ goog.vec.vec4f.Type;
  28. /**
  29. * Creates a vec4f with all elements initialized to zero.
  30. *
  31. * @return {!goog.vec.vec4f.Type} The new vec4f.
  32. */
  33. goog.vec.vec4f.create = function() {
  34. return new Float32Array(4);
  35. };
  36. /**
  37. * Creates a new vec4f initialized with the value from the given array.
  38. *
  39. * @param {!Array<number>} vec The source 4 element array.
  40. * @return {!goog.vec.vec4f.Type} The new vec4f.
  41. */
  42. goog.vec.vec4f.createFromArray = function(vec) {
  43. var newVec = goog.vec.vec4f.create();
  44. goog.vec.vec4f.setFromArray(newVec, vec);
  45. return newVec;
  46. };
  47. /**
  48. * Creates a new vec4f initialized with the supplied values.
  49. *
  50. * @param {number} v0 The value for element at index 0.
  51. * @param {number} v1 The value for element at index 1.
  52. * @param {number} v2 The value for element at index 2.
  53. * @param {number} v3 The value for element at index 3.
  54. * @return {!goog.vec.vec4f.Type} The new vector.
  55. */
  56. goog.vec.vec4f.createFromValues = function(v0, v1, v2, v3) {
  57. var vec = goog.vec.vec4f.create();
  58. goog.vec.vec4f.setFromValues(vec, v0, v1, v2, v3);
  59. return vec;
  60. };
  61. /**
  62. * Creates a clone of the given vec4f.
  63. *
  64. * @param {!goog.vec.vec4f.Type} vec The source vec4f.
  65. * @return {!goog.vec.vec4f.Type} The new cloned vec4f.
  66. */
  67. goog.vec.vec4f.clone = function(vec) {
  68. var newVec = goog.vec.vec4f.create();
  69. goog.vec.vec4f.setFromVec4f(newVec, vec);
  70. return newVec;
  71. };
  72. /**
  73. * Initializes the vector with the given values.
  74. *
  75. * @param {!goog.vec.vec4f.Type} vec The vector to receive the values.
  76. * @param {number} v0 The value for element at index 0.
  77. * @param {number} v1 The value for element at index 1.
  78. * @param {number} v2 The value for element at index 2.
  79. * @param {number} v3 The value for element at index 3.
  80. * @return {!goog.vec.vec4f.Type} Return vec so that operations can be
  81. * chained together.
  82. */
  83. goog.vec.vec4f.setFromValues = function(vec, v0, v1, v2, v3) {
  84. vec[0] = v0;
  85. vec[1] = v1;
  86. vec[2] = v2;
  87. vec[3] = v3;
  88. return vec;
  89. };
  90. /**
  91. * Initializes vec4f vec from vec4f src.
  92. *
  93. * @param {!goog.vec.vec4f.Type} vec The destination vector.
  94. * @param {!goog.vec.vec4f.Type} src The source vector.
  95. * @return {!goog.vec.vec4f.Type} Return vec so that operations can be
  96. * chained together.
  97. */
  98. goog.vec.vec4f.setFromVec4f = function(vec, src) {
  99. vec[0] = src[0];
  100. vec[1] = src[1];
  101. vec[2] = src[2];
  102. vec[3] = src[3];
  103. return vec;
  104. };
  105. /**
  106. * Initializes vec4f vec from vec4d src (typed as a Float64Array to
  107. * avoid circular goog.requires).
  108. *
  109. * @param {!goog.vec.vec4f.Type} vec The destination vector.
  110. * @param {Float64Array} src The source vector.
  111. * @return {!goog.vec.vec4f.Type} Return vec so that operations can be
  112. * chained together.
  113. */
  114. goog.vec.vec4f.setFromVec4d = function(vec, src) {
  115. vec[0] = src[0];
  116. vec[1] = src[1];
  117. vec[2] = src[2];
  118. vec[3] = src[3];
  119. return vec;
  120. };
  121. /**
  122. * Initializes vec4f vec from Array src.
  123. *
  124. * @param {!goog.vec.vec4f.Type} vec The destination vector.
  125. * @param {Array<number>} src The source vector.
  126. * @return {!goog.vec.vec4f.Type} Return vec so that operations can be
  127. * chained together.
  128. */
  129. goog.vec.vec4f.setFromArray = function(vec, src) {
  130. vec[0] = src[0];
  131. vec[1] = src[1];
  132. vec[2] = src[2];
  133. vec[3] = src[3];
  134. return vec;
  135. };
  136. /**
  137. * Performs a component-wise addition of vec0 and vec1 together storing the
  138. * result into resultVec.
  139. *
  140. * @param {!goog.vec.vec4f.Type} vec0 The first addend.
  141. * @param {!goog.vec.vec4f.Type} vec1 The second addend.
  142. * @param {!goog.vec.vec4f.Type} resultVec The vector to
  143. * receive the result. May be vec0 or vec1.
  144. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  145. * chained together.
  146. */
  147. goog.vec.vec4f.add = function(vec0, vec1, resultVec) {
  148. resultVec[0] = vec0[0] + vec1[0];
  149. resultVec[1] = vec0[1] + vec1[1];
  150. resultVec[2] = vec0[2] + vec1[2];
  151. resultVec[3] = vec0[3] + vec1[3];
  152. return resultVec;
  153. };
  154. /**
  155. * Performs a component-wise subtraction of vec1 from vec0 storing the
  156. * result into resultVec.
  157. *
  158. * @param {!goog.vec.vec4f.Type} vec0 The minuend.
  159. * @param {!goog.vec.vec4f.Type} vec1 The subtrahend.
  160. * @param {!goog.vec.vec4f.Type} resultVec The vector to
  161. * receive the result. May be vec0 or vec1.
  162. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  163. * chained together.
  164. */
  165. goog.vec.vec4f.subtract = function(vec0, vec1, resultVec) {
  166. resultVec[0] = vec0[0] - vec1[0];
  167. resultVec[1] = vec0[1] - vec1[1];
  168. resultVec[2] = vec0[2] - vec1[2];
  169. resultVec[3] = vec0[3] - vec1[3];
  170. return resultVec;
  171. };
  172. /**
  173. * Negates vec0, storing the result into resultVec.
  174. *
  175. * @param {!goog.vec.vec4f.Type} vec0 The vector to negate.
  176. * @param {!goog.vec.vec4f.Type} resultVec The vector to
  177. * receive the result. May be vec0.
  178. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  179. * chained together.
  180. */
  181. goog.vec.vec4f.negate = function(vec0, resultVec) {
  182. resultVec[0] = -vec0[0];
  183. resultVec[1] = -vec0[1];
  184. resultVec[2] = -vec0[2];
  185. resultVec[3] = -vec0[3];
  186. return resultVec;
  187. };
  188. /**
  189. * Takes the absolute value of each component of vec0 storing the result in
  190. * resultVec.
  191. *
  192. * @param {!goog.vec.vec4f.Type} vec0 The source vector.
  193. * @param {!goog.vec.vec4f.Type} resultVec The vector to receive the result.
  194. * May be vec0.
  195. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  196. * chained together.
  197. */
  198. goog.vec.vec4f.abs = function(vec0, resultVec) {
  199. resultVec[0] = Math.abs(vec0[0]);
  200. resultVec[1] = Math.abs(vec0[1]);
  201. resultVec[2] = Math.abs(vec0[2]);
  202. resultVec[3] = Math.abs(vec0[3]);
  203. return resultVec;
  204. };
  205. /**
  206. * Multiplies each component of vec0 with scalar storing the product into
  207. * resultVec.
  208. *
  209. * @param {!goog.vec.vec4f.Type} vec0 The source vector.
  210. * @param {number} scalar The value to multiply with each component of vec0.
  211. * @param {!goog.vec.vec4f.Type} resultVec The vector to
  212. * receive the result. May be vec0.
  213. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  214. * chained together.
  215. */
  216. goog.vec.vec4f.scale = function(vec0, scalar, resultVec) {
  217. resultVec[0] = vec0[0] * scalar;
  218. resultVec[1] = vec0[1] * scalar;
  219. resultVec[2] = vec0[2] * scalar;
  220. resultVec[3] = vec0[3] * scalar;
  221. return resultVec;
  222. };
  223. /**
  224. * Returns the magnitudeSquared of the given vector.
  225. *
  226. * @param {!goog.vec.vec4f.Type} vec0 The vector.
  227. * @return {number} The magnitude of the vector.
  228. */
  229. goog.vec.vec4f.magnitudeSquared = function(vec0) {
  230. var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
  231. return x * x + y * y + z * z + w * w;
  232. };
  233. /**
  234. * Returns the magnitude of the given vector.
  235. *
  236. * @param {!goog.vec.vec4f.Type} vec0 The vector.
  237. * @return {number} The magnitude of the vector.
  238. */
  239. goog.vec.vec4f.magnitude = function(vec0) {
  240. var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
  241. return Math.sqrt(x * x + y * y + z * z + w * w);
  242. };
  243. /**
  244. * Normalizes the given vector storing the result into resultVec.
  245. *
  246. * @param {!goog.vec.vec4f.Type} vec0 The vector to normalize.
  247. * @param {!goog.vec.vec4f.Type} resultVec The vector to
  248. * receive the result. May be vec0.
  249. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  250. * chained together.
  251. */
  252. goog.vec.vec4f.normalize = function(vec0, resultVec) {
  253. var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
  254. var ilen = 1 / Math.sqrt(x * x + y * y + z * z + w * w);
  255. resultVec[0] = x * ilen;
  256. resultVec[1] = y * ilen;
  257. resultVec[2] = z * ilen;
  258. resultVec[3] = w * ilen;
  259. return resultVec;
  260. };
  261. /**
  262. * Returns the scalar product of vectors v0 and v1.
  263. *
  264. * @param {!goog.vec.vec4f.Type} v0 The first vector.
  265. * @param {!goog.vec.vec4f.Type} v1 The second vector.
  266. * @return {number} The scalar product.
  267. */
  268. goog.vec.vec4f.dot = function(v0, v1) {
  269. return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2] + v0[3] * v1[3];
  270. };
  271. /**
  272. * Linearly interpolate from v0 to v1 according to f. The value of f should be
  273. * in the range [0..1] otherwise the results are undefined.
  274. *
  275. * @param {!goog.vec.vec4f.Type} v0 The first vector.
  276. * @param {!goog.vec.vec4f.Type} v1 The second vector.
  277. * @param {number} f The interpolation factor.
  278. * @param {!goog.vec.vec4f.Type} resultVec The vector to receive the
  279. * results (may be v0 or v1).
  280. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  281. * chained together.
  282. */
  283. goog.vec.vec4f.lerp = function(v0, v1, f, resultVec) {
  284. var x = v0[0], y = v0[1], z = v0[2], w = v0[3];
  285. resultVec[0] = (v1[0] - x) * f + x;
  286. resultVec[1] = (v1[1] - y) * f + y;
  287. resultVec[2] = (v1[2] - z) * f + z;
  288. resultVec[3] = (v1[3] - w) * f + w;
  289. return resultVec;
  290. };
  291. /**
  292. * Compares the components of vec0 with the components of another vector or
  293. * scalar, storing the larger values in resultVec.
  294. *
  295. * @param {!goog.vec.vec4f.Type} vec0 The source vector.
  296. * @param {!goog.vec.vec4f.Type|number} limit The limit vector or scalar.
  297. * @param {!goog.vec.vec4f.Type} resultVec The vector to receive the
  298. * results (may be vec0 or limit).
  299. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  300. * chained together.
  301. */
  302. goog.vec.vec4f.max = function(vec0, limit, resultVec) {
  303. if (typeof limit === 'number') {
  304. resultVec[0] = Math.max(vec0[0], limit);
  305. resultVec[1] = Math.max(vec0[1], limit);
  306. resultVec[2] = Math.max(vec0[2], limit);
  307. resultVec[3] = Math.max(vec0[3], limit);
  308. } else {
  309. resultVec[0] = Math.max(vec0[0], limit[0]);
  310. resultVec[1] = Math.max(vec0[1], limit[1]);
  311. resultVec[2] = Math.max(vec0[2], limit[2]);
  312. resultVec[3] = Math.max(vec0[3], limit[3]);
  313. }
  314. return resultVec;
  315. };
  316. /**
  317. * Compares the components of vec0 with the components of another vector or
  318. * scalar, storing the smaller values in resultVec.
  319. *
  320. * @param {!goog.vec.vec4f.Type} vec0 The source vector.
  321. * @param {!goog.vec.vec4f.Type|number} limit The limit vector or scalar.
  322. * @param {!goog.vec.vec4f.Type} resultVec The vector to receive the
  323. * results (may be vec0 or limit).
  324. * @return {!goog.vec.vec4f.Type} Return resultVec so that operations can be
  325. * chained together.
  326. */
  327. goog.vec.vec4f.min = function(vec0, limit, resultVec) {
  328. if (typeof limit === 'number') {
  329. resultVec[0] = Math.min(vec0[0], limit);
  330. resultVec[1] = Math.min(vec0[1], limit);
  331. resultVec[2] = Math.min(vec0[2], limit);
  332. resultVec[3] = Math.min(vec0[3], limit);
  333. } else {
  334. resultVec[0] = Math.min(vec0[0], limit[0]);
  335. resultVec[1] = Math.min(vec0[1], limit[1]);
  336. resultVec[2] = Math.min(vec0[2], limit[2]);
  337. resultVec[3] = Math.min(vec0[3], limit[3]);
  338. }
  339. return resultVec;
  340. };
  341. /**
  342. * Returns true if the components of v0 are equal to the components of v1.
  343. *
  344. * @param {!goog.vec.vec4f.Type} v0 The first vector.
  345. * @param {!goog.vec.vec4f.Type} v1 The second vector.
  346. * @return {boolean} True if the vectors are equal, false otherwise.
  347. */
  348. goog.vec.vec4f.equals = function(v0, v1) {
  349. return v0.length == v1.length && v0[0] == v1[0] && v0[1] == v1[1] &&
  350. v0[2] == v1[2] && v0[3] == v1[3];
  351. };