3 #include "MainWindow.h" 6 #include <QtTest/QtTest> 8 #include "SplinePair.h" 10 #include "Test/TestSpline.h" 16 const QString WEBPAGE (
"https://tools.timodenk.com/cubic-spline-interpolation");
22 const int NUM_ITERATIONS = 24;
29 void TestSpline::cleanupTestCase ()
34 bool TestSpline::coefCheckX (
const vector<double> &t,
35 #ifdef SHOWCOEFFICIENTS
36 const vector<SplinePair> &xy,
38 const vector<SplinePair> & ,
43 double aUntranslated = 0, bUntranslated = 0, cUntranslated = 0, dUntranslated = 0;
46 #ifdef SHOWCOEFFICIENTS 48 <<
"(t,x) inputs to be copied to " << WEBPAGE.toLatin1().data()
50 for (i = 0; i < t.size(); i++) {
51 cout << t[i] <<
" " << xy[i].x() << endl;
54 <<
"x=d*(t-ti)^3+c*(t-ti)^2+b*(t-ti)+a natural cubic spline results to be used in this code" 56 for (i = 0; i < t.size() - 1; i++) {
61 s.m_elements[i].a().x(),
62 s.m_elements[i].b().x(),
63 s.m_elements[i].c().x(),
64 s.m_elements[i].d().x());
67 <<
"x=d*t^3+c*t^2+b*t+a outputs to be compared to results from " << WEBPAGE.toLatin1().data()
70 for (i = 0; i < t.size() - 1; i++) {
71 s.computeUntranslatedCoefficients (s.m_elements[i].a().x(),
72 s.m_elements[i].b().x(),
73 s.m_elements[i].c().x(),
74 s.m_elements[i].d().x(),
80 #ifdef SHOWCOEFFICIENTS 94 success &= (qAbs (aUntranslated - -8.3608) < 8.3608 / 10000.0);
95 success &= (qAbs (bUntranslated - 4.2505) < 4.2505 / 10000.0);
96 success &= (qAbs (cUntranslated - -0.63092) < 0.63092 / 10000.0);
97 success &= (qAbs (dUntranslated - 0.035051) < 0.035051 / 10000.0);
102 bool TestSpline::coefCheckY (
const vector<double> &t,
103 #ifdef SHOWCOEFFICIENTS
104 const vector<SplinePair> &xy,
106 const vector<SplinePair> & ,
111 double aUntranslated = 0, bUntranslated = 0, cUntranslated = 0, dUntranslated = 0;
114 #ifdef SHOWCOEFFICIENTS 116 <<
"(t,y) inputs to be copied to " << WEBPAGE.toLatin1().data()
118 for (i = 0; i < xy.size(); i++) {
119 cout << t[i] <<
" " << xy[i].y() << endl;
122 <<
"y=d*(t-ti)^3+c*(t-ti)^2+b*(t-ti)+a natural cubic spline results to be used in this code" 124 for (i = 0; i < xy.size() - 1; i++) {
129 s.m_elements[i].a().y(),
130 s.m_elements[i].b().y(),
131 s.m_elements[i].c().y(),
132 s.m_elements[i].d().y());
135 <<
"y=d*t^3+c*t^2+b*t+a outputs to be compared to results from " << WEBPAGE.toLatin1().data()
138 for (i = 0; i < t.size() - 1; i++) {
139 s.computeUntranslatedCoefficients (s.m_elements[i].a().y(),
140 s.m_elements[i].b().y(),
141 s.m_elements[i].c().y(),
142 s.m_elements[i].d().y(),
148 #ifdef SHOWCOEFFICIENTS 162 success &= (qAbs (aUntranslated - -7.0) < 7.0 / 10000.0);
163 success &= (qAbs (bUntranslated - 3.5667) < 3.5667 / 10000.0);
164 success &= (qAbs (cUntranslated - -0.6) < 0.6 / 10000.0);
165 success &= (qAbs (dUntranslated - 0.033333) < 0.033333 / 10000.0);
170 void TestSpline::coefShow (
const QString &leftHandSide,
171 const QString &independentVariable,
179 cout << leftHandSide.toLatin1().data() << scientific
180 << d <<
"*" << independentVariable.toLatin1().data() <<
"^3 + " 181 << c <<
"*" << independentVariable.toLatin1().data() <<
"^2 + " 182 << b <<
"*" << independentVariable.toLatin1().data() <<
" + " 183 << a <<
" (" << tLow <<
"<t<" << tHigh <<
")" << endl;
187 void TestSpline::initTestCase ()
189 const QString NO_ERROR_REPORT_LOG_FILE;
190 const QString NO_REGRESSION_OPEN_FILE;
191 const bool NO_GNUPLOT_LOG_FILES =
false;
192 const bool NO_REGRESSION_IMPORT =
false;
193 const bool NO_RESET =
false;
194 const bool NO_EXPORT_ONLY =
false;
195 const bool NO_EXPORT_IMAGE_ONLY =
false;
196 const QString NO_EXPORT_IMAGE_EXTENSION;
197 const bool DEBUG_FLAG =
false;
198 const QStringList NO_LOAD_STARTUP_FILES;
199 const QStringList NO_COMMAND_LINE;
201 initializeLogging (
"engauge_test",
206 NO_REGRESSION_OPEN_FILE,
207 NO_REGRESSION_IMPORT,
208 NO_GNUPLOT_LOG_FILES,
211 NO_EXPORT_IMAGE_ONLY,
212 NO_EXPORT_IMAGE_EXTENSION,
213 NO_LOAD_STARTUP_FILES,
218 void TestSpline::testCoefficientsFromOrdinals ()
222 vector<SplinePair> xy;
234 vector<SplinePair>::const_iterator itr;
235 for (itr = xy.begin(); itr != xy.end(); itr++) {
236 t.push_back (counter++);
242 success &= coefCheckX (t,
245 success &= coefCheckY (t,
252 void TestSpline::testSharpTransition ()
254 const int NUM_T = 60;
255 const double SPLINE_EPSILON = 0.01;
257 map<double, bool> xMerged;
262 vector<SplinePair> xyBefore;
274 vector<SplinePair>::const_iterator itrB;
275 for (itrB = xyBefore.begin(); itrB != xyBefore.end(); itrB++) {
277 t.push_back (pair.
x());
278 xMerged [pair.
x ()] =
true;
282 double tStart = t[0];
283 double tStop = t[t.size() - 1];
284 for (
int i = 0; i <= NUM_T; i++) {
285 double t = tStart + (double) i * (tStop - tStart) / (double) NUM_T;
287 if (xMerged.find (t) == xMerged.end ()) {
293 Spline s (t, xyBefore, SPLINE_DISABLE_T_CHECK);
296 vector<SplinePair> xyAfter;
297 map<double, bool>::const_iterator itrX;
298 for (itrX = xMerged.begin(); itrX != xMerged.end(); itrX++) {
299 double x = itrX->first;
301 xyAfter.push_back (pair);
305 cout <<
"set datafile missing \"?\"" << endl;
306 cout <<
"plot \"gnuplot.in\" using 1:2 with linespoints, \"gnuplot.in\" using 1:3 with lines" << endl;
310 map<double, bool>::const_iterator itrM;
311 for (itrM = xMerged.begin(); itrM != xMerged.end(); itrM++) {
312 double x = itrM->first;
314 string yB =
"?", yA =
"?";
316 vector<SplinePair>::iterator itrB;
317 for (itrB = xyBefore.begin(); itrB != xyBefore.end(); itrB++) {
318 if (itrB->x() == x) {
326 vector<SplinePair>::iterator itrA;
327 for (itrA = xyAfter.begin(); itrA != xyAfter.end(); itrA++) {
328 if (itrA->x() == x) {
336 if (itrB != xyBefore.end() &&
337 itrA != xyAfter.end()) {
340 double yBefore = itrB->y();
341 double yAfter = itrA->y();
342 if (qAbs (yBefore - yAfter) > SPLINE_EPSILON) {
348 cout << x <<
", " << yB <<
", " << yA << endl;
355 void TestSpline::testSplinesAsControlPoints ()
357 const int T_START = 1, T_STOP = 7;
358 const double SPLINE_EPSILON = 0.01;
359 const int NUM_T = 60;
364 vector<SplinePair> xy;
367 t.push_back (T_START);
373 t.push_back (T_STOP);
386 for (
int i = 0; i <= NUM_T; i++) {
387 double t = T_START + (double) i * (T_STOP - T_START) / (double) NUM_T;
389 SplinePair spBezier = s.interpolateControlPoints (t);
391 double xCoeff = spCoeff.
x();
392 double yCoeff = spCoeff.
y();
393 double xControl = spBezier.
x();
394 double yControl = spBezier.
y();
396 if (qAbs (xCoeff - xControl) > SPLINE_EPSILON) {
400 if (qAbs (yCoeff - yControl) > SPLINE_EPSILON) {
Cubic interpolation given independent and dependent value vectors.
double y() const
Get method for y.
TestSpline(QObject *parent=0)
Single constructor.
double x() const
Get method for x.
Unit test of spline library.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Single X/Y pair for cubic spline interpolation initialization and calculations.