-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathgcodedrawer.cpp
280 lines (227 loc) · 7.53 KB
/
gcodedrawer.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
// This file is a part of "grblControl" application.
// Copyright 2015 Hayrullin Denis Ravilevich
#include <QDebug>
#include "gcodedrawer.h"
#include "math.h"
#include "frmmain.h"
GcodeDrawer::GcodeDrawer()
{
m_geometryUpdated = false;
}
void GcodeDrawer::update()
{
m_indexes.clear();
m_geometryUpdated = false;
ShaderDrawable::update();
}
void GcodeDrawer::update(QList<int> indexes)
{
// Store segments to update
m_indexes += indexes;
ShaderDrawable::update();
}
bool GcodeDrawer::updateData()
{
if (m_indexes.isEmpty()) {
qDebug() << "updating geometry" << this;
QList<LineSegment*> *list = m_viewParser->getLines();
VertexData vertex;
// Create vertices array
// Clear all vertex data
m_lines.clear();
m_points.clear();
bool drawFirstPoint = true;
for (int i = 0; i < list->count(); i++) {
if (std::isnan(list->at(i)->getEnd().z())) continue;
// Find first point of toolpath
if (drawFirstPoint) {
if (std::isnan(list->at(i)->getEnd().x()) || std::isnan(list->at(i)->getEnd().y())) continue;
// Draw first toolpath point
vertex.color = Util::colorToVector(m_colorStart);
vertex.position = list->at(i)->getEnd();
vertex.start = QVector3D(sNan, sNan, sNan);
m_points.append(vertex);
drawFirstPoint = false;
continue;
}
// Prepare vertices
if (list->at(i)->isFastTraverse()) vertex.start = list->at(i)->getStart();
else vertex.start = QVector3D(sNan, sNan, sNan);
// Simplify geometry
int j = i;
if (m_simplify && i < list->count() - 1) {
QVector3D start = list->at(i)->getEnd() - list->at(i)->getStart();
QVector3D next;
double length = start.length();
bool straight = false;
do {
list->at(i)->setVertexIndex(m_lines.count()); // Store vertex index
i++;
if (i < list->count() - 1) {
next = list->at(i)->getEnd() - list->at(i)->getStart();
length += next.length();
straight = start.crossProduct(start.normalized(), next.normalized()).length() < 0.025;
}
// Split short & straight lines
} while ((length < m_simplifyPrecision || straight) && i < list->count()
&& getSegmentType(list->at(i)) == getSegmentType(list->at(j)));
i--;
} else {
list->at(i)->setVertexIndex(m_lines.count()); // Store vertex index
}
// Set color
vertex.color = getSegmentColor(list->at(i));
// Line start
vertex.position = list->at(j)->getStart();
m_lines.append(vertex);
// Line end
vertex.position = list->at(i)->getEnd();
m_lines.append(vertex);
// Draw last toolpath point
if (i == list->count() - 1) {
vertex.color = Util::colorToVector(m_colorEnd);
vertex.position = list->at(i)->getEnd();
vertex.start = QVector3D(sNan, sNan, sNan);
m_points.append(vertex);
}
}
m_indexes.clear();
m_geometryUpdated = true;
return true;
} else {
// Update vertices
QList<LineSegment*> list = m_viewParser->getLineSegmentList();
int vertexIndex;
// Try to map vbo
VertexData* data;
data = (VertexData*)m_vbo.map(QOpenGLBuffer::ReadWrite); // Already binded
// Data to update
VertexData *vertices;
vertices = data ? data : m_lines.data();
// Prepare colors
QVector3D drawnColor = Util::colorToVector(m_colorDrawn);
QVector3D highlightColor = Util::colorToVector(m_colorHighlight);
// Update vertices for each line segment
foreach (int i, m_indexes) {
// Update vertex pair
if (i < 0 || i > list.count() - 1) continue;
vertexIndex = list[i]->vertexIndex();
if (vertexIndex >= 0) {
// Update vertex array
if (vertices[vertexIndex].color == drawnColor // If vertex of drawn segment
&& getSegmentColor(list[i]) == highlightColor); // dont highlight
else {
vertices[vertexIndex].color = getSegmentColor(list[i]);
vertices[vertexIndex + 1].color = vertices[vertexIndex].color;
}
}
}
m_indexes.clear();
if (data) {
m_vbo.unmap();
return false; // Update only vao
} else return true; // Update full vbo via allocate
}
}
QVector3D GcodeDrawer::getSegmentColor(LineSegment *segment)
{
if (segment->drawn()) return Util::colorToVector(m_colorDrawn);//QVector3D(0.85, 0.85, 0.85);
else if (segment->isHightlight()) return Util::colorToVector(m_colorHighlight);//QVector3D(0.57, 0.51, 0.9);
else if (segment->isFastTraverse()) return Util::colorToVector(m_colorNormal);// QVector3D(0.0, 0.0, 0.0);
else if (segment->isZMovement()) return Util::colorToVector(m_colorZMovement);//QVector3D(1.0, 0.0, 0.0);
else return Util::colorToVector(m_colorNormal);//QVector3D(0.0, 0.0, 0.0);
}
int GcodeDrawer::getSegmentType(LineSegment* segment)
{
return segment->isFastTraverse() + segment->isZMovement() * 2;
}
QVector3D GcodeDrawer::getSizes()
{
QVector3D min = m_viewParser->getMinimumExtremes();
QVector3D max = m_viewParser->getMaximumExtremes();
return QVector3D(max.x() - min.x(), max.y() - min.y(), max.z() - min.z());
}
QVector3D GcodeDrawer::getMinimumExtremes()
{
return m_viewParser->getMinimumExtremes();
}
QVector3D GcodeDrawer::getMaximumExtremes()
{
return m_viewParser->getMaximumExtremes();
}
void GcodeDrawer::setViewParser(GcodeViewParse* viewParser)
{
m_viewParser = viewParser;
}
GcodeViewParse *GcodeDrawer::viewParser()
{
return m_viewParser;
}
bool GcodeDrawer::simplify() const
{
return m_simplify;
}
void GcodeDrawer::setSimplify(bool simplify)
{
m_simplify = simplify;
}
double GcodeDrawer::simplifyPrecision() const
{
return m_simplifyPrecision;
}
void GcodeDrawer::setSimplifyPrecision(double simplifyPrecision)
{
m_simplifyPrecision = simplifyPrecision;
}
bool GcodeDrawer::geometryUpdated()
{
return m_geometryUpdated;
}
QColor GcodeDrawer::colorNormal() const
{
return m_colorNormal;
}
void GcodeDrawer::setColorNormal(const QColor &colorNormal)
{
m_colorNormal = colorNormal;
}
QColor GcodeDrawer::colorHighlight() const
{
return m_colorHighlight;
}
void GcodeDrawer::setColorHighlight(const QColor &colorHighlight)
{
m_colorHighlight = colorHighlight;
}
QColor GcodeDrawer::colorZMovement() const
{
return m_colorZMovement;
}
void GcodeDrawer::setColorZMovement(const QColor &colorZMovement)
{
m_colorZMovement = colorZMovement;
}
QColor GcodeDrawer::colorDrawn() const
{
return m_colorDrawn;
}
void GcodeDrawer::setColorDrawn(const QColor &colorDrawn)
{
m_colorDrawn = colorDrawn;
}
QColor GcodeDrawer::colorStart() const
{
return m_colorStart;
}
void GcodeDrawer::setColorStart(const QColor &colorStart)
{
m_colorStart = colorStart;
}
QColor GcodeDrawer::colorEnd() const
{
return m_colorEnd;
}
void GcodeDrawer::setColorEnd(const QColor &colorEnd)
{
m_colorEnd = colorEnd;
}