Spline.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Copyright (c) 2008-2015 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 "../IO/Log.h"
  23. #include "../Core/Spline.h"
  24. namespace Urho3D
  25. {
  26. const char* interpolationModeNames[] =
  27. {
  28. "Bezier",
  29. 0
  30. };
  31. Spline::Spline() :
  32. interpolationMode_(BEZIER_CURVE)
  33. {
  34. }
  35. Spline::Spline(InterpolationMode mode) :
  36. interpolationMode_(mode)
  37. {
  38. }
  39. Spline::Spline(const Vector<Variant>& knots, InterpolationMode mode) :
  40. interpolationMode_(mode),
  41. knots_(knots)
  42. {
  43. }
  44. Spline::Spline(const Spline& rhs) :
  45. interpolationMode_(rhs.interpolationMode_),
  46. knots_(rhs.knots_)
  47. {
  48. }
  49. Variant Spline::GetPoint(float f) const
  50. {
  51. if (knots_.Size() < 2)
  52. return knots_.Size() == 1 ? knots_[0] : Variant::EMPTY;
  53. if (f > 1.f)
  54. f = 1.f;
  55. else if (f < 0.f)
  56. f = 0.f;
  57. switch (interpolationMode_)
  58. {
  59. case BEZIER_CURVE:
  60. return BezierInterpolation(knots_, f);
  61. default:
  62. LOGERROR("Unsupported interpolation mode");
  63. return Variant::EMPTY;
  64. }
  65. }
  66. void Spline::SetKnot(const Variant& knot, unsigned index)
  67. {
  68. if (index < knots_.Size())
  69. {
  70. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  71. knots_[index] = knot;
  72. else if (knots_.Empty())
  73. knots_.Push(knot);
  74. else
  75. LOGERRORF("Attempted to set a Spline's Knot value of type %s where elements are already using %s", knot.GetTypeName().CString(), knots_[0].GetTypeName().CString());
  76. }
  77. }
  78. void Spline::AddKnot(const Variant& knot)
  79. {
  80. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  81. knots_.Push(knot);
  82. else if (knots_.Empty())
  83. knots_.Push(knot);
  84. else
  85. LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(), knots_[0].GetTypeName().CString());
  86. }
  87. void Spline::AddKnot(const Variant& knot, unsigned index)
  88. {
  89. if (index > knots_.Size())
  90. index = knots_.Size();
  91. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  92. knots_.Insert(index, knot);
  93. else if (knots_.Empty())
  94. knots_.Push(knot);
  95. else
  96. LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(), knots_[0].GetTypeName().CString());
  97. }
  98. Variant Spline::BezierInterpolation(const Vector<Variant>& knots, float t) const
  99. {
  100. if (knots.Size() == 2)
  101. {
  102. switch (knots[0].GetType())
  103. {
  104. case VAR_FLOAT:
  105. case VAR_VECTOR2:
  106. case VAR_VECTOR3:
  107. case VAR_VECTOR4:
  108. case VAR_COLOR:
  109. return LinearInterpolation(knots[0], knots[1], t);
  110. default:
  111. return Variant::EMPTY;
  112. }
  113. }
  114. else
  115. {
  116. Vector<Variant> interpolatedKnots;
  117. for (unsigned i = 1; i < knots.Size(); i++)
  118. {
  119. switch (knots[0].GetType())
  120. {
  121. case VAR_FLOAT:
  122. case VAR_VECTOR2:
  123. case VAR_VECTOR3:
  124. case VAR_VECTOR4:
  125. case VAR_COLOR:
  126. interpolatedKnots.Push(LinearInterpolation(knots[i - 1], knots[i], t));
  127. break;
  128. default:
  129. return Variant::EMPTY;
  130. }
  131. }
  132. return BezierInterpolation(interpolatedKnots, t);
  133. }
  134. }
  135. Variant Spline::LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const
  136. {
  137. switch (lhs.GetType())
  138. {
  139. case VAR_FLOAT:
  140. return Lerp(lhs.GetFloat(), rhs.GetFloat(), t);
  141. case VAR_VECTOR2:
  142. return lhs.GetVector2().Lerp(rhs.GetVector2(), t);
  143. case VAR_VECTOR3:
  144. return lhs.GetVector3().Lerp(rhs.GetVector3(), t);
  145. case VAR_VECTOR4:
  146. return lhs.GetVector4().Lerp(rhs.GetVector4(), t);
  147. case VAR_COLOR:
  148. return lhs.GetColor().Lerp(rhs.GetColor(), t);
  149. default:
  150. return Variant::EMPTY;
  151. }
  152. }
  153. }