Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_bspline_patch.cpp

raw
1 #include "stk_middle_mesh/bspline_patch.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<utils::Point> get_points_uniform(double xmin, double xmax, double ymin, double ymax, int nptsX, int nptsY)
12 {
13 std::vector<utils::Point> xVals(nptsX * nptsY);
14 double deltaX = (xmax - xmin) / (nptsX - 1);
15 double deltaY = (ymax - ymin) / (nptsY - 1);
16
17 for (int i = 0; i < nptsX; ++i)
18 for (int j = 0; j < nptsY; ++j)
19 xVals[i * nptsY + j] = utils::Point(xmin + deltaX * i, ymin + deltaY * j);
20
21 return xVals;
22 }
23
24 template <typename T>
25 void get_f_values(std::vector<utils::Point>& values, T func)
26 {
27 for (size_t i = 0; i < values.size(); ++i)
28 values[i].z = func(values[i]);
29 }
30
31 /*
32 void writeFile(const std::string& fname, double xmin, double xmax, double ymin, double ymax,
33 int npts_x, int npts_y, const std::vector<utils::Point>& values)
34 {
35 std::ofstream xfile;
36 xfile.open(fname + "_x.txt");
37 xfile << std::setprecision(16);
38
39 double delta_x = (xmax - xmin)/(npts_x - 1);
40 for (int i=0; i < npts_x; ++i)
41 xfile << xmin + delta_x * i << std::endl;
42
43 xfile.close();
44
45 std::ofstream yfile;
46 yfile.open(fname + "_y.txt");
47 yfile << std::setprecision(16);
48
49 double delta_y = (ymax - ymin)/(npts_y - 1);
50 for (int i=0; i < npts_y; ++i)
51 yfile << ymin + delta_y * i << std::endl;
52
53 yfile.close();
54
55
56 std::ofstream valsfile;
57 valsfile << std::setprecision(16);
58
59 valsfile.open(fname + "_vals.txt");
60
61 for (int j=0; j < npts_y; ++j)
62 {
63 for (int i=0; i < npts_x; ++i)
64 {
65 int idx = i * npts_y + j;
66 valsfile << values[idx].z << " ";
67 }
68
69 valsfile << std::endl;
70 }
71
72 valsfile.close();
73 }
74 */
75
76 template <typename T>
77 void test_bsplines(T f)
78 {
79 auto vals = get_points_uniform(0, 2, 0, 2, 6, 6);
80 get_f_values(vals, f);
81
82 utils::impl::BSplinePatch patch(4);
83 patch.construct_patch(vals);
84
85 auto testVals = get_points_uniform(0, 2, 0, 2, 10, 10);
86 for (size_t i = 0; i < testVals.size(); ++i)
87 {
88 auto testPt = testVals[i];
89 auto patchPt = patch.eval_point(testPt.x, testPt.y);
90 EXPECT_NEAR(patchPt.z, f(testPt), 1e-13);
91 }
92 }
93
94 } // namespace
95
96 // 1 dimensional polynomials
97 // 2 dimensional polynomials
98 // 2 dimensional polynomials specifying pt0 to be outside the range of the other data
99
100 TEST(BSplinePatch, Zero)
101 {
102 auto f = [](const utils::Point& /*pt*/) { return 0; };
103 test_bsplines(f);
104 }
105
106 TEST(BSplinePatch, Constant)
107 {
108 auto f = [](const utils::Point& /*pt*/) { return 2; };
109 test_bsplines(f);
110 }
111
112 TEST(BSplinePatch, Linearx)
113 {
114 auto f = [](const utils::Point& pt) { return 2 * pt.x; };
115 test_bsplines(f);
116 }
117
118 TEST(BSplinePatch, Quadraticx)
119 {
120 auto f = [](const utils::Point& pt) { return 2 * pt.x * pt.x; };
121 test_bsplines(f);
122 }
123
124 TEST(BSplinePatch, Cubicx)
125 {
126 auto f = [](const utils::Point& pt) { return 2 * std::pow(pt.x, 3); };
127 test_bsplines(f);
128 }
129
130 TEST(BSplinePatch, Lineary)
131 {
132 auto f = [](const utils::Point& pt) { return 2 * pt.y; };
133 test_bsplines(f);
134 }
135
136 TEST(BSplinePatch, Quadraticy)
137 {
138 auto f = [](const utils::Point& pt) { return 2 * pt.y * pt.y; };
139 test_bsplines(f);
140 }
141
142 TEST(BSplinePatch, Cubicy)
143 {
144 auto f = [](const utils::Point& pt) { return 2 * std::pow(pt.y, 3); };
145 test_bsplines(f);
146 }
147
148 TEST(BSplinePatch, Linearxy)
149 {
150 auto f = [](const utils::Point& pt) { return 2 * pt.x * pt.y; };
151 test_bsplines(f);
152 }
153
154 TEST(BSplinePatch, Quadraticxy)
155 {
156 auto f = [](const utils::Point& pt) { return 2 * pt.x * pt.x * pt.y * pt.y; };
157 test_bsplines(f);
158 }
159
160 TEST(BSplinePatch, Cubicxy)
161 {
162 auto f = [](const utils::Point& pt) { return 2 * std::pow(pt.x, 3) * std::pow(pt.y, 3); };
163 test_bsplines(f);
164 }
165
166 TEST(BSplinePatch, CubicxyUsingPt0)
167 {
168 auto f = [](const utils::Point& pt) { return 2 * std::pow(pt.x, 3) * std::pow(pt.y, 3); };
169 auto vals = get_points_uniform(0, 2, 0, 2, 6, 6);
170 get_f_values(vals, f);
171
172 utils::Point pt0(3, 3);
173 utils::impl::BSplinePatch patch(2);
174 patch.construct_patch(vals, pt0);
175
176 auto testVals = get_points_uniform(0, 3, 0, 3, 10, 10);
177 for (size_t i = 0; i < testVals.size(); ++i)
178 {
179 auto testPt = testVals[i];
180 auto patchPt = patch.eval_point(testPt.x, testPt.y);
181 EXPECT_NEAR(patchPt.z, f(testPt), 1e-11);
182 }
183 }
184 } // namespace impl
185 } // namespace middle_mesh
186 } // namespace stk