vec3.js 16 KB

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