-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtksurface.cxx
672 lines (619 loc) · 25.2 KB
/
vtksurface.cxx
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#include <QColorDialog>
#include <QInputDialog>
#include <vtkSTLWriter.h>
#include <vtkSTLReader.h>
#include <vtkCleanPolyData.h>
#include <vtkAppendPolyData.h>
#include <vtkActor.h>
#include <vtkMarchingContourFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkContourFilter.h>
#include <vtkPolyDataConnectivityFilter.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkCellLinks.h>
#include <vtkUnsignedCharArray.h>
#include <vtkFillHolesFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkVectorText.h>
#include <vtkPropPicker.h>
#include "vtksurface.h"
#include "window.h"
#include "vtkwindow.h"
vector<Annotation> Annotations;
// Define interaction style
class MouseInteractorStylePP : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorStylePP* New();
vtkTypeMacro(MouseInteractorStylePP, vtkInteractorStyleTrackballCamera);
int yTextPos = 700;
bool pick = false;
bool annotate = false;
VTKSurface* surface;
virtual void OnLeftButtonDown()
{
if(pick) {
std::cout << "Picking pixel: " << this->Interactor->GetEventPosition()[0] << " " << this->Interactor->GetEventPosition()[1] << std::endl;
// Pick from this location.
vtkSmartPointer<vtkPropPicker> picker = vtkSmartPointer<vtkPropPicker>::New();
picker->Pick(this->Interactor->GetEventPosition()[0],
this->Interactor->GetEventPosition()[1],
0, // always zero.
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
double picked[3];
picker->GetPickPosition(picked);
std::cout << "Picked value: " << picked[0] << " " << picked[1] << " " << picked[2] << std::endl;
vtkSmartPointer<vtkActor> actor = picker->GetActor();
if(annotate && actor) {
vector<vtkSmartPointer<vtkActor> > act = surface->getActors();
for(int i=0; i<act.size(); i++) {
if(actor==act[i]) {
Annotation a;
a.x = picked[0] ;
a.y = picked[1] ;
a.z = picked[2] ;
//QColor colour = QColorDialog::getColor();
a.r = 1.0;
a.g = 1.0;
a.b = 1.0;
vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetCenter(a.x, a.y , a.z);
sphereSource->SetRadius(5.0);
vtkSmartPointer<vtkPolyDataMapper> mapper4 = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> actor4 = vtkSmartPointer<vtkActor>::New();
mapper4->SetInputConnection(sphereSource->GetOutputPort());
actor4->SetMapper(mapper4);
actor4->GetProperty()->SetColor(a.r, a.g, a.b);
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor4);
QColor colour = QColorDialog::getColor();
a.r = colour.red()/255.0;
a.g = colour.green()/255.0;
a.b = colour.blue()/255.0;
actor4->GetProperty()->SetColor(a.r, a.g, a.b);
// Setup the text and add it to the renderer
QString str = QInputDialog::getText(0, "Annotation Text", "Enter Text:");
a.text = str.toStdString();
vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput (a.text.c_str());
textActor->SetPosition ( 10, yTextPos );
textActor->GetTextProperty()->SetFontSize ( 16 );
textActor->GetTextProperty()->SetColor (a.r, a.g, a.b);
a.actor = actor4;
a.surfaceActor = act[i];
a.textActor = textActor;
a.index = i;
Annotations.push_back(a);
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor2D (textActor);
yTextPos = yTextPos - 20;
}
}
annotate=false;
}
else {
vtkSmartPointer<vtkActor> actor = picker->GetActor();
if(actor) {
vector<vtkSmartPointer<vtkActor> > a = surface->getActors();
for(int i=0; i<a.size(); i++) {
if(actor==a[i]) {
if(actor->GetProperty()->GetColor()[0]==0.0 && actor->GetProperty()->GetColor()[1]==0.0 && actor->GetProperty()->GetColor()[2]==1.0) {
actor->GetProperty()->SetColor(1.0, 1.0, 1.0);
cout << "Unpicking" << endl;
}
else {
actor->GetProperty()->SetColor(0.0, 0.0, 1.0);
cout << "Picking" << endl;
}
}
}
}
}
this->Interactor->GetRenderWindow()->Render();
// Forward events
//vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
else {
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
}
void setPick(bool p) {
pick = p;
}
void setAnnotate(bool a) {
annotate = a;
}
void setVTKSurface(VTKSurface* s) {
surface = s;
}
};
vtkStandardNewMacro(MouseInteractorStylePP);
VTKSurface::VTKSurface(double isoValue_start, double isoValue_end, float r, float g, float b) : Surface(isoValue_start, isoValue_end)
{
modifiedData = vtkSmartPointer<vtkImageData>::New();
//could use vtk contour filter to get iso-surfaces from ranges
//surface = vtkSmartPointer<vtkMarchingCubes>::New();
surface = vtkSmartPointer<vtkContourFilter>::New();
//confilter = vtkSmartPointer<vtkPolyDataConnectivityFilter>::New();
surface->SetNumberOfContours(1);
red = r; blue = b; green =g;
polyArray = vtkSmartPointer<vtkCellArray>::New();
surfacePolygons = vtkSmartPointer<vtkPolyData>::New();
wholeSurfacePolygons = vtkSmartPointer<vtkPolyData>::New();
cellLinksFilter = vtkSmartPointer<vtkCellLinks>::New();
renderer = vtkSmartPointer<vtkRenderer>::New();
count = 0;
current_surface = 0;
whole = true;
threshold = 5000;
data2 = vtkSmartPointer<vtkPolyData>::New();
celldata2 = vtkSmartPointer<vtkCellArray>::New();
small=0;
big=0;
actor = vtkSmartPointer<vtkActor>::New();
actor2 = vtkSmartPointer<vtkActor>::New();
mapper2 = vtkSmartPointer<vtkPolyDataMapper>::New();
pick = false;
style = vtkSmartPointer<MouseInteractorStylePP>::New();
}
vector<vtkSmartPointer<vtkActor> > VTKSurface::getActors() {
return allActors;
}
void VTKSurface::setLayers(Layer* layers, int n) {
this->layers = layers;
numberOfLayers = n;
}
void VTKSurface::setImageData(ImageData* data) {
imageData = (VTKImageData*) data;
imageData->getImageData()->GetExtent(extent);
imageData->getImageData()->GetBounds(bounds);
}
// creating the modified 0-1 data and generating the surface
void VTKSurface::createSurface()
{
modifiedData->DeepCopy(imageData->getImageData());
for(int x=extent[0];x<=extent[1];x++) {
for(int y=extent[2];y<=extent[3];y++) {
for(int z=extent[4];z<=extent[5];z++) {
//double* pixel = static_cast<double*>(data->GetOutput()->GetScalarComponentAsFloat(x,y,z,1));
//float* pixel1 = static_cast<float*>(modifiedData->GetScalarPointer(x,y,z));
float pixel2 = (imageData->getImageData()->GetScalarComponentAsFloat(x,y,z,0));
//cout << "x: " << extent[0] << "x: " << extent[1] << " y: " << extent[2] << " y: " << extent[3] << " z: " << extent[4] << " z: " << extent[5] << endl;
/*if(pixel2>=isovalue_start && pixel2<=isovalue_end)
modifiedData->SetScalarComponentFromFloat(x,y,z,0,1.0);
else
modifiedData->SetScalarComponentFromFloat(x,y,z,0,0.0);*/
modifiedData->SetScalarComponentFromFloat(x,y,z,0,0.0);
if(x>extent[0] && x<extent[1] && y>extent[2] && y<extent[3] && z>extent[4] && z<extent[5]) {
for(int i=0; i<numberOfLayers; i++) {
if(layers[i].on && pixel2>=layers[i].isovalueStart && pixel2<=layers[i].isovalueEnd) {
modifiedData->SetScalarComponentFromFloat(x,y,z,0,1.0);
break;
}
}
}
}
}
}
surface->ComputeNormalsOn();
surface->GenerateTrianglesOn();
surface->SetInputData(modifiedData);
surface->SetValue(0, 0.5);
}
long VTKSurface::findConnectedVertsRecur(vtkIdType ID)
{
vector <vtkIdType> all_connected_cells;
all_connected_cells.push_back(ID);
int i=0;
while(i<all_connected_cells.size()) {
//cout << all_connected_cells[i] << endl;
int num_of_cells = cellLinksFilter->GetNcells(all_connected_cells[i]);
vtkIdType *connectedCells;
vtkIdType npts; vtkIdType *pts;
//find cell links and call recursion on them
// getting all cells (triangles) of that point all_connected_cells[i]
connectedCells = cellLinksFilter->GetCells(all_connected_cells[i]);
//surface->GetOutput()->GetPointCells(all_connected_cells[i], num_of_cells, connectedCells );
for(int j=0;j<num_of_cells;j++)
{
surface->GetOutput()->GetCellPoints(connectedCells[j], npts, pts );
//surface->GetOutput()->GetCellPoints(connectedCells[j], npts, pts );
if(!Array2[connectedCells[j]]) {
subsurfaces.push_back(pts);
Array2[connectedCells[j]]=true;
}
if(!Array[pts[0]]) {
Array[pts[0]]=true;
all_connected_cells.push_back(pts[0]);
}
if(!Array[pts[1]]) {
Array[pts[1]]=true;
all_connected_cells.push_back(pts[1]);
}
if(!Array[pts[2]]) {
Array[pts[2]]=true;
all_connected_cells.push_back(pts[2]);
}
}
i++;
}
return all_connected_cells.size();
}
void VTKSurface::makeConnectedSurfaces()
{
//map-> Array
cellLinksFilter->Allocate(surface->GetOutput()->GetNumberOfPoints());
cellLinksFilter->BuildLinks(surface->GetOutput(), polyArray);
for(int i=0;i < surface->GetOutput()->GetNumberOfPoints();i++)
{
if(Array[i] == false )
{
// Point i visited
Array[i] = true;
//call recursion on every pointID in connectedCells
subsurfaces.clear();
long c = findConnectedVertsRecur(i);
createConnectedSurfaces();
}
}
sort(connectedComponents.begin(), connectedComponents.end(), compareFunc());
// checking number of open edges to check if the surface is closed
vtkIdType npts; vtkIdType *pts;
for(int i=0; i<connectedComponents.size(); i++) {
connectedComponents[i]->InitTraversal();
map<QString, int> edgeCount;
while(connectedComponents[i]->GetNextCell(npts, pts)) {
QString edge1, edge2, edge3;
if(pts[0] < pts[1] ) {
QString num1, num2;
num1.setNum(pts[0] );
num2.setNum(pts[1] );
edge1 = num1 + num2;
}
else {
QString num1, num2;
num1.setNum(pts[1] );
num2.setNum(pts[0] );
edge1 = num1 + num2;
}
edgeCount[edge1] +=1 ;
if(pts[1] < pts[2] ) {
QString num1, num2;
num1.setNum(pts[1] );
num2.setNum(pts[2] );
edge2 = num1 + num2;
}
else {
QString num1, num2;
num1.setNum(pts[2] );
num2.setNum(pts[1] );
edge2 = num1 + num2;
}
edgeCount[edge2] +=1;
if(pts[0] < pts[2] )
{
QString num1, num2;
num1.setNum(pts[0] );
num2.setNum(pts[2] );
edge3 = num1 + num2;
}
else
{
QString num1, num2;
num1.setNum(pts[2] );
num2.setNum(pts[0] );
edge3 = num1 + num2;
}
edgeCount[edge3] +=1;
}
int numOfOpenEdges = 0;
typedef std::map<QString, int>::iterator it;
for(it iterator = edgeCount.begin(); iterator != edgeCount.end(); iterator++ ) {
// iterator->first = key
// iterator->second = value
if (iterator->second < 2)
numOfOpenEdges +=1;
}
numberOfOpenEdges.push_back(numOfOpenEdges);
}
}
void VTKSurface::showCompleteSurface() {
whole = true;
surfacePolygons->SetPolys(polyArray);
surface->Update();
actor2->GetProperty()->SetOpacity(1.0);
for(int i=0; i<actor1.size(); i++) {
actor1[i]->GetProperty()->SetOpacity(1.0);
actor1[i]->GetProperty()->SetColor(red, blue, green);
}
actor2->GetProperty()->SetColor(1.0, 0.0, 0.0);
actor->VisibilityOff();
renderer->GetRenderWindow()->Render();
}
void VTKSurface::showComponentSurface() {
whole = false;
if(current_surface>=connectedComponents.size())
current_surface=connectedComponents.size()-1;
surfacePolygons->SetPolys(connectedComponents[current_surface]);
surface->Update();
if(surface->GetOutput()->GetPolys()->GetNumberOfCells()>threshold)
actor->GetProperty()->SetColor(red, blue, green);
else
actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
actor2->GetProperty()->SetOpacity(0.1);
for(int i=0; i<actor1.size(); i++) {
actor1[i]->GetProperty()->SetOpacity(0.1);
actor1[i]->GetProperty()->SetColor(red, blue, green);
}
actor2->GetProperty()->SetColor(1.0, 0.0, 0.0);
actor->VisibilityOn();
if(!visible[current_surface])
actor->VisibilityOff();
renderer->GetRenderWindow()->Render();
}
void VTKSurface::showNextSurface() {
current_surface += 1;
if(current_surface>=connectedComponents.size())
current_surface = 0;
cout << "surfaceId, numOfOpenEdges "<< current_surface << " , " <<numberOfOpenEdges[current_surface ] << endl;
}
void VTKSurface::showPreviousSurface() {
current_surface -= 1;
if(current_surface<0)
current_surface = connectedComponents.size()-1;
cout << "surfaceId, numOfOpenEdges "<< current_surface << " , " <<numberOfOpenEdges[current_surface ] << endl;
}
int VTKSurface::getCurrentSurface() {
return current_surface+1;
}
// Get minimum number of triangles in all connected components
long VTKSurface::getMinimum() {
long mini = 10000000;
for(int i=0; i<connectedComponents.size(); i++) {
if(connectedComponents[i]->GetNumberOfCells()<mini)
mini=connectedComponents[i]->GetNumberOfCells();
}
return mini;
}
// Get maximum number of triangles in all connected components
long VTKSurface::getMaximum() {
long maxi = 0;
for(int i=0; i<connectedComponents.size(); i++) {
if(connectedComponents[i]->GetNumberOfCells()>maxi)
maxi=connectedComponents[i]->GetNumberOfCells();
}
return maxi;
}
// updates all the actors according to the threshold
void VTKSurface::updateThreshold(int val) {
threshold = val;
vtkIdType npts; vtkIdType *pts;
big=0;
small=0;
cout << "here1 " << val << " " << threshold << endl;
for(int i=0; i<connectedComponents.size(); i++) {
connectedComponents[i]->InitTraversal();
if(connectedComponents[i]->GetNumberOfCells()>threshold)
big+=connectedComponents[i]->GetNumberOfCells();
else
small+=connectedComponents[i]->GetNumberOfCells();
}
cout << "small :" << small << endl;
for(int i=0; i<actor1.size(); i++)
renderer->RemoveActor(actor1[i]);
celldata2->Reset();
celldata2 = vtkSmartPointer<vtkCellArray>::New();
celldata2->Allocate(celldata2->EstimateSize(small+1, 3));
actor1.clear();
cout << connectedComponents.size() << endl;
//vtkIdType npts; vtkIdType *pts;
for(int i=0; i<connectedComponents.size(); i++) {
if(connectedComponents[i]->GetNumberOfCells()>threshold) {
if(visible[i]) {
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->ShallowCopy(wholeSurfacePolygons);
polydata->SetPolys(connectedComponents[i]);
vtkSmartPointer<vtkPolyDataMapper> map = vtkSmartPointer<vtkPolyDataMapper>::New();
map->SetInputData(polydata);
map->ScalarVisibilityOff();
allActors[i]->SetMapper(map);
allActors[i]->GetProperty()->SetColor(red, blue, green);
allActors[i]->GetProperty()->SetOpacity(1.0);
actor1.push_back(allActors[i]);
renderer->AddActor(allActors[i]);
}
}
else {
while(connectedComponents[i]->GetNextCell(npts, pts))
if(visible[i]) {
celldata2->InsertNextCell(npts, pts);
}
}
}
vtkIdType temppts[] = {0,0,0};
celldata2->InsertNextCell(3, temppts);
data2->SetPolys(celldata2);
renderer->GetRenderWindow()->Render();
}
// removing all surfaces which are below threshold
void VTKSurface::removeSurfaces() {
int i=0;
while(i<connectedComponents.size() && connectedComponents[i]->GetNumberOfCells()>threshold)
i++;
//connectedComponents.erase (connectedComponents.begin()+i,connectedComponents.end());
//numberOfOpenEdges.erase (numberOfOpenEdges.begin()+i,numberOfOpenEdges.end());
for(; i<visible.size(); i++)
visible[i]=false;
updateThreshold(threshold);
}
// removing a particular connected component surface
void VTKSurface::removeSurface() {
//connectedComponents.erase (connectedComponents.begin()+current_surface);
//numberOfOpenEdges.erase (numberOfOpenEdges.begin()+current_surface);
visible[current_surface]=false;
updateThreshold(threshold);
for(int i=Annotations.size()-1; i>=0; i--) {
if(Annotations[i].index == current_surface) {
renderer->RemoveActor(Annotations[i].actor);
renderer->RemoveActor2D(Annotations[i].textActor);
Annotations.erase(Annotations.begin()+i);
}
}
if(!whole)
showComponentSurface();
}
// getting the number of triangles for the current surface
long VTKSurface::getNumberOfTriangles(bool status) {
if(status)
return polyArray->GetNumberOfCells();
else
return connectedComponents[current_surface]->GetNumberOfCells();
}
// create the connected surface from the temporary subsurfaces vector and appending it to connectedComponents
void VTKSurface::createConnectedSurfaces()
{
vtkSmartPointer<vtkCellArray> temp = vtkSmartPointer<vtkCellArray>::New();
vtkIdType s = temp->EstimateSize(subsurfaces.size(), 3);
temp->Allocate(s);
for(int i=0;i < subsurfaces.size();i++)
{
temp->InsertNextCell(3, subsurfaces[i]);
}
connectedComponents.push_back(temp);
if(temp->GetNumberOfCells()>threshold)
big += temp->GetNumberOfCells();
else
small += temp->GetNumberOfCells();
}
// Pick mode vs edit mode
void VTKSurface::changeMode(int mode) {
if(mode==0)
pick=false;
else
pick=true;
style->setPick(pick);
}
// shift to annotation mode
void VTKSurface::addAnnotation() {
style->setAnnotate(true);
}
void VTKSurface::render(Window *window)
{
((VTKWindow*)window)->getWidget()->GetRenderWindow()->AddRenderer(renderer);
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(surface->GetOutputPort());
mapper->ScalarVisibilityOff();
actor->SetMapper(mapper);
renderer->AddActor(actor);
renderer->SetBackground(.0, .0, .0);
renderer->ResetCamera();
polyArray->DeepCopy(surface->GetOutput()->GetPolys());
polyArray->InitTraversal();
surface->GetOutput()->BuildCells();
surface->GetOutput()->BuildLinks();
surfacePolygons = surface->GetOutput();
wholeSurfacePolygons->DeepCopy(surface->GetOutput());
vtkIdType npts; vtkIdType *pts;
int count_not3 =0;
int count_3 =0;
while(polyArray->GetNextCell(npts, pts))
{
if(npts != 3) {
count_not3 +=1;
}
else if (npts == 3) {
count_3 +=1;
}
}
cout << "count of npts not 3 : " << count_not3 << endl;
cout << "count of npts 3 : " << count_3 << endl;
//confilter->GetOutput()->GenerateTrianglesOn();
cout << " Num of triangles : "<< surface->GetOutput()->GetNumberOfStrips() << endl;
cout << " Num of polys : "<< surface->GetOutput()->GetNumberOfPolys() << endl;
//cout << " Num of strips : "<< confilter->GetOutput()->GetNumberOfStrips() << endl;
cout << " Num of points : "<< surface->GetOutput()->GetNumberOfPoints() << endl;
cout << "Annotations : " << Annotations.size()<< endl;
makeConnectedSurfaces();
//surface->update();
actor->VisibilityOff();
celldata2->Allocate(celldata2->EstimateSize(small, 3));
cout << connectedComponents.size() << endl;
//vtkIdType npts; vtkIdType *pts;
for(int i=0; i<connectedComponents.size(); i++) {
allActors.push_back(vtkSmartPointer<vtkActor>::New());
visible.push_back(true);
connectedComponents[i]->InitTraversal();
// add all major component actors to actor1 vector
if(connectedComponents[i]->GetNumberOfCells()>threshold) {
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->ShallowCopy(wholeSurfacePolygons);
polydata->SetPolys(connectedComponents[i]);
vtkSmartPointer<vtkPolyDataMapper> map = vtkSmartPointer<vtkPolyDataMapper>::New();
map->SetInputData(polydata);
map->ScalarVisibilityOff();
allActors[i]->SetMapper(map);
allActors[i]->GetProperty()->SetColor(red, blue, green);
allActors[i]->GetProperty()->SetOpacity(1.0);
actor1.push_back(allActors[i]);
renderer->AddActor(allActors[i]);
}
// concatenate all minor component to celldata2
else {
while(connectedComponents[i]->GetNextCell(npts, pts))
celldata2->InsertNextCell(npts, pts);
}
}
data2->DeepCopy(wholeSurfacePolygons);
data2->SetPolys(celldata2);
mapper2->SetInputData(data2);
mapper2->ScalarVisibilityOff();
actor2->SetMapper(mapper2);
actor2->GetProperty()->SetColor(1.0, 0.0, 0.0);
actor2->GetProperty()->SetOpacity(1.0);
renderer->AddActor(actor2);
//vtkSmartPointer<vtkPointPicker> pointPicker = vtkSmartPointer<vtkPointPicker>::New();
// interactor to pick points
QVTKInteractor* iren = QVTKInteractor::New();
style->SetDefaultRenderer(renderer);
renderer->GetRenderWindow()->SetInteractor(iren);
iren->SetRenderWindow(renderer->GetRenderWindow());
iren->Initialize();
iren->SetInteractorStyle(style);
style->setVTKSurface(this);
//iren->SetPicker(pointPicker);
}
void VTKSurface::setSpacing(vector<double> spacing)
{
spacing_x = spacing[0]; spacing_y = spacing[1]; spacing_z = spacing[2];
}
// to write out all the picked components to an stl file
void VTKSurface::print() {
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
for(int i=0; i<actor1.size(); i++) {
if(actor1[i]->GetProperty()->GetColor()[0]==0.0 && actor1[i]->GetProperty()->GetColor()[1]==0.0 && actor1[i]->GetProperty()->GetColor()[2]==1.0) {
vtkPolyDataMapper* mapper = (vtkPolyDataMapper*) actor1[i]->GetMapper();
vtkPolyData* data = mapper->GetInput();
cout << "Printing Actor " << i << endl;
appendFilter->AddInputData(data);
}
}
appendFilter->Update();
// Remove any duplicate points.
vtkSmartPointer<vtkCleanPolyData> cleanFilter = vtkSmartPointer<vtkCleanPolyData>::New();
cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
cleanFilter->Update();
// write data to stl format
QString str = QInputDialog::getText(0, "Enter filename", "Example : filename.stl:");
std::string filename = str.toStdString();
vtkSmartPointer<vtkSTLWriter> stlWriter = vtkSmartPointer<vtkSTLWriter>::New();
stlWriter->SetFileName(filename.c_str());
stlWriter->SetInputConnection(cleanFilter->GetOutputPort());
stlWriter->Write();
}