Spline.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace ARLocation
  4. {
  5. public enum SplineType
  6. {
  7. CatmullromSpline,
  8. LinearSpline,
  9. }
  10. public abstract class Spline
  11. {
  12. /// <summary>
  13. /// The points interpolated of the spline.
  14. /// </summary>
  15. public Vector3[] Points { get; protected set; }
  16. /// <summary>
  17. /// The CatmullRom curve-segments of the spline.
  18. /// </summary>
  19. protected Curve[] segments;
  20. /// <summary>
  21. /// The number of segments that make up the spline.
  22. /// </summary>
  23. protected int segmentCount = 0;
  24. /// <summary>
  25. /// The full (estimated) length of the spline.
  26. /// </summary>
  27. public float Length { get; protected set; }
  28. protected float[] lengths;
  29. /// <summary>
  30. /// Calculate the catmull-rom segments. Also estimates the curve's length.
  31. /// </summary>
  32. /// <param name="n">The number sample points used to estimate each segment's length.</param>
  33. public abstract void CalculateSegments(int n);
  34. /// <summary>
  35. /// Returns the point of the spline at a given arc-length.
  36. /// </summary>
  37. /// <param name="s">The arc-length.</param>
  38. /// <returns></returns>
  39. public Vector3 GetPointAtArcLength(float s)
  40. {
  41. s = Mathf.Clamp(s, 0, Length);
  42. for (var i = 0; i < segmentCount; i++)
  43. {
  44. if (s <= lengths[i])
  45. {
  46. var offset = i == 0 ? 0 : lengths[i - 1];
  47. return segments[i].GetPointAtLength(s - offset);
  48. }
  49. }
  50. return segments[segmentCount - 1].GetPoint(1);
  51. }
  52. /// <summary>
  53. /// Returns a CurvePointData whith the point and tangent of the spline
  54. /// at a given arc-length.
  55. /// </summary>
  56. /// <param name="s">The arc-length.</param>
  57. /// <returns></returns>
  58. public CurvePointData GetPointAndTangentAtArcLength(float s)
  59. {
  60. s = Mathf.Clamp(s, 0, Length);
  61. for (var i = 0; i < segmentCount; i++)
  62. {
  63. if (s <= lengths[i])
  64. {
  65. var offset = i == 0 ? 0 : lengths[i - 1];
  66. return segments[i].GetPointAndTangentAtLength(s - offset);
  67. }
  68. }
  69. return segments[segmentCount - 1].GetPointAndTangentAtLength(1);
  70. }
  71. /// <summary>
  72. /// Draws the curve using a given LineRenderer, with points being processed by a given
  73. /// function beforehand.
  74. /// </summary>
  75. /// <param name="renderer"></param>
  76. /// <param name="func"></param>
  77. /// <param name="n"></param>
  78. public void DrawCurveWithLineRenderer(LineRenderer renderer, System.Func<Vector3, Vector3> func, int n = 100)
  79. {
  80. var points = new List<Vector3>();
  81. float s = 0.0f;
  82. while (s <= Length)
  83. {
  84. var pointData = GetPointAndTangentAtArcLength(s);
  85. points.Add(func(pointData.point));
  86. s += Length / (n + 1.0f);
  87. }
  88. var arr = points.ToArray();
  89. renderer.positionCount = arr.Length;
  90. renderer.SetPositions(arr);
  91. }
  92. /// <summary>
  93. /// Calculates a sample of (N+2) equidistant points along the spline.
  94. /// </summary>
  95. /// <param name="n">The number of points in the sample will be (N+2).</param>
  96. /// <param name="func">A function that can be used to transform the sampled poins.</param>
  97. /// <returns></returns>
  98. public Vector3[] SamplePoints(int n, System.Func<Vector3, Vector3> func)
  99. {
  100. var sample = new Vector3[n + 2];
  101. var delta = Length / (n + 1.0f);
  102. var s = 0.0f;
  103. for (var i = 0; i < (n + 2); i++)
  104. {
  105. sample[i] = func(GetPointAtArcLength(s));
  106. s += delta;
  107. }
  108. return sample;
  109. }
  110. /// <summary>
  111. /// Calculates a sample of (N+2) equidistant points along the spline.
  112. /// </summary>
  113. /// <param name="n">The number of points in the sample will be (N+2).</param>
  114. /// <returns></returns>
  115. public Vector3[] SamplePoints(int n)
  116. {
  117. return SamplePoints(n, (p) => p);
  118. }
  119. /// <summary>
  120. /// Draw the curve and sample point using Gizmos.
  121. /// </summary>
  122. public void DrawGizmos()
  123. {
  124. DrawPointsGizmos();
  125. DrawCurveLengthGizmos();
  126. }
  127. private void DrawPointsGizmos()
  128. {
  129. foreach (var p in Points)
  130. {
  131. Gizmos.color = Color.blue;
  132. Gizmos.DrawSphere(p, 0.1f);
  133. }
  134. }
  135. private void DrawCurveLengthGizmos()
  136. {
  137. var p = GetPointAtArcLength(0f);
  138. float s = 0.0f;
  139. while (s <= Length)
  140. {
  141. Gizmos.color = Color.green;
  142. var pointData = GetPointAndTangentAtArcLength(s);
  143. Vector3 n = pointData.point;
  144. Gizmos.DrawLine(p, n);
  145. p = n;
  146. s += 0.1f;
  147. Gizmos.color = Color.magenta;
  148. var tan = pointData.tangent;
  149. Gizmos.color = Color.blue;
  150. Gizmos.DrawLine(n, n + tan);
  151. }
  152. }
  153. }
  154. }