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