1 /******************************************************************************************************
2  * (C) 2018 markummitchell@github.com. This file is part of Engauge Digitizer, which is released      *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission.     *
5  ******************************************************************************************************/
6 
7 #include "gnuplot.h"
8 #include "GridIndependentToDependent.h"
9 #include "GridLog.h"
10 #include <iostream>
11 #include <QFile>
12 #include <QPoint>
13 #include <QTextStream>
14 
15 // Whole image is too much information so only stuff near this center point is included
16 const int DETAILED_CENTER_X = 704;
17 const int DETAILED_CENTER_Y = 521;
18 const int DETAILED_RADIUS = 8;
19 
20 // Derived constants
21 const int DETAILED_X_MIN = DETAILED_CENTER_X - DETAILED_RADIUS;
22 const int DETAILED_X_MAX = DETAILED_CENTER_X + DETAILED_RADIUS;
23 const int DETAILED_Y_MIN = DETAILED_CENTER_Y - DETAILED_RADIUS;
24 const int DETAILED_Y_MAX = DETAILED_CENTER_Y + DETAILED_RADIUS;
25 
GridLog(bool isGnuplot)26 GridLog::GridLog(bool isGnuplot) :
27   m_isGnuplot (isGnuplot),
28   m_logStr (&m_log)
29 {
30   if (m_isGnuplot) {
31 
32     // Show border around region of interest
33     m_logStr << DETAILED_X_MIN << " " << - DETAILED_Y_MIN << "\n";
34     m_logStr << DETAILED_X_MAX << " " << - DETAILED_Y_MIN << "\n";
35     m_logStr << DETAILED_X_MAX << " " << - DETAILED_Y_MAX << "\n";
36     m_logStr << DETAILED_X_MIN << " " << - DETAILED_Y_MAX << "\n";
37     m_logStr << DETAILED_X_MIN << " " << - DETAILED_Y_MIN << "\n";
38     m_logStr << "\n";
39   }
40 }
41 
~GridLog()42 GridLog::~GridLog()
43 {
44   if (m_isGnuplot) {
45 
46     // Save the log stream that has been accumulated in memory
47     QString filename ("grid.gnuplot");
48     QFile file (filename);
49     QTextStream fileStr (&file);
50 
51     std::cout << GNUPLOT_FILE_MESSAGE.toLatin1().data() << filename.toLatin1().data() << "\n";
52 
53     file.open (QIODevice::WriteOnly | QIODevice::Append);
54     fileStr << m_log;
55     file.close ();
56   }
57 }
58 
inBounds(int x,int y) const59 bool GridLog::inBounds (int x, int y) const
60 {
61   return
62       DETAILED_X_MIN < x &&
63       DETAILED_Y_MIN < y &&
64       x < DETAILED_X_MAX &&
65       y < DETAILED_Y_MAX;
66 }
67 
showInputPixel(const QPoint & p,double halfWidth)68 void GridLog::showInputPixel (const QPoint &p,
69                               double halfWidth)
70 {
71   int x = p.x();
72   int y = p.y();
73 
74   if (DETAILED_X_MIN <= x &&
75       DETAILED_Y_MIN <= y &&
76       x <= DETAILED_X_MAX &&
77       y <= DETAILED_Y_MAX) {
78 
79     m_logStr << x - halfWidth << " " << - (y - halfWidth) << "\n";
80     m_logStr << x + halfWidth << " " << - (y - halfWidth) << "\n";
81     m_logStr << x + halfWidth << " " << - (y + halfWidth) << "\n";
82     m_logStr << x - halfWidth << " " << - (y + halfWidth) << "\n";
83     m_logStr << x - halfWidth << " " << - (y - halfWidth) << "\n";
84     m_logStr << "\n";
85   }
86 }
87 
showOutputScanLinePixel(int x,int y,double radius)88 void GridLog::showOutputScanLinePixel (int x,
89                                        int y,
90                                        double radius)
91 {
92   if (m_isGnuplot && inBounds (x, y)) {
93 
94     // Draw a diamond
95     m_logStr << x          << " " << - (y - radius) << "\n";
96     m_logStr << x + radius << " " << - (y         ) << "\n";
97     m_logStr << x          << " " << - (y + radius) << "\n";
98     m_logStr << x - radius << " " << - (y         ) << "\n";
99     m_logStr << x          << " " << - (y - radius) << "\n";
100     m_logStr << "\n";
101   }
102 }
103 
showOutputTrapezoid(const QPoint & p0,const QPoint & p1,const QPoint & p2,const QPoint & p3)104 void GridLog::showOutputTrapezoid (const QPoint &p0,
105                                    const QPoint &p1,
106                                    const QPoint &p2,
107                                    const QPoint &p3)
108 {
109   if (m_isGnuplot) {
110 
111     // Log if any pixel is in the region of interest
112     if (inBounds (p0.x(), p0.y()) ||
113         inBounds (p1.x(), p1.y()) ||
114         inBounds (p2.x(), p2.y()) ||
115         inBounds (p3.x(), p3.y())) {
116 
117       m_logStr << p0.x() << " " << - p0.y() << "\n";
118       m_logStr << p1.x() << " " << - p1.y() << "\n";
119       m_logStr << p2.x() << " " << - p2.y() << "\n";
120       m_logStr << p3.x() << " " << - p3.y() << "\n";
121       m_logStr << p0.x() << " " << - p0.y() << "\n";
122       m_logStr << "\n";
123     }
124   }
125 }
126