Spline.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "../Precompiled.h"
  23. #include "../Core/Spline.h"
  24. #include "../IO/Log.h"
  25. namespace Atomic
  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",
  77. knot.GetTypeName().CString(), knots_[0].GetTypeName().CString());
  78. }
  79. }
  80. void Spline::AddKnot(const Variant& knot)
  81. {
  82. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  83. knots_.Push(knot);
  84. else if (knots_.Empty())
  85. knots_.Push(knot);
  86. else
  87. LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(),
  88. knots_[0].GetTypeName().CString());
  89. }
  90. void Spline::AddKnot(const Variant& knot, unsigned index)
  91. {
  92. if (index > knots_.Size())
  93. index = knots_.Size();
  94. if (knots_.Size() > 0 && knots_[0].GetType() == knot.GetType())
  95. knots_.Insert(index, knot);
  96. else if (knots_.Empty())
  97. knots_.Push(knot);
  98. else
  99. LOGERRORF("Attempted to add Knot to Spline of type %s where elements are already using %s", knot.GetTypeName().CString(),
  100. knots_[0].GetTypeName().CString());
  101. }
  102. Variant Spline::BezierInterpolation(const Vector<Variant>& knots, float t) const
  103. {
  104. if (knots.Size() == 2)
  105. {
  106. switch (knots[0].GetType())
  107. {
  108. case VAR_FLOAT:
  109. case VAR_VECTOR2:
  110. case VAR_VECTOR3:
  111. case VAR_VECTOR4:
  112. case VAR_COLOR:
  113. case VAR_DOUBLE:
  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. case VAR_DOUBLE:
  132. interpolatedKnots.Push(LinearInterpolation(knots[i - 1], knots[i], t));
  133. break;
  134. default:
  135. return Variant::EMPTY;
  136. }
  137. }
  138. return BezierInterpolation(interpolatedKnots, t);
  139. }
  140. }
  141. Variant Spline::LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const
  142. {
  143. switch (lhs.GetType())
  144. {
  145. case VAR_FLOAT:
  146. return Lerp(lhs.GetFloat(), rhs.GetFloat(), t);
  147. case VAR_VECTOR2:
  148. return lhs.GetVector2().Lerp(rhs.GetVector2(), t);
  149. case VAR_VECTOR3:
  150. return lhs.GetVector3().Lerp(rhs.GetVector3(), t);
  151. case VAR_VECTOR4:
  152. return lhs.GetVector4().Lerp(rhs.GetVector4(), t);
  153. case VAR_COLOR:
  154. return lhs.GetColor().Lerp(rhs.GetColor(), t);
  155. case VAR_DOUBLE:
  156. return Lerp(lhs.GetDouble(), rhs.GetDouble(), t);
  157. default:
  158. return Variant::EMPTY;
  159. }
  160. }
  161. }