vec4f.js 12 KB

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