spline.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * BezierSpline
  3. * https://github.com/leszekr/bezier-spline-js
  4. *
  5. * @private
  6. * @copyright
  7. * Copyright (c) 2013 Leszek Rybicki
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. var Spline = /** @class */ (function () {
  28. function Spline(options) {
  29. this.points = options.points || [];
  30. this.duration = options.duration || 10000;
  31. this.sharpness = options.sharpness || 0.85;
  32. this.centers = [];
  33. this.controls = [];
  34. this.stepLength = options.stepLength || 60;
  35. this.length = this.points.length;
  36. this.delay = 0;
  37. // this is to ensure compatibility with the 2d version
  38. for (var i = 0; i < this.length; i++) {
  39. this.points[i].z = this.points[i].z || 0;
  40. }
  41. for (var i = 0; i < this.length - 1; i++) {
  42. var p1 = this.points[i];
  43. var p2 = this.points[i + 1];
  44. this.centers.push({
  45. x: (p1.x + p2.x) / 2,
  46. y: (p1.y + p2.y) / 2,
  47. z: (p1.z + p2.z) / 2,
  48. });
  49. }
  50. this.controls.push([this.points[0], this.points[0]]);
  51. for (var i = 0; i < this.centers.length - 1; i++) {
  52. var dx = this.points[i + 1].x - (this.centers[i].x + this.centers[i + 1].x) / 2;
  53. var dy = this.points[i + 1].y - (this.centers[i].y + this.centers[i + 1].y) / 2;
  54. var dz = this.points[i + 1].z - (this.centers[i].y + this.centers[i + 1].z) / 2;
  55. this.controls.push([
  56. {
  57. x: (1.0 - this.sharpness) * this.points[i + 1].x +
  58. this.sharpness * (this.centers[i].x + dx),
  59. y: (1.0 - this.sharpness) * this.points[i + 1].y +
  60. this.sharpness * (this.centers[i].y + dy),
  61. z: (1.0 - this.sharpness) * this.points[i + 1].z +
  62. this.sharpness * (this.centers[i].z + dz),
  63. },
  64. {
  65. x: (1.0 - this.sharpness) * this.points[i + 1].x +
  66. this.sharpness * (this.centers[i + 1].x + dx),
  67. y: (1.0 - this.sharpness) * this.points[i + 1].y +
  68. this.sharpness * (this.centers[i + 1].y + dy),
  69. z: (1.0 - this.sharpness) * this.points[i + 1].z +
  70. this.sharpness * (this.centers[i + 1].z + dz),
  71. },
  72. ]);
  73. }
  74. this.controls.push([
  75. this.points[this.length - 1],
  76. this.points[this.length - 1],
  77. ]);
  78. this.steps = this.cacheSteps(this.stepLength);
  79. return this;
  80. }
  81. /**
  82. * Caches an array of equidistant (more or less) points on the curve.
  83. */
  84. Spline.prototype.cacheSteps = function (mindist) {
  85. var steps = [];
  86. var laststep = this.pos(0);
  87. steps.push(0);
  88. for (var t = 0; t < this.duration; t += 10) {
  89. var step = this.pos(t);
  90. var dist = Math.sqrt((step.x - laststep.x) * (step.x - laststep.x) +
  91. (step.y - laststep.y) * (step.y - laststep.y) +
  92. (step.z - laststep.z) * (step.z - laststep.z));
  93. if (dist > mindist) {
  94. steps.push(t);
  95. laststep = step;
  96. }
  97. }
  98. return steps;
  99. };
  100. /**
  101. * returns angle and speed in the given point in the curve
  102. */
  103. Spline.prototype.vector = function (t) {
  104. var p1 = this.pos(t + 10);
  105. var p2 = this.pos(t - 10);
  106. return {
  107. angle: (180 * Math.atan2(p1.y - p2.y, p1.x - p2.x)) / 3.14,
  108. speed: Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) +
  109. (p2.y - p1.y) * (p2.y - p1.y) +
  110. (p2.z - p1.z) * (p2.z - p1.z)),
  111. };
  112. };
  113. /**
  114. * Gets the position of the point, given time.
  115. *
  116. * WARNING: The speed is not constant. The time it takes between control points is constant.
  117. *
  118. * For constant speed, use Spline.steps[i];
  119. */
  120. Spline.prototype.pos = function (time) {
  121. var t = time - this.delay;
  122. if (t < 0) {
  123. t = 0;
  124. }
  125. if (t > this.duration) {
  126. t = this.duration - 1;
  127. }
  128. // t = t-this.delay;
  129. var t2 = t / this.duration;
  130. if (t2 >= 1) {
  131. return this.points[this.length - 1];
  132. }
  133. var n = Math.floor((this.points.length - 1) * t2);
  134. var t1 = (this.length - 1) * t2 - n;
  135. return bezier(t1, this.points[n], this.controls[n][1], this.controls[n + 1][0], this.points[n + 1]);
  136. };
  137. return Spline;
  138. }());
  139. export default Spline;
  140. function bezier(t, p1, c1, c2, p2) {
  141. var b = B(t);
  142. var pos = {
  143. x: p2.x * b[0] + c2.x * b[1] + c1.x * b[2] + p1.x * b[3],
  144. y: p2.y * b[0] + c2.y * b[1] + c1.y * b[2] + p1.y * b[3],
  145. z: p2.z * b[0] + c2.z * b[1] + c1.z * b[2] + p1.z * b[3],
  146. };
  147. return pos;
  148. }
  149. function B(t) {
  150. var t2 = t * t;
  151. var t3 = t2 * t;
  152. return [
  153. t3,
  154. 3 * t2 * (1 - t),
  155. 3 * t * (1 - t) * (1 - t),
  156. (1 - t) * (1 - t) * (1 - t),
  157. ];
  158. }