diff options
author | Dickby <[email protected]> | 2023-09-16 19:32:33 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2023-09-16 18:32:33 +0100 |
commit | a53ec98b82b0c7d8054121ae05233b89c610e62f (patch) | |
tree | bf13e1d9b09e9c07c20d82bfabf61930824b4bf2 | |
parent | d126d2c09204a09b0b10ce4f999520fc901fbf0a (diff) | |
download | Hyprland-a53ec98b82b0c7d8054121ae05233b89c610e62f.tar.gz Hyprland-a53ec98b82b0c7d8054121ae05233b89c610e62f.zip |
bezier: Optimize CBezierCurve::getYForPoint (#3321)
-rw-r--r-- | src/helpers/BezierCurve.cpp | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/src/helpers/BezierCurve.cpp b/src/helpers/BezierCurve.cpp index 9c8fc514..e79863a3 100644 --- a/src/helpers/BezierCurve.cpp +++ b/src/helpers/BezierCurve.cpp @@ -48,27 +48,25 @@ float CBezierCurve::getXForT(float t) { // Todo: this probably can be done better and faster float CBezierCurve::getYForPoint(float x) { - if (x >= 1.0) - return 1.0; - - // binary search for the range UPDOWN X - int upperT = BAKEDPOINTS - 1; - int lowerT = 0; - int mid = upperT / 2; - - while (std::abs(upperT - lowerT) > 1) { - if (m_aPointsBaked[mid].x > x) { - upperT = mid; - } else { - lowerT = mid; - } - - mid = (upperT + lowerT) / 2; + if (x >= 1.f) + return 1.f; + + int index = 0; + bool below = true; + for (int step = (BAKEDPOINTS + 1) / 2; step > 0; step /= 2) { + if (below) + index += step; + else + index -= step; + + below = m_aPointsBaked[index].x < x; } + int lowerIndex = index - (!below || index == BAKEDPOINTS - 1); + // in the name of performance i shall make a hack - const auto LOWERPOINT = &m_aPointsBaked[std::clamp(lowerT, 0, BAKEDPOINTS - 1)]; - const auto UPPERPOINT = &m_aPointsBaked[std::clamp(upperT, 0, BAKEDPOINTS - 1)]; + const auto LOWERPOINT = &m_aPointsBaked[lowerIndex]; + const auto UPPERPOINT = &m_aPointsBaked[lowerIndex + 1]; const auto PERCINDELTA = (x - LOWERPOINT->x) / (UPPERPOINT->x - LOWERPOINT->x); @@ -76,4 +74,4 @@ float CBezierCurve::getYForPoint(float x) { return 0.f; return LOWERPOINT->y + (UPPERPOINT->y - LOWERPOINT->y) * PERCINDELTA; -}
\ No newline at end of file +} |