Spline.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #pragma once
  23. #include "Color.h"
  24. #include "Variant.h"
  25. #include "Vector2.h"
  26. #include "Vector3.h"
  27. #include "Vector4.h"
  28. namespace Urho3D
  29. {
  30. enum InterpolationMode
  31. {
  32. BEZIER_CURVE = 0
  33. };
  34. /// Spline class to get a point on it based off the interpolation mode.
  35. class URHO3D_API Spline
  36. {
  37. public:
  38. /// Default constructor.
  39. Spline();
  40. /// Constructor setting InterpolationMode.
  41. Spline(InterpolationMode mode);
  42. /// Constructor setting Knots and InterpolationMode.
  43. Spline(const Vector<Variant>& knots, InterpolationMode mode = BEZIER_CURVE);
  44. /// Copy constructor.
  45. Spline(const Spline& rhs);
  46. /// Copy operator.
  47. void operator= (const Spline& rhs)
  48. {
  49. knots_ = rhs.knots_;
  50. interpolationMode_ = rhs.interpolationMode_;
  51. }
  52. /// Equality operator.
  53. bool operator== (const Spline& rhs) const
  54. {
  55. return (knots_ == rhs.knots_ && interpolationMode_ == rhs.interpolationMode_);
  56. }
  57. /// Non Equality operator.
  58. bool operator!= (const Spline& rhs) const
  59. {
  60. return !(*this == rhs);
  61. }
  62. /// Return the ImplementationMode.
  63. InterpolationMode GetInterpolationMode() const { return interpolationMode_; }
  64. /// Return the Knots of the Spline.
  65. const VariantVector& GetKnots() const { return knots_; }
  66. /// Return the Knot at the specific index.
  67. Variant GetKnot(unsigned index) const { return knots_[index]; }
  68. /// Return the T of the point of the Spline at f from 0.f - 1.f.
  69. Variant GetPoint(float f) const;
  70. /// Set the InterpolationMode of the Spline.
  71. void SetInterpolationMode(InterpolationMode interpolationMode) { interpolationMode_ = interpolationMode; }
  72. /// Set the Knots of the Spline.
  73. void SetKnots(const Vector<Variant>& knots) { knots_ = knots; }
  74. /// Set the Knot value of an existing Knot.
  75. void SetKnot(const Variant& knot, unsigned index);
  76. /// Add a Knot to the end of the Spline.
  77. void AddKnot(const Variant& knot);
  78. /// Add a Knot to the Spline at a specific index.
  79. void AddKnot(const Variant& knot, unsigned index);
  80. /// Remove the last Knot on the Spline.
  81. void RemoveKnot() { knots_.Pop(); }
  82. /// Remove the Knot at the specific index.
  83. void RemoveKnot(unsigned index) { knots_.Erase(index); }
  84. /// Clear the Spline.
  85. void Clear() { knots_.Clear(); }
  86. private:
  87. /// Perform Bezier Interpolation on the Spline.
  88. Variant BezierInterpolation(const Vector<Variant>& knots, float t) const;
  89. /// LinearInterpolation between two Variants based on underlying type.
  90. Variant LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const;
  91. /// InterpolationMode.
  92. InterpolationMode interpolationMode_;
  93. /// Knots on the Spline.
  94. VariantVector knots_;
  95. };
  96. }