Last active
September 9, 2024 21:41
-
-
Save kpcftsz/c3b324021284b107f190a4843529051f to your computer and use it in GitHub Desktop.
B-spline utility for C++17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************** | |
BSpline.h - Utilities for BSpline operations | |
This is a C++ adaptation of Tagussan's JavaScript BSpline library. Typically | |
BSpline basis functions are implemented recursively by De Boor's algorithm, | |
but in this case we're using pre-calculated uniform knot BSpline bases for | |
speed. Unlike Tagussan's library, this only supports 2D coordinate systems. | |
--- | |
The MIT License (MIT) | |
Copyright (c) 2014-2024 KP, Tagussan | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
***************************************************************************/ | |
#pragma once | |
#include <vector> | |
#include <utility> | |
namespace bspline { | |
struct XYf { float x, y; }; | |
template <unsigned int Degree> | |
class BSpline | |
{ | |
public: | |
BSpline(std::vector<XYf> points): | |
points(std::move(points)) | |
{ | |
} | |
constexpr float Basis(float x) | |
{ | |
static_assert(Degree >= 2 && Degree <= 5, "Degree must be within range 2-5"); | |
if constexpr (Degree == 2) | |
{ | |
if (-0.5 <= x && x < 0.5) | |
return 0.75 - x * x; | |
else if (0.5 <= x && x <= 1.5) | |
return 1.125 + (-1.5 + x / 2.0) * x; | |
else if (-1.5 <= x && x < -0.5) | |
return 1.125 + (1.5 + x / 2.0) * x; | |
else | |
return 0; | |
} | |
else if constexpr (Degree == 3) | |
{ | |
if (-1 <= x && x < 0) | |
return 2.0 / 3.0 + (-1.0 - x / 2.0) * x * x; | |
else if (1 <= x && x <= 2) | |
return 4.0 / 3.0 + x * (-2.0 + (1.0 - x / 6.0) * x); | |
else if (-2 <= x && x < -1) | |
return 4.0 / 3.0 + x * (2.0 + (1.0 + x / 6.0) * x); | |
else if (0 <= x && x < 1) | |
return 2.0 / 3.0 + (-1.0 + x / 2.0) * x * x; | |
else | |
return 0; | |
} | |
else if constexpr (Degree == 4) | |
{ | |
if (-1.5 <= x && x < -0.5) | |
return 55.0 / 96.0 + x * (-(5.0 / 24.0) + x * (-(5.0 / 4.0) + (-(5.0 / 6.0) - x / 6.0) * x)); | |
else if (0.5 <= x && x < 1.5) | |
return 55.0 / 96.0 + x * (5.0 / 24.0 + x * (-(5.0 / 4.0) + (5.0 / 6.0 - x / 6.0) * x)); | |
else if (1.5 <= x && x <= 2.5) | |
return 625.0 / 384.0 + x * (-(125.0 / 48.0) + x * (25.0 / 16.0 + (-(5.0 / 12.0) + x / 24.0) * x)); | |
else if (-2.5 <= x && x <= -1.5) | |
return 625.0 / 384.0 + x * (125.0 / 48.0 + x * (25.0 / 16.0 + (5.0 / 12.0 + x / 24.0) * x)); | |
else if (-1.5 <= x && x < 1.5) | |
return 115.0 / 192.0 + x * x * (-(5.0 / 8.0) + x * x / 4.0); | |
else | |
return 0; | |
} | |
else if constexpr (Degree == 5) | |
{ | |
if (-2 <= x && x < -1) | |
return 17.0 / 40.0 + x * (-(5.0 / 8.0) + x * (-(7.0 / 4.0) + x * (-(5.0 / 4.0) + (-(3.0 / 8.0) - x / 24.0) * x))); | |
else if (0 <= x && x < 1) | |
return 11.0 / 20.0 + x * x * (-(1.0 / 2.0) + (1.0 / 4.0 - x / 12.0) * x * x); | |
else if (2 <= x && x <= 3) | |
return 81.0 / 40.0 + x * (-(27.0 / 8.0) + x * (9.0 / 4.0 + x * (-(3.0 / 4.0) + (1.0 / 8.0 - x / 120.0) * x))); | |
else if (-3 <= x && x < -2) | |
return 81.0 / 40.0 + x * (27.0 / 8.0 + x * (9.0 / 4.0 + x * (3.0 / 4.0 + (1.0 / 8.0 + x / 120.0) * x))); | |
else if (1 <= x && x < 2) | |
return 17.0 / 40.0 + x * (5.0 / 8.0 + x * (-(7.0 / 4.0) + x * (5.0 / 4.0 + (-(3.0 / 8.0) + x / 24.0) * x))); | |
else if (-1 <= x && x < 0) | |
return 11.0 / 20.0 + x * x * (-(1.0 / 2.0) + (1.0 / 4.0 + x / 12.0) * x * x); | |
else | |
return 0; | |
} | |
} | |
XYf SeqAt(float n) | |
{ | |
float margin = Degree + 1.0; | |
if (n < margin) | |
return points.front(); | |
else if (points.size() + margin <= n) | |
return points.back(); | |
else | |
return points[n - margin]; | |
} | |
XYf CalcAt(float t) | |
{ | |
t *= (Degree + 1) * 2 + points.size(); | |
float ti = floor(t); | |
XYf result = {0, 0}; | |
for (float i = ti - Degree; i <= ti + Degree; i++) | |
{ | |
result.x += SeqAt(i).x * Basis(t - i); | |
result.y += SeqAt(i).y * Basis(t - i); | |
} | |
return result; | |
} | |
public: | |
std::vector<XYf> points; | |
}; | |
} // namespace bspline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment