vec4.js 15 KB

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