fit-curve.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define(["module"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(module);
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod);
  11. global.fitCurve = mod.exports;
  12. }
  13. })(this, function (module) {
  14. "use strict";
  15. function _classCallCheck(instance, Constructor) {
  16. if (!(instance instanceof Constructor)) {
  17. throw new TypeError("Cannot call a class as a function");
  18. }
  19. }
  20. /**
  21. * @preserve JavaScript implementation of
  22. * Algorithm for Automatically Fitting Digitized Curves
  23. * by Philip J. Schneider
  24. * "Graphics Gems", Academic Press, 1990
  25. *
  26. * The MIT License (MIT)
  27. *
  28. * https://github.com/soswow/fit-curves
  29. */
  30. /**
  31. * Fit one or more Bezier curves to a set of points.
  32. *
  33. * @param {Array<Array<Number>>} points - Array of digitized points, e.g. [[5,5],[5,50],[110,140],[210,160],[320,110]]
  34. * @param {Number} maxError - Tolerance, squared error between points and fitted curve
  35. * @returns {Array<Array<Array<Number>>>} Array of Bezier curves, where each element is [first-point, control-point-1, control-point-2, second-point] and points are [x, y]
  36. */
  37. function fitCurve(points, maxError, progressCallback) {
  38. if (!Array.isArray(points)) {
  39. throw new TypeError("First argument should be an array");
  40. }
  41. points.forEach(function (point) {
  42. if (!Array.isArray(point) || point.some(function (item) {
  43. return typeof item !== 'number';
  44. }) || point.length !== points[0].length) {
  45. throw Error("Each point should be an array of numbers. Each point should have the same amount of numbers.");
  46. }
  47. });
  48. // Remove duplicate points
  49. points = points.filter(function (point, i) {
  50. return i === 0 || !point.every(function (val, j) {
  51. return val === points[i - 1][j];
  52. });
  53. });
  54. if (points.length < 2) {
  55. return [];
  56. }
  57. var len = points.length;
  58. var leftTangent = createTangent(points[1], points[0]);
  59. var rightTangent = createTangent(points[len - 2], points[len - 1]);
  60. return fitCubic(points, leftTangent, rightTangent, maxError, progressCallback);
  61. }
  62. /**
  63. * Fit a Bezier curve to a (sub)set of digitized points.
  64. * Your code should not call this function directly. Use {@link fitCurve} instead.
  65. *
  66. * @param {Array<Array<Number>>} points - Array of digitized points, e.g. [[5,5],[5,50],[110,140],[210,160],[320,110]]
  67. * @param {Array<Number>} leftTangent - Unit tangent vector at start point
  68. * @param {Array<Number>} rightTangent - Unit tangent vector at end point
  69. * @param {Number} error - Tolerance, squared error between points and fitted curve
  70. * @returns {Array<Array<Array<Number>>>} Array of Bezier curves, where each element is [first-point, control-point-1, control-point-2, second-point] and points are [x, y]
  71. */
  72. function fitCubic(points, leftTangent, rightTangent, error, progressCallback) {
  73. var MaxIterations = 20; //Max times to try iterating (to find an acceptable curve)
  74. var bezCurve, //Control points of fitted Bezier curve
  75. u, //Parameter values for point
  76. uPrime, //Improved parameter values
  77. maxError, prevErr, //Maximum fitting error
  78. splitPoint, prevSplit, //Point to split point set at if we need more than one curve
  79. centerVector, toCenterTangent, fromCenterTangent, //Unit tangent vector(s) at splitPoint
  80. beziers, //Array of fitted Bezier curves if we need more than one curve
  81. dist, i;
  82. //console.log('fitCubic, ', points.length);
  83. //Use heuristic if region only has two points in it
  84. if (points.length === 2) {
  85. dist = maths.vectorLen(maths.subtract(points[0], points[1])) / 3.0;
  86. bezCurve = [points[0], maths.addArrays(points[0], maths.mulItems(leftTangent, dist)), maths.addArrays(points[1], maths.mulItems(rightTangent, dist)), points[1]];
  87. return [bezCurve];
  88. }
  89. //Parameterize points, and attempt to fit curve
  90. u = chordLengthParameterize(points);
  91. var _generateAndReport = generateAndReport(points, u, u, leftTangent, rightTangent, progressCallback);
  92. bezCurve = _generateAndReport[0];
  93. maxError = _generateAndReport[1];
  94. splitPoint = _generateAndReport[2];
  95. if (maxError === 0 || maxError < error) {
  96. return [bezCurve];
  97. }
  98. //If error not too large, try some reparameterization and iteration
  99. if (maxError < error * error) {
  100. uPrime = u;
  101. prevErr = maxError;
  102. prevSplit = splitPoint;
  103. for (i = 0; i < MaxIterations; i++) {
  104. uPrime = reparameterize(bezCurve, points, uPrime);
  105. var _generateAndReport2 = generateAndReport(points, u, uPrime, leftTangent, rightTangent, progressCallback);
  106. bezCurve = _generateAndReport2[0];
  107. maxError = _generateAndReport2[1];
  108. splitPoint = _generateAndReport2[2];
  109. if (maxError < error) {
  110. return [bezCurve];
  111. }
  112. //If the development of the fitted curve grinds to a halt,
  113. //we abort this attempt (and try a shorter curve):
  114. else if (splitPoint === prevSplit) {
  115. var errChange = maxError / prevErr;
  116. if (errChange > .9999 && errChange < 1.0001) {
  117. break;
  118. }
  119. }
  120. prevErr = maxError;
  121. prevSplit = splitPoint;
  122. }
  123. }
  124. //Fitting failed -- split at max error point and fit recursively
  125. beziers = [];
  126. //To create a smooth transition from one curve segment to the next, we
  127. //calculate the line between the points directly before and after the
  128. //center, and use that as the tangent both to and from the center point.
  129. centerVector = maths.subtract(points[splitPoint - 1], points[splitPoint + 1]);
  130. //However, this won't work if they're the same point, because the line we
  131. //want to use as a tangent would be 0. Instead, we calculate the line from
  132. //that "double-point" to the center point, and use its tangent.
  133. if (centerVector.every(function (val) {
  134. return val === 0;
  135. })) {
  136. //[x,y] -> [-y,x]: http://stackoverflow.com/a/4780141/1869660
  137. centerVector = maths.subtract(points[splitPoint - 1], points[splitPoint]);
  138. var _ref = [-centerVector[1], centerVector[0]];
  139. centerVector[0] = _ref[0];
  140. centerVector[1] = _ref[1];
  141. }
  142. toCenterTangent = maths.normalize(centerVector);
  143. //To and from need to point in opposite directions:
  144. fromCenterTangent = maths.mulItems(toCenterTangent, -1);
  145. /*
  146. Note: An alternative to this "divide and conquer" recursion could be to always
  147. let new curve segments start by trying to go all the way to the end,
  148. instead of only to the end of the current subdivided polyline.
  149. That might let many segments fit a few points more, reducing the number of total segments.
  150. However, a few tests have shown that the segment reduction is insignificant
  151. (240 pts, 100 err: 25 curves vs 27 curves. 140 pts, 100 err: 17 curves on both),
  152. and the results take twice as many steps and milliseconds to finish,
  153. without looking any better than what we already have.
  154. */
  155. beziers = beziers.concat(fitCubic(points.slice(0, splitPoint + 1), leftTangent, toCenterTangent, error, progressCallback));
  156. beziers = beziers.concat(fitCubic(points.slice(splitPoint), fromCenterTangent, rightTangent, error, progressCallback));
  157. return beziers;
  158. };
  159. function generateAndReport(points, paramsOrig, paramsPrime, leftTangent, rightTangent, progressCallback) {
  160. var bezCurve, maxError, splitPoint;
  161. bezCurve = generateBezier(points, paramsPrime, leftTangent, rightTangent, progressCallback);
  162. //Find max deviation of points to fitted curve.
  163. //Here we always use the original parameters (from chordLengthParameterize()),
  164. //because we need to compare the current curve to the actual source polyline,
  165. //and not the currently iterated parameters which reparameterize() & generateBezier() use,
  166. //as those have probably drifted far away and may no longer be in ascending order.
  167. var _computeMaxError = computeMaxError(points, bezCurve, paramsOrig);
  168. maxError = _computeMaxError[0];
  169. splitPoint = _computeMaxError[1];
  170. if (progressCallback) {
  171. progressCallback({
  172. bez: bezCurve,
  173. points: points,
  174. params: paramsOrig,
  175. maxErr: maxError,
  176. maxPoint: splitPoint
  177. });
  178. }
  179. return [bezCurve, maxError, splitPoint];
  180. }
  181. /**
  182. * Use least-squares method to find Bezier control points for region.
  183. *
  184. * @param {Array<Array<Number>>} points - Array of digitized points
  185. * @param {Array<Number>} parameters - Parameter values for region
  186. * @param {Array<Number>} leftTangent - Unit tangent vector at start point
  187. * @param {Array<Number>} rightTangent - Unit tangent vector at end point
  188. * @returns {Array<Array<Number>>} Approximated Bezier curve: [first-point, control-point-1, control-point-2, second-point] where points are [x, y]
  189. */
  190. function generateBezier(points, parameters, leftTangent, rightTangent) {
  191. var bezCurve,
  192. //Bezier curve ctl pts
  193. A,
  194. a,
  195. //Precomputed rhs for eqn
  196. C,
  197. X,
  198. //Matrices C & X
  199. det_C0_C1,
  200. det_C0_X,
  201. det_X_C1,
  202. //Determinants of matrices
  203. alpha_l,
  204. alpha_r,
  205. //Alpha values, left and right
  206. epsilon,
  207. segLength,
  208. i,
  209. len,
  210. tmp,
  211. u,
  212. ux,
  213. firstPoint = points[0],
  214. lastPoint = points[points.length - 1];
  215. bezCurve = [firstPoint, null, null, lastPoint];
  216. //console.log('gb', parameters.length);
  217. //Compute the A's
  218. A = maths.zeros_Xx2x2(parameters.length);
  219. for (i = 0, len = parameters.length; i < len; i++) {
  220. u = parameters[i];
  221. ux = 1 - u;
  222. a = A[i];
  223. a[0] = maths.mulItems(leftTangent, 3 * u * (ux * ux));
  224. a[1] = maths.mulItems(rightTangent, 3 * ux * (u * u));
  225. }
  226. //Create the C and X matrices
  227. C = [[0, 0], [0, 0]];
  228. X = [0, 0];
  229. for (i = 0, len = points.length; i < len; i++) {
  230. u = parameters[i];
  231. a = A[i];
  232. C[0][0] += maths.dot(a[0], a[0]);
  233. C[0][1] += maths.dot(a[0], a[1]);
  234. C[1][0] += maths.dot(a[0], a[1]);
  235. C[1][1] += maths.dot(a[1], a[1]);
  236. tmp = maths.subtract(points[i], bezier.q([firstPoint, firstPoint, lastPoint, lastPoint], u));
  237. X[0] += maths.dot(a[0], tmp);
  238. X[1] += maths.dot(a[1], tmp);
  239. }
  240. //Compute the determinants of C and X
  241. det_C0_C1 = C[0][0] * C[1][1] - C[1][0] * C[0][1];
  242. det_C0_X = C[0][0] * X[1] - C[1][0] * X[0];
  243. det_X_C1 = X[0] * C[1][1] - X[1] * C[0][1];
  244. //Finally, derive alpha values
  245. alpha_l = det_C0_C1 === 0 ? 0 : det_X_C1 / det_C0_C1;
  246. alpha_r = det_C0_C1 === 0 ? 0 : det_C0_X / det_C0_C1;
  247. //If alpha negative, use the Wu/Barsky heuristic (see text).
  248. //If alpha is 0, you get coincident control points that lead to
  249. //divide by zero in any subsequent NewtonRaphsonRootFind() call.
  250. segLength = maths.vectorLen(maths.subtract(firstPoint, lastPoint));
  251. epsilon = 1.0e-6 * segLength;
  252. if (alpha_l < epsilon || alpha_r < epsilon) {
  253. //Fall back on standard (probably inaccurate) formula, and subdivide further if needed.
  254. bezCurve[1] = maths.addArrays(firstPoint, maths.mulItems(leftTangent, segLength / 3.0));
  255. bezCurve[2] = maths.addArrays(lastPoint, maths.mulItems(rightTangent, segLength / 3.0));
  256. } else {
  257. //First and last control points of the Bezier curve are
  258. //positioned exactly at the first and last data points
  259. //Control points 1 and 2 are positioned an alpha distance out
  260. //on the tangent vectors, left and right, respectively
  261. bezCurve[1] = maths.addArrays(firstPoint, maths.mulItems(leftTangent, alpha_l));
  262. bezCurve[2] = maths.addArrays(lastPoint, maths.mulItems(rightTangent, alpha_r));
  263. }
  264. return bezCurve;
  265. };
  266. /**
  267. * Given set of points and their parameterization, try to find a better parameterization.
  268. *
  269. * @param {Array<Array<Number>>} bezier - Current fitted curve
  270. * @param {Array<Array<Number>>} points - Array of digitized points
  271. * @param {Array<Number>} parameters - Current parameter values
  272. * @returns {Array<Number>} New parameter values
  273. */
  274. function reparameterize(bezier, points, parameters) {
  275. /*
  276. var j, len, point, results, u;
  277. results = [];
  278. for (j = 0, len = points.length; j < len; j++) {
  279. point = points[j], u = parameters[j];
  280. results.push(newtonRaphsonRootFind(bezier, point, u));
  281. }
  282. return results;
  283. //*/
  284. return parameters.map(function (p, i) {
  285. return newtonRaphsonRootFind(bezier, points[i], p);
  286. });
  287. };
  288. /**
  289. * Use Newton-Raphson iteration to find better root.
  290. *
  291. * @param {Array<Array<Number>>} bez - Current fitted curve
  292. * @param {Array<Number>} point - Digitized point
  293. * @param {Number} u - Parameter value for "P"
  294. * @returns {Number} New u
  295. */
  296. function newtonRaphsonRootFind(bez, point, u) {
  297. /*
  298. Newton's root finding algorithm calculates f(x)=0 by reiterating
  299. x_n+1 = x_n - f(x_n)/f'(x_n)
  300. We are trying to find curve parameter u for some point p that minimizes
  301. the distance from that point to the curve. Distance point to curve is d=q(u)-p.
  302. At minimum distance the point is perpendicular to the curve.
  303. We are solving
  304. f = q(u)-p * q'(u) = 0
  305. with
  306. f' = q'(u) * q'(u) + q(u)-p * q''(u)
  307. gives
  308. u_n+1 = u_n - |q(u_n)-p * q'(u_n)| / |q'(u_n)**2 + q(u_n)-p * q''(u_n)|
  309. */
  310. var d = maths.subtract(bezier.q(bez, u), point),
  311. qprime = bezier.qprime(bez, u),
  312. numerator = maths.mulMatrix(d, qprime),
  313. denominator = maths.sum(maths.squareItems(qprime)) + 2 * maths.mulMatrix(d, bezier.qprimeprime(bez, u));
  314. if (denominator === 0) {
  315. return u;
  316. } else {
  317. return u - numerator / denominator;
  318. }
  319. };
  320. /**
  321. * Assign parameter values to digitized points using relative distances between points.
  322. *
  323. * @param {Array<Array<Number>>} points - Array of digitized points
  324. * @returns {Array<Number>} Parameter values
  325. */
  326. function chordLengthParameterize(points) {
  327. var u = [],
  328. currU,
  329. prevU,
  330. prevP;
  331. points.forEach(function (p, i) {
  332. currU = i ? prevU + maths.vectorLen(maths.subtract(p, prevP)) : 0;
  333. u.push(currU);
  334. prevU = currU;
  335. prevP = p;
  336. });
  337. u = u.map(function (x) {
  338. return x / prevU;
  339. });
  340. return u;
  341. };
  342. /**
  343. * Find the maximum squared distance of digitized points to fitted curve.
  344. *
  345. * @param {Array<Array<Number>>} points - Array of digitized points
  346. * @param {Array<Array<Number>>} bez - Fitted curve
  347. * @param {Array<Number>} parameters - Parameterization of points
  348. * @returns {Array<Number>} Maximum error (squared) and point of max error
  349. */
  350. function computeMaxError(points, bez, parameters) {
  351. var dist, //Current error
  352. maxDist, //Maximum error
  353. splitPoint, //Point of maximum error
  354. v, //Vector from point to curve
  355. i, count, point, t;
  356. maxDist = 0;
  357. splitPoint = Math.floor(points.length / 2);
  358. var t_distMap = mapTtoRelativeDistances(bez, 10);
  359. for (i = 0, count = points.length; i < count; i++) {
  360. point = points[i];
  361. //Find 't' for a point on the bez curve that's as close to 'point' as possible:
  362. t = find_t(bez, parameters[i], t_distMap, 10);
  363. v = maths.subtract(bezier.q(bez, t), point);
  364. dist = v[0] * v[0] + v[1] * v[1];
  365. if (dist > maxDist) {
  366. maxDist = dist;
  367. splitPoint = i;
  368. }
  369. }
  370. return [maxDist, splitPoint];
  371. };
  372. //Sample 't's and map them to relative distances along the curve:
  373. var mapTtoRelativeDistances = function mapTtoRelativeDistances(bez, B_parts) {
  374. var B_t_curr;
  375. var B_t_dist = [0];
  376. var B_t_prev = bez[0];
  377. var sumLen = 0;
  378. for (var i = 1; i <= B_parts; i++) {
  379. B_t_curr = bezier.q(bez, i / B_parts);
  380. sumLen += maths.vectorLen(maths.subtract(B_t_curr, B_t_prev));
  381. B_t_dist.push(sumLen);
  382. B_t_prev = B_t_curr;
  383. }
  384. //Normalize B_length to the same interval as the parameter distances; 0 to 1:
  385. B_t_dist = B_t_dist.map(function (x) {
  386. return x / sumLen;
  387. });
  388. return B_t_dist;
  389. };
  390. function find_t(bez, param, t_distMap, B_parts) {
  391. if (param < 0) {
  392. return 0;
  393. }
  394. if (param > 1) {
  395. return 1;
  396. }
  397. /*
  398. 'param' is a value between 0 and 1 telling us the relative position
  399. of a point on the source polyline (linearly from the start (0) to the end (1)).
  400. To see if a given curve - 'bez' - is a close approximation of the polyline,
  401. we compare such a poly-point to the point on the curve that's the same
  402. relative distance along the curve's length.
  403. But finding that curve-point takes a little work:
  404. There is a function "B(t)" to find points along a curve from the parametric parameter 't'
  405. (also relative from 0 to 1: http://stackoverflow.com/a/32841764/1869660
  406. http://pomax.github.io/bezierinfo/#explanation),
  407. but 't' isn't linear by length (http://gamedev.stackexchange.com/questions/105230).
  408. So, we sample some points along the curve using a handful of values for 't'.
  409. Then, we calculate the length between those samples via plain euclidean distance;
  410. B(t) concentrates the points around sharp turns, so this should give us a good-enough outline of the curve.
  411. Thus, for a given relative distance ('param'), we can now find an upper and lower value
  412. for the corresponding 't' by searching through those sampled distances.
  413. Finally, we just use linear interpolation to find a better value for the exact 't'.
  414. More info:
  415. http://gamedev.stackexchange.com/questions/105230/points-evenly-spaced-along-a-bezier-curve
  416. http://stackoverflow.com/questions/29438398/cheap-way-of-calculating-cubic-bezier-length
  417. http://steve.hollasch.net/cgindex/curves/cbezarclen.html
  418. https://github.com/retuxx/tinyspline
  419. */
  420. var lenMax, lenMin, tMax, tMin, t;
  421. //Find the two t-s that the current param distance lies between,
  422. //and then interpolate a somewhat accurate value for the exact t:
  423. for (var i = 1; i <= B_parts; i++) {
  424. if (param <= t_distMap[i]) {
  425. tMin = (i - 1) / B_parts;
  426. tMax = i / B_parts;
  427. lenMin = t_distMap[i - 1];
  428. lenMax = t_distMap[i];
  429. t = (param - lenMin) / (lenMax - lenMin) * (tMax - tMin) + tMin;
  430. break;
  431. }
  432. }
  433. return t;
  434. }
  435. /**
  436. * Creates a vector of length 1 which shows the direction from B to A
  437. */
  438. function createTangent(pointA, pointB) {
  439. return maths.normalize(maths.subtract(pointA, pointB));
  440. }
  441. /*
  442. Simplified versions of what we need from math.js
  443. Optimized for our input, which is only numbers and 1x2 arrays (i.e. [x, y] coordinates).
  444. */
  445. var maths = function () {
  446. function maths() {
  447. _classCallCheck(this, maths);
  448. }
  449. maths.zeros_Xx2x2 = function zeros_Xx2x2(x) {
  450. var zs = [];
  451. while (x--) {
  452. zs.push([0, 0]);
  453. }
  454. return zs;
  455. };
  456. maths.mulItems = function mulItems(items, multiplier) {
  457. return items.map(function (x) {
  458. return x * multiplier;
  459. });
  460. };
  461. maths.mulMatrix = function mulMatrix(m1, m2) {
  462. //https://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29
  463. //Simplified to only handle 1-dimensional matrices (i.e. arrays) of equal length:
  464. return m1.reduce(function (sum, x1, i) {
  465. return sum + x1 * m2[i];
  466. }, 0);
  467. };
  468. maths.subtract = function subtract(arr1, arr2) {
  469. return arr1.map(function (x1, i) {
  470. return x1 - arr2[i];
  471. });
  472. };
  473. maths.addArrays = function addArrays(arr1, arr2) {
  474. return arr1.map(function (x1, i) {
  475. return x1 + arr2[i];
  476. });
  477. };
  478. maths.addItems = function addItems(items, addition) {
  479. return items.map(function (x) {
  480. return x + addition;
  481. });
  482. };
  483. maths.sum = function sum(items) {
  484. return items.reduce(function (sum, x) {
  485. return sum + x;
  486. });
  487. };
  488. maths.dot = function dot(m1, m2) {
  489. return maths.mulMatrix(m1, m2);
  490. };
  491. maths.vectorLen = function vectorLen(v) {
  492. return Math.hypot.apply(Math, v);
  493. };
  494. maths.divItems = function divItems(items, divisor) {
  495. return items.map(function (x) {
  496. return x / divisor;
  497. });
  498. };
  499. maths.squareItems = function squareItems(items) {
  500. return items.map(function (x) {
  501. return x * x;
  502. });
  503. };
  504. maths.normalize = function normalize(v) {
  505. return this.divItems(v, this.vectorLen(v));
  506. };
  507. return maths;
  508. }();
  509. var bezier = function () {
  510. function bezier() {
  511. _classCallCheck(this, bezier);
  512. }
  513. bezier.q = function q(ctrlPoly, t) {
  514. var tx = 1.0 - t;
  515. var pA = maths.mulItems(ctrlPoly[0], tx * tx * tx),
  516. pB = maths.mulItems(ctrlPoly[1], 3 * tx * tx * t),
  517. pC = maths.mulItems(ctrlPoly[2], 3 * tx * t * t),
  518. pD = maths.mulItems(ctrlPoly[3], t * t * t);
  519. return maths.addArrays(maths.addArrays(pA, pB), maths.addArrays(pC, pD));
  520. };
  521. bezier.qprime = function qprime(ctrlPoly, t) {
  522. var tx = 1.0 - t;
  523. var pA = maths.mulItems(maths.subtract(ctrlPoly[1], ctrlPoly[0]), 3 * tx * tx),
  524. pB = maths.mulItems(maths.subtract(ctrlPoly[2], ctrlPoly[1]), 6 * tx * t),
  525. pC = maths.mulItems(maths.subtract(ctrlPoly[3], ctrlPoly[2]), 3 * t * t);
  526. return maths.addArrays(maths.addArrays(pA, pB), pC);
  527. };
  528. bezier.qprimeprime = function qprimeprime(ctrlPoly, t) {
  529. return maths.addArrays(maths.mulItems(maths.addArrays(maths.subtract(ctrlPoly[2], maths.mulItems(ctrlPoly[1], 2)), ctrlPoly[0]), 6 * (1.0 - t)), maths.mulItems(maths.addArrays(maths.subtract(ctrlPoly[3], maths.mulItems(ctrlPoly[2], 2)), ctrlPoly[1]), 6 * t));
  530. };
  531. return bezier;
  532. }();
  533. module.exports = fitCurve;
  534. module.exports.fitCubic = fitCubic;
  535. module.exports.createTangent = createTangent;
  536. });