vec4.js 12 KB

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