2
0

Spline.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 < 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 > knots_.Size())
  95. index = knots_.Size();
  96. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  97. knots_.Insert(index, knot);
  98. else if (knots_.Empty())
  99. knots_.Push(knot);
  100. else
  101. LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(), knots_[0].GetTypeName().CString());
  102. }
  103. Variant Spline::BezierInterpolation(const Vector<Variant>& knots, float t) const
  104. {
  105. if (knots.Size() == 2)
  106. {
  107. switch (knots[0].GetType())
  108. {
  109. case VAR_FLOAT:
  110. case VAR_VECTOR2:
  111. case VAR_VECTOR3:
  112. case VAR_VECTOR4:
  113. case VAR_COLOR:
  114. return LinearInterpolation(knots[0], knots[1], t);
  115. default:
  116. return Variant::EMPTY;
  117. }
  118. }
  119. else
  120. {
  121. Vector<Variant> interpolatedKnots;
  122. for (unsigned i = 1; i < knots.Size(); i++)
  123. {
  124. switch (knots[0].GetType())
  125. {
  126. case VAR_FLOAT:
  127. case VAR_VECTOR2:
  128. case VAR_VECTOR3:
  129. case VAR_VECTOR4:
  130. case VAR_COLOR:
  131. interpolatedKnots.Push(LinearInterpolation(knots[i - 1], knots[i], t));
  132. break;
  133. default:
  134. return Variant::EMPTY;
  135. }
  136. }
  137. return BezierInterpolation(interpolatedKnots, t);
  138. }
  139. }
  140. Variant Spline::LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const
  141. {
  142. switch (lhs.GetType())
  143. {
  144. case VAR_FLOAT:
  145. return Lerp(lhs.GetFloat(), rhs.GetFloat(), t);
  146. case VAR_VECTOR2:
  147. return lhs.GetVector2().Lerp(rhs.GetVector2(), t);
  148. case VAR_VECTOR3:
  149. return lhs.GetVector3().Lerp(rhs.GetVector3(), t);
  150. case VAR_VECTOR4:
  151. return lhs.GetVector4().Lerp(rhs.GetVector4(), t);
  152. case VAR_COLOR:
  153. return lhs.GetColor().Lerp(rhs.GetColor(), t);
  154. default:
  155. return Variant::EMPTY;
  156. }
  157. }
  158. }