Spline.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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* LOGIC_CATEGORY;
  31. static const char* interpolationModeNames[] =
  32. {
  33. "Bezier",
  34. 0
  35. };
  36. Spline::Spline(Context* context) :
  37. Component(context),
  38. interpolationMode_(BEZIER_CURVE),
  39. speed_(1.f),
  40. elapsedTime_(0.f),
  41. length_(0.f),
  42. traveled_(0.f),
  43. dirty_(false)
  44. {
  45. }
  46. void Spline::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<Spline>(LOGIC_CATEGORY);
  49. ATTRIBUTE(Spline, VAR_FLOAT, "Speed", speed_, 1.f, AM_FILE);
  50. ENUM_ATTRIBUTE(Spline, "Interpolation Mode", interpolationMode_, interpolationModeNames, BEZIER_CURVE, AM_FILE);
  51. ATTRIBUTE(Spline, VAR_FLOAT, "Traveled", traveled_, 0.f, AM_FILE | AM_NOEDIT);
  52. ATTRIBUTE(Spline, VAR_FLOAT, "Elapsed Time", elapsedTime_, 0.f, AM_FILE | AM_NOEDIT);
  53. ACCESSOR_ATTRIBUTE(Spline, VAR_VARIANTVECTOR, "Control Points", GetControlPointsAttr, SetControlPointsAttr, VariantVector, Variant::emptyVariantVector, AM_FILE);
  54. }
  55. void Spline::SetControlPoints(const PODVector<Vector3>& controlPoints)
  56. {
  57. controlPoints_ = controlPoints;
  58. // We can calculate the length here because all the control points have changed so it shouldn't be too expensive.
  59. CalculateLength();
  60. }
  61. void Spline::SetPosition(float factor)
  62. {
  63. float t = factor;
  64. if (t < 0.f)
  65. t = 0.0f;
  66. else if (t > 1.0f)
  67. t = 1.0f;
  68. traveled_ = t;
  69. }
  70. Vector3 Spline::GetPosition() const
  71. {
  72. return GetPoint(traveled_);
  73. }
  74. Vector3 Spline::GetPoint(float factor) const
  75. {
  76. float t = factor;
  77. if (t < 0.f)
  78. t = 0.0f;
  79. else if (t > 1.0f)
  80. t = 1.0f;
  81. switch (interpolationMode_)
  82. {
  83. case BEZIER_CURVE:
  84. return BezierMove(controlPoints_, t);
  85. default:
  86. return Vector3::ZERO;
  87. }
  88. }
  89. void Spline::Push(const Vector3& controlPoint)
  90. {
  91. controlPoints_.Push(controlPoint);
  92. // 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.
  93. dirty_ = true;
  94. }
  95. void Spline::Pop()
  96. {
  97. controlPoints_.Pop();
  98. // 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.
  99. dirty_ = true;
  100. }
  101. void Spline::Move(float timeStep)
  102. {
  103. if (dirty_)
  104. CalculateLength();
  105. if (traveled_ >= 1.0f || length_ <= 0.0f)
  106. return;
  107. elapsedTime_ += timeStep;
  108. // 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.
  109. float distanceCovered = elapsedTime_ * speed_;
  110. traveled_ = distanceCovered / length_;
  111. switch (interpolationMode_)
  112. {
  113. case BEZIER_CURVE:
  114. if (controlPoints_.Size() < 2)
  115. {
  116. LOGERRORF("Spline on Node[%d,%s] in Bezier Curve mode attempted with less than two control points.", GetNode()->GetID(), GetNode()->GetName().CString());
  117. return;
  118. }
  119. GetNode()->SetPosition(BezierMove(controlPoints_,traveled_));
  120. break;
  121. }
  122. }
  123. void Spline::Reset()
  124. {
  125. traveled_ = 0.f;
  126. elapsedTime_ = 0.f;
  127. }
  128. Urho3D::VariantVector Spline::GetControlPointsAttr() const
  129. {
  130. VariantVector ret;
  131. // Store number of points first for editing
  132. ret.Push(controlPoints_.Size());
  133. for (unsigned i = 0; i < controlPoints_.Size(); i++)
  134. ret.Push(controlPoints_[i]);
  135. return ret;
  136. }
  137. void Spline::SetControlPointsAttr(VariantVector value)
  138. {
  139. controlPoints_.Clear();
  140. unsigned index = 0;
  141. unsigned numPoints = index < value.Size() ? value[index++].GetUInt() : 0;
  142. // Prevent negative value being assigned from the editor
  143. if (numPoints > M_MAX_INT)
  144. numPoints = 0;
  145. for (unsigned i = 0; i < numPoints; ++i)
  146. {
  147. Vector3 point = index < value.Size() ? value[index++].GetVector3() : Vector3::ZERO;
  148. controlPoints_.Push(point);
  149. }
  150. CalculateLength();
  151. }
  152. void Spline::CalculateLength()
  153. {
  154. if (dirty_)
  155. dirty_ = false;
  156. length_ = 0.f;
  157. if (controlPoints_.Size() <= 0)
  158. {
  159. return;
  160. }
  161. switch (interpolationMode_)
  162. {
  163. case BEZIER_CURVE:
  164. Vector3 a = controlPoints_[0];
  165. for (float f = 0.000f; f <= 1.000f; f += 0.001f)
  166. {
  167. Vector3 b = BezierMove(controlPoints_, f);
  168. length_ += Abs((a - b).Length());
  169. a = b;
  170. }
  171. break;
  172. }
  173. }
  174. Vector3 Spline::BezierMove(const PODVector<Vector3>& controlPoints, float t) const
  175. {
  176. if (controlPoints.Size() == 2)
  177. {
  178. return controlPoints[0].Lerp(controlPoints[1], t);
  179. }
  180. else
  181. {
  182. PODVector<Vector3> newControlPoints;
  183. for (unsigned i = 1; i < controlPoints.Size(); i++)
  184. {
  185. newControlPoints.Push(controlPoints[i - 1].Lerp(controlPoints[i], t));
  186. }
  187. return BezierMove(newControlPoints, t);
  188. }
  189. }
  190. }