Spline.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Core/Variant.h"
  25. #include "../Math/Color.h"
  26. #include "../Math/Vector2.h"
  27. #include "../Math/Vector3.h"
  28. #include "../Math/Vector4.h"
  29. namespace Urho3D
  30. {
  31. enum InterpolationMode
  32. {
  33. /// Bezier interpolation.
  34. BEZIER_CURVE = 0,
  35. /// Catmull-Rom interpolation. The first and last knots control velocity and are not included on the path.
  36. CATMULL_ROM_CURVE,
  37. /// Linear interpolation.
  38. LINEAR_CURVE,
  39. /// Catmull-Rom full path interpolation. Start and end knots are duplicated or looped as necessary to move through the full path.
  40. CATMULL_ROM_FULL_CURVE
  41. };
  42. /// Spline class to get a point on it based off the interpolation mode.
  43. class URHO3D_API Spline
  44. {
  45. public:
  46. /// Default constructor.
  47. Spline();
  48. /// Constructor setting interpolation mode.
  49. explicit Spline(InterpolationMode mode);
  50. /// Constructor setting knots and interpolation mode.
  51. explicit Spline(const Vector<Variant>& knots, InterpolationMode mode = BEZIER_CURVE);
  52. /// Copy constructor.
  53. Spline(const Spline& rhs) = default;
  54. /// Copy operator.
  55. Spline& operator =(const Spline& rhs) = default;
  56. /// Equality operator.
  57. bool operator ==(const Spline& rhs) const
  58. {
  59. return (knots_ == rhs.knots_ && interpolationMode_ == rhs.interpolationMode_);
  60. }
  61. /// Inequality operator.
  62. bool operator !=(const Spline& rhs) const
  63. {
  64. return !(*this == rhs);
  65. }
  66. /// Return the interpolation mode.
  67. /// @property
  68. InterpolationMode GetInterpolationMode() const { return interpolationMode_; }
  69. /// Return the knots of the spline.
  70. const VariantVector& GetKnots() const { return knots_; }
  71. /// Return the knot at the specific index.
  72. /// @property
  73. Variant GetKnot(unsigned index) const { return knots_[index]; }
  74. /// Return the T of the point of the spline at f from 0.f - 1.f.
  75. Variant GetPoint(float f) const;
  76. /// Set the interpolation mode.
  77. /// @property
  78. void SetInterpolationMode(InterpolationMode interpolationMode) { interpolationMode_ = interpolationMode; }
  79. /// Set the knots of the spline.
  80. void SetKnots(const Vector<Variant>& knots) { knots_ = knots; }
  81. /// Set the value of an existing knot.
  82. void SetKnot(const Variant& knot, unsigned index);
  83. /// Add a knot to the end of the spline.
  84. void AddKnot(const Variant& knot);
  85. /// Add a knot to the spline at a specific index.
  86. void AddKnot(const Variant& knot, unsigned index);
  87. /// Remove the last knot on the spline.
  88. void RemoveKnot() { knots_.Pop(); }
  89. /// Remove the knot at the specific index.
  90. void RemoveKnot(unsigned index) { knots_.Erase(index); }
  91. /// Clear the spline.
  92. void Clear() { knots_.Clear(); }
  93. private:
  94. /// Perform Bezier interpolation on the spline.
  95. Variant BezierInterpolation(const Vector<Variant>& knots, float t) const;
  96. /// Perform Spline interpolation on the spline.
  97. Variant CatmullRomInterpolation(const Vector<Variant>& knots, float t) const;
  98. /// Perform linear interpolation on the spline.
  99. Variant LinearInterpolation(const Vector<Variant>& knots, float t) const;
  100. /// Linear interpolation between two Variants based on underlying type.
  101. Variant LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const;
  102. /// Interpolation mode.
  103. InterpolationMode interpolationMode_;
  104. /// Knots on the spline.
  105. VariantVector knots_;
  106. };
  107. }