vec3f.js 17 KB

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