Engauge Digitizer  2
TestProjectedPoint.cpp
1 #include "Logger.h"
2 #include "MainWindow.h"
3 #include "mmsubs.h"
4 #include <qmath.h>
5 #include <QtTest/QtTest>
6 #include "Spline.h"
7 #include "SplinePair.h"
8 #include "Test/TestProjectedPoint.h"
9 
10 QTEST_MAIN (TestProjectedPoint)
11 
12 using namespace std;
13 
14 const double PI = 3.1415926535;
15 const double RADIANS_TO_DEGREES = 180.0 / PI;
16 
18  QObject(parent)
19 {
20 }
21 
22 void TestProjectedPoint::cleanupTestCase ()
23 {
24 
25 }
26 
27 void TestProjectedPoint::initTestCase ()
28 {
29  const QString NO_ERROR_REPORT_LOG_FILE;
30  const bool NO_GNUPLOT_LOG_FILES = 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_GNUPLOT_LOG_FILES,
40  NO_LOAD_STARTUP_FILES);
41  w.show ();
42 }
43 
44 void TestProjectedPoint::testProjectedPoints ()
45 {
46  double radiusCircle = 1.0, radiusProjection = 2.0 * radiusCircle;
47  double xToProjectRight = radiusProjection, yToProjectRight = 0.0;
48  double xToProjectUp = 0.0, yToProjectUp = 2.0 * radiusCircle;
49  double xProjectionRight, yProjectionRight, projectedDistanceOutsideLineRight;
50  double xProjectionUp, yProjectionUp, projectedDistanceOutsideLineUp;
51  double distanceToLine; // Ignored
52 
53  // To prevent ambiguity at multiples of angleCriticalRight and angleCriticalUp, the angle step is NOT a factor of the
54  // critical angles
55  int angleStep = 13;
56 
57  // Critical angle in degrees
58  int angleCriticalRight = (int) (0.5 + qAcos (radiusCircle / radiusProjection) * RADIANS_TO_DEGREES);
59  int angleCriticalUp = (int) (0.5 + qAsin (radiusCircle / radiusProjection) * RADIANS_TO_DEGREES);
60 
61  for (int angle = 0; angle <= 360; angle += angleStep) {
62 
63  double xStart = radiusCircle * cos (angle * PI / 180.0);
64  double yStart = radiusCircle * sin (angle * PI / 180.0);
65  double xStop = -1.0 * xStart;
66  double yStop = -1.0 * yStart;
67 
68  double xMin = qMin (xStart, xStop);
69  double yMin = qMin (yStart, yStop);
70  double xMax = qMax (xStart, xStop);
71  double yMax = qMax (yStart, yStop);
72 
73  // Project point on right
74  projectPointOntoLine (xToProjectRight,
75  yToProjectRight,
76  xStart,
77  yStart,
78  xStop,
79  yStop,
80  &xProjectionRight,
81  &yProjectionRight,
82  &projectedDistanceOutsideLineRight,
83  &distanceToLine);
84 
85  // If and only if angle is between angleCritical to 180 - angleCritical, and
86  // 180 + angleCritical to 360 - angleCritical will there be a projection inside the line
87  if ((angleCriticalRight <= angle && angle <= 180 - angleCriticalRight) ||
88  (180 + angleCriticalRight <= angle && angle <= 360 - angleCriticalRight)) {
89 
90  QVERIFY (projectedDistanceOutsideLineRight == 0);
91  } else {
92  QVERIFY (projectedDistanceOutsideLineRight != 0);
93  }
94  QVERIFY (xMin <= xProjectionRight);
95  QVERIFY (yMin <= yProjectionRight);
96  QVERIFY (xProjectionRight <= xMax);
97  QVERIFY (yProjectionRight <= yMax);
98 
99  // Project point that is up
100  projectPointOntoLine (xToProjectUp,
101  yToProjectUp,
102  xStart,
103  yStart,
104  xStop,
105  yStop,
106  &xProjectionUp,
107  &yProjectionUp,
108  &projectedDistanceOutsideLineUp,
109  &distanceToLine);
110 
111  // If and only if angle is between -angleCritical to angleCritical, and
112  // 180 - angleCritical to 180 + angleCritical will there be a projection inside the line
113  if ((angle <= angleCriticalUp) ||
114  (180 - angleCriticalUp <= angle && angle <= 180 + angleCriticalUp) ||
115  (360 - angleCriticalUp <= angle)) {
116 
117  QVERIFY (projectedDistanceOutsideLineUp == 0);
118  } else {
119  QVERIFY (projectedDistanceOutsideLineUp != 0);
120  }
121  QVERIFY (xMin <= xProjectionUp);
122  QVERIFY (yMin <= yProjectionUp);
123  QVERIFY (xProjectionUp <= xMax);
124  QVERIFY (yProjectionUp <= yMax);
125  }
126 }
Unit test of spline library.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:66