-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkWidget.cpp
281 lines (221 loc) · 7.53 KB
/
vtkWidget.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
#include <omp.h>
#include <QVTKInteractor.h>
#include "vtkWidget.h"
VtkWidget* VtkWidget::vwidget = NULL;
void VtkWidget::CallbackFunction (vtkObject* caller,
long unsigned int vtkNotUsed(eventId),
void* vtkNotUsed(clientData),
void* vtkNotUsed(callData))
{
vtkImageTracerWidget* tracerWidget = static_cast<vtkImageTracerWidget*>(caller);
vtkSmartPointer<vtkPolyData> path = vtkSmartPointer<vtkPolyData>::New();
tracerWidget->GetPath(path);
emit vwidget->selectedSurface(path);
}
VtkWidget::VtkWidget(QWidget * parent)
: QWidget()
{
this->setParent(parent);
initializeMainWindow();
vwidget = this;
}
// Multi view constructor
VtkWidget::VtkWidget(VtkView *mvcont)
: QWidget()
{
this->setParent(mvcont);
id = mvcont->getNextViewerId();
if (id > 0)
{
copyExistingPointers(mvcont);
}
else
{
initializeMainWindow();
}
vwidget = this;
}
// NB: vtkSmartPointer and ITK Pointer will be deleted automatically when out-of-scope.
VtkWidget::~VtkWidget(){
// Widgets
mQVTKWidget = NULL;
mLayout = NULL;
}
void VtkWidget::initializeMainWindow()
{
mVtkImageData = NULL; // RGB for 2D images (including stack)
mVtkPolyData = NULL; // for 3D surface polygon data
mQVTKWidget = NULL;
mLayout = NULL;
setAttribute(Qt::WA_DeleteOnClose);
int width = this->frameGeometry().width();
int height = this->frameGeometry().height();
this->setMinimumHeight(height/2);
this->setMinimumWidth(width/2);
}
void VtkWidget::copyExistingPointers(VtkView *vcont)
{
mVtkPolyData = vcont->getViewer(0)->mVtkPolyData;
rendering3D();
}
OBJECTTYPE VtkWidget::render(QString filename)
{
QFileInfo fi(filename);
QString extension = fi.suffix().toLower();
std::string fnstr = filename.toLocal8Bit().constData();
this->setObjectName(filename);
if (extension==tr("obj"))
{
if(read3D(filename)) return MODEL3D;
else return EMPTY;
}
else if (extension==tr("jpg") || extension==tr("jpeg"))
{
if(read2D(filename)) return IMAGE2D;
else return EMPTY;
}
}
bool VtkWidget::read3D(QString filename)
{
vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName(filename.toStdString().c_str());
reader->Update();
mVtkPolyData = reader->GetOutput();
if (mVtkPolyData == NULL)
return false;
rendering3D();
return true;
}
void VtkWidget::rendering3D()
{
if (mQVTKWidget != NULL)
delete mQVTKWidget;
mQVTKWidget = new QVTKWidget(this);
if (mLayout!=NULL)
delete mLayout;
mLayout = new QVBoxLayout( this ); // size stretching trick
mLayout->setMargin( 0 );
mLayout->addWidget( mQVTKWidget, 1 );
this->setLayout(mLayout);
mMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mMapper->SetInputConnection(mVtkPolyData->GetProducerPort());
mMapper->ScalarVisibilityOff();
mActor = vtkSmartPointer<vtkActor>::New();
mActor->SetMapper(mMapper);
mActor->GetProperty()->SetInterpolationToFlat();
vtkSmartPointer<vtkProperty> property = vtkSmartPointer<vtkProperty>::New();
property = mActor->GetProperty();
double ambient[3] = {0.2, 0.2, 0.2};
property->SetAmbientColor(ambient); // for surface
double diffuse[3] = {1., 1., 1.};
property->SetDiffuseColor(diffuse); // for surface
double specular[3] = {1., 1., 1.};
property->SetSpecularColor(specular); // for surface
mActor->SetTexture(NULL);
mRenderer = vtkSmartPointer<vtkRenderer>::New();
mActor->PickableOn();
mRenderer->AddActor(mActor);// Add actor to renderer
mQVTKWidget->GetRenderWindow()->AddRenderer(mRenderer);
mRenderer->ResetCamera();
mRenderer->ResetCameraClippingRange();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); // 3D image
//vtkSmartPointer<vtkInteractorStyleTrackballActor> style = vtkSmartPointer<vtkInteractorStyleTrackballActor>::New(); // 3D image
mInteractor = vtkSmartPointer<QVTKInteractor>::New();
mInteractor->SetRenderWindow(mQVTKWidget->GetRenderWindow());
mInteractor->GetInteractorStyle()->SetDefaultRenderer(mRenderer);
mInteractor->SetInteractorStyle(style);
mInteractor->Initialize();
mQVTKWidget->show();
mQVTKWidget->update();
}
bool VtkWidget::read2D(QString filename)
{
mRenderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName(filename.toStdString().c_str());
reader->Update();
mVtkImageData = reader->GetOutput();
if (mVtkImageData==NULL)
return false;
rendering2D();
return true;
}
void VtkWidget::rendering2D()
{
if (mQVTKWidget != NULL)
delete mQVTKWidget;
mQVTKWidget = new QVTKWidget(this);
if (mLayout!=NULL)
delete mLayout;
mLayout = new QVBoxLayout( this ); // size stretching trick
mLayout->setMargin( 0 );
mLayout->addWidget( mQVTKWidget, 1 );
this->setLayout(mLayout);
mActor2D = vtkSmartPointer<vtkImageActor>::New();
mActor2D->GetMapper()->SetInput(mVtkImageData);
mRenderer = vtkSmartPointer<vtkRenderer>::New();
mRenderer->AddActor(mActor2D);// Add actor to renderer
mQVTKWidget->GetRenderWindow()->AddRenderer(mRenderer);
mRenderer->SetLayer(0);
vtkCamera* camera = mRenderer->GetActiveCamera();
// reset camer to maximize the screenshot in window
double origin[3];
double spacing[3];
int extent[6];
mVtkImageData->GetOrigin( origin );
mVtkImageData->GetSpacing( spacing );
mVtkImageData->GetExtent( extent );
camera->ParallelProjectionOn();
double xc = origin[0] + 0.5*(extent[0] + extent[1])*spacing[0];
double yc = origin[1] + 0.5*(extent[2] + extent[3])*spacing[1];
//double xd = (extent[1] - extent[0] + 1)*spacing[0];
double yd = (extent[3] - extent[2] + 1)*spacing[1];
double d = camera->GetDistance();
camera->SetParallelScale(0.5*yd);
camera->SetFocalPoint(xc,yc,0.0);
camera->SetPosition(xc,yc,d);
vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();
mInteractor = vtkSmartPointer<QVTKInteractor>::New();
mInteractor->SetRenderWindow(mQVTKWidget->GetRenderWindow());
mInteractor->GetInteractorStyle()->SetDefaultRenderer(mRenderer);
mInteractor->SetInteractorStyle(style);
mInteractor->Initialize();
tracer = vtkSmartPointer<vtkImageTracerWidget>::New();
tracer->GetLineProperty()->SetLineWidth(5);
tracer->SetInteractor(mInteractor);
tracer->SetViewProp(mActor2D);
tracer->SetCaptureRadius(0.001);
vtkSmartPointer<vtkCallbackCommand> callback = vtkSmartPointer<vtkCallbackCommand>::New();
callback->SetCallback(VtkWidget::CallbackFunction);
tracer->AddObserver(vtkCommand::EndInteractionEvent, callback);
tracer->On();
vwidget = this;
mQVTKWidget->show(); // no Render() is required for 3D
mQVTKWidget->update(); //MK: this is important!
}
VtkView * VtkWidget::mvc()
{
QObject * curParent = this->parent();
while(qobject_cast<VtkView *>(curParent) == 0)
{
curParent = curParent->parent();
}
return qobject_cast<VtkView *>(curParent);
}
void VtkWidget::screenshot(QString file)
{
vtkSmartPointer<vtkRenderWindow> renderWindow = mRenderer->GetRenderWindow();
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
windowToImageFilter->Update();
vtkSmartPointer<vtkJPEGWriter> writer = vtkSmartPointer<vtkJPEGWriter>::New();
writer->SetFileName(file);
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
}
void VtkWidget::renderUpdate()
{
mQVTKWidget->show();
mQVTKWidget->update();
}