Spline.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Spline.h"
  5. #include "../IO/Log.h"
  6. namespace Urho3D
  7. {
  8. const char* interpolationModeNames[] =
  9. {
  10. "Bezier",
  11. "Catmull-Rom",
  12. "Linear",
  13. "Catmull-Rom Full",
  14. nullptr
  15. };
  16. Spline::Spline() :
  17. interpolationMode_(BEZIER_CURVE)
  18. {
  19. }
  20. Spline::Spline(InterpolationMode mode) :
  21. interpolationMode_(mode)
  22. {
  23. }
  24. Spline::Spline(const Vector<Variant>& knots, InterpolationMode mode) :
  25. interpolationMode_(mode),
  26. knots_(knots)
  27. {
  28. }
  29. Variant Spline::GetPoint(float f) const
  30. {
  31. if (knots_.Size() < 2)
  32. return knots_.Size() == 1 ? knots_[0] : Variant::EMPTY;
  33. if (f > 1.f)
  34. f = 1.f;
  35. else if (f < 0.f)
  36. f = 0.f;
  37. switch (interpolationMode_)
  38. {
  39. case BEZIER_CURVE:
  40. return BezierInterpolation(knots_, f);
  41. case CATMULL_ROM_CURVE:
  42. return CatmullRomInterpolation(knots_, f);
  43. case LINEAR_CURVE:
  44. return LinearInterpolation(knots_, f);
  45. case CATMULL_ROM_FULL_CURVE:
  46. {
  47. /// \todo Do not allocate a new vector each time
  48. Vector<Variant> fullKnots;
  49. if (knots_.Size() > 1)
  50. {
  51. // Non-cyclic case: duplicate start and end
  52. if (knots_.Front() != knots_.Back())
  53. {
  54. fullKnots.Push(knots_.Front());
  55. fullKnots.Push(knots_);
  56. fullKnots.Push(knots_.Back());
  57. }
  58. // Cyclic case: smooth the tangents
  59. else
  60. {
  61. fullKnots.Push(knots_[knots_.Size() - 2]);
  62. fullKnots.Push(knots_);
  63. fullKnots.Push(knots_[1]);
  64. }
  65. }
  66. return CatmullRomInterpolation(fullKnots, f);
  67. }
  68. default:
  69. URHO3D_LOGERROR("Unsupported interpolation mode");
  70. return Variant::EMPTY;
  71. }
  72. }
  73. void Spline::SetKnot(const Variant& knot, unsigned index)
  74. {
  75. if (index < knots_.Size())
  76. {
  77. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  78. knots_[index] = knot;
  79. else if (knots_.Empty())
  80. knots_.Push(knot);
  81. else
  82. URHO3D_LOGERRORF("Attempted to set a Spline's Knot value of type %s where elements are already using %s",
  83. knot.GetTypeName().CString(), knots_[0].GetTypeName().CString());
  84. }
  85. }
  86. void Spline::AddKnot(const Variant& knot)
  87. {
  88. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  89. knots_.Push(knot);
  90. else if (knots_.Empty())
  91. knots_.Push(knot);
  92. else
  93. URHO3D_LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(),
  94. knots_[0].GetTypeName().CString());
  95. }
  96. void Spline::AddKnot(const Variant& knot, unsigned index)
  97. {
  98. if (index > knots_.Size())
  99. index = knots_.Size();
  100. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  101. knots_.Insert(index, knot);
  102. else if (knots_.Empty())
  103. knots_.Push(knot);
  104. else
  105. URHO3D_LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(),
  106. knots_[0].GetTypeName().CString());
  107. }
  108. Variant Spline::BezierInterpolation(const Vector<Variant>& knots, float t) const
  109. {
  110. if (knots.Size() == 2)
  111. {
  112. switch (knots[0].GetType())
  113. {
  114. case VAR_FLOAT:
  115. case VAR_VECTOR2:
  116. case VAR_VECTOR3:
  117. case VAR_VECTOR4:
  118. case VAR_COLOR:
  119. case VAR_DOUBLE:
  120. return LinearInterpolation(knots[0], knots[1], t);
  121. default:
  122. return Variant::EMPTY;
  123. }
  124. }
  125. else
  126. {
  127. /// \todo Do not allocate a new vector each time
  128. Vector<Variant> interpolatedKnots;
  129. for (unsigned i = 1; i < knots.Size(); i++)
  130. {
  131. switch (knots[0].GetType())
  132. {
  133. case VAR_FLOAT:
  134. case VAR_VECTOR2:
  135. case VAR_VECTOR3:
  136. case VAR_VECTOR4:
  137. case VAR_COLOR:
  138. case VAR_DOUBLE:
  139. interpolatedKnots.Push(LinearInterpolation(knots[i - 1], knots[i], t));
  140. break;
  141. default:
  142. return Variant::EMPTY;
  143. }
  144. }
  145. return BezierInterpolation(interpolatedKnots, t);
  146. }
  147. }
  148. template <typename T> Variant CalculateCatmullRom(const T& p0, const T& p1, const T& p2, const T& p3, float t, float t2, float t3)
  149. {
  150. return Variant(0.5f * ((2.0f * p1) + (-p0 + p2) * t +
  151. (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * t2 +
  152. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3));
  153. }
  154. Variant Spline::CatmullRomInterpolation(const Vector<Variant>& knots, float t) const
  155. {
  156. if (knots.Size() < 4)
  157. return Variant::EMPTY;
  158. else
  159. {
  160. if (t >= 1.f)
  161. return knots[knots.Size() - 2];
  162. auto originIndex = static_cast<int>(t * (knots.Size() - 3));
  163. t = fmodf(t * (knots.Size() - 3), 1.f);
  164. float t2 = t * t;
  165. float t3 = t2 * t;
  166. switch (knots[originIndex].GetType())
  167. {
  168. case VAR_FLOAT:
  169. return CalculateCatmullRom(knots[originIndex].GetFloat(), knots[originIndex + 1].GetFloat(),
  170. knots[originIndex + 2].GetFloat(), knots[originIndex + 3].GetFloat(), t, t2, t3);
  171. case VAR_VECTOR2:
  172. return CalculateCatmullRom(knots[originIndex].GetVector2(), knots[originIndex + 1].GetVector2(),
  173. knots[originIndex + 2].GetVector2(), knots[originIndex + 3].GetVector2(), t, t2, t3);
  174. case VAR_VECTOR3:
  175. return CalculateCatmullRom(knots[originIndex].GetVector3(), knots[originIndex + 1].GetVector3(),
  176. knots[originIndex + 2].GetVector3(), knots[originIndex + 3].GetVector3(), t, t2, t3);
  177. case VAR_VECTOR4:
  178. return CalculateCatmullRom(knots[originIndex].GetVector4(), knots[originIndex + 1].GetVector4(),
  179. knots[originIndex + 2].GetVector4(), knots[originIndex + 3].GetVector4(), t, t2, t3);
  180. case VAR_COLOR:
  181. return CalculateCatmullRom(knots[originIndex].GetColor(), knots[originIndex + 1].GetColor(),
  182. knots[originIndex + 2].GetColor(), knots[originIndex + 3].GetColor(), t, t2, t3);
  183. case VAR_DOUBLE:
  184. return CalculateCatmullRom(knots[originIndex].GetDouble(), knots[originIndex + 1].GetDouble(),
  185. knots[originIndex + 2].GetDouble(), knots[originIndex + 3].GetDouble(), t, t2, t3);
  186. default:
  187. return Variant::EMPTY;
  188. }
  189. }
  190. }
  191. Variant Spline::LinearInterpolation(const Vector<Variant>& knots, float t) const
  192. {
  193. if (knots.Size() < 2)
  194. return Variant::EMPTY;
  195. else
  196. {
  197. if (t >= 1.f)
  198. return knots.Back();
  199. int originIndex = Clamp((int)(t * (knots.Size() - 1)), 0, (int)(knots.Size() - 2));
  200. t = fmodf(t * (knots.Size() - 1), 1.f);
  201. return LinearInterpolation(knots[originIndex], knots[originIndex + 1], t);
  202. }
  203. }
  204. Variant Spline::LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const
  205. {
  206. switch (lhs.GetType())
  207. {
  208. case VAR_FLOAT:
  209. return Lerp(lhs.GetFloat(), rhs.GetFloat(), t);
  210. case VAR_VECTOR2:
  211. return lhs.GetVector2().Lerp(rhs.GetVector2(), t);
  212. case VAR_VECTOR3:
  213. return lhs.GetVector3().Lerp(rhs.GetVector3(), t);
  214. case VAR_VECTOR4:
  215. return lhs.GetVector4().Lerp(rhs.GetVector4(), t);
  216. case VAR_COLOR:
  217. return lhs.GetColor().Lerp(rhs.GetColor(), t);
  218. case VAR_DOUBLE:
  219. return Lerp(lhs.GetDouble(), rhs.GetDouble(), t);
  220. default:
  221. return Variant::EMPTY;
  222. }
  223. }
  224. }