-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
278 lines (237 loc) · 8.51 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "laserfunctionsilda.h"
#include "ildaserializer.h"
#include <vector>
#include <iostream>
#include <fstream>
//#include <QFileDialog>
#include <QMessageBox>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
scene(new LaserScene)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
rotateTime = new QTime();
rotateTime->start();
redrawTimer = new QTimer(this);
connect(redrawTimer, SIGNAL(timeout()), this, SLOT(redraw()));
redrawTimer->start();
rotAngle = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionQuit_triggered()
{
exit(0);
}
void MainWindow::redraw() {
// Prepare for new frame
scene->clearScene();
scene->offsetX = 1;
scene->offsetY = 1;
int rotate = rotateTime->elapsed()/10;
if (rotateTime->elapsed()/10 > 255) {
rotateTime->restart();
rotate = 0;
}
ui->statusBar->showMessage(QString("Time: %1").arg(rotate));
LaserFunctions::cycleColours(scene, rotate);
scene->offsetX = 103;
scene->offsetY = 103;
LaserFunctions::spinSquareEdges(scene, rotate);
scene->offsetX = 51;
scene->offsetY = 108;
LaserFunctions::spinCubeZ(scene, rotate);
scene->offsetX = 103;
scene->offsetY = 51;
LaserFunctions::spinCubeY(scene, rotate);
scene->offsetX = 51;
scene->offsetY = 244;
LaserFunctions::spinCubeYZ(scene, rotate);
}
void MainWindow::on_action_Export_triggered()
{
ExportILDA();
}
void MainWindow::on_pushButton_clicked()
{
ExportILDA();
}
void MainWindow::ExportILDA() {
vector<vector<coordinate_data>> allData(md.vertices.size() == 0 ? 256 : 128);
for (int i = 0; i < allData.size() - 1; i++) {
if (md.vertices.size() == 0)
allData[i] = LaserFunctionsILDA::spinCubeYZ(i);
else {
double rotDeg = i*M_1_PI/6.4;
vector3d rotAng = {0.05, 0.03, 0.02};
rotAng.normalize();
rotAng.x *= rotDeg/(M_1_PI*2.5);
md.rotate(rotAng);
md.filterVisible(ui->allowedOverlaps->value());
allData[i] = md.exportILDA(ui->hiddenLineVisible->isChecked());
}
}
vector<char> chrsVec = ILDASerializer::coordinates(allData);
vector<char> colsVec = ILDASerializer::colourTable();
//QString filename = QFileDialog::getSaveFileName(this, "Save File", QDir::currentPath(), "ILDA v5.1 (*.ild)");
//string fn = "012356789";
//fn.substr(fn.length() - 3);
//filename = filename.isNull() ? "Null!" : filename.endsWith(".ild", Qt::CaseInsensitive) ? filename : filename + ".ild";
//QMessageBox::information(this, "Title", filename, QMessageBox::Ok | QMessageBox::Close);
ofstream out("out.ild", std::ofstream::binary);
out.write((const char*)colsVec.data(), colsVec.size());
out.write((const char*)chrsVec.data(), chrsVec.size());
out.close();
QMessageBox::information(this, "Success", "Successfully exported ILDA file!", QMessageBox::Ok);
}
bool canDraw(vector3d pos, vector<vector<vector3d>> points) {
for (vector<vector<vector3d>>::iterator it = points.begin(); it != points.end(); it++) {
vector<vector3d> f = *it;
for (vector<vector3d>::iterator it1 = f.begin(); it1 != f.end(); it1++) {
vector3d pt1 = *it1;
vector3d pt2 = *(it1 + 1);
if (it1 + 1 == f.end()) {
pt2 = *f.begin();
}
if (pos.y > pt1.y == pos.y > pt2.y)
break;
if (pos.x >= pt1.x != pos.x >= pt2.x)
return false;
}
}
return true;
}
void MainWindow::on_action_Import_triggered()
{
if (md.vertices.size() == 0) {
md.processData("/home/silvea/Documents/Laser Shit/Objs/Monkey.obj");
for (int i=0; i < md.vertices.size(); i++) {
cout << md.vertices[i].pos.x << "," << md.vertices[i].pos.y << "," << md.vertices[i].pos.z << endl;
}
disconnect(redrawTimer, SIGNAL(timeout()), this, SLOT(redraw()));
connect(redrawTimer, SIGNAL(timeout()), this, SLOT(spinImport()));
}
}
void MainWindow::spinImport()
{
scene->clearScene();
scene->offsetX = 0;
scene->offsetY = 0;
if (rotateTime->elapsed()/10 > 511) {
rotateTime->restart();
}
double rotDeg = rotateTime->elapsed()*M_1_PI/256;
ui->statusBar->showMessage(QString("Time: %1").arg(rotateTime->elapsed()/10));
scene->setColour(Qt::white);
vector<vector<vector3d>> points;
if (ui->spinObject->isChecked())
{
vector3d rotAng = {0.05, 0.03, 0.02};
rotAng.normalize();
rotAng.x *= rotDeg/(M_1_PI*2.5);
md.rotate(rotAng);
}
//vector<face *> facesToDraw = md.filterVisible(ui->allowedOverlaps->value());
vector<segment3d> vecs = md.filterEdges();
for (int i=0; i < vecs.size(); i++) {
//for (int i=facesToDraw.size()-1; i > -1; i--) {
//face * f = facesToDraw[i];
scene->setBlanking(true);
//for (int j=0; j < f->verts.size()+1; j++) {
//vertex v = *f->verts[j != f->verts.size() ? j : 0];
//vector3d vNorm;
//vector3d vPoint = v.pos;
segment3d vPoint = vecs[i];
if (true)//f->canDraw && f->canDraw1)
scene->setColour(QColor(127, 255, 255, 255));
else
//if (ui->hiddenLineVisible->isChecked())
//scene->setColour(QColor(255, 0, 255, (vPoint.z + 1) * 96));
//else
continue;
#if FALSE
>> YO COMPILER IS A DUM DUM <<
vNorm.x = v.pos.x - md.center.x;
vNorm.y = v.pos.y - md.center.y;
vNorm.z = v.pos.z - md.center.z;
//vPoint.x = vNorm.x * sin(rotDeg) + vNorm.y * -cos(rotDeg);
//vPoint.y = vNorm.y * sin(rotDeg) + vNorm.z * -cos(rotDeg);
//vPoint.z = vNorm.z * sin(rotDeg) + vNorm.x * -cos(rotDeg);
// Euler rotation, order XYZ
vPoint.x = vNorm.x;
vPoint.y = vNorm.y * cos(rotDeg) - vNorm.z * sin(rotDeg);
vPoint.z = vNorm.z * cos(rotDeg) + vNorm.y * sin(rotDeg);
vNorm = vPoint;
vPoint.x = vNorm.x * cos(rotDeg) + vNorm.z * sin(rotDeg);
vPoint.z = vNorm.z * cos(rotDeg) - vNorm.x * sin(rotDeg);
vNorm = vPoint;
vPoint.x = vNorm.x * cos(rotDeg) - vNorm.y * sin(rotDeg);
vPoint.y = vNorm.y * cos(rotDeg) + vNorm.x * sin(rotDeg);
//if (!canDraw(vPoint, points))
//break;
#endif
//if (!canDraw(vPoint, points))
//break;
//scene->setPos(vPoint.x * -100 + 100, vPoint.y * -100 + 100);
scene->setPos(vPoint.start.x * -100 + 100, vPoint.start.y * -100 + 100);
scene->setBlanking(false);
scene->setPos(vPoint.end.x * -100 + 100, vPoint.end.y * -100 + 100);
scene->setBlanking(true);
//if (j == 0) {
//scene->setBlanking(false);
//}
//}
}
/*vector3d vPoint, vNorm;
vNorm.x = 1;
vNorm.y = 1;
vNorm.z = 1;
// Euler rotation, order XYZ
vPoint.x = vNorm.x;
vPoint.y = vNorm.y * cos(rotDeg) - vNorm.z * sin(rotDeg);
vPoint.z = vNorm.z * cos(rotDeg) + vNorm.y * sin(rotDeg);
vNorm = vPoint;
vPoint.x = vNorm.x * cos(rotDeg) + vNorm.z * sin(rotDeg);
vPoint.z = vNorm.z * cos(rotDeg) - vNorm.x * sin(rotDeg);
vNorm = vPoint;
vPoint.x = vNorm.x * cos(rotDeg) - vNorm.y * sin(rotDeg);
vPoint.y = vNorm.y * cos(rotDeg) + vNorm.x * sin(rotDeg);
scene->setBlanking(true);
scene->setColour(Qt::red);
//scene->setPos(sin(rotDeg)*100 + 100, 100);
scene->setPos(vPoint.y*100 + 100, vPoint.z*100 + 100);
scene->setBlanking(false);
scene->setPos(100, 100);
scene->setColour(Qt::green);
//scene->setPos(100, -cos(rotDeg)*100 + 100);
scene->setPos(vPoint.x*100 + 100, vPoint.z*100 + 100);
scene->setBlanking(true);
scene->setPos(100, 100);
scene->setColour(Qt::blue);
scene->setBlanking(false);
//scene->setPos(cos(rotDeg)*100 + 100, -sin(rotDeg)*100 + 100);
scene->setPos(vPoint.x*100 + 100, vPoint.y*100 + 100);*/
scene->setBlanking(true);
}
void MainWindow::on_stepSpin_clicked()
{
if (!ui->spinObject->isChecked())
{
vector3d rotAng;
rotAng.x = 0.05;
rotAng.y = 0.03;
rotAng.z = 0.02;
md.rotate(rotAng);
}
}
void MainWindow::on_spinObject_toggled(bool checked)
{
ui->stepSpin->setEnabled(!checked);
}