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  initializeLogging ("engauge_test",
33  "engauge_test.log",
34  DEBUG_FLAG);
35 
36  MainWindow w (NO_ERROR_REPORT_LOG_FILE,
37  NO_GNUPLOT_LOG_FILES);
38  w.show ();
39 }
40 
41 void TestProjectedPoint::testProjectedPoints ()
42 {
43  double radiusCircle = 1.0, radiusProjection = 2.0 * radiusCircle;
44  double xToProjectRight = radiusProjection, yToProjectRight = 0.0;
45  double xToProjectUp = 0.0, yToProjectUp = 2.0 * radiusCircle;
46  double xProjectionRight, yProjectionRight, projectedDistanceOutsideLineRight;
47  double xProjectionUp, yProjectionUp, projectedDistanceOutsideLineUp;
48  double distanceToLine; // Ignored
49 
50  // To prevent ambiguity at multiples of angleCriticalRight and angleCriticalUp, the angle step is NOT a factor of the
51  // critical angles
52  int angleStep = 13;
53 
54  // Critical angle in degrees
55  int angleCriticalRight = (int) (0.5 + qAcos (radiusCircle / radiusProjection) * RADIANS_TO_DEGREES);
56  int angleCriticalUp = (int) (0.5 + qAsin (radiusCircle / radiusProjection) * RADIANS_TO_DEGREES);
57 
58  for (int angle = 0; angle <= 360; angle += angleStep) {
59 
60  double xStart = radiusCircle * cos (angle * PI / 180.0);
61  double yStart = radiusCircle * sin (angle * PI / 180.0);
62  double xStop = -1.0 * xStart;
63  double yStop = -1.0 * yStart;
64 
65  double xMin = qMin (xStart, xStop);
66  double yMin = qMin (yStart, yStop);
67  double xMax = qMax (xStart, xStop);
68  double yMax = qMax (yStart, yStop);
69 
70  // Project point on right
71  projectPointOntoLine (xToProjectRight,
72  yToProjectRight,
73  xStart,
74  yStart,
75  xStop,
76  yStop,
77  &xProjectionRight,
78  &yProjectionRight,
79  &projectedDistanceOutsideLineRight,
80  &distanceToLine);
81 
82  // If and only if angle is between angleCritical to 180 - angleCritical, and
83  // 180 + angleCritical to 360 - angleCritical will there be a projection inside the line
84  if ((angleCriticalRight <= angle && angle <= 180 - angleCriticalRight) ||
85  (180 + angleCriticalRight <= angle && angle <= 360 - angleCriticalRight)) {
86 
87  QVERIFY (projectedDistanceOutsideLineRight == 0);
88  } else {
89  QVERIFY (projectedDistanceOutsideLineRight != 0);
90  }
91  QVERIFY (xMin <= xProjectionRight);
92  QVERIFY (yMin <= yProjectionRight);
93  QVERIFY (xProjectionRight <= xMax);
94  QVERIFY (yProjectionRight <= yMax);
95 
96  // Project point that is up
97  projectPointOntoLine (xToProjectUp,
98  yToProjectUp,
99  xStart,
100  yStart,
101  xStop,
102  yStop,
103  &xProjectionUp,
104  &yProjectionUp,
105  &projectedDistanceOutsideLineUp,
106  &distanceToLine);
107 
108  // If and only if angle is between -angleCritical to angleCritical, and
109  // 180 - angleCritical to 180 + angleCritical will there be a projection inside the line
110  if ((angle <= angleCriticalUp) ||
111  (180 - angleCriticalUp <= angle && angle <= 180 + angleCriticalUp) ||
112  (360 - angleCriticalUp <= angle)) {
113 
114  QVERIFY (projectedDistanceOutsideLineUp == 0);
115  } else {
116  QVERIFY (projectedDistanceOutsideLineUp != 0);
117  }
118  QVERIFY (xMin <= xProjectionUp);
119  QVERIFY (yMin <= yProjectionUp);
120  QVERIFY (xProjectionUp <= xMax);
121  QVERIFY (yProjectionUp <= yMax);
122  }
123 }
Unit test of spline library.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:60