-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplaywindow.cpp
206 lines (180 loc) · 6.11 KB
/
displaywindow.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
#include "displaywindow.h"
#include "steploader.h"
#include "triangulate.h"
#include "vtk.h"
//QT
#include <qcolor.h>
#include <qcolordialog.h>
#include <qfiledialog.h>
#include <qfileinfo.h>
#include <qpoint.h>
//VTK
#include <QVTKInteractor.h>
#include <vtkActor.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <iostream>
DisplayWindow::DisplayWindow(QWidget *parent) : QMainWindow(parent)
{
actor_count = 0;
showMaximized();
setup_vtk();
setup_layout();
setup_actions();
setup_menus();
setup_buttons();
}
DisplayWindow::~DisplayWindow() {
//Actions
delete load_action;
delete background_action;
//Menus
delete file_menu;
delete options_menu;
delete menu;
//Buttons
delete copy_button;
delete delete_button;
//Layouts
delete layout;
delete button_layout;
//Widgets
delete button_container;
delete list;
delete widget;
}
void DisplayWindow::setup_buttons() {
connect(copy_button, SIGNAL(clicked()), this, SLOT(copy_model()));
connect(delete_button, SIGNAL(clicked()), this, SLOT(delete_model()));
}
void DisplayWindow::setup_layout() {
widget = new QWidget();
layout = new QGridLayout();
button_container = new QWidget();
button_layout = new QHBoxLayout();
widget->setLayout(layout);
button_container->setLayout(button_layout);
setCentralWidget(widget);
list = new QListWidget();
layout->addWidget(vtk_widget, 0, 0, 2, 1);
layout->addWidget(button_container, 0, 1, 1, 1);
layout->addWidget(list, 1, 1, 1, 1);
layout->setColumnStretch(0, 3);
copy_button = new QPushButton(tr("Copy"));
delete_button = new QPushButton(tr("Delete"));
button_layout->addWidget(copy_button);
button_layout->addWidget(delete_button);
}
void DisplayWindow::setup_menus() {
menu = new QMenuBar();
file_menu = menu->addMenu(tr("&File"));
file_menu->addAction(load_action);
file_menu->addAction(quit_action);
options_menu = menu->addMenu(tr("&Options"));
options_menu->addAction(background_action);
setMenuBar(menu);
}
void DisplayWindow::setup_actions() {
load_action = new QAction(tr("&Load"), this);
load_action->setShortcuts(QKeySequence::Open);
load_action->setStatusTip(tr("Load a model"));
connect(load_action, SIGNAL(triggered()), this, SLOT(load_file()));
quit_action = new QAction(tr("&Quit"), this);
quit_action->setShortcuts(QKeySequence::Close);
quit_action->setStatusTip(tr("Exit Program"));
connect(quit_action, SIGNAL(triggered()), this, SLOT(close()));
background_action = new QAction(tr("&Set Background Colour"), this);
background_action->setStatusTip(tr("What do you think it might do??"));
connect(background_action, SIGNAL(triggered()), this, SLOT(set_background_colour()));
}
void DisplayWindow::setup_vtk() {
vtk_widget = new QVTKWidget();
renderer = vtkSmartPointer<vtkRenderer>::New();
renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<QVTKInteractor> interactor = vtk_widget->GetInteractor();
vtkSmartPointer<vtkInteractorStyleTrackballActor> style = vtkSmartPointer<vtkInteractorStyleTrackballActor>::New();
interactor->SetInteractorStyle(style);
interactor->SetRenderWindow(renderWindow);
vtk_widget->SetRenderWindow(renderWindow);
vtk_widget->show();
vtk_widget->update();
}
void DisplayWindow::load_file() {
add_model(load_model());
}
model DisplayWindow::load_model() {
QString filename;
while(filename.isEmpty()) {
filename = QFileDialog::getOpenFileName(this, tr("Open Model"), "", tr("STEP (*.stp *.step);; STL (*.stl);; WILD(*.*)"));
}
StepLoader loader(filename);
QFileInfo info(filename);
QString name = info.fileName();
return model(name, loader.get_shape());
}
void DisplayWindow::add_model(model& shape) {
list->addItem(shape.name);
vtkSmartPointer<vtkActor> actor = display_model(shape);
if(!name_actor_map.contains(shape.name)) {
name_actor_map.insert(shape.name, actor);
}
else {
shape.name = new_name(shape.name);
name_actor_map.insert(shape.name, actor);
}
}
vtkSmartPointer<vtkActor> DisplayWindow::display_model(const model& shape) {
VTK_Helper helper;
vtkSmartPointer<vtkPolyData> polydata = helper.cascade_to_vtk(triangulate(shape.shape));
helper.colour_original_faces();
return display_polydata(polydata);
}
vtkSmartPointer<vtkActor> DisplayWindow::display_polydata(const vtkSmartPointer<vtkPolyData> polydata) {
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polydata);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->SetPosition(2 * (actor_count % 5), - floor(actor_count / 5), 0);
normalise_vtk_actor(actor);
renderer->AddActor(actor);
actor_count++;
renderer->ResetCamera();
vtk_widget->SetRenderWindow(renderWindow);
vtk_widget->show();
vtk_widget->update();
return actor;
}
void DisplayWindow::set_background_colour() {
QColor colour = QColorDialog::getColor();
renderer->SetBackground(colour.redF(), colour.greenF(), colour.blueF());
}
void DisplayWindow::copy_model() {
if(list->currentItem()) {
QListWidgetItem* item = list->currentItem();
vtkSmartPointer<vtkActor> actor = name_actor_map[item->text()];
vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::SafeDownCast(actor->GetMapper()->GetInput());
vtkSmartPointer<vtkPolyData> polydata_copy = vtkSmartPointer<vtkPolyData>::New();
polydata_copy->DeepCopy(polydata);
vtkSmartPointer<vtkActor> new_actor = display_polydata(polydata_copy);
QString copy_name = new_name(item->text());
list->addItem(copy_name);
name_actor_map.insert(copy_name, new_actor);
}
}
void DisplayWindow::delete_model() {
if(list->currentItem()) {
QListWidgetItem* item = list->currentItem();
vtkSmartPointer<vtkActor> actor = name_actor_map[item->text()];
renderer->RemoveActor(actor);
vtk_widget->update();
delete item;
}
}
QString DisplayWindow::new_name(QString name) {
//This will probably be changed as it could be a little misleading
if(name_actor_map.contains(name))
return new_name(name.append("_copy"));
return name;
}