| 1 |
#include "stk_middle_mesh/bspline_approximation_1d.hpp" |
| 2 |
#include "gtest/gtest.h" |
| 3 |
#include <fstream> |
| 4 |
|
| 5 |
namespace stk { |
| 6 |
namespace middle_mesh { |
| 7 |
namespace impl { |
| 8 |
|
| 9 |
namespace { |
| 10 |
|
| 11 |
std::vector<double> get_x_uniform(double xmin, double xmax, int npts) |
| 12 |
{ |
| 13 |
std::vector<double> xVals(npts); |
| 14 |
double deltaX = (xmax - xmin) / (npts - 1); |
| 15 |
|
| 16 |
for (int i = 0; i < npts; ++i) |
| 17 |
xVals[i] = xmin + deltaX * i; |
| 18 |
|
| 19 |
return xVals; |
| 20 |
} |
| 21 |
|
| 22 |
template <typename T> |
| 23 |
std::vector<double> get_f_values(const std::vector<double>& xValues, T func) |
| 24 |
{ |
| 25 |
std::vector<double> fVals(xValues.size()); |
| 26 |
for (size_t i = 0; i < xValues.size(); ++i) |
| 27 |
fVals[i] = func(xValues[i]); |
| 28 |
|
| 29 |
return fVals; |
| 30 |
} |
| 31 |
|
| 32 |
} // namespace |
| 33 |
|
| 34 |
TEST(BSplineApproximation1D, Zero) |
| 35 |
{ |
| 36 |
auto xVals = get_x_uniform(0, 1, 4); |
| 37 |
auto fVals = get_f_values(xVals, [](double /*x*/) { return 0; }); |
| 38 |
|
| 39 |
utils::impl::BSplineApproximation1D approximator(4); |
| 40 |
approximator.approximate(xVals, fVals); |
| 41 |
|
| 42 |
auto xTest = get_x_uniform(0, 1, 100); |
| 43 |
for (auto x : xTest) |
| 44 |
EXPECT_NEAR(approximator.eval(x), 0, 1e-13); |
| 45 |
} |
| 46 |
|
| 47 |
TEST(BSplineApproximation1D, Constant) |
| 48 |
{ |
| 49 |
auto xVals = get_x_uniform(0, 1, 20); |
| 50 |
// std::vector<double> x_vals = {0, 1.0/3.0, 2.0/3.0, 1}; |
| 51 |
auto fVals = get_f_values(xVals, [](double /*x*/) { return 2; }); |
| 52 |
|
| 53 |
utils::impl::BSplineApproximation1D approximator(4); |
| 54 |
approximator.approximate(xVals, fVals); |
| 55 |
|
| 56 |
auto xTest = get_x_uniform(0, 1, 100); |
| 57 |
std::vector<double> fValsApprox; |
| 58 |
for (auto x : xTest) |
| 59 |
{ |
| 60 |
fValsApprox.push_back(approximator.eval(x)); |
| 61 |
EXPECT_NEAR(approximator.eval(x), 2, 1e-13); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
TEST(BSplineApproximation1D, Linear) |
| 66 |
{ |
| 67 |
auto xVals = get_x_uniform(0, 1, 100); |
| 68 |
// std::vector<double> x_vals = {0, 1.0/3.0, 2.0/3.0, 1}; |
| 69 |
auto fVals = get_f_values(xVals, [](double x) { return 2 * x; }); |
| 70 |
|
| 71 |
utils::impl::BSplineApproximation1D approximator(40); |
| 72 |
approximator.approximate(xVals, fVals); |
| 73 |
|
| 74 |
auto xTest = get_x_uniform(0, 1, 100); |
| 75 |
std::vector<double> fValsApprox; |
| 76 |
for (auto x : xTest) |
| 77 |
{ |
| 78 |
fValsApprox.push_back(approximator.eval(x)); |
| 79 |
EXPECT_NEAR(approximator.eval(x), 2 * x, 1e-13); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
TEST(BSplineApproximation1D, Quadratic) |
| 84 |
{ |
| 85 |
auto xVals = get_x_uniform(0, 1, 100); |
| 86 |
// std::vector<double> x_vals = {0, 1.0/3.0, 2.0/3.0, 1}; |
| 87 |
auto fVals = get_f_values(xVals, [](double x) { return 2 * x * x; }); |
| 88 |
|
| 89 |
utils::impl::BSplineApproximation1D approximator(40); |
| 90 |
approximator.approximate(xVals, fVals); |
| 91 |
|
| 92 |
auto xTest = get_x_uniform(0, 1, 100); |
| 93 |
std::vector<double> fValsApprox; |
| 94 |
for (auto x : xTest) |
| 95 |
{ |
| 96 |
fValsApprox.push_back(approximator.eval(x)); |
| 97 |
EXPECT_NEAR(approximator.eval(x), 2 * x * x, 1e-13); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
TEST(BSplineApproximation1D, Cubic) |
| 102 |
{ |
| 103 |
auto xVals = get_x_uniform(0, 1, 20); |
| 104 |
// std::vector<double> x_vals = {0, 1.0/3.0, 2.0/3.0, 1}; |
| 105 |
auto fVals = get_f_values(xVals, [](double x) { return 2 * x * x * x; }); |
| 106 |
|
| 107 |
utils::impl::BSplineApproximation1D approximator(4); |
| 108 |
approximator.approximate(xVals, fVals); |
| 109 |
|
| 110 |
auto xTest = get_x_uniform(0, 1, 100); |
| 111 |
std::vector<double> fValsApprox; |
| 112 |
for (auto x : xTest) |
| 113 |
{ |
| 114 |
fValsApprox.push_back(approximator.eval(x)); |
| 115 |
EXPECT_NEAR(approximator.eval(x), 2 * x * x * x, 1e-13); |
| 116 |
} |
| 117 |
} |
| 118 |
} // namespace impl |
| 119 |
} // namespace middle_mesh |
| 120 |
} // namespace stk |