Engauge Digitizer  2
TestSegmentFill.cpp
1 #include <iostream>
2 #include "Logger.h"
3 #include "MainWindow.h"
4 #include <QCryptographicHash>
5 #include <QGraphicsScene>
6 #include <QGraphicsView>
7 #include <QList>
8 #include <qmath.h>
9 #include <QTextStream>
10 #include <QtTest/QtTest>
11 #include "Segment.h"
12 #include "SegmentFactory.h"
13 #include "Spline.h"
14 #include "SplinePair.h"
15 #include "Test/TestSegmentFill.h"
16 
17 QTEST_MAIN (TestSegmentFill)
18 
19 using namespace std;
20 
22  QObject(parent)
23 {
24 }
25 
26 void TestSegmentFill::cleanupTestCase ()
27 {
28 
29 }
30 
31 void TestSegmentFill::initTestCase ()
32 {
33  const QString NO_ERROR_REPORT_LOG_FILE;
34  const QString NO_REGRESSION_OPEN_FILE;
35  const bool NO_GNUPLOT_LOG_FILES = false;
36  const bool NO_REGRESSION_IMPORT = false;
37  const bool NO_RESET = false;
38  const bool DEBUG_FLAG = false;
39  const QStringList NO_LOAD_STARTUP_FILES;
40 
41  initializeLogging ("engauge_test",
42  "engauge_test.log",
43  DEBUG_FLAG);
44 
45  MainWindow m (NO_ERROR_REPORT_LOG_FILE,
46  NO_REGRESSION_OPEN_FILE,
47  NO_GNUPLOT_LOG_FILES,
48  NO_REGRESSION_IMPORT,
49  NO_RESET,
50  NO_LOAD_STARTUP_FILES);
51  m.show ();
52 }
53 
54 void TestSegmentFill::testFindSegments()
55 {
56  const bool NO_GNUPLOT = false;
57  const bool NO_DLG = false;
58  const QString OUT_FILE_ACTUAL ("../test/test_segment_fill.gnuplot_actual");
59  const QString OUT_FILE_EXPECTED ("../test/test_segment_fill.gnuplot_expected");
60 
61  QList<Segment*> segments;
62 
63  // The relative paths in this method will fail unless the directory is correct
64  QDir::setCurrent (QApplication::applicationDirPath());
65 
66  QImage img ("../samples/corners.png");
67 
68  QGraphicsScene *scene = new QGraphicsScene;
69  SegmentFactory segmentFactory (*scene,
70  NO_GNUPLOT);
71 
72  DocumentModelSegments modelSegments;
73 
74  segmentFactory.clearSegments (segments);
75 
76  // This will crash if dialog box appears since QApplication is not executing and therefore cannot process events
77  segmentFactory.makeSegments (img,
78  modelSegments,
79  segments,
80  NO_DLG);
81 
82  // Open output file
83  QFile out (OUT_FILE_ACTUAL);
84  QTextStream outStr (&out);
85 
86  out.open(QIODevice::WriteOnly | QIODevice::Text);
87 
88  // Output to file
89  for (int indexS = 0; indexS < segments.count(); indexS++) {
90  Segment* segment = segments [indexS];
91 
92  QList<QPoint> points = segment->fillPoints (modelSegments);
93 
94  // Skip segments with only one point since they are apparently random
95  if (points.count() > 1) {
96 
97  for (int indexP = 0; indexP < points.count(); indexP++) {
98  QPoint point = points [indexP];
99 
100  // Output in gnuplot format for plotting. A space precedes each field. This can be plotted with
101  // plot "../test/test_segment_fill.gnuplot_actual" w lp
102  outStr << point.x() << " " << point.y() << endl;
103  }
104 
105  // Blank line between curves
106  outStr << endl;
107  }
108  }
109 
110  out.close();
111 
112  // Hash values
113  QCryptographicHash hashActual (QCryptographicHash::Sha1);
114  QCryptographicHash hashExpected (QCryptographicHash::Sha1);
115  QFile fileActual (OUT_FILE_ACTUAL);
116  QFile fileExpected (OUT_FILE_EXPECTED);
117 
118  bool success = false;
119  if (fileActual.open(QIODevice::ReadOnly) && fileExpected.open(QIODevice::ReadOnly)) {
120  hashActual.addData (fileActual.readAll());
121  hashExpected.addData (fileExpected.readAll());
122  QByteArray signatureActual = hashActual.result();
123  QByteArray signatureExpected = hashExpected.result();
124 
125  // Compare
126  success = (signatureActual == signatureExpected);
127  }
128 
129  QVERIFY (success);
130 }
void makeSegments(const QImage &imageFiltered, const DocumentModelSegments &modelSegments, QList< Segment *> &segments, bool useDlg=true)
Main entry point for creating all Segments for the filtered image.
Factory class for Segment objects.
Unit test of segment fill feature.
void clearSegments(QList< Segment *> &segments)
Remove the segments created by makeSegments.
Selectable piecewise-defined line that follows a filtered line in the image.
Definition: Segment.h:21
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments)
Create evenly spaced points along the segment.
Definition: Segment.cpp:209
TestSegmentFill(QObject *parent=0)
Single constructor.
Model for DlgSettingsSegments and CmdSettingsSegments.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89