]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxSTLWidget_03.cxx
d8568557e8591bf595fbb34bbde01127d7b35a79
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / wxSTLWidget_03.cxx
1
2 #include "vtkObjectFactory.h"
3
4 #include "vtkActor.h"
5 #include "vtkCylinderSource.h"
6 #include "vtkPolyDataMapper.h"
7 #include "vtkRenderer.h"
8 #include "vtkRenderWindow.h"
9 #include "vtkRenderWindowInteractor.h"
10 #include "vtkProperty.h"
11 #include "vtkCamera.h"
12 #include "vtkPoints.h"
13 #include "vtkCellArray.h"
14 #include "vtkPolyData.h"
15 #include "vtkPolyDataMapper.h"
16 #include "vtkPolyDataWriter.h"
17 #include "vtkPolyDataReader.h"
18 #include "vtkImageReader.h"
19 #include "vtkImageViewer.h"
20 #include "vtkImageViewer2.h"
21 #include "vtkImageToStructuredPoints.h"
22 #include <vtkImageThreshold.h> 
23 #include "vtkImageWriter.h"
24
25
26 #include "vtkExtractVOI.h"
27 #include "vtkImageClip.h"
28 #include "vtkImageResample.h"
29 #include "vtkImageThreshold.h"
30 #include "vtkImageCast.h"
31 #include "vtkImageSeedConnectivity.h"
32 #include "vtkImageData.h"
33 #include "vtkMarchingCubes.h"
34 #include "vtkImageReslice.h"
35 #include "vtkTransform.h"
36 #include "vtkSphereSource.h"
37 #include "vtkDoubleArray.h"
38 #include "vtkPointData.h"
39 #include "vtkCommand.h"
40 #include "vtkCallbackCommand.h"
41 #include "vtkImageResample.h"
42 #include "vtkMath.h"
43 #include "vtkStripper.h"
44 #include <vtkSTLReader.h> 
45 #include "vtkMetaImageWriter.h"
46
47
48 #include "vtkTriangleFilter.h"
49 #include "vtkSTLWriter.h"
50 #include "vtkPolyDataConnectivityFilter.h"
51 #include "vtkClosePolyData.h"
52 #include "vtkAppendFilter.h"
53
54 #include "wxSTLWidget_03.h"
55
56 #include <wx/splitter.h>
57
58 #include <wx/filedlg.h>
59
60
61
62 //-------------------------------------------------------------------
63 //-------------------------------------------------------------------
64 //-------------------------------------------------------------------
65 wxSTLWidget_03::wxSTLWidget_03(wxWindow *parent)//, marInterface* mar)
66 : wxPanel( parent, -1) 
67 {
68         _wxMaracasMPR           = NULL;
69         _maxSize                = 2000;
70         _minSize                = 300;
71         _imagedata = NULL;
72         _stlMarchingCubesLevel  = 128;
73         _stlDeltaGaussLevel             = 100;
74
75         /*_mar=new marInterface();      
76         _mar->_parameters->setRescaleSlope( mar->_parameters->getRescaleSlope() );
77         _mar->_parameters->setRescaleIntercept( mar->_parameters->getRescaleIntercept() );
78         _mar->_parameters->setIntParam( marParameters::e_voxel_type, marParameters::VOX_TYPE_MINIMUM );
79         _mar->_parameters->setDoubleParam( marParameters::e_voxel_x_dimension, mar->_parameters->getDoubleParam(marParameters::e_voxel_x_dimension) );
80         _mar->_parameters->setDoubleParam( marParameters::e_voxel_y_dimension, mar->_parameters->getDoubleParam(marParameters::e_voxel_y_dimension) );
81         _mar->_parameters->setDoubleParam( marParameters::e_voxel_z_dimension, mar->_parameters->getDoubleParam(marParameters::e_voxel_z_dimension) );
82         int voi[6];
83         mar->_experiment->getVOI( voi );
84         _mar->_experiment->setVOI( voi );
85         _mar->SetDicom(mar->_dicom);
86         _mar->initExperiment( );*/
87          
88
89         wxBoxSizer              *sizer          = new wxBoxSizer(wxVERTICAL  );
90         wxSplitterWindow        *pnlSplitter    = new wxSplitterWindow( this , -1);
91         wxPanel                 *viewPanel      = CreateViewPanel(pnlSplitter);
92         wxPanel                 *controlPanel   = CreateControlPanel(pnlSplitter);
93
94         sizer           -> Add( pnlSplitter ,1,wxGROW  ,0);
95         pnlSplitter     -> SetMinimumPaneSize( 150 );
96         pnlSplitter     -> SplitVertically( viewPanel, controlPanel, 600 );
97
98 //      pnlSplitter     -> SplitVertically( new wxPanel(pnlSplitter,-1), new wxPanel(pnlSplitter,-1) , 600 );
99 //      pnlSplitter     -> SplitVertically( viewPanel, new wxPanel(pnlSplitter,-1) , 600 );
100
101         this            -> SetSizer(sizer);
102
103         // Result 2: Volume + Axis
104 //      _2_prgov                = NULL;
105         _2_mapfinal             = NULL;
106         _2_stripfinal           = NULL;
107         _2_isoMapperMC6         = NULL;
108         _2_isoActorMC6          = NULL;
109         _2_isoMC6               = NULL;
110
111         
112
113         _loadSTLMapper          = NULL;
114         _loadActorSTL           = NULL;
115
116         
117         //JOIN REGIONS
118         arteryImageData         = NULL;
119         joinMarchingCubes       = NULL;
120     joinMapper                  = NULL; 
121         joinActor               = NULL;
122
123         //DHC STL SURFACES
124         stlExterna = NULL;
125         stlInterna = NULL;
126         dsm1=NULL;
127         actorInternal = NULL;
128         dsm2 = NULL;
129         actorExternal=NULL;
130         _thresh=NULL;
131
132
133 }
134 //-------------------------------------------------------------------
135 wxSTLWidget_03::~wxSTLWidget_03(){
136         ResetTree2_JF();
137         Reset_vtk_STLFile();
138 }
139 //-------------------------------------------------------------------
140 wxPanel* wxSTLWidget_03::CreateViewPanel(wxWindow *parent)
141 {
142         wxPanel *panel          =       new wxPanel(parent,-1);
143     wxBoxSizer *sizer   =       new wxBoxSizer(wxVERTICAL);
144
145
146    // MPR
147         //vtkImageData *imagedata;
148         //imagedata = _mar->_experiment->getDynData( )->getVolume( )->castVtk();
149         //imagedata->Update();  
150         _wxMaracasMPR = new wxMaracasMPR( panel);//JCP 10-03-2009, new marImageData(imagedata), 1 );
151         _wxMaracasMPR->ConfigureVTK();
152         
153         sizer->Add(     _wxMaracasMPR , 1, wxEXPAND, 0);
154         panel->SetSizer(sizer);
155         panel->SetAutoLayout(true);
156         panel->SetSize(400,400);
157         panel->Layout();
158         return panel;
159 }
160
161 void wxSTLWidget_03::setImage(vtkImageData* img){
162
163         _wxMaracasMPR->setImageData(img, 1);
164         _wxMaracasMPR->ConfigureVTK();
165
166
167         this->ConfigureProcessing(img,0,0,0);
168         this->ConfigureSTL();
169
170         _wxMaracasMPR->Refresh();
171         
172         //this->Refresh();
173 }
174 //-------------------------------------------------------------------
175 wxWindow* wxSTLWidget_03::CreateSelectAPointPanel(wxWindow *parent)
176 {
177         wxPanel *panel          = new wxPanel(parent,-1);
178
179
180     wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
181
182         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
183         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
184
185         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
186         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
187
188         sizer->Add(new wxStaticText(panel,-1,_T(" Select a 3D Point...")));
189         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
190
191         panel->SetSizer(sizer);
192         panel->SetAutoLayout(true);
193         panel->SetSize(400,600);
194         panel->Layout();
195
196
197         return panel;
198 }
199 //-------------------------------------------------------------------
200 wxWindow* wxSTLWidget_03::CreateSegmentationPanel(wxWindow *parent)
201 {
202         wxPanel *panel          = new wxPanel(parent,-1);
203
204
205         _opacity_Vol            = new wxSlider( panel, -1, 40, 0, 100                            , wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL | wxSL_LABELS );
206         _opacity_Vol    ->SetSize(250,20);  
207         /*SIL
208         _sl_maxSize                     = new wxSlider( panel, -1, 300 , 0, 2000 , wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL  |  wxSL_AUTOTICKS | wxSL_LABELS );
209         _sl_minSize                     = new wxSlider( panel, -1, 200 , 0, 2000 , wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL  |  wxSL_AUTOTICKS | wxSL_LABELS );
210         
211         _sl_minSizeSpin = new wxSlider(panel , -1,5,1,10,wxDefaultPosition , wxSize(25,45), wxSL_VERTICAL | wxSL_AUTOTICKS |wxSL_LEFT  );
212         _sl_minSizeSpin->SetRange(1,8);
213         _sl_minSizeSpin->SetValue(5);
214
215         _sl_maxSizeSpin = new wxSlider(panel , -1,5,1,10,wxDefaultPosition , wxSize(25,45), wxSL_VERTICAL | wxSL_AUTOTICKS |wxSL_LEFT  );
216         _sl_maxSizeSpin->SetRange(1,8);
217         _sl_maxSizeSpin->SetValue(5);
218
219         */
220
221         _sl_RangeSizeSpin = new wxSlider(panel , -1,5,1,10,wxDefaultPosition , wxSize(25,45), wxSL_VERTICAL | wxSL_AUTOTICKS |wxSL_LEFT  );
222         _sl_RangeSizeSpin->SetRange(1,8);
223         _sl_RangeSizeSpin->SetValue(5);
224
225
226         //SIL//
227         sl_barrange_segmentation = new mBarRange(panel, 300,70);
228         sl_barrange_segmentation -> setVisibleLabels ( true );
229         sl_barrange_segmentation -> setIfWithActualDrawed( false );
230         sl_barrange_segmentation -> setActiveStateTo ( true );
231         sl_barrange_segmentation -> setRepresentedValues (0,  2000);
232         sl_barrange_segmentation -> setDeviceBlitStart (10,10);
233         sl_barrange_segmentation -> setDeviceEndMargin(10);
234         parent->SetBackgroundColour(wxColour(255,255,255));
235         sl_barrange_segmentation-> setBackgroundColor( wxColour(255,255,255 ));
236
237
238         wxButton *btnExtract                            = new wxButton(panel,-1,_T("Segmentation"));
239         wxButton *btnSaveBinaryFile                     = new wxButton(panel,-1,_T("Save Binary File"));
240
241 //SIL// Connect(_sl_minSizeSpin->GetId()        , wxEVT_COMMAND_SLIDER_UPDATED   , (wxObjectEventFunction) &wxSTLWidget_03::OnMinSpin ); 
242 //SIL// Connect(_sl_maxSizeSpin->GetId()        , wxEVT_COMMAND_SLIDER_UPDATED   , (wxObjectEventFunction) &wxSTLWidget_03::OnMaxSpin ); 
243         Connect(_opacity_Vol->GetId()           , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &wxSTLWidget_03::OnOpacity_Vol                                        ); 
244         Connect(btnExtract->GetId()                     , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &wxSTLWidget_03::OnBtnExtraction                                      ); 
245         Connect(btnSaveBinaryFile->GetId()      , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &wxSTLWidget_03::OnBtnSaveBinaryFile                          ); 
246
247         //SIL//
248         Connect(_sl_RangeSizeSpin->GetId()      , wxEVT_COMMAND_SLIDER_UPDATED   , (wxObjectEventFunction) &wxSTLWidget_03::OnRangeSpin ); 
249         Connect(sl_barrange_segmentation->GetId(),wxEVT_TSBAR_START,(wxObjectEventFunction) (wxCommandEventFunction)  &wxSTLWidget_03::OnStartChanged_range );
250         Connect(sl_barrange_segmentation->GetId(),wxEVT_TSBAR_END,(wxObjectEventFunction) (wxCommandEventFunction)  &wxSTLWidget_03::OnEndChanged_range );      
251         Connect(sl_barrange_segmentation->GetId(),wxEVT_TSBAR_MOVED,(wxObjectEventFunction) (wxCommandEventFunction)  &wxSTLWidget_03::OnBarMoved_range );
252
253     wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
254
255         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
256         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
257
258
259         sizer->Add(new wxStaticText(panel,-1,_T("  - - -  Segmentation - - - ")));
260         sizer->Add(new wxStaticText(panel,-1,_T("  "))); 
261         
262
263         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
264         sizer->Add(new wxStaticText(panel,-1,_T("  "))); 
265
266         /*sizer->Add(new wxStaticText(panel,-1,_T(" Threshold min")));
267         sizer->Add(new wxStaticText(panel,-1,_T("  "))); 
268         sizer->Add(_sl_minSizeSpin, 1, wxALL|wxEXPAND, 0);*/
269         //sizer->Add(_sl_minSize, 1, wxALL|wxEXPAND, 0);
270
271         sizer->Add(new wxStaticText(panel,-1,_T(" Threshold min-max")));
272         sizer->Add(new wxStaticText(panel,-1,_T("  "))); 
273         sizer->Add(_sl_RangeSizeSpin, 1, wxALL|wxEXPAND, 0);
274         sizer->Add(sl_barrange_segmentation,3,wxALL|wxGROW,0 );
275         
276         //SIL//sizer->Add(new wxStaticText(panel,-1,_T(" Threshold max")));
277         //SIL//sizer->Add(new wxStaticText(panel,-1,_T("  "))); 
278         //SIL// sizer->Add(_sl_maxSizeSpin, 1, wxALL|wxEXPAND, 0);
279         //SIL//sizer->Add(_sl_maxSize, 1, wxALL|wxEXPAND, 0);
280         
281         sizer->Add(btnExtract);
282         sizer->Add(new wxStaticText(panel,-1,_T(" ")));
283
284         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
285         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
286         sizer->Add(new wxStaticText(panel,-1,_T("Opacity - Result Volume")));
287         sizer->Add(_opacity_Vol);
288
289         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
290         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
291         sizer->Add(btnSaveBinaryFile);
292         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
293         
294         panel->SetSizer(sizer);
295         panel->SetAutoLayout(true);
296         panel->SetSize(400,600);
297         panel->Layout();
298
299         return panel;
300 }
301 //-------------------------------------------------
302 wxWindow* wxSTLWidget_03::CreateSmoothingPanel(wxWindow *parent)
303 {
304         wxPanel *panel          = new wxPanel(parent,-1);
305
306         stlSliderDeltaGauss     = new wxSlider( panel, -1, (int)(_stlDeltaGaussLevel) , 0, 300 , wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL | wxSL_LABELS );
307         stlSliderMarchingCubes= new wxSlider( panel, -1, (int)(_stlMarchingCubesLevel) , 0, 256 , wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL | wxSL_LABELS );
308         stlSliderOpacityInternal = new wxSlider(panel, -1, 100,0,100, wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL | wxSL_LABELS);
309         stlSliderOpacityExternal = new wxSlider(panel, -1, 100,0,100, wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL | wxSL_LABELS);
310         wxButton *btnFileSTL = new wxButton(panel,-1,_T("Generate STL files"));
311
312         Connect(btnFileSTL->GetId()                             , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &wxSTLWidget_03::OnBtnCreateFileSTL                   );
313         Connect(stlSliderDeltaGauss->GetId()    , wxEVT_SCROLL_THUMBRELEASE             , (wxObjectEventFunction) &wxSTLWidget_03::OnChangeSTLGaussLevel                );
314         Connect(stlSliderMarchingCubes->GetId() , wxEVT_SCROLL_THUMBRELEASE             , (wxObjectEventFunction) &wxSTLWidget_03::OnChangeSTLMarchingCubesLevel);
315         Connect(stlSliderOpacityInternal->GetId(), wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxSTLWidget_03::OnOpacitySTLInternal                 );
316         Connect(stlSliderOpacityExternal->GetId(), wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxSTLWidget_03::OnOpacitySTLExternal                 );
317
318
319     wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
320
321         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
322         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
323
324         sizer->Add(new wxStaticText(panel,-1,_T("-- Smoothing --")));
325         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
326         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
327         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
328         
329         sizer->Add(new wxStaticText(panel,-1,_T(" Delta Gauss")));
330         sizer->Add(stlSliderDeltaGauss);
331
332         sizer->Add(new wxStaticText(panel,-1,_T(" Marching Cubes Level")));
333         sizer->Add(stlSliderMarchingCubes);
334
335         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
336         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
337         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
338         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
339
340         sizer->Add(new wxStaticText(panel, -1,_T(" Opacity STL Internal")));
341         sizer->Add(stlSliderOpacityInternal);
342
343         sizer->Add(new wxStaticText(panel, -1,_T(" Opacity STL External")));
344         sizer->Add(stlSliderOpacityExternal);
345
346         sizer->Add(btnFileSTL);
347         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
348
349
350         panel->SetSizer(sizer);
351         panel->SetAutoLayout(true);
352         panel->SetSize(400,600);
353         panel->Layout();
354         return panel;
355 }
356 //-------------------------------------------------
357 wxWindow* wxSTLWidget_03::CreateReadSTLFilePanel(wxWindow *parent)
358 {
359         wxPanel *panel          = new wxPanel(parent,-1);
360
361         wxButton *btnReadSTLFile                        = new wxButton(panel,-1,_T("Read STL File"));
362         wxButton *btnEraseReadSTLFile           = new wxButton(panel,-1,_T("Erase STL File"));
363         _sl_opacity_STL_file                            = new wxSlider( panel, -1, 100 , 0, 100 , wxDefaultPosition, wxSize(200,40), wxSL_HORIZONTAL | wxSL_LABELS );
364
365         Connect(btnReadSTLFile->GetId()                 , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &wxSTLWidget_03::OnBtnSTLFileLoad                     ); 
366         Connect(btnEraseReadSTLFile->GetId()    , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &wxSTLWidget_03::OnBtnSTLFileErase                    ); 
367         Connect(_sl_opacity_STL_file->GetId()   , wxEVT_SCROLL_THUMBRELEASE     , (wxObjectEventFunction) &wxSTLWidget_03::OnOpacitySTLFile                             ); 
368
369
370     wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
371
372         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
373         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
374         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
375         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
376
377         sizer->Add(new wxStaticText(panel,-1,_T("-- Read STL File --")));
378         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
379
380         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
381         sizer->Add(new wxStaticText(panel,-1,_T("  ")));
382
383         sizer->Add(btnReadSTLFile );
384         sizer->Add(btnEraseReadSTLFile );
385         sizer->Add(new wxStaticText(panel, -1,_T(" Opacity STL File")));
386         sizer->Add(_sl_opacity_STL_file);
387
388
389         panel->SetSizer(sizer);
390         panel->SetAutoLayout(true);
391         panel->SetSize(400,600);
392         panel->Layout();
393         return panel;
394 }
395 //-------------------------------------------------------------------
396 wxPanel* wxSTLWidget_03::CreateControlPanel(wxWindow *parent)
397 {
398         wxPanel *panel                  = new wxPanel(parent,-1);
399
400         wxNotebook *noteBook    = new wxNotebook(panel, -1);
401         noteBook->AddPage(CreateSelectAPointPanel(noteBook),_T("0 ->") );
402         noteBook->AddPage(CreateSegmentationPanel(noteBook),_T("1 ->") );
403         noteBook->AddPage(CreateSmoothingPanel(noteBook)   ,_T("2 ->") );
404         noteBook->AddPage(CreateReadSTLFilePanel(noteBook) ,_T("3 "  ) );
405         noteBook->SetSelection(0);
406         noteBook->SetSize(350,250);
407     wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
408         sizer->Add(noteBook);
409         panel->SetSizer(sizer);
410         panel->SetAutoLayout(true);
411         panel->SetSize(400,600);
412         panel->Layout();
413
414         return panel;
415 }
416 //------------------------------------------------------------------------
417 void wxSTLWidget_03::Refresh()
418 {
419         if (_wxMaracasMPR!=NULL){
420                 _wxMaracasMPR->RefreshView();
421         }
422 }
423
424 //------------------------------------------------------------------------
425
426
427 void wxSTLWidget_03::ConfigureProcessing(vtkImageData *img, int x, int y, int z)
428 {
429     wxBusyCursor wait;
430
431         _imagedata=img;
432
433
434         double puntoactualprov[3];
435         puntoactualprov[0] = x;
436         puntoactualprov[1] = y;
437         puntoactualprov[2] = z;
438
439         double espprin[3];
440         int extprin[6];                         
441         
442         _imagedata->GetSpacing(espprin);
443         _imagedata->GetExtent(extprin);
444
445
446 // MAZV 27 sep 2006
447 //      puntoactualprov[0]=puntoactualprov[0]*espprin[0];
448 //      puntoactualprov[1]=puntoactualprov[1]*espprin[1];
449 //      puntoactualprov[2]=puntoactualprov[2]*espprin[2];
450         
451     _imagedata->GetScalarRange( _range );
452
453         // Update Controls
454         //SIL//
455         /*_sl_minSize->SetRange( (int)(_range[0]), (int)( _range[1]) );
456         _sl_minSize->SetValue( (int)(_range[1]/4) );
457
458         _sl_maxSize->SetRange( (int)(_range[0]) , (int)(_range[1]));
459         _sl_maxSize->SetValue( (int)(_range[1]/2) );*/
460
461         //SIL//
462         minRangeValue = _range[0];
463         maxRangeValue = _range[1];
464
465         sl_barrange_segmentation ->setRepresentedValues(minRangeValue, maxRangeValue);
466         sl_barrange_segmentation ->SetStart((int)(maxRangeValue/4));
467         sl_barrange_segmentation ->SetEnd((int)(maxRangeValue/2));
468         sl_barrange_segmentation -> RefreshForce();     
469         lastResizeRef_Value = ( maxRangeValue - minRangeValue)/2;       
470
471 // MAZV 27 sep 2006
472 //      _highthreshold->SetRange(_range[0], _range[1]);
473 //      _highthreshold->SetValue( _range[1] );
474
475 //      _zslice->SetRange(extprin[4], extprin[5]);
476 //      _zslice->SetValue(extprin[5]/2);
477     
478         if(_thresh==NULL){
479                 _thresh = vtkImageThreshold::New();
480                 //_thresh->ReleaseDataFlagOff();
481                 _thresh->SetInValue(255);
482                 _thresh->SetOutputScalarTypeToUnsignedShort();
483                 _thresh->SetOutValue(0);
484                 //_thresh->ThresholdBetween(_sl_minSize->GetValue(), _range[1]);
485                 //SIL//
486                 _cast = vtkImageCast::New();
487                 _cast->SetInput(_thresh->GetOutput());
488                 _cast->SetOutputScalarTypeToUnsignedChar();
489
490                 _connect = vtkImageSeedConnectivity::New();
491                 _connect->SetInput(_cast->GetOutput());
492                 _connect->SetInputConnectValue(255);
493                 _connect->SetOutputConnectedValue(255);
494                 _connect->SetOutputUnconnectedValue(0);
495
496                 cast3 = vtkImageCast::New();
497                 cast3->SetInput(_connect->GetOutput());
498                 cast3->SetOutputScalarTypeToUnsignedShort();
499
500                 _thresh2 = vtkImageThreshold::New();
501                 //_thresh2->ReleaseDataFlagOff();
502                 _thresh2->SetInValue(255);
503                 _thresh2->SetOutputScalarTypeToUnsignedShort();
504                 _thresh2->SetOutValue(0);
505                 //SIL//_thresh2->ThresholdBetween(_sl_minSize->GetValue(), _sl_maxSize->GetValue());
506
507                 cast2 = vtkImageCast::New();
508                 cast2->SetInput(_thresh2->GetOutput());
509                 cast2->SetOutputScalarTypeToUnsignedChar();
510                 _connect2 = vtkImageSeedConnectivity::New();
511                 _connect2->SetInput(cast2->GetOutput());
512                 _connect2->SetInputConnectValue(255);
513                 _connect2->SetOutputConnectedValue(255);
514                 _connect2->SetOutputUnconnectedValue(0);
515
516                 cast4 = vtkImageCast::New();
517                 cast4->SetInput(_connect2->GetOutput());
518                 cast4->SetOutputScalarTypeToUnsignedShort();
519         }
520         _thresh->RemoveAllInputs();
521         _thresh->SetInput(_imagedata);  
522         _thresh->ThresholdBetween(sl_barrange_segmentation->GetStart(), sl_barrange_segmentation->GetEnd());    
523         _thresh->Update();
524         
525         _cast->Update();  
526         
527
528         _connect->RemoveAllSeeds();
529         _connect->AddSeed((int)(puntoactualprov[0]), (int)(puntoactualprov[1]), (int)(puntoactualprov[2]));
530         //_connect->Update();   
531         //cast3->Update();
532 //SIL// 
533         _thresh2->RemoveAllInputs();
534         _thresh2->SetInput(_imagedata); 
535         _thresh2->ThresholdBetween(sl_barrange_segmentation->GetStart(), sl_barrange_segmentation->GetEnd());
536         _thresh2->Update();
537         cast2->Update();
538         _connect2->RemoveAllSeeds();
539         _connect2->AddSeed( (int)(puntoactualprov[0]), (int)(puntoactualprov[1]), (int)(puntoactualprov[2]));
540         //_connect2->Update();  
541         //cast4->Update();
542 }
543
544 //------------------------------------------------------------------------
545
546
547
548 void wxSTLWidget_03::ConfigureVTK()
549 {
550         wxBusyCursor wait;
551 //      vtkImageData    *imagedata              = _mar->_experiment->getDynData( )->getVolume( )->castVtk();
552         //marImageData  *marimagedata;//                = _mar->_experiment->getDynData( )->GetMarImageData();
553         
554         //CONFIGURACION ADICIONAL
555         this->ConfigureSTL();
556         //this->ConfigureProcessing(marimagedata,0,0,0);
557         
558 }
559
560 //------------------------------------------------------------------------
561
562 void wxSTLWidget_03::ResetTree2_JF()
563 {
564         // Remove 
565
566         try{
567
568                 vtkRenderer *ren = _wxMaracasMPR->GetWxvtkmpr3Dview_BB()->GetWxvtk3Dbaseview()->GetRenderer();
569
570                 if (_2_isoActorMC6){
571                         ren->RemoveActor(_2_isoActorMC6);
572                 }
573                 if (_2_stripfinal){
574                         ren->RemoveActor(_2_stripfinal);
575                 }
576
577                 // Delete 
578         //      if (_2_prgov            ) {     _2_prgov                -> Delete(); }
579                 if (_2_mapfinal         ) {     _2_mapfinal             -> Delete(); }
580                 if (_2_stripfinal       ) {     _2_stripfinal   -> Delete(); }
581                 if (_2_isoMapperMC6     ) {     _2_isoMapperMC6 -> Delete(); }
582                 if (_2_isoActorMC6      ) {     _2_isoActorMC6  -> Delete(); }
583                 if (_2_isoMC6           ) {     _2_isoMC6               -> Delete(); }
584
585                 // Init
586         //      _2_prgov                = NULL;
587                 _2_mapfinal             = NULL;
588                 _2_stripfinal   = NULL;
589                 _2_isoMapperMC6 = NULL;
590                 _2_isoActorMC6  = NULL;
591                 _2_isoMC6               = NULL;
592         }catch(char* e){
593                 std::cout<<e<<std::endl;
594         }
595 }
596
597
598
599
600
601
602 //------------------------------------------------------------------------
603 void wxSTLWidget_03::ExtractSurface(int x, int y, int z)
604 {
605         
606         double opacityvol               = ((double)_opacity_Vol->GetValue())/100;
607 //      vtkImageData *imagedata = _mar->_experiment->getDynData( )->getVolume( )->castVtk();
608
609         double  puntoactualprov[3];
610         double  espprin[3];
611         int             extprin[6];                             
612
613
614         puntoactualprov[0]      =       x;
615         puntoactualprov[1]      =       y;
616         puntoactualprov[2]      =       z;
617         
618         _imagedata->UpdateInformation();
619         _imagedata->SetUpdateExtent(_imagedata->GetWholeExtent());
620         _imagedata->Update();
621
622         _imagedata->GetSpacing(espprin);
623         _imagedata->GetExtent(extprin);
624
625
626
627
628         _connect->RemoveAllSeeds ();
629         _connect->AddSeed( (int)(puntoactualprov[0] ), (int)(puntoactualprov[1]) ,(int)( puntoactualprov[2]) );
630         _connect->Update();
631         cast3->Update();
632
633
634
635         _thresh->ThresholdBetween(3000, 3002 );
636         _thresh2->ThresholdBetween(3000, 3001);
637 //      _thresh->Update();
638 //      _thresh2->Update();
639
640         _thresh->ThresholdBetween(_minSize, _range[1] );
641         _thresh->Update();
642
643         _thresh2->ThresholdBetween(_minSize, _maxSize);
644         _thresh2->Update();
645
646         cast2->Update();
647         _connect2->RemoveAllSeeds ();
648         _connect2->AddSeed( (int)(puntoactualprov[0]) , (int)(puntoactualprov[1]) , (int)(puntoactualprov[2]) );
649         _connect2->Update();
650         cast4->Update();
651
652
653
654
655         // Visualisation - result volume
656    
657     _2_isoMC6 = vtkMarchingCubes::New();
658         _2_isoMC6->SetInput(cast4->GetOutput());
659
660     _2_isoMC6->SetValue(0, 128);
661         _2_isoMC6->Update();
662
663         _2_isoMapperMC6 = vtkPolyDataMapper::New();
664     _2_isoMapperMC6->SetInput(_2_isoMC6->GetOutput());
665     _2_isoMapperMC6->ScalarVisibilityOff();
666     _2_isoMapperMC6->ImmediateModeRenderingOn();
667
668         vtkRenderer *ren = _wxMaracasMPR->GetWxvtkmpr3Dview_BB()->GetWxvtk3Dbaseview()->GetRenderer();
669
670         if (_2_isoActorMC6!=NULL)
671         {
672                 ren->RemoveActor(_2_isoActorMC6);
673         }
674
675
676         _2_isoActorMC6 = vtkActor::New();
677     _2_isoActorMC6->SetMapper(_2_isoMapperMC6);
678     _2_isoActorMC6->GetProperty()->SetColor(1.0, 1.0, 0.5);
679     _2_isoActorMC6->GetProperty()->SetOpacity( opacityvol );
680     
681         // Interface Update
682
683         ren->AddActor(_2_isoActorMC6);
684
685 }
686
687 //------------------------------------------------------------------------
688 void wxSTLWidget_03::OnOpacity_Vol(wxScrollEvent& event)
689 {
690         if (_2_isoActorMC6!=NULL){
691                 double value = ((double)_opacity_Vol->GetValue())/100;
692                 _2_isoActorMC6->GetProperty( )->SetOpacity( value );
693                 Refresh();
694         }
695 }
696 //------------------------------------------------------------------------
697
698 void wxSTLWidget_03::OnMinSpin          (       wxScrollEvent &          event           )      
699 {
700         /*wxSlider *sl_A=_sl_minSize;
701         wxSlider *sl_B=_sl_minSizeSpin;
702         int value = sl_A->GetValue();
703         int delta=(int)pow( 4 , sl_B->GetValue() );
704         int min=value - delta/2;
705         int max=value + delta/2;
706         if (min<0)
707         {
708                 min=0;
709                 max=delta;
710         }
711         sl_A->SetRange(min,max);
712
713
714         */
715 }
716
717 //------------------------------------------------------------------------
718
719 void wxSTLWidget_03::OnMaxSpin          (       wxScrollEvent &          event           )      
720 {
721         /*wxSlider *sl_A=_sl_maxSize;
722         wxSlider *sl_B=_sl_maxSizeSpin;
723         int value = sl_A->GetValue();
724         int delta = (int)pow( 4 , sl_B->GetValue() );
725         int min=value - delta/2;
726         int max=value + delta/2;
727         if (min<0)
728         {
729                 min=0;
730                 max=delta;
731         }
732         sl_A->SetRange(min,max);*/      
733 }
734
735 //------------------------------------------------------------------------
736
737 void wxSTLWidget_03::OnRangeSpin        (       wxScrollEvent &          event           )      
738 {
739         wxSlider *sl_B=_sl_RangeSizeSpin;
740
741         int value = lastResizeRef_Value;
742         int delta = (int)pow( 4 , sl_B->GetValue() );
743         int startResized = value - delta/2;
744         int endResized = value + delta/2;
745
746         if( startResized < minRangeValue )
747                 startResized = minRangeValue;
748
749         if( endResized > maxRangeValue )
750                 endResized = maxRangeValue;
751
752         sl_barrange_segmentation->setRepresentedValues(startResized,endResized);
753         sl_barrange_segmentation -> RefreshForce();     
754 }
755
756 //------------------------------------------------------------------------
757 void wxSTLWidget_03:: OnStartChanged_range(wxCommandEvent& event)
758 {
759         lastResizeRef_Value = sl_barrange_segmentation->GetStart();
760 }
761 //------------------------------------------------------------------------
762 void wxSTLWidget_03:: OnEndChanged_range(wxCommandEvent& event)
763 {
764         lastResizeRef_Value = sl_barrange_segmentation->GetEnd();   
765 }
766 //------------------------------------------------------------------------
767 void wxSTLWidget_03:: OnBarMoved_range(wxCommandEvent& event)
768 {
769         lastResizeRef_Value = (sl_barrange_segmentation->GetEnd() - sl_barrange_segmentation->GetStart())/2 ;
770 }
771 //------------------------------------------------------------------------
772
773 void wxSTLWidget_03::OnBtnSaveBinaryFile(wxCommandEvent& event)
774 {
775         wxString dirSTL;// = _mar->_parameters->getStringParam( 
776     //marParameters::e_installation_directory ); 
777         dirSTL = ( dirSTL == _T("NO_DIRECTORY") ) ? wxGetHomeDir( ) : dirSTL;
778         wxFileDialog dialog( this, _T("Choose a directory..."), ( !dirSTL.IsEmpty( ) )?
779         dirSTL: wxGetHomeDir( ) );
780
781         if( dialog.ShowModal( ) == wxID_OK ) 
782         {
783                 vtkMetaImageWriter  *writer= vtkMetaImageWriter::New();
784                 writer->SetFileName( dialog.GetPath().mb_str(wxConvUTF8) );     
785                 writer->SetInput(cast4->GetOutput());
786                 writer->Write();
787                 writer->Delete();
788         }
789 }
790
791 //------------------------------------------------------------------------
792 void wxSTLWidget_03::OnBtnExtraction(wxCommandEvent& event)
793 {
794         /*wxBusyCursor wait;
795         _minSize = _sl_minSize->GetValue();
796         _maxSize = _sl_maxSize->GetValue();
797
798         double px=_wxMaracasMPR->GetVtkMPRBaseData()->GetX();
799         double py=_wxMaracasMPR->GetVtkMPRBaseData()->GetY();
800         double pz=_wxMaracasMPR->GetVtkMPRBaseData()->GetZ();
801         ExtractSurface((int)(px),(int)(py),(int)(pz));
802         generateSTLSurfaces(); //DHC: AFTER SEGMENTATION THE TREE, THE STL SURFACES ARE CALCULATED.
803
804         Refresh();
805 */
806         //SIL//
807         wxBusyCursor wait;
808         _minSize = sl_barrange_segmentation->GetStart();
809         _maxSize = sl_barrange_segmentation->GetEnd();
810
811         double px=_wxMaracasMPR->GetVtkMPRBaseData()->GetX();
812         double py=_wxMaracasMPR->GetVtkMPRBaseData()->GetY();
813         double pz=_wxMaracasMPR->GetVtkMPRBaseData()->GetZ();
814         ExtractSurface((int)(px),(int)(py),(int)(pz));
815         generateSTLSurfaces(); //DHC: AFTER SEGMENTATION THE TREE, THE STL SURFACES ARE CALCULATED.
816
817         Refresh();
818 }
819
820 //------------------------------------------------------------------------
821 void wxSTLWidget_03::OnOpacitySTLFile(wxScrollEvent& event)
822 {
823         if (_loadActorSTL!=NULL)
824         {
825                 double value = ((double)_sl_opacity_STL_file->GetValue())/100;
826                 _loadActorSTL->GetProperty( )->SetOpacity( value );
827                 Refresh();
828         }
829 }
830 //------------------------------------------------------------------------
831 void wxSTLWidget_03::Reset_vtk_STLFile()
832 {
833         // Remove
834         try{
835                 vtkRenderer *ren = _wxMaracasMPR->GetWxvtkmpr3Dview_BB()->GetWxvtk3Dbaseview()->GetRenderer();
836                 if (_loadActorSTL){
837                         ren->RemoveActor(_loadActorSTL);
838                 }
839
840                 // Delete 
841                 if (_loadSTLMapper              ) {     _loadSTLMapper          -> Delete(); }
842                 if (_loadActorSTL               ) {     _loadActorSTL           -> Delete(); }
843
844                 // Init
845                 _loadSTLMapper          = NULL;
846                 _loadActorSTL           = NULL;
847         }catch(char * e){
848                 std::cout<<e<<std::endl;
849         }
850
851 }
852 //------------------------------------------------------------------------
853 void wxSTLWidget_03::OnBtnSTLFileErase(wxCommandEvent& event)
854 {
855         Reset_vtk_STLFile();
856         Refresh();
857 }
858 //------------------------------------------------------------------------
859 void wxSTLWidget_03::OnBtnSTLFileLoad(wxCommandEvent& event)
860 {
861         wxString dirSTL;// = _mar->_parameters->getStringParam( 
862     //marParameters::e_installation_directory ); 
863         dirSTL = ( dirSTL == _T("NO_DIRECTORY") ) ? wxGetHomeDir( ) : dirSTL;
864         wxFileDialog dialog( this, _T("Choose a directory..."), ( !dirSTL.IsEmpty( ) )?
865         dirSTL: wxGetHomeDir( ) );
866
867         if( dialog.ShowModal( ) == wxID_OK ) 
868         {
869                 Reset_vtk_STLFile();
870                 vtkSTLReader *imgReader= vtkSTLReader::New();
871                 imgReader->SetFileName( dialog.GetPath().mb_str(wxConvUTF8) );  
872                 _loadSTLMapper = vtkPolyDataMapper::New();
873                 _loadSTLMapper->SetInput(imgReader->GetOutput());
874                 _loadActorSTL = vtkActor::New();
875                 _loadActorSTL->SetMapper(_loadSTLMapper);
876                 _loadActorSTL->GetProperty()->SetColor( 1, 0, 0);
877                 _loadActorSTL->GetProperty()->SetOpacity( 0.5 );
878                 vtkRenderer *ren = _wxMaracasMPR->GetWxvtkmpr3Dview_BB()->GetWxvtk3Dbaseview()->GetRenderer();
879                 ren->AddActor(_loadActorSTL);
880                 imgReader->Delete();
881         }
882         //By default *always* update e_installation_directory:
883         //_mar->_parameters->setStringParam( marParameters::e_installation_directory, dialog.GetPath( ) ); 
884         //_mar->saveParameters( );
885 }
886
887
888
889 // ------------------------------------------------------------------------
890 // END JOIN FUNCTIONS - DHC
891 // ------------------------------------------------------------------------
892
893 // ------------------------------------------------------------------------
894 // START STL FUNCTIONS - DHC
895 // ------------------------------------------------------------------------
896
897 void wxSTLWidget_03::ConfigureSTL()
898 {
899
900         if(stlExterna == NULL){
901                 stlExterna = vtkPolyData::New();
902                 stlInterna = vtkPolyData::New();
903
904                 dsm1 = vtkPolyDataMapper ::New();
905                 dsm1->SetInput (stlInterna); 
906                 dsm1->ScalarVisibilityOff();
907
908                 actorInternal = vtkActor::New();
909                 actorInternal->SetMapper (dsm1);
910                 actorInternal->GetProperty()->SetColor (0,1,0);
911
912                 dsm2 = vtkPolyDataMapper ::New();
913                 dsm2->SetInput (stlExterna);
914                 dsm2->ScalarVisibilityOff();
915
916                 actorExternal= vtkActor::New();
917                 actorExternal->SetMapper (dsm2);
918                 actorExternal->GetProperty()->SetRepresentationToWireframe();
919
920         //    vtkRenderer *ren = _maracasSurfaceWidget->GetVtk3DSurfaceWidget()->GetRenderer();
921                 vtkRenderer *ren = _wxMaracasMPR->GetWxvtkmpr3Dview_BB()->GetWxvtk3Dbaseview()->GetRenderer();
922                 ren->AddActor(actorInternal);
923                 ren->AddActor(actorExternal);
924
925                 stlExtractor = new vtkSTLExtractor();  
926
927         }
928
929         
930 }
931
932 void wxSTLWidget_03::generateSTLSurfaces()
933 {
934 // EED 09Janvier2007
935 //      stlExtractor->setVolume(_2_prgov->GetVolumen());
936
937         stlExtractor->setVolume(cast4->GetOutput());
938
939         stlExtractor->setSigmaLevel(_stlDeltaGaussLevel);
940         stlExtractor->setMarchingCubesLevel(_stlMarchingCubesLevel);
941         stlExtractor->calculate();
942         stlInterna->DeepCopy(stlExtractor->getInnerSurface());
943         stlExterna->DeepCopy(stlExtractor->getOuterSurface());
944 }
945
946
947 void wxSTLWidget_03::OnOpacitySTLExternal(wxScrollEvent& event){
948         double value = ((double)stlSliderOpacityExternal->GetValue())/100;
949     actorExternal->GetProperty( )->SetOpacity( value );
950         Refresh();
951 }
952
953
954 void wxSTLWidget_03::OnOpacitySTLInternal(wxScrollEvent& event){
955         double value = ((double)stlSliderOpacityInternal->GetValue())/100;
956     actorInternal->GetProperty( )->SetOpacity( value );
957         Refresh();
958 }
959
960 void wxSTLWidget_03::OnBtnCreateFileSTL(wxCommandEvent& event)
961 {
962
963         wxString dirSTL;// = _mar->_parameters->getStringParam( 
964     //marParameters::e_installation_directory ); 
965         dirSTL = ( dirSTL == _T("NO_DIRECTORY") ) ? wxGetHomeDir( ) : dirSTL;
966
967         
968 //      wxDirDialog dialog( this, "Choose a directory...", ( !dirSTL.IsEmpty( ) )?dirSTL: wxGetHomeDir( ) );
969         wxFileDialog dialog( this, _T("Choose a directory..."), dirSTL , _T(""), _T("*.*"), wxSAVE );
970
971
972         if( dialog.ShowModal( ) == wxID_OK ) 
973         {
974            
975         
976                 // ------------------------------------------------------------------------
977                 //  1.  GENERATE STL FILES
978                 // ------------------------------------------------------------------------
979 //              const char* fileprefix = "c:\\Creatis\\";
980                 std::string prefix ( dialog.GetPath().mb_str(wxConvUTF8) );
981                 std::string filename;
982
983                 // 1.1. Se hace un filtro triangular puesto que el stl writer solo recibe poligonos triangulares.
984
985         vtkTriangleFilter *filtro = vtkTriangleFilter::New();
986                 filtro->SetInput(stlInterna);
987                 vtkPolyDataConnectivityFilter *pdcf = vtkPolyDataConnectivityFilter::New();
988         pdcf->SetInput( filtro->GetOutput() );
989         vtkClosePolyData *cpd = vtkClosePolyData::New();
990         cpd->SetInput( pdcf->GetOutput() );
991
992                 // 1.2 se escribe a disco el archivo stl de la superficie interna
993         cpd->Update();
994         vtkSTLWriter *writerI = vtkSTLWriter::New();
995         writerI->SetInput( cpd->GetOutput() );
996 //        prefix = fileprefix;
997                 filename=prefix+"_internal.stl";
998         writerI->SetFileName(filename.c_str());
999         writerI->SetFileTypeToASCII();
1000         writerI->Write();
1001         writerI->Delete();
1002
1003                 // 1.3 se escribe a disco el archivo stl de la superficie externa
1004                 filtro->SetInput(stlExterna);
1005         cpd->Update();
1006         vtkSTLWriter *writerE = vtkSTLWriter::New();
1007         writerE->SetInput( cpd->GetOutput() );
1008 //        prefix = fileprefix;
1009                 filename=prefix+"_external.stl";
1010         writerE->SetFileName( filename.c_str() );
1011         writerE->SetFileTypeToASCII();
1012         writerE->Write();
1013         writerE->Delete();
1014    
1015         filtro->Delete();
1016         cpd->Delete();
1017         pdcf->Delete();
1018         }
1019
1020         //By default *always* update e_installation_directory:
1021         //_mar->_parameters->setStringParam( marParameters::e_installation_directory, dialog.GetPath( ) ); 
1022         //_mar->saveParameters( );
1023 }
1024
1025
1026 void wxSTLWidget_03::OnChangeSTLGaussLevel(wxScrollEvent& event)
1027 {
1028         wxBusyCursor wait;
1029         _stlDeltaGaussLevel  = ((double)stlSliderDeltaGauss->GetValue())/100;
1030         generateSTLSurfaces();
1031         Refresh();
1032 }
1033
1034
1035 void wxSTLWidget_03::OnChangeSTLMarchingCubesLevel(wxScrollEvent& event)
1036 {
1037         wxBusyCursor wait;
1038         _stlMarchingCubesLevel = ((double)stlSliderMarchingCubes->GetValue());
1039         generateSTLSurfaces();
1040         Refresh();
1041         
1042 }
1043
1044
1045 // ------------------------------------------------------------------------
1046 // END STL FUNCTIONS - DHC
1047 // ------------------------------------------------------------------------
1048
1049
1050
1051
1052
1053
1054