vec3.js 16 KB

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