Spline.cpp 5.0 KB

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