Spline.cpp 5.4 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. attached_(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. ATTRIBUTE(Spline, VAR_FLOAT, "Length", length_, 0.f, AM_FILE | AM_NOEDIT);
  50. ATTRIBUTE(Spline, VAR_BOOL, "Attached", attached_, false, AM_FILE);
  51. }
  52. void Spline::SetControlPoints(const Vector<Vector3> controlPoints)
  53. {
  54. controlPoints_ = controlPoints;
  55. }
  56. void Spline::SetInterpolationMode(InterpolationMode interpolationMode)
  57. {
  58. interpolationMode_ = interpolationMode;
  59. }
  60. Vector3 Spline::GetPosition(float factor)
  61. {
  62. float t = factor;
  63. if (t < 0.f)
  64. t = 0.0f;
  65. else if (t > 1.0f)
  66. t = 1.0f;
  67. switch (interpolationMode_)
  68. {
  69. case BEZIER_CURVE:
  70. return BezierMove(controlPoints_, t);
  71. default:
  72. return Vector3::ZERO;
  73. }
  74. }
  75. void Spline::Push(const Vector3& controlPoint)
  76. {
  77. controlPoints_.Push(controlPoint);
  78. }
  79. void Spline::Pop()
  80. {
  81. controlPoints_.Pop();
  82. }
  83. void Spline::Attach()
  84. {
  85. if (controlPoints_.Size() > 0)
  86. {
  87. CalculateLength();
  88. switch (interpolationMode_)
  89. {
  90. case BEZIER_CURVE:
  91. if (controlPoints_.Size() < 2)
  92. {
  93. LOGERRORF("Spline on Node[%d,%s] in Beizer Curve mode attempted with less than two control points.", GetNode()->GetID(), GetNode()->GetName().CString());
  94. return;
  95. }
  96. GetNode()->SetPosition(BezierMove(controlPoints_, traveled_));
  97. break;
  98. }
  99. attached_ = true;
  100. }
  101. }
  102. void Spline::Move(float timeStep)
  103. {
  104. if (!attached_ || traveled_ >= 1.0f || length_ <= 0.0f)
  105. return;
  106. elapsedTime_ += timeStep;
  107. float distanceCovered = elapsedTime_ * speed_;
  108. traveled_ = distanceCovered / length_;
  109. switch (interpolationMode_)
  110. {
  111. case BEZIER_CURVE:
  112. if (controlPoints_.Size() < 2)
  113. {
  114. LOGERRORF("Spline on Node[%d,%s] in Beizer Curve mode attempted with less than two control points.", GetNode()->GetID(), GetNode()->GetName().CString());
  115. return;
  116. }
  117. GetNode()->SetPosition(BezierMove(controlPoints_,traveled_));
  118. break;
  119. }
  120. }
  121. void Spline::Detach()
  122. {
  123. attached_ = false;
  124. }
  125. void Spline::Reset()
  126. {
  127. attached_ = false;
  128. traveled_ = 0.f;
  129. elapsedTime_ = 0.f;
  130. }
  131. Urho3D::VariantVector Spline::GetControlPointsAttr() const
  132. {
  133. VariantVector ret;
  134. for (unsigned i = 0; i < controlPoints_.Size(); i++)
  135. ret.Push(controlPoints_[i]);
  136. return ret;
  137. }
  138. void Spline::SetControlPointsAttr(VariantVector value)
  139. {
  140. for (unsigned i = 0; i < value.Size(); i++)
  141. controlPoints_.Push(value[i].GetVector3());
  142. }
  143. void Spline::CalculateLength()
  144. {
  145. length_ = 0.f;
  146. if (controlPoints_.Size() <= 0)
  147. {
  148. return;
  149. }
  150. switch (interpolationMode_)
  151. {
  152. case BEZIER_CURVE:
  153. Vector3 a = controlPoints_[0];
  154. for (float f = 0.000f; f <= 1.000f; f += 0.001f)
  155. {
  156. Vector3 b = BezierMove(controlPoints_, f);
  157. length_ += Abs((a - b).Length());
  158. a = b;
  159. }
  160. break;
  161. }
  162. }
  163. Vector3 Spline::BezierMove(Vector<Vector3>& controlPoints, float t)
  164. {
  165. if (controlPoints.Size() == 2)
  166. {
  167. return controlPoints[0].Lerp(controlPoints[1], t);
  168. }
  169. else
  170. {
  171. Vector<Vector3> newControlPoints;
  172. for (unsigned i = 1; i < controlPoints.Size(); i++)
  173. {
  174. newControlPoints.Push(controlPoints[i - 1].Lerp(controlPoints[i], t));
  175. }
  176. return BezierMove(newControlPoints, t);
  177. }
  178. }
  179. }