Spline.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Spline.h"
  24. #include "../IO/Log.h"
  25. namespace Urho3D
  26. {
  27. const char* interpolationModeNames[] =
  28. {
  29. "Bezier",
  30. "Catmull-Rom",
  31. "Linear",
  32. "Catmull-Rom Full",
  33. nullptr
  34. };
  35. Spline::Spline() :
  36. interpolationMode_(BEZIER_CURVE)
  37. {
  38. }
  39. Spline::Spline(InterpolationMode mode) :
  40. interpolationMode_(mode)
  41. {
  42. }
  43. Spline::Spline(const Vector<Variant>& knots, InterpolationMode mode) :
  44. interpolationMode_(mode),
  45. knots_(knots)
  46. {
  47. }
  48. Spline::Spline(const Spline& rhs) :
  49. interpolationMode_(rhs.interpolationMode_),
  50. knots_(rhs.knots_)
  51. {
  52. }
  53. Variant Spline::GetPoint(float f) const
  54. {
  55. if (knots_.Size() < 2)
  56. return knots_.Size() == 1 ? knots_[0] : Variant::EMPTY;
  57. if (f > 1.f)
  58. f = 1.f;
  59. else if (f < 0.f)
  60. f = 0.f;
  61. switch (interpolationMode_)
  62. {
  63. case BEZIER_CURVE:
  64. return BezierInterpolation(knots_, f);
  65. case CATMULL_ROM_CURVE:
  66. return CatmullRomInterpolation(knots_, f);
  67. case LINEAR_CURVE:
  68. return LinearInterpolation(knots_, f);
  69. case CATMULL_ROM_FULL_CURVE:
  70. {
  71. /// \todo Do not allocate a new vector each time
  72. Vector<Variant> fullKnots;
  73. if (knots_.Size() > 1)
  74. {
  75. // Non-cyclic case: duplicate start and end
  76. if (knots_.Front() != knots_.Back())
  77. {
  78. fullKnots.Push(knots_.Front());
  79. fullKnots.Push(knots_);
  80. fullKnots.Push(knots_.Back());
  81. }
  82. // Cyclic case: smooth the tangents
  83. else
  84. {
  85. fullKnots.Push(knots_[knots_.Size() - 2]);
  86. fullKnots.Push(knots_);
  87. fullKnots.Push(knots_[1]);
  88. }
  89. }
  90. return CatmullRomInterpolation(fullKnots, f);
  91. }
  92. default:
  93. URHO3D_LOGERROR("Unsupported interpolation mode");
  94. return Variant::EMPTY;
  95. }
  96. }
  97. void Spline::SetKnot(const Variant& knot, unsigned index)
  98. {
  99. if (index < knots_.Size())
  100. {
  101. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  102. knots_[index] = knot;
  103. else if (knots_.Empty())
  104. knots_.Push(knot);
  105. else
  106. URHO3D_LOGERRORF("Attempted to set a Spline's Knot value of type %s where elements are already using %s",
  107. knot.GetTypeName().CString(), knots_[0].GetTypeName().CString());
  108. }
  109. }
  110. void Spline::AddKnot(const Variant& knot)
  111. {
  112. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  113. knots_.Push(knot);
  114. else if (knots_.Empty())
  115. knots_.Push(knot);
  116. else
  117. URHO3D_LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(),
  118. knots_[0].GetTypeName().CString());
  119. }
  120. void Spline::AddKnot(const Variant& knot, unsigned index)
  121. {
  122. if (index > knots_.Size())
  123. index = knots_.Size();
  124. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  125. knots_.Insert(index, knot);
  126. else if (knots_.Empty())
  127. knots_.Push(knot);
  128. else
  129. URHO3D_LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(),
  130. knots_[0].GetTypeName().CString());
  131. }
  132. Variant Spline::BezierInterpolation(const Vector<Variant>& knots, float t) const
  133. {
  134. if (knots.Size() == 2)
  135. {
  136. switch (knots[0].GetType())
  137. {
  138. case VAR_FLOAT:
  139. case VAR_VECTOR2:
  140. case VAR_VECTOR3:
  141. case VAR_VECTOR4:
  142. case VAR_COLOR:
  143. case VAR_DOUBLE:
  144. return LinearInterpolation(knots[0], knots[1], t);
  145. default:
  146. return Variant::EMPTY;
  147. }
  148. }
  149. else
  150. {
  151. /// \todo Do not allocate a new vector each time
  152. Vector<Variant> interpolatedKnots;
  153. for (unsigned i = 1; i < knots.Size(); i++)
  154. {
  155. switch (knots[0].GetType())
  156. {
  157. case VAR_FLOAT:
  158. case VAR_VECTOR2:
  159. case VAR_VECTOR3:
  160. case VAR_VECTOR4:
  161. case VAR_COLOR:
  162. case VAR_DOUBLE:
  163. interpolatedKnots.Push(LinearInterpolation(knots[i - 1], knots[i], t));
  164. break;
  165. default:
  166. return Variant::EMPTY;
  167. }
  168. }
  169. return BezierInterpolation(interpolatedKnots, t);
  170. }
  171. }
  172. template <typename T> Variant CalculateCatmullRom(const T& p0, const T& p1, const T& p2, const T& p3, float t, float t2, float t3)
  173. {
  174. return Variant(0.5f * ((2.0f * p1) + (-p0 + p2) * t +
  175. (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * t2 +
  176. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3));
  177. }
  178. Variant Spline::CatmullRomInterpolation(const Vector<Variant>& knots, float t) const
  179. {
  180. if (knots.Size() < 4)
  181. return Variant::EMPTY;
  182. else
  183. {
  184. if (t >= 1.f)
  185. return knots[knots.Size() - 2];
  186. auto originIndex = static_cast<int>(t * (knots.Size() - 3));
  187. t = fmodf(t * (knots.Size() - 3), 1.f);
  188. float t2 = t * t;
  189. float t3 = t2 * t;
  190. switch (knots[originIndex].GetType())
  191. {
  192. case VAR_FLOAT:
  193. return CalculateCatmullRom(knots[originIndex].GetFloat(), knots[originIndex + 1].GetFloat(),
  194. knots[originIndex + 2].GetFloat(), knots[originIndex + 3].GetFloat(), t, t2, t3);
  195. case VAR_VECTOR2:
  196. return CalculateCatmullRom(knots[originIndex].GetVector2(), knots[originIndex + 1].GetVector2(),
  197. knots[originIndex + 2].GetVector2(), knots[originIndex + 3].GetVector2(), t, t2, t3);
  198. case VAR_VECTOR3:
  199. return CalculateCatmullRom(knots[originIndex].GetVector3(), knots[originIndex + 1].GetVector3(),
  200. knots[originIndex + 2].GetVector3(), knots[originIndex + 3].GetVector3(), t, t2, t3);
  201. case VAR_VECTOR4:
  202. return CalculateCatmullRom(knots[originIndex].GetVector4(), knots[originIndex + 1].GetVector4(),
  203. knots[originIndex + 2].GetVector4(), knots[originIndex + 3].GetVector4(), t, t2, t3);
  204. case VAR_COLOR:
  205. return CalculateCatmullRom(knots[originIndex].GetColor(), knots[originIndex + 1].GetColor(),
  206. knots[originIndex + 2].GetColor(), knots[originIndex + 3].GetColor(), t, t2, t3);
  207. case VAR_DOUBLE:
  208. return CalculateCatmullRom(knots[originIndex].GetDouble(), knots[originIndex + 1].GetDouble(),
  209. knots[originIndex + 2].GetDouble(), knots[originIndex + 3].GetDouble(), t, t2, t3);
  210. default:
  211. return Variant::EMPTY;
  212. }
  213. }
  214. }
  215. Variant Spline::LinearInterpolation(const Vector<Variant>& knots, float t) const
  216. {
  217. if (knots.Size() < 2)
  218. return Variant::EMPTY;
  219. else
  220. {
  221. if (t >= 1.f)
  222. return knots.Back();
  223. int originIndex = Clamp((int)(t * (knots.Size() - 1)), 0, (int)(knots.Size() - 2));
  224. t = fmodf(t * (knots.Size() - 1), 1.f);
  225. return LinearInterpolation(knots[originIndex], knots[originIndex + 1], t);
  226. }
  227. }
  228. Variant Spline::LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const
  229. {
  230. switch (lhs.GetType())
  231. {
  232. case VAR_FLOAT:
  233. return Lerp(lhs.GetFloat(), rhs.GetFloat(), t);
  234. case VAR_VECTOR2:
  235. return lhs.GetVector2().Lerp(rhs.GetVector2(), t);
  236. case VAR_VECTOR3:
  237. return lhs.GetVector3().Lerp(rhs.GetVector3(), t);
  238. case VAR_VECTOR4:
  239. return lhs.GetVector4().Lerp(rhs.GetVector4(), t);
  240. case VAR_COLOR:
  241. return lhs.GetColor().Lerp(rhs.GetColor(), t);
  242. case VAR_DOUBLE:
  243. return Lerp(lhs.GetDouble(), rhs.GetDouble(), t);
  244. default:
  245. return Variant::EMPTY;
  246. }
  247. }
  248. }