forked from zalohasa/GrblHoming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevelingRenderArea.cpp
352 lines (287 loc) · 11.5 KB
/
LevelingRenderArea.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "LevelingRenderArea.h"
/*
* Leveling render area to display the Z leveling data in a visual way.
*
* Gonzalo López 2014
*
*/
#include <QPainter>
#include <QLinearGradient>
#include <iostream>
#include <stdlib.h>
#include <QMouseEvent>
#include <vector>
#include <QMutexLocker>
#define IMAGE_MARGIN 6
#define ELLIPSE_SIZE 8
#define TEXT_MARGIN_Z 4
using std::cout;
using std::endl;
LevelingRenderArea::LevelingRenderArea(QWidget *parent)
: QWidget(parent)
{
interpolator = NULL;
//this->setSizePolicy(QSizePolicy::Expanding);
drawAxis = false;
setCursor(Qt::CrossCursor);
connect(&thread, SIGNAL(renderedImage(QImage)), this, SLOT(updatePixmap(QImage)));
}
QSize LevelingRenderArea::sizeHint() const
{
return QSize(1000, 1000);
}
void LevelingRenderArea::resizeEvent(QResizeEvent *)
{
QSize s;
s.setWidth(size().width() - (IMAGE_MARGIN*2));
s.setHeight(size().height() - (IMAGE_MARGIN*4));
pixmap = QPixmap();
thread.render(this->interpolator, s);
this->update();
}
void LevelingRenderArea::setInterpolator(const Interpolator *interpolator)
{
this->interpolator = interpolator;
pixmap = QPixmap();
QSize s;
s.setWidth(size().width() - (IMAGE_MARGIN*2));
s.setHeight(size().height() - (IMAGE_MARGIN*4));
thread.render(interpolator, s);
this->update();
}
void LevelingRenderArea::updatePixmap(const QImage &image)
{
pixmap = QPixmap::fromImage(image);
update();
}
void LevelingRenderArea::enterEvent(QEvent *event)
{
Q_UNUSED(event);
setMouseTracking(true);
drawAxis = true;
}
void LevelingRenderArea::leaveEvent(QEvent *event)
{
Q_UNUSED(event);
setMouseTracking(false);
drawAxis = false;
update();
}
void LevelingRenderArea::mouseMoveEvent(QMouseEvent *event)
{
mousePoint = event->pos();
update();
}
void LevelingRenderArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRect viewport = painter.viewport();
painter.fillRect(viewport, Qt::lightGray);
if (this->interpolator == NULL){
return;
}
if (interpolator->getType() == Interpolator::SINGLE)
{
return;
}
if (pixmap.isNull())
{
painter.setPen(Qt::black);
painter.drawText(rect(), Qt::AlignCenter, tr("Rendering image, please wait..."));
return;
}
painter.drawPixmap(QPoint(IMAGE_MARGIN,IMAGE_MARGIN), pixmap);
//Draw the min/max text.
painter.drawText(QRect(IMAGE_MARGIN + ELLIPSE_SIZE, IMAGE_MARGIN+pixmap.height()- ELLIPSE_SIZE, pixmap.width() - (ELLIPSE_SIZE*2),
height() - (IMAGE_MARGIN+pixmap.height()- ELLIPSE_SIZE)),
Qt::AlignCenter,
QString("Min: ").append(QString::number(interpolator->getMinZValue()))
.append(" - Max: ").append(QString::number(interpolator->getMaxZValue()))
.append(" - Difference: ").append(QString::number(interpolator->getMaxZValue() - interpolator->getMinZValue())));
//Draw the crosshair and the Z level value text.
if (drawAxis && mousePoint.x() >= IMAGE_MARGIN + ELLIPSE_SIZE
&& mousePoint.x()<=pixmap.width()-ELLIPSE_SIZE+IMAGE_MARGIN
&& mousePoint.y() >= IMAGE_MARGIN + ELLIPSE_SIZE
&& mousePoint.y() <= pixmap.height()-ELLIPSE_SIZE+IMAGE_MARGIN)
{
double i = mousePoint.x() - IMAGE_MARGIN;
double j = mousePoint.y() - IMAGE_MARGIN;
painter.setPen(QPen(Qt::black, 1, Qt::DotLine));
painter.drawLine(QPoint(mousePoint.x(), IMAGE_MARGIN+ELLIPSE_SIZE), QPoint(mousePoint.x(), IMAGE_MARGIN+pixmap.height()- ELLIPSE_SIZE));
painter.drawLine(QPoint(IMAGE_MARGIN+ELLIPSE_SIZE, mousePoint.y()), QPoint(IMAGE_MARGIN + pixmap.width() - ELLIPSE_SIZE, mousePoint.y()));
double remappedI = RenderThread::remap(double(i), ELLIPSE_SIZE, pixmap.width() - ELLIPSE_SIZE - 1, 0, interpolator->getXValue(interpolator->getXSteps() - 1));
double remappedJ = RenderThread::remap(double((pixmap.height() - 1)-j), ELLIPSE_SIZE, pixmap.height() - ELLIPSE_SIZE - 1, 0, interpolator->getYValue(interpolator->getYSteps() - 1));
//Do the interpolation
double zVal;
interpolator->interpolate(remappedI, remappedJ, zVal);
int xText = 0, yText = 0;
QString text = QString("Z: ").append(QString::number(zVal));
QFontMetrics m = painter.fontMetrics();
int textWidth = m.width(text);
int textHeight = m.height();
if (i > pixmap.width() - textWidth - ELLIPSE_SIZE - 5)
{
xText = mousePoint.x() - textWidth - TEXT_MARGIN_Z;
}else{
xText = mousePoint.x() + TEXT_MARGIN_Z;
}
if (j < textHeight + ELLIPSE_SIZE + 5)
{
yText = mousePoint.y() + textHeight;
}else{
yText = mousePoint.y() - TEXT_MARGIN_Z;
}
QString yDataText = QString("Y: ").append(QString::number(remappedJ));
QString xDataText = QString("X: ").append(QString::number(remappedI));
int yTextWidth = m.width(yDataText);
//Z value text
painter.drawText(QPoint(xText, yText), text);
//X and Y value text
if(j <= (IMAGE_MARGIN+pixmap.height()- ELLIPSE_SIZE) - textHeight - 10)
painter.drawText(QPoint(xText, (IMAGE_MARGIN+pixmap.height()- ELLIPSE_SIZE)-TEXT_MARGIN_Z), xDataText);
if (j > IMAGE_MARGIN + ELLIPSE_SIZE + textHeight + TEXT_MARGIN_Z + 10)
painter.drawText(QPoint(xText, IMAGE_MARGIN + ELLIPSE_SIZE + textHeight), xDataText);
if (i > yTextWidth + IMAGE_MARGIN + ELLIPSE_SIZE+TEXT_MARGIN_Z + 10)
painter.drawText(QPoint(IMAGE_MARGIN + ELLIPSE_SIZE+TEXT_MARGIN_Z, yText), yDataText);
if (i < IMAGE_MARGIN + pixmap.width() - ELLIPSE_SIZE - yTextWidth - textWidth - TEXT_MARGIN_Z - 10)
painter.drawText(QPoint(IMAGE_MARGIN + pixmap.width() - ELLIPSE_SIZE - yTextWidth - TEXT_MARGIN_Z, yText), yDataText);
}
}
double RenderThread::remap(double value, double low1, double high1, double low2, double high2)
{
return low2 + (value - low1) * ((high2 - low2) / (high1 - low1));
}
double RenderThread::remap(double value, double high1, double high2)
{
return value * high2 / high1;
}
RenderThread::RenderThread(QObject *parent)
: QThread(parent)
{
abort = false;
restart = false;
//Create a gradient with the five colors that will represent the height.
QLinearGradient gr(QPointF(0,0), QPointF(1005,0));
gr.setColorAt(0, QColor(0,0,255));
gr.setColorAt(0.25, QColor(0, 255, 255));
gr.setColorAt(0.5, QColor(0, 255, 0));
gr.setColorAt(0.75, QColor(255, 255, 0));
gr.setColorAt(1, QColor(255, 0, 0));
//Create an image 1006 pixels width and 1 pixel height to draw the gradient.
gradientImage = QImage(1006, 1, QImage::Format_RGB32);
QPainter p(&gradientImage);
p.fillRect(gradientImage.rect(), gr);
}
RenderThread::~RenderThread()
{
mutex.lock();
abort = true;
condition.wakeOne();
mutex.unlock();
wait();
}
void RenderThread::render(const Interpolator *interpolator, QSize size)
{
if (interpolator == NULL || interpolator->getType() == Interpolator::SINGLE)
{
return;
}
QMutexLocker locker(&mutex);
this->interpolator = interpolator;
this->size = size;
if (!isRunning()){
start(LowPriority);
} else {
restart = true;
condition.wakeOne();
}
}
void RenderThread::run()
{
forever{
mutex.lock();
QSize size = this->size;
const Interpolator *interpolator = this->interpolator;
mutex.unlock();
QImage finalImage(size, QImage::Format_RGB32);
QPainter painter(&finalImage);
painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(finalImage.rect(), Qt::lightGray);
for (int j = ELLIPSE_SIZE; j <= size.height()-ELLIPSE_SIZE-1; j++)
{
uint * line = reinterpret_cast<uint*>(finalImage.scanLine(j));
for (int i = ELLIPSE_SIZE; i <= size.width() - ELLIPSE_SIZE-1; i++){
if (restart) {
break;
}
if (abort)
{
return;
}
double yVal = 0;
//Remap the viewport xy coordinate to interpoler xy
double remappedI = remap(double(i), ELLIPSE_SIZE, size.width() - ELLIPSE_SIZE - 1, 0, interpolator->getXValue(interpolator->getXSteps() - 1));
double remappedJ = remap(double((size.height() - 1)-j), ELLIPSE_SIZE, size.height() - ELLIPSE_SIZE - 1, 0, interpolator->getYValue(interpolator->getYSteps() - 1));
//Do the interpolation
interpolator->interpolate(remappedI, remappedJ, yVal);
double yValRemapped = remap(yVal, interpolator->getMinZValue(), interpolator->getMaxZValue(), 5, 1005);
// std::cout << "Original i: " << i << " RemappedI = " << remappedI << " - Original J: "
// << j << " RemappedJ = " << remappedJ << " Value: "
// << yVal << " Remapped: " << yValRemapped << std::endl;
//Avoid trying to get colors from the image gradient outside its size.
//This can happen with some image zones.
QColor c;
if (yValRemapped<0){
c = QColor(0, 0, 255);
} else if (yValRemapped > 1005)
{
c = QColor(255, 0, 0);
}
else{
c = gradientImage.pixel(yValRemapped, 0);
}
//Paint the pixel
line[i] = c.rgb();
}
if (restart)
{
break;
}
}
if (!restart){
//Draw the test points
painter.setPen(QPen(Qt::black, 1));
for (unsigned int i = 0; i<interpolator->getXSteps(); i++)
{
for (unsigned int j = 0; j<interpolator->getYSteps(); j++)
{
int x = remap(interpolator->getXValue(i), 0, interpolator->getXValue(interpolator->getXSteps() - 1), ELLIPSE_SIZE, size.width() - ELLIPSE_SIZE - 1 );
int y = remap(interpolator->getYValue(j), 0, interpolator->getYValue(interpolator->getYSteps() - 1), ELLIPSE_SIZE, size.height() - ELLIPSE_SIZE - 1);
painter.drawEllipse(QPoint(x,y),ELLIPSE_SIZE,ELLIPSE_SIZE);
}
}
//Draw the grid lines.
painter.setPen(QPen(Qt::black, 1, Qt::DashLine));
for (unsigned int i = 0; i<interpolator->getXSteps(); i++)
{
int x = remap(interpolator->getXValue(i), 0, interpolator->getXValue(interpolator->getXSteps() - 1), ELLIPSE_SIZE, size.width() - ELLIPSE_SIZE - 1);
painter.drawLine(QPoint(x, 0), QPoint(x, size.height()-1));
}
for (unsigned int j = 0; j<interpolator->getYSteps(); j++)
{
int y = remap(interpolator->getYValue(j), 0, interpolator->getYValue(interpolator->getYSteps() - 1), ELLIPSE_SIZE, size.height() - ELLIPSE_SIZE - 1);
painter.drawLine(QPoint(0, y), QPoint(size.width()-1, y));
}
emit renderedImage(finalImage);
}
//Wait for the next image.
mutex.lock();
if (!restart)
{
condition.wait(&mutex);
}
restart = false;
mutex.unlock();
}
}