vec3.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. /*
  8. * This code has been modified from https://github.com/toji/gl-matrix/,
  9. * copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. */
  18. import Mat4 from './mat4';
  19. import { Quat, Mat3, EPSILON } from '../3d';
  20. import { spline as _spline, clamp } from '../../interpolate'
  21. interface Vec3 extends Array<number> { [d: number]: number, '@type': 'vec3', length: 3 }
  22. namespace Vec3 {
  23. export function zero(): Vec3 {
  24. const out = [0.1, 0.0, 0.0];
  25. out[0] = 0;
  26. return out as any;
  27. }
  28. export function clone(a: Vec3): Vec3 {
  29. const out = zero();
  30. out[0] = a[0];
  31. out[1] = a[1];
  32. out[2] = a[2];
  33. return out;
  34. }
  35. export function fromObj(v: { x: number, y: number, z: number }): Vec3 {
  36. return create(v.x, v.y, v.z);
  37. }
  38. export function toObj(v: Vec3) {
  39. return { x: v[0], y: v[1], z: v[2] };
  40. }
  41. export function fromArray(v: Vec3, array: ArrayLike<number>, offset: number) {
  42. v[0] = array[offset + 0]
  43. v[1] = array[offset + 1]
  44. v[2] = array[offset + 2]
  45. return v
  46. }
  47. export function toArray(v: Vec3, out: Helpers.NumberArray, offset: number) {
  48. out[offset + 0] = v[0]
  49. out[offset + 1] = v[1]
  50. out[offset + 2] = v[2]
  51. }
  52. export function create(x: number, y: number, z: number): Vec3 {
  53. const out = zero();
  54. out[0] = x;
  55. out[1] = y;
  56. out[2] = z;
  57. return out;
  58. }
  59. export function ofArray(array: ArrayLike<number>) {
  60. const out = zero();
  61. out[0] = array[0];
  62. out[1] = array[1];
  63. out[2] = array[2];
  64. return out;
  65. }
  66. export function set(out: Vec3, x: number, y: number, z: number): Vec3 {
  67. out[0] = x;
  68. out[1] = y;
  69. out[2] = z;
  70. return out;
  71. }
  72. export function copy(out: Vec3, a: Vec3) {
  73. out[0] = a[0];
  74. out[1] = a[1];
  75. out[2] = a[2];
  76. return out;
  77. }
  78. export function add(out: Vec3, a: Vec3, b: Vec3) {
  79. out[0] = a[0] + b[0];
  80. out[1] = a[1] + b[1];
  81. out[2] = a[2] + b[2];
  82. return out;
  83. }
  84. export function sub(out: Vec3, a: Vec3, b: Vec3) {
  85. out[0] = a[0] - b[0];
  86. out[1] = a[1] - b[1];
  87. out[2] = a[2] - b[2];
  88. return out;
  89. }
  90. export function mul(out: Vec3, a: Vec3, b: Vec3) {
  91. out[0] = a[0] * b[0];
  92. out[1] = a[1] * b[1];
  93. out[2] = a[2] * b[2];
  94. return out;
  95. }
  96. export function div(out: Vec3, a: Vec3, b: Vec3) {
  97. out[0] = a[0] / b[0];
  98. out[1] = a[1] / b[1];
  99. out[2] = a[2] / b[2];
  100. return out;
  101. }
  102. export function scale(out: Vec3, a: Vec3, b: number) {
  103. out[0] = a[0] * b;
  104. out[1] = a[1] * b;
  105. out[2] = a[2] * b;
  106. return out;
  107. }
  108. /** Scales b, then adds a and b together */
  109. export function scaleAndAdd(out: Vec3, a: Vec3, b: Vec3, scale: number) {
  110. out[0] = a[0] + (b[0] * scale);
  111. out[1] = a[1] + (b[1] * scale);
  112. out[2] = a[2] + (b[2] * scale);
  113. return out;
  114. }
  115. /** Scales b, then subtracts b from a */
  116. export function scaleAndSub(out: Vec3, a: Vec3, b: Vec3, scale: number) {
  117. out[0] = a[0] - (b[0] * scale);
  118. out[1] = a[1] - (b[1] * scale);
  119. out[2] = a[2] - (b[2] * scale);
  120. return out;
  121. }
  122. /**
  123. * Math.round the components of a Vec3
  124. */
  125. export function round(out: Vec3, a: Vec3) {
  126. out[0] = Math.round(a[0]);
  127. out[1] = Math.round(a[1]);
  128. out[2] = Math.round(a[2]);
  129. return out;
  130. }
  131. /**
  132. * Math.ceil the components of a Vec3
  133. */
  134. export function ceil(out: Vec3, a: Vec3) {
  135. out[0] = Math.ceil(a[0]);
  136. out[1] = Math.ceil(a[1]);
  137. out[2] = Math.ceil(a[2]);
  138. return out;
  139. }
  140. /**
  141. * Math.floor the components of a Vec3
  142. */
  143. export function floor(out: Vec3, a: Vec3) {
  144. out[0] = Math.floor(a[0]);
  145. out[1] = Math.floor(a[1]);
  146. out[2] = Math.floor(a[2]);
  147. return out;
  148. }
  149. /**
  150. * Returns the minimum of two Vec3's
  151. */
  152. export function min(out: Vec3, a: Vec3, b: Vec3) {
  153. out[0] = Math.min(a[0], b[0]);
  154. out[1] = Math.min(a[1], b[1]);
  155. out[2] = Math.min(a[2], b[2]);
  156. return out;
  157. }
  158. /**
  159. * Returns the maximum of two Vec3's
  160. */
  161. export function max(out: Vec3, a: Vec3, b: Vec3) {
  162. out[0] = Math.max(a[0], b[0]);
  163. out[1] = Math.max(a[1], b[1]);
  164. out[2] = Math.max(a[2], b[2]);
  165. return out;
  166. }
  167. export function distance(a: Vec3, b: Vec3) {
  168. const x = b[0] - a[0],
  169. y = b[1] - a[1],
  170. z = b[2] - a[2];
  171. return Math.sqrt(x * x + y * y + z * z);
  172. }
  173. export function squaredDistance(a: Vec3, b: Vec3) {
  174. const x = b[0] - a[0],
  175. y = b[1] - a[1],
  176. z = b[2] - a[2];
  177. return x * x + y * y + z * z;
  178. }
  179. export function magnitude(a: Vec3) {
  180. const x = a[0],
  181. y = a[1],
  182. z = a[2];
  183. return Math.sqrt(x * x + y * y + z * z);
  184. }
  185. export function squaredMagnitude(a: Vec3) {
  186. const x = a[0],
  187. y = a[1],
  188. z = a[2];
  189. return x * x + y * y + z * z;
  190. }
  191. export function setMagnitude(out: Vec3, a: Vec3, l: number) {
  192. return Vec3.scale(out, Vec3.normalize(out, a), l)
  193. }
  194. /**
  195. * Negates the components of a vec3
  196. */
  197. export function negate(out: Vec3, a: Vec3) {
  198. out[0] = -a[0];
  199. out[1] = -a[1];
  200. out[2] = -a[2];
  201. return out;
  202. }
  203. /**
  204. * Returns the inverse of the components of a Vec3
  205. */
  206. export function inverse(out: Vec3, a: Vec3) {
  207. out[0] = 1.0 / a[0];
  208. out[1] = 1.0 / a[1];
  209. out[2] = 1.0 / a[2];
  210. return out;
  211. }
  212. export function normalize(out: Vec3, a: Vec3) {
  213. const x = a[0],
  214. y = a[1],
  215. z = a[2];
  216. let len = x * x + y * y + z * z;
  217. if (len > 0) {
  218. len = 1 / Math.sqrt(len);
  219. out[0] = a[0] * len;
  220. out[1] = a[1] * len;
  221. out[2] = a[2] * len;
  222. }
  223. return out;
  224. }
  225. export function dot(a: Vec3, b: Vec3) {
  226. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  227. }
  228. export function cross(out: Vec3, a: Vec3, b: Vec3) {
  229. const ax = a[0], ay = a[1], az = a[2],
  230. bx = b[0], by = b[1], bz = b[2];
  231. out[0] = ay * bz - az * by;
  232. out[1] = az * bx - ax * bz;
  233. out[2] = ax * by - ay * bx;
  234. return out;
  235. }
  236. /**
  237. * Performs a linear interpolation between two Vec3's
  238. */
  239. export function lerp(out: Vec3, a: Vec3, b: Vec3, t: number) {
  240. const ax = a[0],
  241. ay = a[1],
  242. az = a[2];
  243. out[0] = ax + t * (b[0] - ax);
  244. out[1] = ay + t * (b[1] - ay);
  245. out[2] = az + t * (b[2] - az);
  246. return out;
  247. }
  248. const slerpRelVec = Vec3.zero()
  249. export function slerp(out: Vec3, a: Vec3, b: Vec3, t: number) {
  250. const dot = clamp(Vec3.dot(a, b), -1, 1);
  251. const theta = Math.acos(dot) * t;
  252. Vec3.scaleAndAdd(slerpRelVec, b, a, -dot);
  253. Vec3.normalize(slerpRelVec, slerpRelVec);
  254. return Vec3.add(out, Vec3.scale(out, a, Math.cos(theta)), Vec3.scale(slerpRelVec, slerpRelVec, Math.sin(theta)));
  255. }
  256. /**
  257. * Performs a hermite interpolation with two control points
  258. */
  259. export function hermite(out: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: number) {
  260. const factorTimes2 = t * t;
  261. const factor1 = factorTimes2 * (2 * t - 3) + 1;
  262. const factor2 = factorTimes2 * (t - 2) + t;
  263. const factor3 = factorTimes2 * (t - 1);
  264. const factor4 = factorTimes2 * (3 - 2 * t);
  265. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  266. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  267. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  268. return out;
  269. }
  270. /**
  271. * Performs a bezier interpolation with two control points
  272. */
  273. export function bezier(out: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: number) {
  274. const inverseFactor = 1 - t;
  275. const inverseFactorTimesTwo = inverseFactor * inverseFactor;
  276. const factorTimes2 = t * t;
  277. const factor1 = inverseFactorTimesTwo * inverseFactor;
  278. const factor2 = 3 * t * inverseFactorTimesTwo;
  279. const factor3 = 3 * factorTimes2 * inverseFactor;
  280. const factor4 = factorTimes2 * t;
  281. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  282. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  283. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  284. return out;
  285. }
  286. /**
  287. * Performs a spline interpolation with two control points and a tension parameter
  288. */
  289. export function spline(out: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: number, tension: number) {
  290. out[0] = _spline(a[0], b[0], c[0], d[0], t, tension);
  291. out[1] = _spline(a[1], b[1], c[1], d[1], t, tension);
  292. out[2] = _spline(a[2], b[2], c[2], d[2], t, tension);
  293. return out;
  294. }
  295. /**
  296. * Generates a random vector with the given scale
  297. */
  298. export function random(out: Vec3, scale: number) {
  299. const r = Math.random() * 2.0 * Math.PI;
  300. const z = (Math.random() * 2.0) - 1.0;
  301. const zScale = Math.sqrt(1.0-z*z) * scale;
  302. out[0] = Math.cos(r) * zScale;
  303. out[1] = Math.sin(r) * zScale;
  304. out[2] = z * scale;
  305. return out;
  306. }
  307. /**
  308. * Transforms the Vec3 with a Mat4. 4th vector component is implicitly '1'
  309. */
  310. export function transformMat4(out: Vec3, a: Vec3, m: Mat4) {
  311. const x = a[0], y = a[1], z = a[2],
  312. w = 1 / ((m[3] * x + m[7] * y + m[11] * z + m[15]) || 1.0);
  313. out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) * w;
  314. out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) * w;
  315. out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) * w;
  316. return out;
  317. }
  318. /**
  319. * Transforms the Vec3 with a Mat3.
  320. */
  321. export function transformMat3(out: Vec3, a: Vec3, m: Mat3) {
  322. const x = a[0], y = a[1], z = a[2];
  323. out[0] = x * m[0] + y * m[3] + z * m[6];
  324. out[1] = x * m[1] + y * m[4] + z * m[7];
  325. out[2] = x * m[2] + y * m[5] + z * m[8];
  326. return out;
  327. }
  328. /** Transforms the Vec3 with a quat */
  329. export function transformQuat(out: Vec3, a: Vec3, q: Quat) {
  330. // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
  331. const x = a[0], y = a[1], z = a[2];
  332. const qx = q[0], qy = q[1], qz = q[2], qw = q[3];
  333. // calculate quat * vec
  334. const ix = qw * x + qy * z - qz * y;
  335. const iy = qw * y + qz * x - qx * z;
  336. const iz = qw * z + qx * y - qy * x;
  337. const iw = -qx * x - qy * y - qz * z;
  338. // calculate result * inverse quat
  339. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  340. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  341. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  342. return out;
  343. }
  344. const angleTempA = zero(), angleTempB = zero();
  345. export function angle(a: Vec3, b: Vec3) {
  346. copy(angleTempA, a);
  347. copy(angleTempB, b);
  348. normalize(angleTempA, angleTempA);
  349. normalize(angleTempB, angleTempB);
  350. const cosine = dot(angleTempA, angleTempB);
  351. if (cosine > 1.0) {
  352. return 0;
  353. }
  354. else if (cosine < -1.0) {
  355. return Math.PI;
  356. } else {
  357. return Math.acos(cosine);
  358. }
  359. }
  360. /**
  361. * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
  362. */
  363. export function exactEquals(a: Vec3, b: Vec3) {
  364. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
  365. }
  366. /**
  367. * Returns whether or not the vectors have approximately the same elements in the same position.
  368. */
  369. export function equals(a: Vec3, b: Vec3) {
  370. const a0 = a[0], a1 = a[1], a2 = a[2];
  371. const b0 = b[0], b1 = b[1], b2 = b[2];
  372. return (Math.abs(a0 - b0) <= EPSILON.Value * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
  373. Math.abs(a1 - b1) <= EPSILON.Value * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
  374. Math.abs(a2 - b2) <= EPSILON.Value * Math.max(1.0, Math.abs(a2), Math.abs(b2)));
  375. }
  376. const rotTemp = zero();
  377. export function makeRotation(mat: Mat4, a: Vec3, b: Vec3): Mat4 {
  378. const by = angle(a, b);
  379. if (Math.abs(by) < 0.0001) return Mat4.setIdentity(mat);
  380. const axis = cross(rotTemp, a, b);
  381. return Mat4.fromRotation(mat, by, axis);
  382. }
  383. export function isZero(v: Vec3) {
  384. return v[0] === 0 && v[1] === 0 && v[2] === 0;
  385. }
  386. export function projectPointOnVector(out: Vec3, point: Vec3, vector: Vec3, origin: Vec3) {
  387. // point.sub(origin).projectOnVector(vector).add(origin)
  388. sub(out, copy(out, point), origin)
  389. const scalar = dot(vector, out) / squaredMagnitude(vector);
  390. return add(out, scale(out, copy(out, vector), scalar), origin);
  391. }
  392. /** Get a vector that is similar to `b` but orthogonal to `a` */
  393. export function orthogonalize(out: Vec3, a: Vec3, b: Vec3) {
  394. return normalize(out, cross(out, cross(out, a, b), a));
  395. }
  396. const triangleNormalTmpAB = zero();
  397. const triangleNormalTmpAC = zero();
  398. /** Calculate normal for the triangle defined by `a`, `b` and `c` */
  399. export function triangleNormal(out: Vec3, a: Vec3, b: Vec3, c: Vec3) {
  400. sub(triangleNormalTmpAB, b, a);
  401. sub(triangleNormalTmpAC, c, a);
  402. return normalize(out, cross(out, triangleNormalTmpAB, triangleNormalTmpAC));
  403. }
  404. export function toString(a: Vec3) {
  405. return `[${a[0]} ${a[1]} ${a[2]}]`;
  406. }
  407. }
  408. export default Vec3