vec3.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 } from '../3d';
  20. interface Vec3 extends Array<number> { [d: number]: number, '@type': 'vec3', length: 3 }
  21. namespace Vec3 {
  22. export function zero(): Vec3 {
  23. const out = [0.1, 0.0, 0.0];
  24. out[0] = 0;
  25. return out as any;
  26. }
  27. export function clone(a: Vec3): Vec3 {
  28. const out = zero();
  29. out[0] = a[0];
  30. out[1] = a[1];
  31. out[2] = a[2];
  32. return out;
  33. }
  34. export function fromObj(v: { x: number, y: number, z: number }): Vec3 {
  35. return create(v.x, v.y, v.z);
  36. }
  37. export function toObj(v: Vec3) {
  38. return { x: v[0], y: v[1], z: v[2] };
  39. }
  40. export function fromArray(v: Vec3, array: Helpers.NumberArray, offset: number) {
  41. v[0] = array[offset + 0]
  42. v[1] = array[offset + 1]
  43. v[2] = array[offset + 2]
  44. }
  45. export function toArray(v: Vec3, out: Helpers.NumberArray, offset: number) {
  46. out[offset + 0] = v[0]
  47. out[offset + 1] = v[1]
  48. out[offset + 2] = v[2]
  49. return v
  50. }
  51. export function create(x: number, y: number, z: number): Vec3 {
  52. const out = zero();
  53. out[0] = x;
  54. out[1] = y;
  55. out[2] = z;
  56. return out;
  57. }
  58. export function ofArray(array: ArrayLike<number>) {
  59. const out = zero();
  60. out[0] = array[0];
  61. out[1] = array[1];
  62. out[2] = array[2];
  63. return out;
  64. }
  65. export function set(out: Vec3, x: number, y: number, z: number): Vec3 {
  66. out[0] = x;
  67. out[1] = y;
  68. out[2] = z;
  69. return out;
  70. }
  71. export function copy(out: Vec3, a: Vec3) {
  72. out[0] = a[0];
  73. out[1] = a[1];
  74. out[2] = a[2];
  75. return out;
  76. }
  77. export function add(out: Vec3, a: Vec3, b: Vec3) {
  78. out[0] = a[0] + b[0];
  79. out[1] = a[1] + b[1];
  80. out[2] = a[2] + b[2];
  81. return out;
  82. }
  83. export function sub(out: Vec3, a: Vec3, b: Vec3) {
  84. out[0] = a[0] - b[0];
  85. out[1] = a[1] - b[1];
  86. out[2] = a[2] - b[2];
  87. return out;
  88. }
  89. export function mul(out: Vec3, a: Vec3, b: Vec3) {
  90. out[0] = a[0] * b[0];
  91. out[1] = a[1] * b[1];
  92. out[2] = a[2] * b[2];
  93. return out;
  94. }
  95. export function div(out: Vec3, a: Vec3, b: Vec3) {
  96. out[0] = a[0] / b[0];
  97. out[1] = a[1] / b[1];
  98. out[2] = a[2] / b[2];
  99. return out;
  100. }
  101. export function scale(out: Vec3, a: Vec3, b: number) {
  102. out[0] = a[0] * b;
  103. out[1] = a[1] * b;
  104. out[2] = a[2] * b;
  105. return out;
  106. }
  107. export function scaleAndAdd(out: Vec3, a: Vec3, b: Vec3, scale: number) {
  108. out[0] = a[0] + (b[0] * scale);
  109. out[1] = a[1] + (b[1] * scale);
  110. out[2] = a[2] + (b[2] * scale);
  111. return out;
  112. }
  113. /**
  114. * Math.round the components of a Vec3
  115. */
  116. export function round(out: Vec3, a: Vec3) {
  117. out[0] = Math.round(a[0]);
  118. out[1] = Math.round(a[1]);
  119. out[2] = Math.round(a[2]);
  120. return out;
  121. }
  122. /**
  123. * Math.ceil the components of a Vec3
  124. */
  125. export function ceil(out: Vec3, a: Vec3) {
  126. out[0] = Math.ceil(a[0]);
  127. out[1] = Math.ceil(a[1]);
  128. out[2] = Math.ceil(a[2]);
  129. return out;
  130. }
  131. /**
  132. * Math.floor the components of a Vec3
  133. */
  134. export function floor(out: Vec3, a: Vec3) {
  135. out[0] = Math.floor(a[0]);
  136. out[1] = Math.floor(a[1]);
  137. out[2] = Math.floor(a[2]);
  138. return out;
  139. }
  140. export function distance(a: Vec3, b: Vec3) {
  141. const x = b[0] - a[0],
  142. y = b[1] - a[1],
  143. z = b[2] - a[2];
  144. return Math.sqrt(x * x + y * y + z * z);
  145. }
  146. export function squaredDistance(a: Vec3, b: Vec3) {
  147. const x = b[0] - a[0],
  148. y = b[1] - a[1],
  149. z = b[2] - a[2];
  150. return x * x + y * y + z * z;
  151. }
  152. export function magnitude(a: Vec3) {
  153. const x = a[0],
  154. y = a[1],
  155. z = a[2];
  156. return Math.sqrt(x * x + y * y + z * z);
  157. }
  158. export function squaredMagnitude(a: Vec3) {
  159. const x = a[0],
  160. y = a[1],
  161. z = a[2];
  162. return x * x + y * y + z * z;
  163. }
  164. export function setMagnitude(out: Vec3, a: Vec3, l: number) {
  165. return Vec3.scale(out, Vec3.normalize(out, a), l)
  166. }
  167. /**
  168. * Returns the inverse of the components of a Vec3
  169. */
  170. export function inverse(out: Vec3, a: Vec3) {
  171. out[0] = 1.0 / a[0];
  172. out[1] = 1.0 / a[1];
  173. out[2] = 1.0 / a[2];
  174. return out;
  175. }
  176. export function normalize(out: Vec3, a: Vec3) {
  177. const x = a[0],
  178. y = a[1],
  179. z = a[2];
  180. let len = x * x + y * y + z * z;
  181. if (len > 0) {
  182. len = 1 / Math.sqrt(len);
  183. out[0] = a[0] * len;
  184. out[1] = a[1] * len;
  185. out[2] = a[2] * len;
  186. }
  187. return out;
  188. }
  189. export function dot(a: Vec3, b: Vec3) {
  190. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  191. }
  192. export function cross(out: Vec3, a: Vec3, b: Vec3) {
  193. const ax = a[0], ay = a[1], az = a[2],
  194. bx = b[0], by = b[1], bz = b[2];
  195. out[0] = ay * bz - az * by;
  196. out[1] = az * bx - ax * bz;
  197. out[2] = ax * by - ay * bx;
  198. return out;
  199. }
  200. /**
  201. * Performs a linear interpolation between two Vec3's
  202. */
  203. export function lerp(out: Vec3, a: Vec3, b: Vec3, t: number) {
  204. const ax = a[0],
  205. ay = a[1],
  206. az = a[2];
  207. out[0] = ax + t * (b[0] - ax);
  208. out[1] = ay + t * (b[1] - ay);
  209. out[2] = az + t * (b[2] - az);
  210. return out;
  211. }
  212. /**
  213. * Performs a hermite interpolation with two control points
  214. */
  215. export function hermite(out: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: number) {
  216. const factorTimes2 = t * t;
  217. const factor1 = factorTimes2 * (2 * t - 3) + 1;
  218. const factor2 = factorTimes2 * (t - 2) + t;
  219. const factor3 = factorTimes2 * (t - 1);
  220. const factor4 = factorTimes2 * (3 - 2 * t);
  221. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  222. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  223. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  224. return out;
  225. }
  226. /**
  227. * Performs a bezier interpolation with two control points
  228. */
  229. export function bezier(out: Vec3, a: Vec3, b: Vec3, c: Vec3, d: Vec3, t: number) {
  230. const inverseFactor = 1 - t;
  231. const inverseFactorTimesTwo = inverseFactor * inverseFactor;
  232. const factorTimes2 = t * t;
  233. const factor1 = inverseFactorTimesTwo * inverseFactor;
  234. const factor2 = 3 * t * inverseFactorTimesTwo;
  235. const factor3 = 3 * factorTimes2 * inverseFactor;
  236. const factor4 = factorTimes2 * t;
  237. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  238. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  239. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  240. return out;
  241. }
  242. /**
  243. * Generates a random vector with the given scale
  244. */
  245. export function random(out: Vec3, scale: number) {
  246. const r = Math.random() * 2.0 * Math.PI;
  247. const z = (Math.random() * 2.0) - 1.0;
  248. const zScale = Math.sqrt(1.0-z*z) * scale;
  249. out[0] = Math.cos(r) * zScale;
  250. out[1] = Math.sin(r) * zScale;
  251. out[2] = z * scale;
  252. return out;
  253. }
  254. /**
  255. * Transforms the Vec3 with a Mat4. 4th vector component is implicitly '1'
  256. */
  257. export function transformMat4(out: Vec3, a: Vec3, m: Mat4) {
  258. const x = a[0], y = a[1], z = a[2],
  259. w = 1 / ((m[3] * x + m[7] * y + m[11] * z + m[15]) || 1.0);
  260. out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) * w;
  261. out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) * w;
  262. out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) * w;
  263. return out;
  264. }
  265. /**
  266. * Transforms the Vec3 with a Mat3.
  267. */
  268. export function transformMat3(out: Vec3, a: Vec3, m: Mat3) {
  269. const x = a[0], y = a[1], z = a[2];
  270. out[0] = x * m[0] + y * m[3] + z * m[6];
  271. out[1] = x * m[1] + y * m[4] + z * m[7];
  272. out[2] = x * m[2] + y * m[5] + z * m[8];
  273. return out;
  274. }
  275. /** Transforms the Vec3 with a quat */
  276. export function transformQuat(out: Vec3, a: Vec3, q: Quat) {
  277. // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
  278. const x = a[0], y = a[1], z = a[2];
  279. const qx = q[0], qy = q[1], qz = q[2], qw = q[3];
  280. // calculate quat * vec
  281. const ix = qw * x + qy * z - qz * y;
  282. const iy = qw * y + qz * x - qx * z;
  283. const iz = qw * z + qx * y - qy * x;
  284. const iw = -qx * x - qy * y - qz * z;
  285. // calculate result * inverse quat
  286. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  287. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  288. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  289. return out;
  290. }
  291. const angleTempA = zero(), angleTempB = zero();
  292. export function angle(a: Vec3, b: Vec3) {
  293. copy(angleTempA, a);
  294. copy(angleTempB, b);
  295. normalize(angleTempA, angleTempA);
  296. normalize(angleTempB, angleTempB);
  297. const cosine = dot(angleTempA, angleTempB);
  298. if (cosine > 1.0) {
  299. return 0;
  300. }
  301. else if (cosine < -1.0) {
  302. return Math.PI;
  303. } else {
  304. return Math.acos(cosine);
  305. }
  306. }
  307. const rotTemp = zero();
  308. export function makeRotation(mat: Mat4, a: Vec3, b: Vec3): Mat4 {
  309. const by = angle(a, b);
  310. if (Math.abs(by) < 0.0001) return Mat4.setIdentity(mat);
  311. const axis = cross(rotTemp, a, b);
  312. return Mat4.fromRotation(mat, by, axis);
  313. }
  314. export function isZero(v: Vec3) {
  315. return v[0] === 0 && v[1] === 0 && v[2] === 0
  316. }
  317. export function toString(a: Vec3) {
  318. return `[${a[0]} ${a[1]} ${a[2]}]`;
  319. }
  320. }
  321. export default Vec3