-
Notifications
You must be signed in to change notification settings - Fork 7
/
dialog.cpp
481 lines (391 loc) · 12.9 KB
/
dialog.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "dialog.h"
#include "ui_dialog.h"
#define JC_VORONOI_IMPLEMENTATION
#include "jc_voronoi.h"
#include "clipper.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDateTime>
#include <QMouseEvent>
#include <cmath>
#include "utils.h"
#include "polysegs.h"
#define M 1000.f
using namespace ClipperLib;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->mGLWidget, &cGLWidget::glInitialized, this, &Dialog::onGlInitialized);
connect(ui->mGLWidget, &cGLWidget::leftMouseButtonPressed, this, &Dialog::onLeftMouseButtonPressed);
connect(ui->mGLWidget, &cGLWidget::rightMouseButtonPressed, this, &Dialog::onRightMouseButtonPressed);
connect(ui->mGLWidget, &cGLWidget::mouseButtonClicked, this, &Dialog::onMouseButtonClicked);
connect(ui->mGLWidget, &cGLWidget::mouseMove, this, &Dialog::onMouseMove);
connect(ui->mGLWidget, &cGLWidget::dragStarted, this, &Dialog::onDragStarted);
connect(ui->mGLWidget, &cGLWidget::dropped, this, &Dialog::onDropped);
mDXF.processDXF(":/default.dxf");
ui->mGLWidget->setFocus();
mDragSubjectIterator = mPoints.end();
}
void Dialog::onGlInitialized()
{
drawScene();
}
void Dialog::drawScene()
{
ui->mGLWidget->clearVBOs();
drawSystem();
drawControlPoints();
if(ui->showClipperCheckbox->isChecked()) {
drawClipper();
}
}
void Dialog::onDragStarted(QVector3D p)
{
auto it = findMinDistPoint(p.toVector2D());
if(it != mPoints.end()) {
mDragSubjectIterator = it;
int x, y;
ui->mGLWidget->worldToScreen(QVector3D(*it, 0), x, y);
QPoint cp = ui->mGLWidget->mapToGlobal(QPoint(x, y));
QCursor::setPos(cp.x(), cp.y());
}
}
void Dialog::onDropped(QVector3D p)
{
if(mDragSubjectIterator != mPoints.end()) {
mDragSubjectIterator->setX(p.x());
mDragSubjectIterator->setY(p.y());
mDragSubjectIterator = mPoints.end();
drawScene();
voronoi();
}
}
void Dialog::onMouseMove(QVector3D p)
{
if(mDragSubjectIterator != mPoints.end()) {
mDragSubjectIterator->setX(p.x());
mDragSubjectIterator->setY(p.y());
drawScene();
voronoi();
}
}
void Dialog::onLeftMouseButtonPressed(QVector3D p)
{
// does nothing atm
}
void Dialog::onRightMouseButtonPressed(QVector3D p)
{
if(mPoints.size()) {
mPoints.erase(findMinDistPoint(p.toVector2D()));
mDragSubjectIterator = mPoints.end();
}
qd << "have points: " << mPoints.size();
drawScene();
voronoi();
}
void Dialog::onMouseButtonClicked(QVector3D p)
{
mPoints.push_back( p.toVector2D() );
mDragSubjectIterator = mPoints.end();
drawScene();
voronoi();
}
std::vector<QVector2D>::iterator Dialog::findMinDistPoint(QVector2D p)
{
float minDist = std::numeric_limits<float>::max();
std::vector<QVector2D>::iterator res = mPoints.end(); // end means nothing found!
for(auto it = mPoints.begin(); it!=mPoints.end(); ++it) {
float dist = (p - *it).length();
if(dist < minDist) {
minDist = dist;
res = it;
}
}
return res;
}
void Dialog::drawClipper()
{
ui->mGLWidget->addToVBO(mDXF.segments().glFloatArray(), GL_LINES, QVector4D(0.1, 0.1, 1, 1));
}
void Dialog::drawSystem()
{
PolySegs system;
for(int i=-2000; i<=2000; i+=100)
{
system.push_back(Seg2f(QVector2D(i,-2000), QVector2D(i, 2000)));
system.push_back(Seg2f(QVector2D(-2000,i), QVector2D(2000,i)));
}
ui->mGLWidget->addToVBO(system.glFloatArray(-2.5f), GL_LINES, QVector4D(0.2, 0.2, 0.2, 0.25));
}
int Dialog::random(int min, int max)
{
return rand() % (max + 1 - min) + min;
}
QString Dialog::DXF_Line(int id, float x1, float y1, float z1, float x2, float y2, float z2)
{
setlocale(LC_NUMERIC, "C");
char b[4096]={0};
sprintf(b,
"LINE\r\n"
"5\r\n"
"%02X\r\n"
"330\r\n"
"1F\r\n"
"100\r\n"
"AcDbEntity\r\n"
"8\r\n"
"0\r\n"
"6\r\n"
"Continuous\r\n"
"62\r\n"
"7\r\n"
"100\r\n"
"AcDbLine\r\n"
"10\r\n"
"%f\r\n"
"20\r\n"
"%f\r\n"
"30\r\n"
"%f\r\n"
"11\r\n"
"%f\r\n"
"21\r\n"
"%f\r\n"
"31\r\n"
"%f\r\n"
"0\r\n",id, x1, y1, z1, x2, y2, z2);
QString res(b);
setlocale(LC_ALL, "");
return res;
}
void Dialog::voronoi(const bool saveFile)
{
if(ui->mDXFFile->text().isEmpty()) {
QMessageBox::warning(this, "Warning", "Please specify an output DXF file.");
return;
}
if(mPoints.empty())
return;
ui->mGLWidget->clearVBOs();
drawScene();
jcv_point* points = 0;
float curves = ui->mCurves->value()/100.f;
int offs = ui->mOffset->value() * M / 2;
points = (jcv_point*)malloc( sizeof(jcv_point) * mPoints.size());
if( !points )
return;
srand(QDateTime::currentSecsSinceEpoch());
int idx=0;
for(auto& p : mPoints) {
points[idx].x = p.x();
points[idx].y = p.y();
qd << points[idx].x << points[idx].y;
idx++;
}
jcv_rect* rect = 0;
jcv_diagram diagram;
memset(&diagram, 0, sizeof(jcv_diagram));
jcv_diagram_generate(mPoints.size(), (const jcv_point*)points, rect, nullptr, &diagram);
if(saveFile)
writeFile(ui->mDXFFile->text(), readFile(":/dxf1.txt"));
int id=115;
Path mainPoly;
auto& s = mDXF.segments();
for(int i=0; i<s.size(); i++)
{
mainPoly << IntPoint(s[i].p0().x()*M, s[i].p0().y()*M) << IntPoint(s[i].p1().x()*M, s[i].p1().y()*M);
}
const jcv_site* sites = jcv_diagram_get_sites( &diagram );
for( int i = 0; i < diagram.numsites; ++i )
{
ClipperOffset co;
Paths solution;
Path poly;
float alpha = ui->mAlpha->value();
float tension = ui->mTension->value();
PolySegs segs;
const jcv_site* site = &sites[i];
const jcv_graphedge* e = site->edges;
while( e ) {
poly << IntPoint(e->pos[0].x*M, e->pos[0].y*M) << IntPoint(e->pos[1].x*M, e->pos[1].y*M);
e = e->next;
}
co.AddPath(poly, jtRound, etClosedPolygon);
solution.clear();
co.Execute(solution, -offs);
// should be only one solution for each site
for(int s=0; s<solution.size(); s++) {
segs.clear();
mPath.mPoints.clear();
for(int i=0; i<solution[s].size(); i++) {
int j=i+1; if(j==solution[s].size()) j=0;
QVector2D v1(solution[s][i].X/M, solution[s][i].Y/M);
QVector2D v2(solution[s][j].X/M, solution[s][j].Y/M);
segs.push_back(Seg2f(v1, v2));
mPath.mPoints.push_back(v1);
}
}
std::vector<QVector2D> pts;
if(ui->mSplineGroupBox->isChecked()) {
float totalLen = mPath.calcTotalLen(alpha, tension);
qd << "total len:" << totalLen;
float samplingStep = totalLen / ui->mSamples->value();
for(float dist=0.f; dist<=totalLen; dist+=samplingStep) {
float t = mPath.getT(dist, alpha, tension);
if(t<0.f) {
qDebug() << dist << "is over" << totalLen;
continue;
}
pts.push_back( mPath.getSplinePoint(t, alpha, tension) );
}
} else if(ui->mBezierGroupBox->isChecked()) {
for(int i=0; i<segs.size(); i++) {
int j=i+1; if(j==segs.size()) j=0;
QVector2D v1 = segs[i].p[1] - segs[i].p[0];
QVector2D v2 = segs[j].p[1] - segs[j].p[0];
QVector2D nv1 = segs[i].p[0] + v1*(1-(curves/2));
QVector2D nv2 = segs[j].p[0] + v2* (curves/2);
for(int t=0; t<=10; t+=1) {
pts.push_back(bezier(nv1, nv2, segs[i].p[1], t*0.1));
}
}
} else {
for(int i=0; i<segs.size(); i++) {
pts.push_back(QVector2D(segs[i].p[0].x(), segs[i].p[0].y()));
}
}
poly.clear();
for(int k=0; k<pts.size(); k++) {
int l=k+1; if(l==pts.size()) l=0;
poly << IntPoint(pts[k].x()*M, pts[k].y()*M) << IntPoint(pts[l].x()*M, pts[l].y()*M);
}
Clipper clipper;
clipper.AddPath(mainPoly, ptSubject, true);
clipper.AddPath(poly, ptClip, true);
clipper.Execute(ctIntersection, solution);
if(!solution.empty()) {
for(int s=0; s<solution.size(); s++) {
segs.clear();
for(int i=0; i<solution[s].size(); i++) {
int j=i+1; if(j==solution[s].size()) j=0;
QVector2D v1(solution[s][i].X/M, solution[s][i].Y/M);
QVector2D v2(solution[s][j].X/M, solution[s][j].Y/M);
segs.push_back(Seg2f(v1, v2));
if(saveFile) {
QString line = DXF_Line(id++, v1.x(), v1.y(), 0, v2.x(), v2.y(), 0);
appendFile(ui->mDXFFile->text(), line.toUtf8());
}
}
ui->mGLWidget->addToVBO(segs.glFloatArray(), GL_LINES, QVector4D(0.1, 0.1, 0.1, 1));
}
}
}
jcv_diagram_free( &diagram );
if(saveFile)
appendFile(ui->mDXFFile->text(), readFile(":/dxf2.txt"));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_mGenerate_clicked()
{
voronoi(true);
}
void Dialog::on_mBrowseButton_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("DXF file"), "voronoi.dxf", tr("DXF files (*.dxf)"));
ui->mDXFFile->setText(fileName);
}
void Dialog::drawControlPoints()
{
const float r=1;
for(auto v: mPoints) {
PolySegs segs;
for(int i=0; i<=360; i+=10) {
float phi = d2r(i);
float nextPhi = d2r(i+10);
segs.push_back(Seg2f(QVector2D(sin(phi)*r, cos(phi)*r), QVector2D(sin(nextPhi)*r, cos(nextPhi)*r)));
}
segs.translate(v);
ui->mGLWidget->addToVBO(segs.glFloatArray(0.5f), GL_LINES, QVector4D(0,0,0,1));
}
}
QVector2D Dialog::intersectLines_noParallel(const Seg2f& S0, const Seg2f& S1)
{
const QVector2D s(S1.p[0] - S0.p[0]);
const QVector2D t(S0.p[1] - S0.p[0]);
const QVector2D u(S1.p[0] - S1.p[1]);
const float det = vCrossZ(t, u);
const float detA = vCrossZ(s, u);
const float alpha = detA / det;
return S0.p[0] + t * alpha;
}
QVector2D Dialog::bezier(const QVector2D& s, const QVector2D& e, const QVector2D& c, const float t)
{
QVector2D q0 = s + (c - s) * t;
QVector2D q1 = c + (e - c) * t;
return q0 + (q1 - q0) * t;
}
void Dialog::on_mDXFClipperPolyBrowseButton_clicked()
{
auto fileName = QFileDialog::getOpenFileName(this, "Open DXF clipper file");
if(fileName.isEmpty())
return;
ui->mDXFClipperPolyFile->setText(fileName);
mDXF.processDXF(fileName);
drawScene();
}
void Dialog::on_mCurves_valueChanged(int arg1)
{
drawScene();
voronoi();
}
void Dialog::on_mOffset_valueChanged(double arg1)
{
drawScene();
voronoi();
}
void Dialog::on_showClipperCheckbox_stateChanged(int arg1)
{
drawScene();
voronoi();
}
void Dialog::on_mAlpha_valueChanged(double arg1)
{
drawScene();
voronoi();
}
void Dialog::on_mTension_valueChanged(double arg1)
{
drawScene();
voronoi();
}
void Dialog::on_mSamples_valueChanged(int arg1)
{
drawScene();
voronoi();
}
void Dialog::on_mSplineGroupBox_toggled(bool arg1)
{
if(ui->mBezierGroupBox->isChecked()) {
ui->mBezierGroupBox->blockSignals(true);
ui->mBezierGroupBox->setChecked(false);
ui->mBezierGroupBox->blockSignals(false);
}
drawScene();
voronoi();
}
void Dialog::on_mBezierGroupBox_toggled(bool arg1)
{
if(ui->mSplineGroupBox->isChecked()) {
ui->mSplineGroupBox->blockSignals(true);
ui->mSplineGroupBox->setChecked(false);
ui->mSplineGroupBox->blockSignals(false);
}
drawScene();
voronoi();
}