Source: extern/math.js

  1. import register from "../util/register.js";
  2. import * as m_vec3 from "../libs/gl_matrix/vec3.js";
  3. import * as m_util from "../intern/util.js";
  4. import * as m_math from "../intern/math.js";
  5. /**
  6. * Auxiliary math methods. For math methods operating on vectors, matrices or quaternions see the
  7. * corresponding modules.
  8. * @module math
  9. * @local Plane
  10. */
  11. function Math(ns, exports) {
  12. var _vec3_tmp = new Float32Array(3);
  13. /**
  14. * Plane represented as vec3 normal and distance to the origin.
  15. * @typedef {Float32Array(4)} Plane
  16. */
  17. /**
  18. * Get the parametric line.
  19. * @method module:math.create_pline_from_points
  20. * @param {Vec3} point1 First point.
  21. * @param {Vec3} point2 Second point.
  22. * @returns {ParametricLine} Parametric line.
  23. */
  24. exports.create_pline_from_points = function(point1, point2) {
  25. var dest = new Float32Array(6);
  26. m_math.set_pline_initial_point(dest, point1);
  27. m_vec3.subtract(point2, point1, _vec3_tmp);
  28. m_math.set_pline_directional_vec(dest, _vec3_tmp);
  29. return dest;
  30. }
  31. /**
  32. * Get the parametric line.
  33. * @method module:math.create_pline_from_point_vec
  34. * @param {Vec3} point First point.
  35. * @param {Vec3} vec Directional vector.
  36. * @returns {ParametricLine} Parametric line.
  37. */
  38. exports.create_pline_from_point_vec = function(point, vec) {
  39. var dest = new Float32Array(6);
  40. m_math.set_pline_initial_point(dest, point);
  41. m_math.set_pline_directional_vec(dest, vec);
  42. return dest;
  43. }
  44. /**
  45. * Init the parametric line.
  46. * @method module:math.create_pline
  47. * @returns {ParametricLine} pline Parametric line.
  48. */
  49. exports.create_pline = function() {
  50. return new Float32Array(6);
  51. }
  52. /**
  53. * Get the parametric line directional vector.
  54. * @method module:math.get_pline_directional_vec
  55. * @param {ParametricLine} pline Parametric line.
  56. * @param {?Vec3} [dest=new Float32Array(3);] Destination vector.
  57. * @returns {?Vec3} Destination vector.
  58. */
  59. exports.get_pline_directional_vec = m_math.get_pline_directional_vec;
  60. /**
  61. * Get the parametric line initial point.
  62. * @method module:math.get_pline_initial_point
  63. * @param {ParametricLine} pline Parametric line.
  64. * @param {?Vec3} [dest=new Float32Array(3);] Destination point.
  65. * @returns {?Vec3} Destination point.
  66. */
  67. exports.get_pline_initial_point = m_math.get_pline_initial_point;
  68. /**
  69. * Set the parametric line initial point.
  70. * @method module:math.set_pline_initial_point
  71. * @param {ParametricLine} pline Parametric line.
  72. * @param {Vec3} vec3 Point.
  73. */
  74. exports.set_pline_initial_point = m_math.set_pline_initial_point;
  75. /**
  76. * Set the parametric line directional vector.
  77. * @method module:math.set_pline_directional_vec
  78. * @param {ParametricLine} pline Parametric line.
  79. * @param {Vec3} vec3 Vector.
  80. */
  81. exports.set_pline_directional_vec = m_math.set_pline_directional_vec;
  82. /**
  83. * Calculate intersection point of a line and a plane.
  84. * @method module:math.line_plane_intersect
  85. * @see Lengyel E. - Mathematics for 3D Game Programming and Computer Graphics,
  86. * Third Edition. Chapter 5.2.1 Intersection of a Line and a Plane
  87. * @param {Vec3} pn Plane normal.
  88. * @param {number} p_dist Plane signed distance from the origin.
  89. * @param {ParametricLine} pline Parametric line.
  90. * @param {Vec3} dest Destination vector.
  91. * @returns {?Vec3} Intersection point or null if the line is parallel to the plane.
  92. */
  93. exports.line_plane_intersect = m_util.line_plane_intersect
  94. /**
  95. * Get the coordinates of a certain point on the given parametric line.
  96. * @method module:math.calc_pline_point
  97. * @param {ParametricLine} pline Parametric line.
  98. * @param {number} t Parameter - distance from the line initial point to a certain point.
  99. * @param {?Vec3} [dest=new Float32Array(3);] Destination point.
  100. * @returns {?Vec3} Destination point.
  101. */
  102. exports.calc_pline_point = m_math.calc_pline_point;
  103. /**
  104. * Calculate distance from point to plane.
  105. * @method module:math.point_plane_dist
  106. * @param {Vec3} point Point.
  107. * @param {Plane} plane Plane.
  108. * @returns {number} Distance.
  109. */
  110. exports.point_plane_dist = m_math.point_plane_dist;
  111. /**
  112. * Interpolate value with no easing, no acceleration.
  113. * @method module:math.linear_tween
  114. * @param {number} t Current time.
  115. * @param {number} b Start value.
  116. * @param {number} c Change in value.
  117. * @param {number} d Duration.
  118. * @returns {number} Interpolated value.
  119. */
  120. exports.linear_tween = m_math.linear_tween;
  121. /**
  122. * Interpolate value with accelerating from zero velocity.
  123. * @method module:math.ease_in_quad
  124. * @param {number} t Current time.
  125. * @param {number} b Start value.
  126. * @param {number} c Change in value.
  127. * @param {number} d Duration.
  128. * @returns {number} Interpolated value.
  129. */
  130. exports.ease_in_quad = m_math.ease_in_quad;
  131. /**
  132. * Interpolate value with decelerating to zero velocity.
  133. * @method module:math.ease_out_quad
  134. * @param {number} t Current time.
  135. * @param {number} b Start value.
  136. * @param {number} c Change in value.
  137. * @param {number} d Duration.
  138. * @returns {number} Interpolated value.
  139. */
  140. exports.ease_out_quad = m_math.ease_out_quad;
  141. /**
  142. * Interpolate value with acceleration until halfway, then deceleration.
  143. * @method module:math.ease_in_out_quad
  144. * @param {number} t Current time.
  145. * @param {number} b Start value.
  146. * @param {number} c Change in value.
  147. * @param {number} d Duration.
  148. * @returns {number} Interpolated value.
  149. */
  150. exports.ease_in_out_quad = m_math.ease_in_out_quad;
  151. /**
  152. * Interpolate value with accelerating from zero velocity.
  153. * @method module:math.ease_in_cubic
  154. * @param {number} t Current time.
  155. * @param {number} b Start value.
  156. * @param {number} c Change in value.
  157. * @param {number} d Duration.
  158. * @returns {number} Interpolated value.
  159. */
  160. exports.ease_in_cubic = m_math.ease_in_cubic;
  161. /**
  162. * Interpolate value with decelerating to zero velocity.
  163. * @method module:math.ease_out_cubic
  164. * @param {number} t Current time.
  165. * @param {number} b Start value.
  166. * @param {number} c Change in value.
  167. * @param {number} d Duration.
  168. * @returns {number} Interpolated value.
  169. */
  170. exports.ease_out_cubic = m_math.ease_out_cubic;
  171. /**
  172. * Interpolate value with acceleration until halfway, then deceleration.
  173. * @method module:math.ease_in_out_cubic
  174. * @param {number} t Current time.
  175. * @param {number} b Start value.
  176. * @param {number} c Change in value.
  177. * @param {number} d Duration.
  178. * @returns {number} Interpolated value.
  179. */
  180. exports.ease_in_out_cubic = m_math.ease_in_out_cubic;
  181. /**
  182. * Interpolate value with accelerating from zero velocity.
  183. * @method module:math.ease_in_quart
  184. * @param {number} t Current time.
  185. * @param {number} b Start value.
  186. * @param {number} c Change in value.
  187. * @param {number} d Duration.
  188. * @returns {number} Interpolated value.
  189. */
  190. exports.ease_in_quart = m_math.ease_in_quart;
  191. /**
  192. * Interpolate value with decelerating to zero velocity.
  193. * @method module:math.ease_out_quart
  194. * @param {number} t Current time.
  195. * @param {number} b Start value.
  196. * @param {number} c Change in value.
  197. * @param {number} d Duration.
  198. * @returns {number} Interpolated value.
  199. */
  200. exports.ease_out_quart = m_math.ease_out_quart;
  201. /**
  202. * Interpolate value with acceleration until halfway, then deceleration.
  203. * @method module:math.ease_in_out_quart
  204. * @param {number} t Current time.
  205. * @param {number} b Start value.
  206. * @param {number} c Change in value.
  207. * @param {number} d Duration.
  208. * @returns {number} Interpolated value.
  209. */
  210. exports.ease_in_out_quart = m_math.ease_in_out_quart;
  211. /**
  212. * Interpolate value with decelerating from zero velocity.
  213. * @method module:math.ease_in_quint
  214. * @param {number} t Current time.
  215. * @param {number} b Start value.
  216. * @param {number} c Change in value.
  217. * @param {number} d Duration.
  218. * @returns {number} Interpolated value.
  219. */
  220. exports.ease_in_quint = m_math.ease_in_quint;
  221. /**
  222. * Interpolate value with decelerating to zero velocity.
  223. * @method module:math.ease_out_quint
  224. * @param {number} t Current time.
  225. * @param {number} b Start value.
  226. * @param {number} c Change in value.
  227. * @param {number} d Duration.
  228. * @returns {number} Interpolated value.
  229. */
  230. exports.ease_out_quint = m_math.ease_out_quint;
  231. /**
  232. * Interpolate value with acceleration until halfway, then deceleration.
  233. * @method module:math.ease_in_out_quint
  234. * @param {number} t Current time.
  235. * @param {number} b Start value.
  236. * @param {number} c Change in value.
  237. * @param {number} d Duration.
  238. * @returns {number} Interpolated value.
  239. */
  240. exports.ease_in_out_quint = m_math.ease_in_out_quint;
  241. /**
  242. * Interpolate value with decelerating from zero velocity.
  243. * @method module:math.ease_in_sine
  244. * @param {number} t Current time.
  245. * @param {number} b Start value.
  246. * @param {number} c Change in value.
  247. * @param {number} d Duration.
  248. * @returns {number} Interpolated value.
  249. */
  250. exports.ease_in_sine = m_math.ease_in_sine;
  251. /**
  252. * Interpolate value with decelerating to zero velocity.
  253. * @method module:math.ease_out_sine
  254. * @param {number} t Current time.
  255. * @param {number} b Start value.
  256. * @param {number} c Change in value.
  257. * @param {number} d Duration.
  258. * @returns {number} Interpolated value.
  259. */
  260. exports.ease_out_sine = m_math.ease_out_sine;
  261. /**
  262. * Interpolate value with acceleration until halfway, then decelerating.
  263. * @method module:math.ease_in_out_sine
  264. * @param {number} t Current time.
  265. * @param {number} b Start value.
  266. * @param {number} c Change in value.
  267. * @param {number} d Duration.
  268. * @returns {number} Interpolated value.
  269. */
  270. exports.ease_in_out_sine = m_math.ease_in_out_sine;
  271. /**
  272. * Interpolate value with decelerating from zero velocity.
  273. * @method module:math.ease_in_expo
  274. * @param {number} t Current time.
  275. * @param {number} b Start value.
  276. * @param {number} c Change in value.
  277. * @param {number} d Duration.
  278. * @returns {number} Interpolated value.
  279. */
  280. exports.ease_in_expo = m_math.ease_in_expo;
  281. /**
  282. * Interpolate value with decelerating to zero velocity.
  283. * @method module:math.ease_out_expo
  284. * @param {number} t Current time.
  285. * @param {number} b Start value.
  286. * @param {number} c Change in value.
  287. * @param {number} d Duration.
  288. * @returns {number} Interpolated value.
  289. */
  290. exports.ease_out_expo = m_math.ease_out_expo;
  291. /**
  292. * Interpolate value with acceleration until halfway, then decelerating.
  293. * @method module:math.ease_in_out_expo
  294. * @param {number} t Current time.
  295. * @param {number} b Start value.
  296. * @param {number} c Change in value.
  297. * @param {number} d Duration.
  298. * @returns {number} Interpolated value.
  299. */
  300. exports.ease_in_out_expo = m_math.ease_in_out_expo;
  301. /**
  302. * Interpolate value with decelerating from zero velocity.
  303. * @method module:math.ease_in_circ
  304. * @param {number} t Current time.
  305. * @param {number} b Start value.
  306. * @param {number} c Change in value.
  307. * @param {number} d Duration.
  308. * @returns {number} Interpolated value.
  309. */
  310. exports.ease_in_circ = m_math.ease_in_circ;
  311. /**
  312. * Interpolate value with decelerating to zero velocity.
  313. * @method module:math.ease_out_circ
  314. * @param {number} t Current time.
  315. * @param {number} b Start value.
  316. * @param {number} c Change in value.
  317. * @param {number} d Duration.
  318. * @returns {number} Interpolated value.
  319. */
  320. exports.ease_out_circ = m_math.ease_out_circ;
  321. /**
  322. * Interpolate value with acceleration until halfway, then deceleration.
  323. * @method module:math.ease_in_out_circ
  324. * @param {number} t Current time.
  325. * @param {number} b Start value.
  326. * @param {number} c Change in value.
  327. * @param {number} d Duration.
  328. * @returns {number} Interpolated value.
  329. */
  330. exports.ease_in_out_circ = m_math.ease_in_out_circ;
  331. /**
  332. * Interpolate value with decelerating from zero velocity.
  333. * @method module:math.ease_in_elastic
  334. * @param {number} t Current time.
  335. * @param {number} b Start value.
  336. * @param {number} c Change in value.
  337. * @param {number} d Duration.
  338. * @returns {number} Interpolated value.
  339. */
  340. exports.ease_in_elastic = m_math.ease_in_elastic;
  341. /**
  342. * Interpolate value with decelerating to zero velocity.
  343. * @method module:math.ease_out_elastic
  344. * @param {number} t Current time.
  345. * @param {number} b Start value.
  346. * @param {number} c Change in value.
  347. * @param {number} d Duration.
  348. * @returns {number} Interpolated value.
  349. */
  350. exports.ease_out_elastic = m_math.ease_out_elastic;
  351. /**
  352. * Interpolate value with acceleration until halfway, then deceleration.
  353. * @method module:math.ease_in_out_elastic
  354. * @param {number} t Current time.
  355. * @param {number} b Start value.
  356. * @param {number} c Change in value.
  357. * @param {number} d Duration.
  358. * @returns {number} Interpolated value.
  359. */
  360. exports.ease_in_out_elastic = m_math.ease_in_out_elastic;
  361. /**
  362. * Interpolate value with decelerating from zero velocity.
  363. * @method module:math.ease_in_back
  364. * @param {number} t Current time.
  365. * @param {number} b Start value.
  366. * @param {number} c Change in value.
  367. * @param {number} d Duration.
  368. * @returns {number} Interpolated value.
  369. */
  370. exports.ease_in_back = m_math.ease_in_back;
  371. /**
  372. * Interpolate value with decelerating to zero velocity.
  373. * @method module:math.ease_out_back
  374. * @param {number} t Current time.
  375. * @param {number} b Start value.
  376. * @param {number} c Change in value.
  377. * @param {number} d Duration.
  378. * @returns {number} Interpolated value.
  379. */
  380. exports.ease_out_back = m_math.ease_out_back;
  381. /**
  382. * Interpolate value with acceleration until halfway, then deceleration.
  383. * @method module:math.ease_in_out_back
  384. * @param {number} t Current time.
  385. * @param {number} b Start value.
  386. * @param {number} c Change in value.
  387. * @param {number} d Duration.
  388. * @returns {number} Interpolated value.
  389. */
  390. exports.ease_in_out_back = m_math.ease_in_out_back;
  391. /**
  392. * Interpolate value with decelerating from zero velocity.
  393. * @method module:math.ease_in_bounce
  394. * @param {number} t Current time.
  395. * @param {number} b Start value.
  396. * @param {number} c Change in value.
  397. * @param {number} d Duration.
  398. * @returns {number} Interpolated value.
  399. */
  400. exports.ease_in_bounce = m_math.ease_in_bounce;
  401. /**
  402. * Interpolate value with decelerating to zero velocity.
  403. * @method module:math.ease_out_bounce
  404. * @param {number} t Current time.
  405. * @param {number} b Start value.
  406. * @param {number} c Change in value.
  407. * @param {number} d Duration.
  408. * @returns {number} Interpolated value.
  409. */
  410. exports.ease_out_bounce = m_math.ease_out_bounce;
  411. /**
  412. * Interpolate value with acceleration until halfway, then deceleration.
  413. * @method module:math.ease_in_out_bounce
  414. * @param {number} t Current time.
  415. * @param {number} b Start value.
  416. * @param {number} c Change in value.
  417. * @param {number} d Duration.
  418. * @returns {number} Interpolated value.
  419. */
  420. exports.ease_in_out_bounce = m_math.ease_in_out_bounce;
  421. }
  422. var math_factory = register("math", Math);
  423. export default math_factory;