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