2
0

LinearCurve.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/ObjectStream/SerializableObject.h>
  6. #include <Jolt/Core/QuickSort.h>
  7. JPH_NAMESPACE_BEGIN
  8. class StreamOut;
  9. class StreamIn;
  10. // A set of points (x, y) that form a linear curve
  11. class JPH_EXPORT LinearCurve
  12. {
  13. public:
  14. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, LinearCurve)
  15. /// A point on the curve
  16. class Point
  17. {
  18. public:
  19. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Point)
  20. float mX = 0.0f;
  21. float mY = 0.0f;
  22. };
  23. /// Remove all points
  24. void Clear() { mPoints.clear(); }
  25. /// Reserve memory for inNumPoints points
  26. void Reserve(uint inNumPoints) { mPoints.reserve(inNumPoints); }
  27. /// Add a point to the curve. Points must be inserted in ascending X or Sort() needs to be called when all points have been added.
  28. /// @param inX X value
  29. /// @param inY Y value
  30. void AddPoint(float inX, float inY) { mPoints.push_back({ inX, inY }); }
  31. /// Sort the points on X ascending
  32. void Sort() { QuickSort(mPoints.begin(), mPoints.end(), [](const Point &inLHS, const Point &inRHS) { return inLHS.mX < inRHS.mX; }); }
  33. /// Get the lowest X value
  34. float GetMinX() const { return mPoints.empty()? 0.0f : mPoints.front().mX; }
  35. /// Get the highest X value
  36. float GetMaxX() const { return mPoints.empty()? 0.0f : mPoints.back().mX; }
  37. /// Sample value on the curve
  38. /// @param inX X value to sample at
  39. /// @return Interpolated Y value
  40. float GetValue(float inX) const;
  41. /// Saves the state of this object in binary form to inStream.
  42. void SaveBinaryState(StreamOut &inStream) const;
  43. /// Restore the state of this object from inStream.
  44. void RestoreBinaryState(StreamIn &inStream);
  45. /// The points on the curve, should be sorted ascending by x
  46. using Points = Array<Point>;
  47. Points mPoints;
  48. };
  49. JPH_NAMESPACE_END