Spline.h 3.4 KB

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