Spline.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  24. #include "Log.h"
  25. #include "Node.h"
  26. #include "Scene.h"
  27. #include "Spline.h"
  28. namespace Urho3D
  29. {
  30. extern const char* SCENE_CATEGORY;
  31. Spline::Spline(Context* context) :
  32. Component(context),
  33. interpolationMode_(BEZIER_CURVE),
  34. speed_(1.f),
  35. elapsedTime_(0.f),
  36. length_(0.f),
  37. traveled_(0.f),
  38. dirty_(false)
  39. {
  40. }
  41. void Spline::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<Spline>(SCENE_CATEGORY);
  44. ACCESSOR_ATTRIBUTE(Spline, VAR_VARIANTVECTOR, "Control Points", GetControlPointsAttr, SetControlPointsAttr, VariantVector, Variant::emptyVariantVector, AM_FILE);
  45. ATTRIBUTE(Spline, VAR_FLOAT, "Speed", speed_, 1.f, AM_FILE);
  46. ATTRIBUTE(Spline, VAR_INT, "Interpolation Mode", interpolationMode_, BEZIER_CURVE, AM_FILE);
  47. ATTRIBUTE(Spline, VAR_FLOAT, "Traveled", traveled_, 0.f, AM_FILE | AM_NOEDIT);
  48. ATTRIBUTE(Spline, VAR_FLOAT, "Elapsed Time", elapsedTime_, 0.f, AM_FILE | AM_NOEDIT);
  49. }
  50. void Spline::SetControlPoints(const PODVector<Vector3>& controlPoints)
  51. {
  52. controlPoints_ = controlPoints;
  53. // We can calculate the length here because all the control points have changed so it shouldn't be too expensive.
  54. CalculateLength();
  55. }
  56. void Spline::SetPosition(float factor)
  57. {
  58. float t = factor;
  59. if (t < 0.f)
  60. t = 0.0f;
  61. else if (t > 1.0f)
  62. t = 1.0f;
  63. traveled_ = t;
  64. }
  65. Vector3 Spline::GetPosition() const
  66. {
  67. return GetPoint(traveled_);
  68. }
  69. Vector3 Spline::GetPoint(float factor) const
  70. {
  71. float t = factor;
  72. if (t < 0.f)
  73. t = 0.0f;
  74. else if (t > 1.0f)
  75. t = 1.0f;
  76. switch (interpolationMode_)
  77. {
  78. case BEZIER_CURVE:
  79. return BezierMove(controlPoints_, t);
  80. default:
  81. return Vector3::ZERO;
  82. }
  83. }
  84. void Spline::Push(const Vector3& controlPoint)
  85. {
  86. controlPoints_.Push(controlPoint);
  87. // Calculate the length at the next move so we don't recalculate the entire length multiple times if more than one control point is changed.
  88. dirty_ = true;
  89. }
  90. void Spline::Pop()
  91. {
  92. controlPoints_.Pop();
  93. // Calculate the length at the next move so we don't recalculate the entire length multiple times if more than one control point is changed.
  94. dirty_ = true;
  95. }
  96. void Spline::Move(float timeStep)
  97. {
  98. if (dirty_)
  99. CalculateLength();
  100. if (traveled_ >= 1.0f || length_ <= 0.0f)
  101. return;
  102. elapsedTime_ += timeStep;
  103. // Calculate where we should be on the spline based on length, speed and time. If that is less than the set traveled_ don't move till caught up.
  104. float distanceCovered = elapsedTime_ * speed_;
  105. traveled_ = distanceCovered / length_;
  106. switch (interpolationMode_)
  107. {
  108. case BEZIER_CURVE:
  109. if (controlPoints_.Size() < 2)
  110. {
  111. LOGERRORF("Spline on Node[%d,%s] in Beizer Curve mode attempted with less than two control points.", GetNode()->GetID(), GetNode()->GetName().CString());
  112. return;
  113. }
  114. GetNode()->SetPosition(BezierMove(controlPoints_,traveled_));
  115. break;
  116. }
  117. }
  118. void Spline::Reset()
  119. {
  120. traveled_ = 0.f;
  121. elapsedTime_ = 0.f;
  122. }
  123. Urho3D::VariantVector Spline::GetControlPointsAttr() const
  124. {
  125. VariantVector ret;
  126. for (unsigned i = 0; i < controlPoints_.Size(); i++)
  127. ret.Push(controlPoints_[i]);
  128. return ret;
  129. }
  130. void Spline::SetControlPointsAttr(VariantVector value)
  131. {
  132. for (unsigned i = 0; i < value.Size(); i++)
  133. controlPoints_.Push(value[i].GetVector3());
  134. CalculateLength();
  135. }
  136. void Spline::CalculateLength()
  137. {
  138. if (dirty_)
  139. dirty_ = false;
  140. length_ = 0.f;
  141. if (controlPoints_.Size() <= 0)
  142. {
  143. return;
  144. }
  145. switch (interpolationMode_)
  146. {
  147. case BEZIER_CURVE:
  148. Vector3 a = controlPoints_[0];
  149. for (float f = 0.000f; f <= 1.000f; f += 0.001f)
  150. {
  151. Vector3 b = BezierMove(controlPoints_, f);
  152. length_ += Abs((a - b).Length());
  153. a = b;
  154. }
  155. break;
  156. }
  157. }
  158. Vector3 Spline::BezierMove(const PODVector<Vector3>& controlPoints, float t) const
  159. {
  160. if (controlPoints.Size() == 2)
  161. {
  162. return controlPoints[0].Lerp(controlPoints[1], t);
  163. }
  164. else
  165. {
  166. PODVector<Vector3> newControlPoints;
  167. for (unsigned i = 1; i < controlPoints.Size(); i++)
  168. {
  169. newControlPoints.Push(controlPoints[i - 1].Lerp(controlPoints[i], t));
  170. }
  171. return BezierMove(newControlPoints, t);
  172. }
  173. }
  174. }