index.cjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _helpers = require('@turf/helpers');
  3. var _invariant = require('@turf/invariant');
  4. // lib/spline.ts
  5. var Spline = class {
  6. constructor(options) {
  7. this.points = options.points || [];
  8. this.duration = options.duration || 1e4;
  9. this.sharpness = options.sharpness || 0.85;
  10. this.centers = [];
  11. this.controls = [];
  12. this.stepLength = options.stepLength || 60;
  13. this.length = this.points.length;
  14. this.delay = 0;
  15. for (let i = 0; i < this.length; i++) {
  16. this.points[i].z = this.points[i].z || 0;
  17. }
  18. for (let i = 0; i < this.length - 1; i++) {
  19. const p1 = this.points[i];
  20. const p2 = this.points[i + 1];
  21. this.centers.push({
  22. x: (p1.x + p2.x) / 2,
  23. y: (p1.y + p2.y) / 2,
  24. z: (p1.z + p2.z) / 2
  25. });
  26. }
  27. this.controls.push([this.points[0], this.points[0]]);
  28. for (let i = 0; i < this.centers.length - 1; i++) {
  29. const dx = this.points[i + 1].x - (this.centers[i].x + this.centers[i + 1].x) / 2;
  30. const dy = this.points[i + 1].y - (this.centers[i].y + this.centers[i + 1].y) / 2;
  31. const dz = this.points[i + 1].z - (this.centers[i].y + this.centers[i + 1].z) / 2;
  32. this.controls.push([
  33. {
  34. x: (1 - this.sharpness) * this.points[i + 1].x + this.sharpness * (this.centers[i].x + dx),
  35. y: (1 - this.sharpness) * this.points[i + 1].y + this.sharpness * (this.centers[i].y + dy),
  36. z: (1 - this.sharpness) * this.points[i + 1].z + this.sharpness * (this.centers[i].z + dz)
  37. },
  38. {
  39. x: (1 - this.sharpness) * this.points[i + 1].x + this.sharpness * (this.centers[i + 1].x + dx),
  40. y: (1 - this.sharpness) * this.points[i + 1].y + this.sharpness * (this.centers[i + 1].y + dy),
  41. z: (1 - this.sharpness) * this.points[i + 1].z + this.sharpness * (this.centers[i + 1].z + dz)
  42. }
  43. ]);
  44. }
  45. this.controls.push([
  46. this.points[this.length - 1],
  47. this.points[this.length - 1]
  48. ]);
  49. this.steps = this.cacheSteps(this.stepLength);
  50. return this;
  51. }
  52. /**
  53. * Caches an array of equidistant (more or less) points on the curve.
  54. */
  55. cacheSteps(mindist) {
  56. const steps = [];
  57. let laststep = this.pos(0);
  58. steps.push(0);
  59. for (let t = 0; t < this.duration; t += 10) {
  60. const step = this.pos(t);
  61. const dist = Math.sqrt(
  62. (step.x - laststep.x) * (step.x - laststep.x) + (step.y - laststep.y) * (step.y - laststep.y) + (step.z - laststep.z) * (step.z - laststep.z)
  63. );
  64. if (dist > mindist) {
  65. steps.push(t);
  66. laststep = step;
  67. }
  68. }
  69. return steps;
  70. }
  71. /**
  72. * returns angle and speed in the given point in the curve
  73. */
  74. vector(t) {
  75. const p1 = this.pos(t + 10);
  76. const p2 = this.pos(t - 10);
  77. return {
  78. angle: 180 * Math.atan2(p1.y - p2.y, p1.x - p2.x) / 3.14,
  79. speed: Math.sqrt(
  80. (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y) + (p2.z - p1.z) * (p2.z - p1.z)
  81. )
  82. };
  83. }
  84. /**
  85. * Gets the position of the point, given time.
  86. *
  87. * WARNING: The speed is not constant. The time it takes between control points is constant.
  88. *
  89. * For constant speed, use Spline.steps[i];
  90. */
  91. pos(time) {
  92. let t = time - this.delay;
  93. if (t < 0) {
  94. t = 0;
  95. }
  96. if (t > this.duration) {
  97. t = this.duration - 1;
  98. }
  99. const t2 = t / this.duration;
  100. if (t2 >= 1) {
  101. return this.points[this.length - 1];
  102. }
  103. const n = Math.floor((this.points.length - 1) * t2);
  104. const t1 = (this.length - 1) * t2 - n;
  105. return bezier(
  106. t1,
  107. this.points[n],
  108. this.controls[n][1],
  109. this.controls[n + 1][0],
  110. this.points[n + 1]
  111. );
  112. }
  113. };
  114. function bezier(t, p1, c1, c2, p2) {
  115. const b = B(t);
  116. const pos = {
  117. x: p2.x * b[0] + c2.x * b[1] + c1.x * b[2] + p1.x * b[3],
  118. y: p2.y * b[0] + c2.y * b[1] + c1.y * b[2] + p1.y * b[3],
  119. z: p2.z * b[0] + c2.z * b[1] + c1.z * b[2] + p1.z * b[3]
  120. };
  121. return pos;
  122. }
  123. function B(t) {
  124. const t2 = t * t;
  125. const t3 = t2 * t;
  126. return [
  127. t3,
  128. 3 * t2 * (1 - t),
  129. 3 * t * (1 - t) * (1 - t),
  130. (1 - t) * (1 - t) * (1 - t)
  131. ];
  132. }
  133. // index.ts
  134. function bezierSpline(line, options = {}) {
  135. const resolution = options.resolution || 1e4;
  136. const sharpness = options.sharpness || 0.85;
  137. const coords = [];
  138. const points = _invariant.getGeom.call(void 0, line).coordinates.map((pt) => {
  139. return { x: pt[0], y: pt[1] };
  140. });
  141. const spline = new Spline({
  142. duration: resolution,
  143. points,
  144. sharpness
  145. });
  146. const pushCoord = (time) => {
  147. var pos = spline.pos(time);
  148. if (Math.floor(time / 100) % 2 === 0) {
  149. coords.push([pos.x, pos.y]);
  150. }
  151. };
  152. for (var i = 0; i < spline.duration; i += 10) {
  153. pushCoord(i);
  154. }
  155. pushCoord(spline.duration);
  156. return _helpers.lineString.call(void 0, coords, options.properties);
  157. }
  158. var turf_bezier_spline_default = bezierSpline;
  159. exports.bezierSpline = bezierSpline; exports.default = turf_bezier_spline_default;
  160. //# sourceMappingURL=index.cjs.map