2
0

Spline.cpp 4.8 KB

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