-
Notifications
You must be signed in to change notification settings - Fork 3
/
qtvtkitem.cpp
294 lines (237 loc) · 10.2 KB
/
qtvtkitem.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
#include "qtvtkitem.h"
void createLine(const double x1, const double y1, const double z1, const double x2, const double y2, const double z2, vtkSmartPointer<vtkPoints> points, vtkSmartPointer<vtkCellArray> cells)
{
vtkSmartPointer<vtkPolyLine> line;
line = vtkSmartPointer<vtkPolyLine>::New();
line->GetPointIds()->SetNumberOfIds(2);
vtkIdType id_1, id_2;
id_1 = points->InsertNextPoint(x1, y1, z1);
id_2 = points->InsertNextPoint(x2, y2, z2);
line->GetPointIds()->SetId(0, id_1);
line->GetPointIds()->SetId(1, id_2);
cells->InsertNextCell(line);
}
qtVtkItem::qtVtkItem(QQmlApplicationEngine* oeigen)
{
engine = std::move(oeigen);
}
void qtVtkItem::init()
{
QObject* topLevel = engine->rootObjects().value(0);
// Create classes instances
m_processingEngine = std::shared_ptr<ProcessingEngine>(new ProcessingEngine());
// Expose C++ classes to QML
qquickWindow = qobject_cast<QQuickWindow*>(topLevel);
qquickWindow->show();
QQuickVTKRenderWindow* QvtkRwd = new QQuickVTKRenderWindow();
// Fetch the QQuick window using the standard object name set up in the constructor
m_QQuickvtkrenderItem = topLevel->findChild<MyQQuickVTKRenderItem*>("ConeView");
m_otherRenderer = m_QQuickvtkrenderItem->renderer();
m_QQuickvtkrenderItem->setqtvtkItem(this);
// Create a cone pipeline and add it to the view
vtkNew<vtkActor> actor;
vtkNew<vtkPolyDataMapper> mapper;
vtkNew<vtkConeSource> cone;
vtkNew<vtkAxesActor> axes;
double axes_length = 100.0;
int16_t axes_label_font_size = 20;
axes->SetTotalLength(axes_length, axes_length, axes_length);
axes->GetXAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone();
axes->GetYAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone();
axes->GetZAxisCaptionActor2D()->GetTextActor()->SetTextScaleModeToNone();
axes->GetXAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize(axes_label_font_size);
axes->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize(axes_label_font_size);
axes->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->SetFontSize(axes_label_font_size);
//add platform
// Platform Model
vtkNew<vtkPolyDataMapper> platformModelMapper;
vtkNew<vtkCubeSource> m_platformModel;
platformModelMapper->SetInputConnection(m_platformModel->GetOutputPort());
vtkNew<vtkActor> m_platformModelActor;
m_platformModelActor->SetMapper(platformModelMapper);
m_platformModelActor->GetProperty()->SetColor(1, 1, 1);
m_platformModelActor->GetProperty()->LightingOn();
m_platformModelActor->GetProperty()->SetOpacity(1);
m_platformModelActor->GetProperty()->SetAmbient(0.45);
m_platformModelActor->GetProperty()->SetDiffuse(0.4);
m_platformModelActor->PickableOff();
// Platform Grid
vtkNew<vtkPolyData> m_platformGrid;
vtkSmartPointer<vtkPolyDataMapper> platformGridMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
platformGridMapper->SetInputData(m_platformGrid);
vtkNew<vtkActor> m_platformGridActor;
m_platformGridActor->SetMapper(platformGridMapper);
m_platformGridActor->GetProperty()->LightingOff();
m_platformGridActor->GetProperty()->SetColor(0.45, 0.45, 0.45);
m_platformGridActor->GetProperty()->SetOpacity(1);
m_platformGridActor->PickableOff();
double m_platformWidth = 200.0;
double m_platformDepth = 200.0;
double m_platformHeight = 200.0;
double m_platformThickness = 2.0;
double m_gridBottomHeight = 0.15;
uint16_t m_gridSize = 10;
// Platform Grid
vtkSmartPointer<vtkPoints> gridPoints = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> gridCells = vtkSmartPointer<vtkCellArray>::New();
for (int16_t i = -m_platformWidth / 2; i <= m_platformWidth / 2; i += m_gridSize)
{
createLine(i, -m_platformDepth / 2, m_gridBottomHeight, i, m_platformDepth / 2, m_gridBottomHeight, gridPoints, gridCells);
}
for (int16_t i = -m_platformDepth / 2; i <= m_platformDepth / 2; i += m_gridSize)
{
createLine(-m_platformWidth / 2, i, m_gridBottomHeight, m_platformWidth / 2, i, m_gridBottomHeight, gridPoints, gridCells);
}
m_platformGrid->SetPoints(gridPoints);
m_platformGrid->SetLines(gridCells);
m_platformModel->SetXLength(m_platformWidth);
m_platformModel->SetYLength(m_platformDepth);
m_platformModel->SetZLength(m_platformThickness);
m_platformModel->SetCenter(0.0, 0.0, -m_platformThickness / 2);
mapper->SetInputConnection(cone->GetOutputPort());
actor->SetMapper(mapper);
// m_QQuickvtkrenderItem->renderer()->AddActor(actor);
m_QQuickvtkrenderItem->renderer()->AddActor(axes);
m_QQuickvtkrenderItem->renderer()->AddActor(m_platformModelActor);
m_QQuickvtkrenderItem->renderer()->AddActor(m_platformGridActor);
camOrientManipulator = vtkSmartPointer<vtkCameraOrientationWidget>::New() ;
// Enable the widget.
m_QQuickvtkrenderItem->renderer()->ResetCamera();
m_QQuickvtkrenderItem->renderer()->SetBackground(0.5, 0.5, 0.7);
m_QQuickvtkrenderItem->renderer()->SetBackground2(0.7, 0.7, 0.7);
m_QQuickvtkrenderItem->renderer()->SetGradientBackground(true);
// m_QQuickvtkrenderItem->update();
// camOrientManipulator->SetCurrentRenderer(m_QQuickvtkrenderItem->renderer());
// vtkNew<vtkRenderWindowInteractor> iRen;
// QQuickVTKRenderWindow* qquickvtkWindow = topLevel->findChild<QQuickVTKRenderWindow*>("RenderWindowQt");
// vtkNew<vtkBoxWidget2> boxWidget;
// boxWidget->SetInteractor(iRen);
// boxWidget->GetRepresentation()->SetPlaceFactor(1); // Default is 0.5
// boxWidget->GetRepresentation()->PlaceWidget(m_platformModelActor->GetBounds());
// // Set up a callback for the interactor to call so we can manipulate the actor
// vtkNew<vtkBoxCallback> boxCallback;
// boxCallback->SetActor(m_platformModelActor);
// boxWidget->AddObserver(vtkCommand::InteractionEvent, boxCallback);
// boxWidget->On();
// Interactor Style
vtkSmartPointer<vtkInteractorStyleTrackballCamera2> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera2>::New();
// style->SetDefaultRenderer(m_QQuickvtkrenderItem->renderer());
// style->SetMotionFactor(10.0);
vtkNew<vtkRenderWindowInteractor> iRen;
m_QQuickvtkrenderItem->renderWindow()->renderWindow()->GetInteractor()->SetInteractorStyle(style);
style->SetDefaultRenderer(m_QQuickvtkrenderItem->renderer());
style->setQtItem(this);
QQuickVTKInteractiveWidget* interactorWidget = new QQuickVTKInteractiveWidget(m_QQuickvtkrenderItem->renderWindow());
interactorWidget->setWidget(camOrientManipulator);
interactorWidget->setEnabled(true);
m_QQuickvtkrenderItem->addWidget(interactorWidget);
camOrientManipulator->SetParentRenderer(m_QQuickvtkrenderItem->renderer());
camOrientManipulator->SetInteractor(m_QQuickvtkrenderItem->renderWindow()->renderWindow()->GetInteractor());
m_QQuickvtkrenderItem->renderWindow()->interactorAdapter()->SetEnableHover(true);
m_QQuickvtkrenderItem->update();
camOrientManipulator->EnabledOn();
camOrientManipulator->On();
double m_camPositionX = -237.885;
double m_camPositionY = -392.348;
double m_camPositionZ = 369.477;
m_QQuickvtkrenderItem->renderer()->GetActiveCamera()->SetPosition(m_camPositionX, m_camPositionY, m_camPositionZ);
m_QQuickvtkrenderItem->renderer()->GetActiveCamera()->SetViewUp(0.0, 0.0, 1.0);
m_QQuickvtkrenderItem->renderer()->GetActiveCamera()->SetFocalPoint(0.0, 0.0, 0.0);
}
void qtVtkItem::openModel(const QUrl &path)
{
QUrl localFilePath;
if (path.isLocalFile())
{
// Remove the "file:///" if present
localFilePath = path.toLocalFile();
}
else
{
localFilePath = path;
}
qDebug() << "QVTKFramebufferObjectItem::addModelFromFile"<<localFilePath;
CommandModelAdd *command = new CommandModelAdd(m_otherRenderer, m_processingEngine, localFilePath);
// connect(command, &CommandModelAdd::ready, m_otherRenderer, &vtkRenderer::Render);
//// connect(command, &CommandModelAdd::done, this, &QVTKFramebufferObjectItem::addModelFromFileDone);
command->start();
addCommand(command);
}
void qtVtkItem::setModelsRepresentation(const int value)
{
m_QQuickvtkrenderItem->pushCommandToQueue([=](){
m_processingEngine->setModelsRepresentation(value);
qquickWindow->resetOpenGLState();
});
}
void qtVtkItem::setModelsOpacity(const double value)
{
m_QQuickvtkrenderItem->pushCommandToQueue([=](){
m_processingEngine->setModelsOpacity(value);
qquickWindow->resetOpenGLState();
});
}
void qtVtkItem::setGouraudInterpolation(const bool value)
{
m_QQuickvtkrenderItem->pushCommandToQueue([=](){
m_processingEngine->setModelsGouraudInterpolation(value);
qquickWindow->resetOpenGLState();
});
}
void qtVtkItem::delSelectedModel()
{
// if(isModelSelected){
// m_otherRenderer->RemoveActor();
// isModelSelected = false ;
// }
}
void qtVtkItem::setSelectedModel(vtkActor* actor, bool flag)
{
m_selectedModel = m_processingEngine->getModelFromActor(actor);
if(m_selectedModel)
m_selectedModel->setSelected(flag);
this->setIsModelSelected(flag);
}
void qtVtkItem::addCommand(CommandModel *command)
{
m_commandsQueueMutex.lock();
m_commandsQueue.push(command);
m_commandsQueueMutex.unlock();
// update();
}
bool qtVtkItem::getIsModelSelected() const
{
// if( m_otherRenderer )
// return vtkInteractorStyleTrackballCamera2::SafeDownCast( m_otherRenderer->GetRenderWindow()->GetInteractor()->GetInteractorStyle() )
// ->getIsModelSelected();
return isModelSelected;
}
void qtVtkItem::setIsModelSelected(bool flag)
{
isModelSelected = flag;
emit isModelSelectedChanged();
}
CommandModel *qtVtkItem::getCommandsQueueFront() const
{
return m_commandsQueue.front();
}
void qtVtkItem::commandsQueuePop()
{
m_commandsQueue.pop();
}
bool qtVtkItem::isCommandsQueueEmpty() const
{
return m_commandsQueue.empty();
}
void qtVtkItem::lockCommandsQueueMutex()
{
m_commandsQueueMutex.lock();
}
void qtVtkItem::unlockCommandsQueueMutex()
{
m_commandsQueueMutex.unlock();
}
void qtVtkItem::modelReadyToImport(CommandModel* command)
{
command->execute();
}