Spline.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright (c) 2008-2017 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. #pragma once
  23. #include "../Core/Variant.h"
  24. #include "../Math/Color.h"
  25. #include "../Math/Vector2.h"
  26. #include "../Math/Vector3.h"
  27. #include "../Math/Vector4.h"
  28. namespace Atomic
  29. {
  30. enum InterpolationMode
  31. {
  32. /// Bezier interpolation.
  33. BEZIER_CURVE = 0,
  34. /// Catmull-Rom interpolation. The first and last knots control velocity and are not included on the path.
  35. CATMULL_ROM_CURVE,
  36. /// Linear interpolation.
  37. LINEAR_CURVE,
  38. /// Catmull-Rom full path interpolation. Start and end knots are duplicated or looped as necessary to move through the full path.
  39. CATMULL_ROM_FULL_CURVE
  40. };
  41. /// Spline class to get a point on it based off the interpolation mode.
  42. class ATOMIC_API Spline
  43. {
  44. public:
  45. /// Default constructor.
  46. Spline();
  47. /// Constructor setting interpolation mode.
  48. Spline(InterpolationMode mode);
  49. /// Constructor setting knots and interpolation mode.
  50. Spline(const Vector<Variant>& knots, InterpolationMode mode = BEZIER_CURVE);
  51. /// Copy constructor.
  52. Spline(const Spline& rhs);
  53. /// Copy operator.
  54. void operator =(const Spline& rhs)
  55. {
  56. knots_ = rhs.knots_;
  57. interpolationMode_ = rhs.interpolationMode_;
  58. }
  59. /// Equality operator.
  60. bool operator ==(const Spline& rhs) const
  61. {
  62. return (knots_ == rhs.knots_ && interpolationMode_ == rhs.interpolationMode_);
  63. }
  64. /// Inequality operator.
  65. bool operator !=(const Spline& rhs) const
  66. {
  67. return !(*this == rhs);
  68. }
  69. /// Return the interpolation mode.
  70. InterpolationMode GetInterpolationMode() const { return interpolationMode_; }
  71. /// Return the knots of the spline.
  72. const VariantVector& GetKnots() const { return knots_; }
  73. /// Return the knot at the specific index.
  74. Variant GetKnot(unsigned index) const { return knots_[index]; }
  75. /// Return the T of the point of the spline at f from 0.f - 1.f.
  76. Variant GetPoint(float f) const;
  77. /// Set the interpolation mode.
  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. }