3 #include "vtkCylinderSource.h"
4 #include "vtkPolyDataMapper.h"
5 #include "vtkRenderer.h"
6 #include "vtkRenderWindow.h"
7 #include "vtkRenderWindowInteractor.h"
8 #include "vtkProperty.h"
10 #include "vtkPoints.h"
11 #include "vtkCellArray.h"
12 #include "vtkPolyData.h"
13 #include "vtkPolyDataMapper.h"
14 #include "vtkPolyDataWriter.h"
15 #include "vtkPolyDataReader.h"
16 #include "vtkImageReader.h"
17 #include "vtkImageViewer.h"
18 #include "vtkImageViewer2.h"
19 #include "vtkImageToStructuredPoints.h"
21 #include "vtkExtractVOI.h"
22 #include "vtkImageClip.h"
23 #include "vtkImageResample.h"
24 #include "vtkImageCast.h"
25 #include "vtkImageThreshold.h"
26 #include "vtkImageCast.h"
27 #include "vtkImageSeedConnectivity.h"
28 #include "vtkImageData.h"
29 #include "vtkMarchingCubes.h"
30 #include "vtkImageReslice.h"
31 #include "vtkTransform.h"
32 #include "vtkSphereSource.h"
33 #include "vtkDoubleArray.h"
34 #include "vtkPointData.h"
35 #include "vtkCommand.h"
36 #include "vtkCallbackCommand.h"
37 #include "vtkImageResample.h"
39 #include "vtkStripper.h"
40 #include "vtkImageWriter.h"
41 #include "vtkBMPWriter.h"
42 #include "vtkImageCast.h"
43 #include "vtkPolyDataWriter.h"
45 #include "wxProcessingCTWidget.h"
47 #include <wx/splitter.h>
48 #include <wx/renderer.h>
49 //-------------------------------------------------------------------
50 //-------------------------------------------------------------------
51 //-------------------------------------------------------------------
55 BEGIN_EVENT_TABLE( wxProcessingCTWidget, wxPanel )
56 EVT_MENU( 12121, wxProcessingCTWidget::OnRefreshView )
59 //-------------------------------------------------------------------
62 wxProcessingCTWidget::wxProcessingCTWidget(wxWindow *parent, marInterfaceCT *mar)
63 : wxPanel( parent, -1)
65 wxSplitterWindow *pnlSplitter = new wxSplitterWindow( this , -1);
66 wxPanel *viewPanel = CreateViewPanel(pnlSplitter);
67 wxPanel *controlPanel = CreateControlPanel(pnlSplitter);
69 pnlSplitter -> SplitVertically( viewPanel, controlPanel ,300);
70 pnlSplitter -> SetMinimumPaneSize( 300 );
72 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL );
73 sizer -> Add( pnlSplitter ,1,wxALL|wxGROW ,0);
74 this -> SetSizer(sizer);
75 this->SetAutoLayout(true);
76 // panel->SetSize(400,400);
86 //-------------------------------------------------------------------
87 wxProcessingCTWidget::~wxProcessingCTWidget(){
90 // _connect->Delete();
96 delete _imageviewer2D_1;
97 delete _imageviewer2D_2;
98 delete _imageviewer2D_3;
100 //-------------------------------------------------------------------
101 wxPanel* wxProcessingCTWidget::CreateViewPanel(wxWindow *parent)
103 wxPanel *panel = new wxPanel(parent,-1);
104 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
106 _imageviewer2D_1 = new wxVtk2DBaseView(panel);
107 _imageviewer2D_2 = new wxVtk2DBaseView(panel);
108 _imageviewer2D_3 = new wxVtk2DBaseView(panel);
109 wxVTKRenderWindowInteractor *iren = _imageviewer2D_1->GetWxVTKRenderWindowInteractor();
110 wxVTKRenderWindowInteractor *iren_2 = _imageviewer2D_2->GetWxVTKRenderWindowInteractor();
111 wxVTKRenderWindowInteractor *iren_3 = _imageviewer2D_3->GetWxVTKRenderWindowInteractor();
112 sizer->Add(iren , 1, wxGROW, 0);
113 sizer->Add(iren_2, 1, wxGROW, 0);
114 sizer->Add(iren_3, 1, wxGROW, 0);
117 panel->SetSizer(sizer);
118 panel->SetAutoLayout(true);
119 // panel->SetSize(400,400);
123 //-------------------------------------------------------------------
124 wxPanel* wxProcessingCTWidget::CreateControlPanel(wxWindow *parent)
127 wxPanel *panel = new wxPanel(parent,-1);
128 _lowthreshold = new wxSlider( panel, -1,65000 , 64000, 65000, wxDefaultPosition, wxSize(25,25), wxSL_HORIZONTAL | wxSL_LABELS | wxSL_AUTOTICKS );
129 _lowthreshold->SetTickFreq(50,0);
131 _midthreshold = new wxSlider( panel, -1, 65000 , 64000, 65000, wxDefaultPosition, wxSize(25,25), wxSL_HORIZONTAL | wxSL_LABELS | wxSL_AUTOTICKS );
132 _midthreshold->SetTickFreq(50,0);
135 _lowthresholdSpin1 = new wxSlider(panel , -1,5,1,10,wxDefaultPosition , wxSize(25,45), wxSL_VERTICAL | wxSL_AUTOTICKS |wxSL_LEFT );
136 _lowthresholdSpin1->SetRange(1,8);
137 _lowthresholdSpin1->SetValue(5);
138 Connect(_lowthresholdSpin1->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxProcessingCTWidget::OnSpinLowThresholdSpin1 );
139 // Connect(_lowthresholdSpin1->GetId() , wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxProcessingCTWidget::OnSpinLowThresholdSpin1 );
141 _lowthresholdSpin2 = new wxSlider(panel , -1,5,1,10,wxDefaultPosition , wxSize(25,45), wxSL_VERTICAL | wxSL_AUTOTICKS |wxSL_LEFT );
142 _lowthresholdSpin2->SetRange(1,8);
143 _lowthresholdSpin2->SetValue(5);
144 Connect(_lowthresholdSpin2->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxProcessingCTWidget::OnSpinLowThresholdSpin2 );
145 // Connect(_lowthresholdSpin2->GetId() , wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxProcessingCTWidget::OnSpinLowThresholdSpin2 );
150 // _highthreshold = new wxSlider( panel, -1, 50, 0, 100 , wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_LABELS );
151 _zslice = new wxSlider( panel, -1, 50, 0, 100 , wxDefaultPosition, wxSize(25,25), wxSL_HORIZONTAL | wxSL_LABELS | wxSL_AUTOTICKS );
152 _zslice->SetTickFreq(10,0);
153 _extract = new wxButton( panel, -1, _T("Axis extraction"));
154 // _midthreshold->SetSize(250,20);
156 //_highthreshold->SetSize(250,20); // Result Volume
158 // wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
159 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL );
161 int flag = wxALL | wxGROW ;
163 sizer->Add(new wxStaticText(panel,-1,_T(" ")) , 1, flag, 0);
164 sizer->Add(new wxStaticText(panel,-1,_T(" ")) , 1, flag, 0);
165 sizer->Add(new wxStaticText(panel,-1,_T("Lower vascular threshold")) , 1, flag, 0);
167 wxBoxSizer *sizerLowThreshold = new wxBoxSizer(wxHORIZONTAL );
168 sizerLowThreshold->Add(_lowthresholdSpin1 );
169 sizerLowThreshold->Add(_lowthreshold , 1, wxALL | wxGROW , 0);
170 sizer->Add(sizerLowThreshold , 1, wxALL | wxGROW , 0);
172 sizer->Add(new wxStaticText(panel,-1,_T(" ")) , 1, flag, 0);
173 sizer->Add(new wxStaticText(panel,-1,_T("Higher vascular threshold")) , 1, flag, 0);
175 wxBoxSizer *sizerMidThreshold = new wxBoxSizer(wxHORIZONTAL );
176 sizerMidThreshold->Add(_lowthresholdSpin2 );
177 sizerMidThreshold->Add(_midthreshold , 1, flag, 0);
178 sizer->Add(sizerMidThreshold , 1, flag, 0);
182 // sizer->Add(new wxStaticText(panel,-1," "));
183 // sizer->Add(new wxStaticText(panel,-1,"Calcification threshold"));
184 // sizer->Add(_highthreshold);
187 sizer->Add(new wxStaticText(panel,-1,_T(" ")) , 1, flag, 0);
188 sizer->Add(new wxStaticText(panel,-1,_T("Z Slice")) , 1, flag, 0);
189 sizer->Add(_zslice , 1, flag, 0);
190 sizer->Add(new wxStaticText(panel,-1,_T(" ")) , 1, flag, 0);
191 sizer->Add(_extract , 1, flag, 0);
193 // panel->SetSize(200,200);
194 panel->SetSizer(sizer);
195 panel->SetAutoLayout(true);
197 // panel->FitInside(parent);
199 Connect(_lowthreshold->GetId() , wxEVT_SCROLL_CHANGED , (wxObjectEventFunction) &wxProcessingCTWidget::OnLowThreshold);
200 Connect(_lowthreshold->GetId() , wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxProcessingCTWidget::OnLowThreshold);
201 Connect(_midthreshold->GetId() , wxEVT_SCROLL_CHANGED , (wxObjectEventFunction) &wxProcessingCTWidget::OnMidThreshold );
202 Connect(_midthreshold->GetId() , wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxProcessingCTWidget::OnMidThreshold );
206 // Connect(_highthreshold->GetId() , wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxProcessingCTWidget::OnHighThreshold );
208 Connect(_zslice->GetId() , wxEVT_SCROLL_CHANGED , (wxObjectEventFunction) &wxProcessingCTWidget::OnZSlice );
209 Connect(_zslice->GetId() , wxEVT_SCROLL_THUMBTRACK , (wxObjectEventFunction) &wxProcessingCTWidget::OnZSlice );
211 Connect(_extract->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxProcessingCTWidget::OnExtract );
216 //------------------------------------------------------------------------
218 void wxProcessingCTWidget::Refresh()
220 int z = (int)(_vtkbasedata_1->GetZ( ));
222 _vtkbasedata_1->SetZ( z );
223 _vtkbasedata_2->SetZ( z );
224 _vtkbasedata_3->SetZ( z );
226 _imageviewer2D_3->Refresh();
227 _imageviewer2D_1->Refresh();
228 _imageviewer2D_2->Refresh();
230 _imageviewer2D_3->RefreshView();
231 _imageviewer2D_1->RefreshView();
232 _imageviewer2D_2->RefreshView();
237 //----------------------------------------------------------------------------
238 void wxProcessingCTWidget::OnSpinLowThresholdSpin1(wxScrollEvent& event)
240 int value = _lowthreshold->GetValue();
241 int delta = (int) (pow( 4 , _lowthresholdSpin1->GetValue() ));
242 int min = value - delta/2;
243 int max = value + delta/2;
249 _lowthreshold->SetRange(min,max);
252 //----------------------------------------------------------------------------
253 void wxProcessingCTWidget::OnSpinLowThresholdSpin2(wxScrollEvent& event)
255 int value = _midthreshold->GetValue();
256 int delta = (int)(pow( 4 , _lowthresholdSpin2->GetValue() ));
257 int min=value - delta/2;
258 int max=value + delta/2;
264 _midthreshold->SetRange(min,max);
268 //----------------------------------------------------------------------------
269 void wxProcessingCTWidget::OnRefreshView(wxCommandEvent & event)
274 //------------------------------------------------------------------------
276 void wxProcessingCTWidget::ConfigureVTK(marImageData *marimagedata, int x, int y, int z)
284 vtkImageData *imagedata=marimagedata->GetImageData();
285 imagedata->UpdateInformation();
286 imagedata->SetUpdateExtent(imagedata->GetWholeExtent());
291 double puntoactualprov[3];
292 puntoactualprov[0] = x;
293 puntoactualprov[1] = y;
294 puntoactualprov[2] = z;
299 imagedata->GetSpacing(espprin);
300 imagedata->GetExtent(extprin);
304 // puntoactualprov[0]=puntoactualprov[0]*espprin[0];
305 // puntoactualprov[1]=puntoactualprov[1]*espprin[1];
306 // puntoactualprov[2]=puntoactualprov[2]*espprin[2];
308 imagedata->GetScalarRange( _range );
312 _lowthreshold->SetRange( (int)(_range[0]), (int)(_range[1]));
313 _lowthreshold->SetValue( (int)(_range[1]/4) );
315 _midthreshold->SetRange( (int)(_range[0]), (int)(_range[1]));
316 _midthreshold->SetValue( (int)(_range[1]/2) );
319 // _highthreshold->SetRange(_range[0], _range[1]);
320 // _highthreshold->SetValue( _range[1] );
322 _zslice->SetRange(extprin[4], extprin[5]);
323 _zslice->SetValue(extprin[5]/2);
326 _thresh = vtkImageThreshold::New();
327 _thresh->SetInput(imagedata);
328 //_thresh->ReleaseDataFlagOff();
329 _thresh->SetInValue(255);
330 _thresh->SetOutputScalarTypeToUnsignedShort();
331 _thresh->SetOutValue(0);
332 _thresh->ThresholdBetween(_lowthreshold->GetValue(), _range[1]);
334 vtkImageCast *cast = vtkImageCast::New();
335 cast->SetInput(_thresh->GetOutput());
336 cast->SetOutputScalarTypeToUnsignedChar();
340 _connect = vtkImageSeedConnectivity::New();
341 _connect->SetInput(cast->GetOutput());
342 _connect->SetInputConnectValue(255);
343 _connect->SetOutputConnectedValue(255);
344 _connect->SetOutputUnconnectedValue(0);
345 _connect->AddSeed( (int)(puntoactualprov[0]), (int)(puntoactualprov[1]), (int)(puntoactualprov[2]));
348 cast3 = vtkImageCast::New();
349 cast3->SetInput(_connect->GetOutput());
350 cast3->SetOutputScalarTypeToUnsignedShort();
353 _thresh2 = vtkImageThreshold::New();
354 _thresh2->SetInput(imagedata);
355 //_thresh2->ReleaseDataFlagOff();
356 _thresh2->SetInValue(255);
357 _thresh2->SetOutputScalarTypeToUnsignedShort();
358 _thresh2->SetOutValue(0);
359 _thresh2->ThresholdBetween(_lowthreshold->GetValue(), _midthreshold->GetValue());
361 vtkImageCast *cast2 = vtkImageCast::New();
362 cast2->SetInput(_thresh2->GetOutput());
363 cast2->SetOutputScalarTypeToUnsignedChar();
367 _connect2 = vtkImageSeedConnectivity::New();
368 _connect2->SetInput(cast2->GetOutput());
369 _connect2->SetInputConnectValue(255);
370 _connect2->SetOutputConnectedValue(255);
371 _connect2->SetOutputUnconnectedValue(0);
372 _connect2->AddSeed( (int)(puntoactualprov[0]), (int)(puntoactualprov[1]), (int)(puntoactualprov[2]) );
375 cast4 = vtkImageCast::New();
376 cast4->SetInput(_connect2->GetOutput());
377 cast4->SetOutputScalarTypeToUnsignedShort();
382 _vtkbasedata_1 = new vtkBaseData();
383 _vtkbasedata_1->SetMarImageData(marimagedata);
386 cast3->GetOutput()->UpdateInformation();
387 cast3->GetOutput()->SetUpdateExtent( cast3->GetOutput()->GetWholeExtent() );
388 cast3->GetOutput()->SetUpdateExtent( cast3->GetOutput()->GetWholeExtent() );
390 cast3->GetOutput()->Update();
391 _vtkbasedata_2 = new vtkBaseData();
392 _vtkbasedata_2->SetMarImageData( new marImageData(cast3->GetOutput()) );
395 cast4->GetOutput()->UpdateInformation();
396 cast4->GetOutput()->SetUpdateExtent(cast4->GetOutput()->GetWholeExtent());
397 cast4->GetOutput()->Update();
398 _vtkbasedata_3 = new vtkBaseData();
399 _vtkbasedata_3->SetMarImageData( new marImageData(cast4->GetOutput() ));
401 _imageviewer2D_1->SetVtkBaseData( _vtkbasedata_1 );
402 _imageviewer2D_1->Configure();
404 _imageviewer2D_2->SetVtkBaseData( _vtkbasedata_2 );
405 _imageviewer2D_2->Configure();
407 _imageviewer2D_3->SetVtkBaseData( _vtkbasedata_3 );
408 _imageviewer2D_3->Configure();
415 //------------------------------------------------------------------------
416 void wxProcessingCTWidget::MidThreshold()
418 _thresh2->ThresholdBetween(_lowthreshold->GetValue(), _midthreshold->GetValue());
423 //------------------------------------------------------------------------
424 void wxProcessingCTWidget::LowThreshold()
427 // _thresh->ThresholdBetween(_lowthreshold->GetValue(), _highthreshold->GetValue());
428 // _thresh->Update();
431 _thresh->ThresholdBetween(_lowthreshold->GetValue(), _range[1] );
438 //------------------------------------------------------------------------
439 void wxProcessingCTWidget::OnLowThreshold(wxScrollEvent& event)
444 //------------------------------------------------------------------------
445 void wxProcessingCTWidget::OnMidThreshold(wxScrollEvent& event){
450 //------------------------------------------------------------------------
452 //void wxProcessingCTWidget::OnHighThreshold(wxScrollEvent& event){
453 // _thresh->ThresholdBetween(_lowthreshold->GetValue(), _highthreshold->GetValue());
457 //------------------------------------------------------------------------
458 void wxProcessingCTWidget::OnZSlice(wxScrollEvent& event){
459 _vtkbasedata_1->SetZ( _zslice->GetValue() );
460 // _vtkbasedata_2->SetZ( _zslice->GetValue() );
461 // _vtkbasedata_3->SetZ( _zslice->GetValue() );
464 //------------------------------------------------------------------------
465 void wxProcessingCTWidget::OnExtract(wxCommandEvent& event){
470 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
471 change->SetInput( _vtkbasedata_3->GetImageData() );
473 _vtkbasedata_3->GetImageData()->GetWholeExtent(ext);
474 change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
477 vtkImageData *image = change->GetOutput();
479 marParameters *marParam = new marParameters();
480 marParam->copyFrom ( *(_mar->getParameters()) );
481 marParam->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
482 marParam->setDoubleParam( marParameters::e_RescaleSlope , 1 );
484 marExperiment *newExperiment = new marExperiment( marParam );
486 kVolume *vol = new kVolume( image );
487 newExperiment->setVOI(ext);
488 newExperiment->initExperiment(vol);
489 newExperiment->setStartPoint( _x, _y, _z);
490 newExperiment->extractVascularTree();
492 //_mar->_experiment->initExperiment(new kVolume(data));
493 _mar->appendAxis(newExperiment->getAxis(0));
494 //_mar->_experiment->setVOI(ext);
496 _mar->prepareQuantification( );
498 wxCommandEvent ev(wxEVT_COMMAND_MENU_SELECTED,20005);
499 _parent->ProcessEvent( ev );
501 int size = _mar->getNumberOfSlices( );
505 msg.Printf(_T("Axis Extracted"));
506 wxMessageDialog(this, msg,_T("Information")).ShowModal();
512 ff=fopen("c:/Temp/DATOS.txt","a+");
513 fprintf(ff,"z:%d - x:%d y:%d\n",size,ext[1]+1,ext[3]+1,ext[5]+1);
516 vtkImageWriter *w = vtkImageWriter::New();
519 for (int i = 0; i < size; i++){
520 std::string path = "C:/TEMP/";
521 w->SetInput(_mar->_experiment->getSliceImage(i));
526 path.append(fil.c_str());
527 w->SetFileName(path.c_str());
534 /* vtkBMPWriter *w = vtkBMPWriter::New();
535 vtkImageCast *c = vtkImageCast::New();
537 for (int i = 0; i < size; i++){
538 std::string path = "C:/TEMP/";
541 c->SetInput(_mar->_experiment->getSliceImage(i));
542 c->SetOutputScalarTypeToUnsignedChar();
545 w->SetInput(c->GetOutput());
550 path.append(fil.c_str());
552 w->SetFileName(path.c_str());
556 //Escritura a pcx con LibIDO
558 for ( int i = 0; i < size; i++){
559 PPIMAGE_USHORT imalibido = (PPIMAGE_USHORT) IdImaAlloc(128,128, IMA_USHORT);
560 for (int j = 0; j < 128; j++){
561 for (int k = 0; k < 128; k++){
562 imalibido[k][j] = (unsigned short)_mar->_experiment->getSliceImage(i)->GetScalarComponentAsDouble(j, k, 0, 0);
565 std::string path = "C:/TEMP/";
569 path.append(fil.c_str());
571 PPIMAGE_UCHAR imagennueva = IdImaRecad16To8(imalibido, -1, -1);
572 IdImaWritePCXFile((char*)path.c_str(), imagennueva);
573 IdImaClear((PPIMAGE)imalibido);
581 //------------------------------------------------------------------------
582 void wxProcessingCTWidget::SetThreshold(int min ,int max)
584 _lowthreshold->SetValue(min);
585 _midthreshold->SetValue(max);
588 //------------------------------------------------------------------------
589 void wxProcessingCTWidget::GetThreshold(int *min ,int *max)
591 *min = _lowthreshold->GetValue();
592 *max = _midthreshold->GetValue();
596 //------------------------------------------------------------------------