Engauge Digitizer  2
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 NO_RESET = false;
30  const bool NO_EXPORT_ONLY = false;
31  const bool DEBUG_FLAG = false;
32  const QStringList NO_LOAD_STARTUP_FILES;
33 
34  initializeLogging ("engauge_test",
35  "engauge_test.log",
36  DEBUG_FLAG);
37 
38  MainWindow w (NO_ERROR_REPORT_LOG_FILE,
39  NO_REGRESSION_OPEN_FILE,
40  NO_REGRESSION_IMPORT,
41  NO_GNUPLOT_LOG_FILES,
42  NO_RESET,
43  NO_EXPORT_ONLY,
44  NO_LOAD_STARTUP_FILES);
45  w.show ();
46 }
47 
48 void TestSpline::testSplinesAsControlPoints ()
49 {
50  const int T_START = 1, T_STOP = 7;
51  const double SPLINE_EPSILON = 0.01;
52  const int NUM_T = 60;
53 
54  bool success = true;
55 
56  vector<double> t;
57  vector<SplinePair> xy;
58 
59  // Independent variable must be evenly spaced
60  t.push_back (T_START);
61  t.push_back (2);
62  t.push_back (3);
63  t.push_back (4);
64  t.push_back (5);
65  t.push_back (6);
66  t.push_back (T_STOP);
67 
68  // Simple curve, with x values tweaked slightly (from even spacing) to make the test data more stressing
69  xy.push_back (SplinePair (1, 0.22));
70  xy.push_back (SplinePair (1.8, 0.04));
71  xy.push_back (SplinePair (3.2, -0.13));
72  xy.push_back (SplinePair (4.3, -0.17));
73  xy.push_back (SplinePair (5, -0.04));
74  xy.push_back (SplinePair (5.8, 0.09));
75  xy.push_back (SplinePair (7, 0.11));
76 
77  Spline s (t, xy);
78 
79  for (int i = 0; i <= NUM_T; i++) {
80  double t = T_START + (double) i * (T_STOP - T_START) / (double) NUM_T;
81  SplinePair spCoeff = s.interpolateCoeff (t);
82  SplinePair spBezier = s.interpolateControlPoints (t);
83 
84  double xCoeff = spCoeff.x();
85  double yCoeff = spCoeff.y();
86  double xControl = spBezier.x();
87  double yControl = spBezier.y();
88 
89  if (qAbs (xCoeff - xControl) > SPLINE_EPSILON) {
90  success = false;
91  }
92 
93  if (qAbs (yCoeff - yControl) > SPLINE_EPSILON) {
94  success = false;
95  }
96  }
97 
98  QVERIFY (success);
99 }
Cubic interpolation given independent and dependent value vectors.
Definition: Spline.h:21
double y() const
Get method for y.
Definition: SplinePair.cpp:71
TestSpline(QObject *parent=0)
Single constructor.
Definition: TestSpline.cpp:13
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:89
Single X/Y pair for cubic spline interpolation initialization and calculations.
Definition: SplinePair.h:11