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