Spline.cpp 6.0 KB

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