vec3d.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  7. // //
  8. // Any edits to this file must be applied to vec3f.js by running: //
  9. // swap_type.sh vec3d.js > vec3f.js //
  10. // //
  11. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  12. /**
  13. * @fileoverview Provides functions for operating on 3 element double (64bit)
  14. * vectors.
  15. *
  16. * The last parameter will typically be the output object and an object
  17. * can be both an input and output parameter to all methods except where
  18. * noted.
  19. *
  20. * See the README for notes about the design and structure of the API
  21. * (especially related to performance).
  22. */
  23. goog.provide('goog.vec.vec3d');
  24. goog.provide('goog.vec.vec3d.Type');
  25. /** @suppress {extraRequire} */
  26. goog.require('goog.vec');
  27. /** @typedef {!goog.vec.Float64} */ goog.vec.vec3d.Type;
  28. /**
  29. * Creates a vec3d with all elements initialized to zero.
  30. *
  31. * @return {!goog.vec.vec3d.Type} The new vec3d.
  32. */
  33. goog.vec.vec3d.create = function() {
  34. return new Float64Array(3);
  35. };
  36. /**
  37. * Creates a new vec3d initialized with the value from the given array.
  38. *
  39. * @param {!Array<number>} vec The source 3 element array.
  40. * @return {!goog.vec.vec3d.Type} The new vec3d.
  41. */
  42. goog.vec.vec3d.createFromArray = function(vec) {
  43. var newVec = goog.vec.vec3d.create();
  44. goog.vec.vec3d.setFromArray(newVec, vec);
  45. return newVec;
  46. };
  47. /**
  48. * Creates a new vec3d initialized with the supplied values.
  49. *
  50. * @param {number} v0 The value for element at index 0.
  51. * @param {number} v1 The value for element at index 1.
  52. * @param {number} v2 The value for element at index 2.
  53. * @return {!goog.vec.vec3d.Type} The new vector.
  54. */
  55. goog.vec.vec3d.createFromValues = function(v0, v1, v2) {
  56. var vec = goog.vec.vec3d.create();
  57. goog.vec.vec3d.setFromValues(vec, v0, v1, v2);
  58. return vec;
  59. };
  60. /**
  61. * Creates a clone of the given vec3d.
  62. *
  63. * @param {!goog.vec.vec3d.Type} vec The source vec3d.
  64. * @return {!goog.vec.vec3d.Type} The new cloned vec3d.
  65. */
  66. goog.vec.vec3d.clone = function(vec) {
  67. var newVec = goog.vec.vec3d.create();
  68. goog.vec.vec3d.setFromVec3d(newVec, vec);
  69. return newVec;
  70. };
  71. /**
  72. * Initializes the vector with the given values.
  73. *
  74. * @param {!goog.vec.vec3d.Type} vec The vector to receive the values.
  75. * @param {number} v0 The value for element at index 0.
  76. * @param {number} v1 The value for element at index 1.
  77. * @param {number} v2 The value for element at index 2.
  78. * @return {!goog.vec.vec3d.Type} Return vec so that operations can be
  79. * chained together.
  80. */
  81. goog.vec.vec3d.setFromValues = function(vec, v0, v1, v2) {
  82. vec[0] = v0;
  83. vec[1] = v1;
  84. vec[2] = v2;
  85. return vec;
  86. };
  87. /**
  88. * Initializes vec3d vec from vec3d src.
  89. *
  90. * @param {!goog.vec.vec3d.Type} vec The destination vector.
  91. * @param {!goog.vec.vec3d.Type} src The source vector.
  92. * @return {!goog.vec.vec3d.Type} Return vec so that operations can be
  93. * chained together.
  94. */
  95. goog.vec.vec3d.setFromVec3d = function(vec, src) {
  96. vec[0] = src[0];
  97. vec[1] = src[1];
  98. vec[2] = src[2];
  99. return vec;
  100. };
  101. /**
  102. * Initializes vec3d vec from vec3f src (typed as a Float32Array to
  103. * avoid circular goog.requires).
  104. *
  105. * @param {!goog.vec.vec3d.Type} vec The destination vector.
  106. * @param {Float32Array} src The source vector.
  107. * @return {!goog.vec.vec3d.Type} Return vec so that operations can be
  108. * chained together.
  109. */
  110. goog.vec.vec3d.setFromVec3f = function(vec, src) {
  111. vec[0] = src[0];
  112. vec[1] = src[1];
  113. vec[2] = src[2];
  114. return vec;
  115. };
  116. /**
  117. * Initializes vec3d vec from Array src.
  118. *
  119. * @param {!goog.vec.vec3d.Type} vec The destination vector.
  120. * @param {Array<number>} src The source vector.
  121. * @return {!goog.vec.vec3d.Type} Return vec so that operations can be
  122. * chained together.
  123. */
  124. goog.vec.vec3d.setFromArray = function(vec, src) {
  125. vec[0] = src[0];
  126. vec[1] = src[1];
  127. vec[2] = src[2];
  128. return vec;
  129. };
  130. /**
  131. * Performs a component-wise addition of vec0 and vec1 together storing the
  132. * result into resultVec.
  133. *
  134. * @param {!goog.vec.vec3d.Type} vec0 The first addend.
  135. * @param {!goog.vec.vec3d.Type} vec1 The second addend.
  136. * @param {!goog.vec.vec3d.Type} resultVec The vector to
  137. * receive the result. May be vec0 or vec1.
  138. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  139. * chained together.
  140. */
  141. goog.vec.vec3d.add = function(vec0, vec1, resultVec) {
  142. resultVec[0] = vec0[0] + vec1[0];
  143. resultVec[1] = vec0[1] + vec1[1];
  144. resultVec[2] = vec0[2] + vec1[2];
  145. return resultVec;
  146. };
  147. /**
  148. * Performs a component-wise subtraction of vec1 from vec0 storing the
  149. * result into resultVec.
  150. *
  151. * @param {!goog.vec.vec3d.Type} vec0 The minuend.
  152. * @param {!goog.vec.vec3d.Type} vec1 The subtrahend.
  153. * @param {!goog.vec.vec3d.Type} resultVec The vector to
  154. * receive the result. May be vec0 or vec1.
  155. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  156. * chained together.
  157. */
  158. goog.vec.vec3d.subtract = function(vec0, vec1, resultVec) {
  159. resultVec[0] = vec0[0] - vec1[0];
  160. resultVec[1] = vec0[1] - vec1[1];
  161. resultVec[2] = vec0[2] - vec1[2];
  162. return resultVec;
  163. };
  164. /**
  165. * Negates vec0, storing the result into resultVec.
  166. *
  167. * @param {!goog.vec.vec3d.Type} vec0 The vector to negate.
  168. * @param {!goog.vec.vec3d.Type} resultVec The vector to
  169. * receive the result. May be vec0.
  170. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  171. * chained together.
  172. */
  173. goog.vec.vec3d.negate = function(vec0, resultVec) {
  174. resultVec[0] = -vec0[0];
  175. resultVec[1] = -vec0[1];
  176. resultVec[2] = -vec0[2];
  177. return resultVec;
  178. };
  179. /**
  180. * Takes the absolute value of each component of vec0 storing the result in
  181. * resultVec.
  182. *
  183. * @param {!goog.vec.vec3d.Type} vec0 The source vector.
  184. * @param {!goog.vec.vec3d.Type} resultVec The vector to receive the result.
  185. * May be vec0.
  186. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  187. * chained together.
  188. */
  189. goog.vec.vec3d.abs = function(vec0, resultVec) {
  190. resultVec[0] = Math.abs(vec0[0]);
  191. resultVec[1] = Math.abs(vec0[1]);
  192. resultVec[2] = Math.abs(vec0[2]);
  193. return resultVec;
  194. };
  195. /**
  196. * Multiplies each component of vec0 with scalar storing the product into
  197. * resultVec.
  198. *
  199. * @param {!goog.vec.vec3d.Type} vec0 The source vector.
  200. * @param {number} scalar The value to multiply with each component of vec0.
  201. * @param {!goog.vec.vec3d.Type} resultVec The vector to
  202. * receive the result. May be vec0.
  203. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  204. * chained together.
  205. */
  206. goog.vec.vec3d.scale = function(vec0, scalar, resultVec) {
  207. resultVec[0] = vec0[0] * scalar;
  208. resultVec[1] = vec0[1] * scalar;
  209. resultVec[2] = vec0[2] * scalar;
  210. return resultVec;
  211. };
  212. /**
  213. * Returns the magnitudeSquared of the given vector.
  214. *
  215. * @param {!goog.vec.vec3d.Type} vec0 The vector.
  216. * @return {number} The magnitude of the vector.
  217. */
  218. goog.vec.vec3d.magnitudeSquared = function(vec0) {
  219. var x = vec0[0], y = vec0[1], z = vec0[2];
  220. return x * x + y * y + z * z;
  221. };
  222. /**
  223. * Returns the magnitude of the given vector.
  224. *
  225. * @param {!goog.vec.vec3d.Type} vec0 The vector.
  226. * @return {number} The magnitude of the vector.
  227. */
  228. goog.vec.vec3d.magnitude = function(vec0) {
  229. var x = vec0[0], y = vec0[1], z = vec0[2];
  230. return Math.sqrt(x * x + y * y + z * z);
  231. };
  232. /**
  233. * Normalizes the given vector storing the result into resultVec.
  234. *
  235. * @param {!goog.vec.vec3d.Type} vec0 The vector to normalize.
  236. * @param {!goog.vec.vec3d.Type} resultVec The vector to
  237. * receive the result. May be vec0.
  238. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  239. * chained together.
  240. */
  241. goog.vec.vec3d.normalize = function(vec0, resultVec) {
  242. var x = vec0[0], y = vec0[1], z = vec0[2];
  243. var ilen = 1 / Math.sqrt(x * x + y * y + z * z);
  244. resultVec[0] = x * ilen;
  245. resultVec[1] = y * ilen;
  246. resultVec[2] = z * ilen;
  247. return resultVec;
  248. };
  249. /**
  250. * Returns the scalar product of vectors v0 and v1.
  251. *
  252. * @param {!goog.vec.vec3d.Type} v0 The first vector.
  253. * @param {!goog.vec.vec3d.Type} v1 The second vector.
  254. * @return {number} The scalar product.
  255. */
  256. goog.vec.vec3d.dot = function(v0, v1) {
  257. return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
  258. };
  259. /**
  260. * Computes the vector (cross) product of v0 and v1 storing the result into
  261. * resultVec.
  262. *
  263. * @param {!goog.vec.vec3d.Type} v0 The first vector.
  264. * @param {!goog.vec.vec3d.Type} v1 The second vector.
  265. * @param {!goog.vec.vec3d.Type} resultVec The vector to receive the
  266. * results. May be either v0 or v1.
  267. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  268. * chained together.
  269. */
  270. goog.vec.vec3d.cross = function(v0, v1, resultVec) {
  271. var x0 = v0[0], y0 = v0[1], z0 = v0[2];
  272. var x1 = v1[0], y1 = v1[1], z1 = v1[2];
  273. resultVec[0] = y0 * z1 - z0 * y1;
  274. resultVec[1] = z0 * x1 - x0 * z1;
  275. resultVec[2] = x0 * y1 - y0 * x1;
  276. return resultVec;
  277. };
  278. /**
  279. * Returns the squared distance between two points.
  280. *
  281. * @param {!goog.vec.vec3d.Type} vec0 First point.
  282. * @param {!goog.vec.vec3d.Type} vec1 Second point.
  283. * @return {number} The squared distance between the points.
  284. */
  285. goog.vec.vec3d.distanceSquared = function(vec0, vec1) {
  286. var x = vec0[0] - vec1[0];
  287. var y = vec0[1] - vec1[1];
  288. var z = vec0[2] - vec1[2];
  289. return x * x + y * y + z * z;
  290. };
  291. /**
  292. * Returns the distance between two points.
  293. *
  294. * @param {!goog.vec.vec3d.Type} vec0 First point.
  295. * @param {!goog.vec.vec3d.Type} vec1 Second point.
  296. * @return {number} The distance between the points.
  297. */
  298. goog.vec.vec3d.distance = function(vec0, vec1) {
  299. return Math.sqrt(goog.vec.vec3d.distanceSquared(vec0, vec1));
  300. };
  301. /**
  302. * Returns a unit vector pointing from one point to another.
  303. * If the input points are equal then the result will be all zeros.
  304. *
  305. * @param {!goog.vec.vec3d.Type} vec0 Origin point.
  306. * @param {!goog.vec.vec3d.Type} vec1 Target point.
  307. * @param {!goog.vec.vec3d.Type} resultVec The vector to receive the
  308. * results (may be vec0 or vec1).
  309. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  310. * chained together.
  311. */
  312. goog.vec.vec3d.direction = function(vec0, vec1, resultVec) {
  313. var x = vec1[0] - vec0[0];
  314. var y = vec1[1] - vec0[1];
  315. var z = vec1[2] - vec0[2];
  316. var d = Math.sqrt(x * x + y * y + z * z);
  317. if (d) {
  318. d = 1 / d;
  319. resultVec[0] = x * d;
  320. resultVec[1] = y * d;
  321. resultVec[2] = z * d;
  322. } else {
  323. resultVec[0] = resultVec[1] = resultVec[2] = 0;
  324. }
  325. return resultVec;
  326. };
  327. /**
  328. * Linearly interpolate from vec0 to v1 according to f. The value of f should be
  329. * in the range [0..1] otherwise the results are undefined.
  330. *
  331. * @param {!goog.vec.vec3d.Type} v0 The first vector.
  332. * @param {!goog.vec.vec3d.Type} v1 The second vector.
  333. * @param {number} f The interpolation factor.
  334. * @param {!goog.vec.vec3d.Type} resultVec The vector to receive the
  335. * results (may be v0 or v1).
  336. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  337. * chained together.
  338. */
  339. goog.vec.vec3d.lerp = function(v0, v1, f, resultVec) {
  340. var x = v0[0], y = v0[1], z = v0[2];
  341. resultVec[0] = (v1[0] - x) * f + x;
  342. resultVec[1] = (v1[1] - y) * f + y;
  343. resultVec[2] = (v1[2] - z) * f + z;
  344. return resultVec;
  345. };
  346. /**
  347. * Perform a spherical linear interpolation from v0 to v1 according to f. The
  348. * value of f should be in the range [0..1] otherwise the results are undefined.
  349. *
  350. * Slerp is normally used to interpolate quaternions, but there is a geometric
  351. * formula for interpolating vectors directly, see "Geometric Slerp" in:
  352. * https://en.wikipedia.org/wiki/Slerp.
  353. *
  354. * This interpolates the vectors' directions via slerp, but linearly
  355. * interpolates the vectors' magnitudes.
  356. *
  357. * Results are undefined if v0 or v1 are of zero magnitude.
  358. *
  359. * @param {!goog.vec.vec3d.Type} v0 The first vector.
  360. * @param {!goog.vec.vec3d.Type} v1 The second vector.
  361. * @param {number} f The interpolation factor.
  362. * @param {!goog.vec.vec3d.Type} resultVec The vector to receive the
  363. * results (may be v0 or v1).
  364. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  365. * chained together.
  366. */
  367. goog.vec.vec3d.slerp = function(v0, v1, f, resultVec) {
  368. var v0Magnitude = goog.vec.vec3d.magnitude(v0);
  369. var v1Magnitude = goog.vec.vec3d.magnitude(v1);
  370. var cosAngle = goog.vec.vec3d.dot(v0, v1) / (v0Magnitude * v1Magnitude);
  371. // If v0 and v1 are almost the same direction, fall back on a straight lerp.
  372. if (cosAngle > 1 - goog.vec.EPSILON) {
  373. return goog.vec.vec3d.lerp(v0, v1, f, resultVec);
  374. }
  375. var angle = 0;
  376. var sinAngle = 0;
  377. // If v0 and v1 are opposite directions, pick an arbitrary 'mid' vector that
  378. // is perpendicular to both, and slerp from v0 -> mid -> v1.
  379. if (cosAngle < -1 + goog.vec.EPSILON) {
  380. var mid = goog.vec.vec3d.create();
  381. var magnitudeFactor = (v0Magnitude + v1Magnitude) / 2;
  382. if (v0[0]) { // v0 not parallel to [0,0,1].
  383. magnitudeFactor /= Math.sqrt(v0[0] * v0[0] + v0[1] + v0[1]);
  384. mid[0] = -v0[1] * magnitudeFactor;
  385. mid[1] = v0[0] * magnitudeFactor;
  386. mid[2] = 0;
  387. } else { // v0 not parallel to [1,0,0].
  388. magnitudeFactor /= Math.sqrt(v0[2] * v0[2] + v0[1] + v0[1]);
  389. mid[0] = 0;
  390. mid[1] = -v0[2] * magnitudeFactor;
  391. mid[2] = v0[1] * magnitudeFactor;
  392. }
  393. // Depending on f, slerp between either v0 and mid, or mid and v1.
  394. if (f <= 0.5) {
  395. v1Magnitude = v0Magnitude;
  396. v1 = mid;
  397. f *= 2;
  398. } else {
  399. v0 = mid;
  400. f = 2 * f - 1;
  401. }
  402. angle = Math.PI / 2;
  403. cosAngle = 0;
  404. sinAngle = 1;
  405. } else {
  406. angle = Math.acos(cosAngle);
  407. sinAngle = Math.sqrt(1 - cosAngle * cosAngle);
  408. }
  409. var coeff0 = (Math.sin((1 - f) * angle) / sinAngle) / v0Magnitude;
  410. var coeff1 = (Math.sin(f * angle) / sinAngle) / v1Magnitude;
  411. var magnitude = (1 - f) * v0Magnitude + f * v1Magnitude;
  412. resultVec[0] = (v0[0] * coeff0 + v1[0] * coeff1) * magnitude;
  413. resultVec[1] = (v0[1] * coeff0 + v1[1] * coeff1) * magnitude;
  414. resultVec[2] = (v0[2] * coeff0 + v1[2] * coeff1) * magnitude;
  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.vec3d.Type} vec0 The source vector.
  422. * @param {!goog.vec.vec3d.Type|number} limit The limit vector or scalar.
  423. * @param {!goog.vec.vec3d.Type} resultVec The vector to receive the
  424. * results (may be vec0 or limit).
  425. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  426. * chained together.
  427. */
  428. goog.vec.vec3d.max = function(vec0, limit, resultVec) {
  429. if (typeof limit === 'number') {
  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.vec3d.Type} vec0 The source vector.
  445. * @param {!goog.vec.vec3d.Type|number} limit The limit vector or scalar.
  446. * @param {!goog.vec.vec3d.Type} resultVec The vector to receive the
  447. * results (may be vec0 or limit).
  448. * @return {!goog.vec.vec3d.Type} Return resultVec so that operations can be
  449. * chained together.
  450. */
  451. goog.vec.vec3d.min = function(vec0, limit, resultVec) {
  452. if (typeof limit === 'number') {
  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.vec3d.Type} v0 The first vector.
  467. * @param {!goog.vec.vec3d.Type} v1 The second vector.
  468. * @return {boolean} True if the vectors are equal, false otherwise.
  469. */
  470. goog.vec.vec3d.equals = function(v0, v1) {
  471. return v0.length == v1.length && v0[0] == v1[0] && v0[1] == v1[1] &&
  472. v0[2] == v1[2];
  473. };