-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainWindow.cpp
523 lines (446 loc) · 16.7 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "mainWindow.h"
#include <QDesktopWidget>
#include <QMdiSubWindow>
#include <QDir>
#include <vtkCellLocator.h>
#include <vtkOBJExporter.h>
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
mdiArea = new QMdiArea;
mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setCentralWidget(mdiArea);
createActions();
menuBar = new QMenuBar(0);
this->setMenuBar(menuBar);
createMenus();
showMaximized();
windowMapper = new QSignalMapper(this);
connect(windowMapper, SIGNAL(mapped(QWidget*)), this, SLOT(setActiveSubWindow(QWidget*)));
connect(windowMapper, SIGNAL(mapped(QWidget*)),this, SLOT(wrapSetActiveSubWindow(QWidget *)));
setAcceptDrops(true);
mdiArea->setAcceptDrops(true);
QDesktopWidget qdw;
setMaximumSize(qdw.size());
setWindowFlags(Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint | Qt::CustomizeWindowHint);
moveStyle = NULL;
updateMenu();
}
MainWindow::~MainWindow()
{
}
void MainWindow::selectOnOnject(vtkSmartPointer<vtkPolyData> path)
{
qDebug() << "There are " << path->GetNumberOfPoints() << " points in the path." ;
vtkSmartPointer<vtkPoints> pt = vtkSmartPointer<vtkPoints>::New();
double point[3];
double displayPt[4];
// get points on the drawing path
for (int i = 0; i < path->GetNumberOfPoints(); i++)
{
path->GetPoint(i, point);
vtkInteractorObserver::ComputeDisplayToWorld(VTKAObject()->mRenderer, point[0], point[1], point[2], displayPt);
pt->InsertPoint(i, displayPt[0], displayPt[1], displayPt[2]);
//qDebug()<<"path point"<<displayPt[0]<<displayPt[1]<<displayPt[2];
}
// create 2D convex hull for the points on the path
vtkSmartPointer<vtkPolyData> pointdata = vtkSmartPointer<vtkPolyData>::New();
pointdata->SetPoints(pt);
vtkSmartPointer<vtkVertexGlyphFilter> vertexFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
vertexFilter->SetInputConnection(pointdata->GetProducerPort());
vertexFilter->Update();
vtkSmartPointer<vtkCleanPolyData> cleaner = vtkSmartPointer<vtkCleanPolyData>::New();
cleaner->SetInputConnection (vertexFilter->GetOutputPort());
vtkSmartPointer<vtkDelaunay2D> delaunay = vtkSmartPointer<vtkDelaunay2D>::New();
delaunay->SetInputConnection (cleaner->GetOutputPort());
delaunay->Update();
vtkSmartPointer<vtkPolyData> selected = vtkSmartPointer<vtkPolyData>::New();
selected->ShallowCopy(delaunay->GetOutput());
/*
vtkSmartPointer<vtkDataSetMapper> mapper1 = vtkSmartPointer<vtkDataSetMapper>::New();
mapper1->SetInput(selected);
vtkSmartPointer<vtkActor> actor1 = vtkSmartPointer<vtkActor>::New();
actor1->SetMapper(mapper1);
actor1->GetProperty()->SetColor(1,0,0);
actor1->GetProperty()->SetLineWidth(10);
actor1->GetProperty()->SetPointSize(5);
VTKAObject()->mRenderer->AddActor(actor1);*/
// do ray tracing to project the convex hull on to the object
vtkSmartPointer<vtkCellLocator> cellLocator = vtkSmartPointer<vtkCellLocator>::New();
cellLocator->SetDataSet(selected);
cellLocator->BuildLocator();
double intersectPoints[3], pcoord[3], t, projection[3];
int sub, result;
vtkSmartPointer<vtkPoints> ptOnObject = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkIdTypeArray> ids = vtkSmartPointer<vtkIdTypeArray>::New();
ids->SetNumberOfComponents(1);
typeIds.clear();
double* direction = new double[3];
double* camera = new double[3];
direction = VTKAObject()->mRenderer->GetActiveCamera()->GetDirectionOfProjection();
camera = VTKAObject()->mRenderer->GetActiveCamera()->GetPosition();
qDebug()<<"get here"<<camera[0]<<camera[1]<<camera[2];
for (int i = 0; i < VTKAObject()->mVtkPolyData->GetNumberOfPoints(); i++)
{
VTKAObject()->mVtkPolyData->GetPoint(i, point); // reuse double point[3] here
if (!isVisible(point, VTKAObject()->mRenderer)) // select only visible point in current camera position
continue;
projection[0] = point[0] - direction[0]*10000;
projection[1] = point[1] - direction[1]*10000;
projection[2] = point[2] - direction[2]*10000;
result = cellLocator->IntersectWithLine(point, projection, 0.0001, t, intersectPoints, pcoord, sub);
//qDebug()<<"result = "<<result<<point[0]<<point[1]<<point[2];
if (result != 0)
{
ptOnObject->InsertNextPoint(point[0], point[1], point[2]);
typeIds.push_back(i);
ids->InsertNextValue(i);
}
}
pointdata = vtkSmartPointer<vtkPolyData>::New();
pointdata->SetPoints(ptOnObject);
vertexFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
vertexFilter->SetInputConnection(pointdata->GetProducerPort());
vertexFilter->Update();
/*
vtkSmartPointer<vtkSelectionNode> selectionNode = vtkSmartPointer<vtkSelectionNode>::New();
selectionNode->SetFieldType(vtkSelectionNode::POINT);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);
selectionNode->GetProperties()->Set(vtkSelectionNode::CONTAINING_CELLS(), 1);
vtkSmartPointer<vtkSelection> selection =
vtkSmartPointer<vtkSelection>::New();
selection->AddNode(selectionNode);
vtkSmartPointer<vtkExtractSelection> extractSelection =
vtkSmartPointer<vtkExtractSelection>::New();
extractSelection->SetInput(0, VTKAObject()->mVtkPolyData);
extractSelection->SetInput(1, selection);
extractSelection->Update();
vtkSmartPointer<vtkUnstructuredGrid> selecteddata = vtkSmartPointer<vtkUnstructuredGrid>::New();
selecteddata->ShallowCopy(extractSelection->GetOutput());*/
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputConnection(vertexFilter->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(1,0,0);
actor->GetProperty()->SetLineWidth(10);
actor->GetProperty()->SetPointSize(5);
VTKAObject()->mRenderer->AddActor(actor);
//VTKAObject()->mInteractor->Start();
moveStyle = new InteractorStyleMoveGlyph;
VTKAObject()->mInteractor->SetInteractorStyle( moveStyle );
moveStyle->Data = VTKAObject()->mVtkPolyData;
//moveStyle->ids = ids;
moveStyle->MoveData = pointdata;
moveStyle->surfaceMapper = mapper;
moveStyle->widget = VTKAObject();
moveStyle->typeId = typeIds;
moveStyle->renderMoveData();
mdiArea->setActiveSubWindow(mdiArea->subWindowList()[0]);
updateMenu();
}
bool MainWindow::isVisible(double *point, vtkSmartPointer<vtkRenderer> render)
{
vtkSmartPointer<vtkPoints> vtkPoint = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkPolyData> pointData = vtkSmartPointer<vtkPolyData>::New();
vtkPoint->SetNumberOfPoints(1);
vtkPoint->SetPoint(0, point[0], point[1], point[2]);
pointData->SetPoints(vtkPoint);
vtkSmartPointer<vtkSelectVisiblePoints> selectVisiblePoints = vtkSmartPointer<vtkSelectVisiblePoints>::New();
selectVisiblePoints->SetInput(pointData);
selectVisiblePoints->SetRenderer(render);
selectVisiblePoints->Update();
if (selectVisiblePoints->GetOutput()->GetNumberOfPoints() != 0)
{
return true;
}
else
{
return false;
}
}
void MainWindow::updateMenu()
{
int size = mdiArea->subWindowList().size();
switch(size)
{
case 0:
openAct->setEnabled(true);
drawAct->setEnabled(false);
selectAct->setEnabled(false);
rotateAct->setEnabled(false);
exportAct->setEnabled(false);
break;
case 1:
openAct->setEnabled(true);
drawAct->setEnabled(true);
selectAct->setEnabled(false);
rotateAct->setEnabled(false);
exportAct->setEnabled(true);
break;
case 2:
openAct->setEnabled(true);
drawAct->setEnabled(true);
selectAct->setEnabled(true);
rotateAct->setEnabled(true);
exportAct->setEnabled(true);
break;
default:
openAct->setEnabled(true);
drawAct->setEnabled(false);
selectAct->setEnabled(false);
rotateAct->setEnabled(false);
exportAct->setEnabled(false);
break;
}
}
void MainWindow::createActions()
{
openAct = new QAction(tr("&Open"), this);
openAct->setStatusTip(tr("Open a 3D model"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
drawAct = new QAction(tr("&Draw"), this);
drawAct->setStatusTip(tr("Draw on 2D image"));
connect(drawAct, SIGNAL(triggered()), this, SLOT(draw()));
selectAct = new QAction(tr("&Select"), this);
selectAct->setStatusTip(tr("Select and move the points"));
connect(selectAct, SIGNAL(triggered()), this, SLOT(select()));
rotateAct = new QAction(tr("&Rotate"), this);
rotateAct->setStatusTip(tr("Rotate the 3D model"));
connect(rotateAct, SIGNAL(triggered()), this, SLOT(rotate()));
exportAct = new QAction(tr("&Export"), this);
exportAct->setStatusTip(tr("Export the 3D model"));
connect(exportAct, SIGNAL(triggered()), this, SLOT(exportModel()));
}
void MainWindow::createMenus()
{
menuBar->addAction(openAct);
menuBar->addAction(drawAct);
menuBar->addAction(selectAct);
menuBar->addAction(rotateAct);
menuBar->addAction(exportAct);
}
void MainWindow::open(QString fileName)
{
QStringList filters;
filters.push_back("*.obj");
filters.push_back("*.jpg");
QString file;
if (fileName.isEmpty())
{
if (mdiArea->subWindowList().size() > 0)
{
mdiArea->closeAllSubWindows();
}
file = QFileDialog::getOpenFileName(this,tr("Open a file"), QDir::currentPath(), filters.join(";;"));
}
else
{
file = fileName;
}
if (!file.isEmpty())
{
file = QDir::toNativeSeparators(file);
QFileInfo fi(file);
if (!fi.isFile())
{
QMessageBox::critical(this, tr("Opening Object Error"), "Unable to open " + fi.fileName() + ": It is not a File!");
return;
}
QStringList nameElement = file.split(QDir::separator());
QString fileNameElement = nameElement[nameElement.size() - 1];
QString path = file;
path.truncate(path.lastIndexOf(QDir::separator())); // PC
newImage();
qDebug()<<file;
OBJECTTYPE open = VTKA()->render(file);
int windows = VTKA()->getId();
if(open == EMPTY)
{
if(mdiArea->currentSubWindow())
{
mdiArea->closeActiveSubWindow();
}
if(fi.exists())
{
QMessageBox::critical(this, tr("Opening Error"), "Unable to open " + fi.fileName() + ". ");
}
else
{
QMessageBox::critical(this, tr("Opening Error"), "Unable to open " + fi.fileName() + ": File not found. ");
}
updateAllViews();
updateMenu();
return;
}
else if (open == MODEL3D)
{
}
currentVtkView()->prjName = fi.fileName();
currentVtkView()->setWindowTitle(currentVtkView()->prjName);
updateAllViews();
if (mdiArea->subWindowList().size() == 1)
updateMenu();
}
}
void MainWindow::draw()
{
if (mdiArea->subWindowList().size() == 2)
mdiArea->removeSubWindow(mdiArea->subWindowList()[1]);
VTKA()->renderUpdate();
QString file = QDir::currentPath();
file.append(QDir::separator() + QString("screenshot.jpg"));
//QSize size = mdiArea->subWindowList()[0]->frameSize();
VTKA()->mvc()->showNormal();
//VTKA()->mvc()->setMaximumSize(size);
VTKA()->screenshot(file);
VTKA()->mvc()->showMaximized();
open(file);
if (mdiArea->subWindowList().size() != 2)
{
QMessageBox::critical(this, tr("Opening Error"), "The screenshot is not correctly generated/loaded!");
return;
}
connect(VTKAImage(), SIGNAL(selectedSurface(vtkSmartPointer<vtkPolyData>)), this, SLOT(selectOnOnject(vtkSmartPointer<vtkPolyData>)));
}
void MainWindow::select()
{
if (moveStyle == NULL)
return;
moveStyle->renderMoveData();
VTKAObject()->mInteractor->SetInteractorStyle( moveStyle );
}
void MainWindow::rotate()
{
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
VTKAObject()->mInteractor->SetInteractorStyle( style );
}
void MainWindow::exportModel()
{
if (VTKAObject() == 0)
return;
QString file = QFileDialog::getSaveFileName((QWidget* )0, "Export Object", QString(), "");
vtkSmartPointer<vtkOBJExporter> writer = vtkSmartPointer<vtkOBJExporter>::New();
writer->SetRenderWindow(VTKAObject()->mRenderer->GetRenderWindow());
writer->SetFilePrefix(file.toStdString().c_str());
writer->Write();
}
void MainWindow::setActiveSubWindow(QWidget *window)
{
if (!window)
return;
mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow *>(window));
}
void MainWindow::wrapSetActiveSubWindow(QWidget* window)
{
QMdiSubWindow* subwindow;
subwindow = dynamic_cast<QMdiSubWindow*>(window);
if(subwindow!= NULL)
{
mdiArea->setActiveSubWindow(subwindow);
}
else
{
qDebug("Type of window is not a QMdiSubWindow*");
}
}
QMdiSubWindow* MainWindow::newImage()
{
VtkView *mvcont = new VtkView(mdiArea);
QMdiSubWindow* subWindow = mdiArea->addSubWindow(mvcont);
subWindow->installEventFilter(this);
VtkWidget *gla = new VtkWidget(mvcont);
vtkSmartPointer<vtkRenderer> render;
mvcont->addView(gla, Qt::Horizontal);// this fuction may destroy the renderer on mdiArea
connect(mvcont,SIGNAL(updateMainWindowMenus()),this,SLOT(updateAllViews()));
gla->mvc()->showMaximized(); // make the mdi window a full screen view
updateAllViews();
return subWindow;
}
void MainWindow::setHandleMenu(QPoint point, Qt::Orientation orientation, QSplitter *origin)
{
VtkView *mvc = currentVtkView();
int epsilon =10;
splitMenu->clear();
unSplitMenu->clear();
//the viewer to split/unsplit is chosen through picking
//Vertical handle allows to split horizontally
if(orientation == Qt::Vertical)
{
splitUpAct->setData(point);
splitDownAct->setData(point);
//check if the viewer on the top is splittable according to its size
int pickingId = mvc->getViewerByPicking(QPoint(point.x(), point.y()-epsilon));
if(pickingId>=0)
splitUpAct->setEnabled(mvc->getViewer(pickingId)->size().width()/2 > mvc->getViewer(pickingId)->minimumSizeHint().width());
//the viewer on top can be closed only if the splitter over the handle that orginated the event has one child
bool unSplittabilityUp = true;
VtkSplitter * upSplitter = qobject_cast<VtkSplitter *>(origin->widget(0));
if(upSplitter)
unSplittabilityUp = !(upSplitter->count()>1);
unsplitUpAct->setEnabled(unSplittabilityUp);
//check if the viewer below is splittable according to its size
pickingId = mvc->getViewerByPicking(QPoint(point.x(), point.y()+epsilon));
if(pickingId>=0)
splitDownAct->setEnabled(mvc->getViewer(pickingId)->size().width()/2 > mvc->getViewer(pickingId)->minimumSizeHint().width());
//the viewer below can be closed only if the splitter ounder the handle that orginated the event has one child
bool unSplittabilityDown = true;
VtkSplitter * downSplitter = qobject_cast<VtkSplitter *>(origin->widget(1));
if(downSplitter)
unSplittabilityDown = !(downSplitter->count()>1);
unsplitDownAct->setEnabled(unSplittabilityDown);
splitMenu->addAction(splitUpAct);
splitMenu->addAction(splitDownAct);
unsplitUpAct->setData(point);
unsplitDownAct->setData(point);
unSplitMenu->addAction(unsplitUpAct);
unSplitMenu->addAction(unsplitDownAct);
}
//Horizontal handle allows to split vertically
else if (orientation == Qt::Horizontal)
{
splitRightAct->setData(point);
splitLeftAct->setData(point);
//check if the viewer on the right is splittable according to its size
int pickingId =mvc->getViewerByPicking(QPoint(point.x()+epsilon, point.y()));
if(pickingId>=0)
splitRightAct->setEnabled(mvc->getViewer(pickingId)->size().height()/2 > mvc->getViewer(pickingId)->minimumSizeHint().height());
//the viewer on the rigth can be closed only if the splitter on the right the handle that orginated the event has one child
bool unSplittabilityRight = true;
VtkSplitter * rightSplitter = qobject_cast<VtkSplitter *>(origin->widget(1));
if(rightSplitter)
unSplittabilityRight = !(rightSplitter->count()>1);
unsplitRightAct->setEnabled(unSplittabilityRight);
//check if the viewer on the left is splittable according to its size
pickingId =mvc->getViewerByPicking(QPoint(point.x()-epsilon, point.y()));
if(pickingId>=0)
splitLeftAct->setEnabled(mvc->getViewer(pickingId)->size().height()/2 > mvc->getViewer(pickingId)->minimumSizeHint().height());
//the viewer on the left can be closed only if the splitter on the left of the handle that orginated the event has one child
bool unSplittabilityLeft = true;
VtkSplitter * leftSplitter = qobject_cast<VtkSplitter *>(origin->widget(0));
if(leftSplitter)
unSplittabilityLeft = !(leftSplitter->count()>1);
unsplitLeftAct->setEnabled(unSplittabilityLeft);
splitMenu->addAction(splitRightAct);
splitMenu->addAction(splitLeftAct);
unsplitRightAct->setData(point);
unsplitLeftAct->setData(point);
unSplitMenu->addAction(unsplitRightAct);
unSplitMenu->addAction(unsplitLeftAct);
}
handleMenu->popup(point);
}
void MainWindow::updateAllViews()
{
VtkView *mvc = currentVtkView();
if(mvc)
{
mvc->updateAllViewer();
}
if(VTKA())
{
VTKA()->update();
}
}