vec3.js 14 KB

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