Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
TestSpline.cpp
1 #include "Logger.h"
2 #include "MainWindow.h"
3 #include <qmath.h>
4 #include <QtTest/QtTest>
5 #include "Spline.h"
6 #include "SplinePair.h"
7 #include "Test/TestSpline.h"
8 
9 QTEST_MAIN (TestSpline)
10 
11 using namespace std;
12 
13 TestSpline::TestSpline(QObject *parent) :
14  QObject(parent)
15 {
16 }
17 
18 void TestSpline::cleanupTestCase ()
19 {
20 
21 }
22 
23 void TestSpline::initTestCase ()
24 {
25  const QString NO_ERROR_REPORT_LOG_FILE;
26  const QString NO_REGRESSION_OPEN_FILE;
27  const bool NO_GNUPLOT_LOG_FILES = false;
28  const bool NO_REGRESSION_IMPORT = false;
29  const bool DEBUG_FLAG = false;
30  const QStringList NO_LOAD_STARTUP_FILES;
31 
32  initializeLogging ("engauge_test",
33  "engauge_test.log",
34  DEBUG_FLAG);
35 
36  MainWindow w (NO_ERROR_REPORT_LOG_FILE,
37  NO_REGRESSION_OPEN_FILE,
38  NO_GNUPLOT_LOG_FILES,
39  NO_REGRESSION_IMPORT,
40  NO_LOAD_STARTUP_FILES);
41  w.show ();
42 }
43 
44 void TestSpline::testSplinesAsControlPoints ()
45 {
46  const int T_START = 1, T_STOP = 7;
47  const double SPLINE_EPSILON = 0.01;
48  const int NUM_T = 60;
49 
50  bool success = true;
51 
52  vector<double> t;
53  vector<SplinePair> xy;
54 
55  // Independent variable must be evenly spaced
56  t.push_back (T_START);
57  t.push_back (2);
58  t.push_back (3);
59  t.push_back (4);
60  t.push_back (5);
61  t.push_back (6);
62  t.push_back (T_STOP);
63 
64  // Simple curve, with x values tweaked slightly (from even spacing) to make the test data more stressing
65  xy.push_back (SplinePair (1, 0.22));
66  xy.push_back (SplinePair (1.8, 0.04));
67  xy.push_back (SplinePair (3.2, -0.13));
68  xy.push_back (SplinePair (4.3, -0.17));
69  xy.push_back (SplinePair (5, -0.04));
70  xy.push_back (SplinePair (5.8, 0.09));
71  xy.push_back (SplinePair (7, 0.11));
72 
73  Spline s (t, xy);
74 
75  for (int i = 0; i <= NUM_T; i++) {
76  double t = T_START + (double) i * (T_STOP - T_START) / (double) NUM_T;
77  SplinePair spCoeff = s.interpolateCoeff (t);
78  SplinePair spBezier = s.interpolateControlPoints (t);
79 
80  double xCoeff = spCoeff.x();
81  double yCoeff = spCoeff.y();
82  double xControl = spBezier.x();
83  double yControl = spBezier.y();
84 
85  if (qAbs (xCoeff - xControl) > SPLINE_EPSILON) {
86  success = false;
87  }
88 
89  if (qAbs (yCoeff - yControl) > SPLINE_EPSILON) {
90  success = false;
91  }
92  }
93 
94  QVERIFY (success);
95 }
Cubic interpolation given independent and dependent value vectors.
Definition: Spline.h:21
double y() const
Get method for y.
Definition: SplinePair.cpp:71
double x() const
Get method for x.
Definition: SplinePair.cpp:66
Unit test of spline library.
Definition: TestSpline.h:7
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:77
Single X/Y pair for cubic spline interpolation initialization and calculations.
Definition: SplinePair.h:11