2 #include "wxPanelCuttingImageData.h"
4 //-------------------------------------------------------------------
6 #include <vtkProperty.h>
7 #include <vtkTextProperty.h>
8 #include <vtkProperty2D.h>
9 //-------------------------------------------------------------------
10 //-------------------------------------------------------------------
12 wxPanelCuttingImageData::wxPanelCuttingImageData (wxWindow *parent)
13 : wxPanel( parent, -1)
16 _histogrammeVector=NULL;
17 _wxvtk3Dbaseview=NULL;
21 Create3DViewObjects();
24 //-------------------------------------------------------------------
25 wxPanelCuttingImageData::~wxPanelCuttingImageData()
31 delete _modelCylinder;
33 _vtksphere -> Delete();
34 _vtkcylinder -> Delete();
35 _cubeMapper -> Delete();
36 _sphereMapper -> Delete();
37 _cylinderMapper -> Delete();
38 _cubeActor -> Delete();
39 _sphereActor -> Delete();
40 _cylinderActor -> Delete();
41 // _xyplot->RemoveAllInputs();
43 _histogrammeVector->Delete();
44 _renplotter->Delete();
46 if(_wxvtkbaseView!=NULL){
47 delete _wxvtkbaseView;
49 //delete _vtkclipping3Ddataviewer;
56 //-------------------------------------------------------------------
57 void wxPanelCuttingImageData::RemoveActors()
59 _wxvtk3Dbaseview->GetRenderer()->RemoveActor( _actualActor );
63 //-------------------------------------------------------------------
64 void wxPanelCuttingImageData::SetWxVtk3DBaseView( wxVtk3DBaseView * wxvtk3Dbaseview )
66 _wxvtk3Dbaseview=wxvtk3Dbaseview;
69 //-------------------------------------------------------------------
71 void wxPanelCuttingImageData::SetVtkClipping3DDataViewer( vtkClipping3DDataViewer *vtkclipping3Ddataviewer )
73 this->_vtkclipping3Ddataviewer = vtkclipping3Ddataviewer;
76 //-------------------------------------------------------------------
77 void wxPanelCuttingImageData::Create3DViewObjects()
80 _vtksphere = vtkSphereSource::New();
81 _vtksphere->SetThetaResolution (20);
82 _vtksphere->SetPhiResolution (20);
83 _sphereMapper = vtkPolyDataMapper::New();
84 _sphereMapper->SetInput( _vtksphere->GetOutput() );
85 _sphereActor = vtkActor::New();
86 _sphereActor->SetMapper(_sphereMapper);
87 _sphereActor->SetOrigin(0, 0, 0);
88 _sphereActor->SetPosition(0, 0, 0);
89 _sphereActor->GetProperty()->SetColor(1, 0, 0);
90 _sphereActor->SetUserTransform( _modelSphere->GetVtkTransform() );
93 _vtkcube = vtkCubeSource::New();
94 _vtkcube->SetXLength (1);
95 _vtkcube->SetYLength (1);
96 _vtkcube->SetZLength (1);
97 _cubeMapper = vtkPolyDataMapper::New();
98 _cubeMapper->SetInput( _vtkcube->GetOutput() );
99 _cubeActor = vtkActor::New();
100 _cubeActor->SetMapper(_cubeMapper);
101 _cubeActor->SetOrigin(0, 0, 0);
102 _cubeActor->SetPosition(0, 0, 0);
103 _cubeActor->GetProperty()->SetColor(1, 0, 0);
104 _cubeActor->SetUserTransform( _modelCube->GetVtkTransform() );
107 _vtkcylinder = vtkCylinderSource::New();
108 _vtkcylinder->SetResolution(20);
109 _cylinderMapper = vtkPolyDataMapper::New();
110 _cylinderMapper->SetInput( _vtkcylinder->GetOutput() );
111 _cylinderActor = vtkActor::New();
112 _cylinderActor->SetMapper(_cylinderMapper);
113 _cylinderActor->SetOrigin(0, 0, 0);
114 _cylinderActor->SetPosition(0, 0, 0);
115 _cylinderActor->GetProperty()->SetColor(1, 0, 0);
116 _cylinderActor->SetUserTransform( _modelCylinder->GetVtkTransform() );
121 //-------------------------------------------------------------------
122 void wxPanelCuttingImageData::CreateModel()
124 _modelCube = new figureCuttingCubeModel();
125 _modelSphere = new figureCuttingSphereModel();
126 _modelCylinder = new figureCuttingCylinderModel();
128 // _modelCube->SetVtkTransform( _modelCube->GetVtkTransform() );
129 // _modelSphere->SetVtkTransform( _modelSphere->GetVtkTransform() );
130 // _modelCylinder->SetVtkTransform( _modelCylinder->GetVtkTransform() );
133 //-------------------------------------------------------------------
135 void wxPanelCuttingImageData::InitHistogramme()
138 if (_imageData==NULL)
142 _imageData->GetScalarRange(rangeA);
144 _xyplot->RemoveAllInputs();
146 if ( _histogrammeVector!=NULL )
148 _histogrammeVector -> Delete();
151 _histogrammeVector = vtkImageData::New();
152 _histogrammeVector -> SetDimensions ( (int)(rangeA[1]),1,1 );
153 _histogrammeVector -> SetScalarTypeToUnsignedShort();
154 _histogrammeVector -> AllocateScalars();
155 _histogrammeVector -> Update();
157 unsigned short *p_vol = (unsigned short*)_histogrammeVector->GetScalarPointer(0,0,0);
158 int i,size = (int) (rangeA[1]);
159 for (i=0; i < size; i++)
164 _xyplot->SetXRange(0, rangeA[1]);
165 _xyplot->SetYRange(0, 10);
166 _xyplot->AddInput( _histogrammeVector );
169 //-------------------------------------------------------------------
170 wxWindow *wxPanelCuttingImageData::CreatePlotHistogrammeInterface()
172 _xyplot = vtkXYPlotActor::New();
174 _xyplot->GetPositionCoordinate()->SetValue(0.00, 0.00, 0);
175 _xyplot->GetPosition2Coordinate()->SetValue(1.0, 1.00, 0); //relative to Position
176 _xyplot->SetXValuesToArcLength();
177 _xyplot->SetNumberOfXLabels(6);
179 _xyplot->SetTitle("Histogramme");
180 _xyplot->SetXTitle("Gray level");
181 _xyplot->SetYTitle("Occurrences ");
182 _xyplot->GetProperty()->SetColor(1, 0, 0);
183 _xyplot->GetProperty()->SetPointSize(2);
184 vtkTextProperty *tprop = _xyplot->GetTitleTextProperty();
185 tprop->SetColor( 1,0,1 );
187 _xyplot->SetAxisTitleTextProperty(tprop);
188 _xyplot->SetAxisLabelTextProperty(tprop);
189 _xyplot->PlotPointsOn();
190 _xyplot->GetProperty()->SetPointSize(3);
192 _wxvtkbaseView = new wxVtkBaseView(this);
193 _wxvtkbaseView->Configure();
195 _renplotter = vtkRenderer::New();
196 vtkRenderWindow *renWin = _wxvtkbaseView->GetRenWin();
197 renWin->AddRenderer( _renplotter );
198 _renplotter->AddActor2D( _xyplot );
200 return _wxvtkbaseView->GetWxVTKRenderWindowInteractor();
203 //-------------------------------------------------------------------
205 void wxPanelCuttingImageData::CreateInterface()
208 wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL); // Principal sizer
210 wxBoxSizer *sizerH0 = new wxBoxSizer(wxHORIZONTAL ); // type of segmentation figure
211 wxBoxSizer *sizerH2 = new wxBoxSizer(wxHORIZONTAL ); // scale
212 wxBoxSizer *sizerH3 = new wxBoxSizer(wxHORIZONTAL ); // rotation
213 wxBoxSizer *sizerH4 = new wxBoxSizer(wxHORIZONTAL ); // intern extern
214 wxBoxSizer *sizerH5 = new wxBoxSizer(wxHORIZONTAL ); // Isovalue
215 wxBoxSizer *sizerH6 = new wxBoxSizer(wxHORIZONTAL ); // Buttons
216 wxFlexGridSizer *sizerH7 = new wxFlexGridSizer(2 ); // Volumic information
219 _typeFig = new wxChoice(this,-1);
220 _opacityFig = new wxSlider(this,-1,100,0,100, wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
222 _scaleX = new wxSlider(this,-1,6,0,500 , wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
223 _scaleY = new wxSlider(this,-1,20,0,500 , wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
224 _scaleZ = new wxSlider(this,-1,7,0,500 , wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
226 _rotationX = new wxSlider(this,-1,0,-360,360, wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
227 _rotationY = new wxSlider(this,-1,0,-360,360, wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
228 _rotationZ = new wxSlider(this,-1,0,-360,360, wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
230 _volIntern = new wxRadioButton(this,-1, _T("Volume intern " ));
231 _volExtern = new wxRadioButton(this,-1, _T("Volume extern " ));
233 _histogrammeAccumulated = new wxCheckBox(this,-1,_T("Histogramme accumulated"));
235 _isoValue = new wxSlider(this,-1, 200, 0,2000, wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
236 _valueBeforeIsoValue = new wxSlider(this,-1,-1,-1,2000, wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
237 _valueAfterIsoValue = new wxSlider(this,-1,-1,-1,2000, wxDefaultPosition, wxSize(200,45), wxSL_HORIZONTAL | wxSL_LABELS);
239 wxButton *btnExtract = new wxButton(this, -1,_T("Extract"));
241 _infoToVo = new wxStaticText(this,-1,_T("########################"));
242 _infoSuVoA = new wxStaticText(this,-1,_T("############"));
243 _infoSuVo = new wxStaticText(this,-1,_T("############"));
244 _infoPixLe = new wxStaticText(this,-1,_T("############"));
245 _infoPixHi = new wxStaticText(this,-1,_T("############"));
250 _typeFig->Append(_T("Cylindre"));
251 _typeFig->Append(_T("Cube"));
252 _typeFig->Append(_T("Sphere"));
253 _typeFig->SetSelection(0);
254 _volIntern->SetValue(true);
256 Connect(_typeFig->GetId() , wxEVT_COMMAND_CHOICE_SELECTED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnTypeFig );
258 Connect(_opacityFig->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnOpacityFig );
259 Connect(_rotationX->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnTransform );
260 Connect(_rotationY->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnTransform );
261 Connect(_rotationZ->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnTransform );
262 Connect(_scaleX->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnTransform );
263 Connect(_scaleY->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnTransform );
264 Connect(_scaleZ->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnTransform );
265 Connect(btnExtract->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxPanelCuttingImageData::OnExtract );
268 // wxStaticText *text=new wxStaticText(this,-1, " ");
270 sizerH0 -> Add( new wxStaticText(this,-1, _T("Fig. Type: "),wxDefaultPosition, wxSize(50,20)) ,1,wxALL ,0);
271 sizerH0 -> Add( _typeFig ,1,wxALL ,0);
272 sizerH0 -> Add( _opacityFig ,1,wxALL|wxEXPAND ,0);
274 sizerH2 -> Add( new wxStaticText(this,-1,_T("Scale : ")) ,1,wxALL ,0);
275 sizerH2 -> Add( _scaleX ,1,wxALL | wxEXPAND ,0 );
276 sizerH2 -> Add( _scaleY ,1,wxALL | wxEXPAND ,0 );
277 sizerH2 -> Add( _scaleZ ,1,wxALL | wxEXPAND ,0 );
279 sizerH3 -> Add( new wxStaticText(this,-1,_T("Rotation : ")) ,1,wxALL ,0);
280 sizerH3 -> Add( _rotationX ,1,wxALL | wxEXPAND ,0 );
281 sizerH3 -> Add( _rotationY ,1,wxALL | wxEXPAND ,0 );
282 sizerH3 -> Add( _rotationZ ,1,wxALL | wxEXPAND ,0 );
285 sizerH4 -> Add( new wxStaticText(this,-1,_T("Intern / Extern : ")) ,1,wxALL ,0);
286 sizerH4 -> Add( _volIntern ,1,wxALL ,0);
287 sizerH4 -> Add( new wxStaticText(this,-1, _T(" ")) ,1,wxALL ,0);
288 sizerH4 -> Add( _volExtern ,1,wxALL ,0);
290 sizerH5 -> Add( new wxStaticText(this,-1,_T("Isovalue ")) ,1,wxALL ,0);
291 sizerH5 -> Add( _isoValue ,1,wxALL | wxEXPAND ,0 );
292 sizerH5 -> Add( _valueBeforeIsoValue ,1,wxALL | wxEXPAND ,0 );
293 sizerH5 -> Add( _valueAfterIsoValue ,1,wxALL | wxEXPAND ,0 );
295 sizerH6 -> Add( new wxStaticText(this,-1, _T(" ")) ,1,wxALL ,0);
296 sizerH6 -> Add( btnExtract ,1,wxALL ,0);
298 sizerH7 -> Add( new wxStaticText(this,-1,_T("Total Volume: "), wxDefaultPosition, wxSize(200,12)) , 1 , wxALL ,0);
299 sizerH7 -> Add( _infoToVo , 1 , wxALL ,0);
300 sizerH7 -> Add( new wxStaticText(this,-1,_T("SubVolume: "), wxDefaultPosition, wxSize(200,12) ) , 1 , wxALL ,0);
301 sizerH7 -> Add( _infoSuVo , 1 , wxALL ,0);
302 sizerH7 -> Add( new wxStaticText(this,-1,_T("SubVolume (ana.): "), wxDefaultPosition, wxSize(200,12)) , 1 , wxALL ,0);
303 sizerH7 -> Add( _infoSuVoA , 1 , wxALL ,0);
304 sizerH7 -> Add( new wxStaticText(this,-1,_T("Pix < isovalue: ") , wxDefaultPosition, wxSize(200,12)) , 1 , wxALL ,0);
305 sizerH7 -> Add( _infoPixLe , 1 , wxALL ,0);
306 sizerH7 -> Add( new wxStaticText(this,-1,_T("Pix > isovalue: "), wxDefaultPosition, wxSize(200,12)) , 1 , wxALL ,0);
307 sizerH7 -> Add( _infoPixHi , 1 , wxALL ,0);
308 // sizerH7 -> SetMinSize(300, 120);
311 topsizer -> Add( sizerH0 ,1,wxALL|wxEXPAND ,0);
314 topsizer -> Add( sizerH2 ,1,wxALL|wxEXPAND ,0);
317 topsizer -> Add( sizerH3 ,1,wxALL|wxEXPAND ,0);
320 topsizer -> Add( sizerH4 ,1,wxALL ,0);
324 topsizer -> Add( sizerH5 ,1,wxALL |wxEXPAND ,0);
327 topsizer -> Add( sizerH6 , 1 , wxALL ,0);
330 topsizer -> Add( _histogrammeAccumulated ,1, wxALL ,0);
332 // Volumic information
333 topsizer -> Add( sizerH7 , 1 , wxALL|wxEXPAND ,0);
337 // wxBoxSizer *sizerHor = new wxBoxSizer(wxHORIZONTAL);
338 wxBoxSizer *sizerHor = new wxBoxSizer(wxVERTICAL);
339 sizerHor -> Add( topsizer , 1 , wxALL | wxEXPAND ,0);
342 wxWindow *panelPlotHistogramme = CreatePlotHistogrammeInterface();
343 sizerHor -> Add( panelPlotHistogramme , 1 , wxGROW ,0);
346 this->SetAutoLayout(true);
347 this->SetSizer( sizerHor );
350 // this->FitInside();
353 //-------------------------------------------------------------------
355 void wxPanelCuttingImageData::OnExtract(wxCommandEvent& event)
362 unsigned short *pOrg;
363 unsigned short *p_histogramme;
366 long int contAfter = 0;
367 long int contBefor = 0;
369 double min=999999999;
370 double max=-999999999;
372 volExt=_volExtern->GetValue();
373 volInt=_volIntern->GetValue();
374 int isoValue = _isoValue->GetValue();
375 int valueBeforeIsoValue = _valueBeforeIsoValue->GetValue();
376 int valueAfterIsoValue = _valueAfterIsoValue ->GetValue();
379 p_histogramme = (unsigned short*)_histogrammeVector->GetScalarPointer(0,0,0);
381 _imageData->GetDimensions(dim);
382 _imageData->GetSpacing(spc);
383 _actualCuttingModel->CalculeInversMatrix();
385 for (xx=0;xx<dim[0]; xx++)
387 for (yy=0;yy<dim[1]; yy++)
389 for (zz=0;zz<dim[2];zz++)
391 inside=_actualCuttingModel->IfPointInside(xx,yy,zz);
392 if ( ((inside==true)&&(volInt==true)) || ((!inside==true)&&(volExt==true)) )
394 pOrg=(unsigned short*)_imageData->GetScalarPointer (xx,yy,zz);
396 if ((*pOrg)<isoValue)
399 if (valueBeforeIsoValue!=-1)
401 *pOrg=valueBeforeIsoValue;
405 if (valueAfterIsoValue!=-1)
407 *pOrg=valueAfterIsoValue;
411 p_histogramme[*pOrg]++;
412 if (*pOrg<min) min=*pOrg;
413 if (*pOrg>max) max=*pOrg;
427 double volumeUnit = spc[0]*spc[1]*spc[2];
428 long int totalSubVolume = contBefor + contAfter;
429 double contBeforPorc = 100*(double)contBefor/(double)totalSubVolume;
430 double contAfterPorc = 100*(double)contAfter/(double)totalSubVolume;
431 infoToVo.Printf(_T("%dx%dx%d = %d"),dim[0],dim[1],dim[2], dim[0]*dim[1]*dim[2] );
432 infoSuVo.Printf(_T("%ld") , totalSubVolume);
433 infoSuVoA.Printf(_T("%.2f"), _actualCuttingModel->GetTheoricVolume() );
434 infoPixLe.Printf(_T("%ld pix. (%.2f %s) - %.2f mm^3"),contBefor, contBeforPorc ,_T("%"),contBefor*volumeUnit);
435 infoPixHi.Printf(_T("%ld pix. (%.2f %s) - %.2f mm^3"),contAfter, contAfterPorc ,_T("%"),contAfter*volumeUnit);
437 _infoToVo->SetLabel(infoToVo);
438 _infoSuVo->SetLabel(infoSuVo);
439 _infoSuVoA->SetLabel(infoSuVoA);
440 _infoPixLe->SetLabel(infoPixLe);
441 _infoPixHi->SetLabel(infoPixHi);
444 if ( _histogrammeAccumulated->GetValue()==true )
447 _histogrammeVector -> GetDimensions ( dimHist );
449 int i,size=dimHist[0];
450 for (i=1; i<=size; i++)
452 p_histogramme[i] = p_histogramme[i] + p_histogramme[i-1];
456 _histogrammeVector->Update();
457 _histogrammeVector->GetScalarRange(range);
458 _xyplot->SetYRange( 0 , range[1] );
459 _xyplot->SetXRange( min , max );
461 _vtkclipping3Ddataviewer->RefreshSurface();
462 _wxvtkbaseView->Refresh();
464 // _wxvtkbaseView->RefreshView();
465 wxCommandEvent newevent1(wxEVT_COMMAND_MENU_SELECTED,12121); // Refresh
466 _wxvtkbaseView->GetWxVTKRenderWindowInteractor()->GetParent()->ProcessEvent(newevent1);
470 //-------------------------------------------------------------------
471 void wxPanelCuttingImageData::OnTypeFig(wxCommandEvent& event)
473 _wxvtk3Dbaseview->GetRenderer()->RemoveActor( _actualActor );
475 if (_typeFig->GetSelection()==0){
476 _actualCuttingModel=_modelCylinder;
477 _actualActor=_cylinderActor;
479 if (_typeFig->GetSelection()==1){
480 _actualCuttingModel=_modelCube;
481 _actualActor=_cubeActor;
483 if (_typeFig->GetSelection()==2){
484 _actualCuttingModel=_modelSphere;
485 _actualActor=_sphereActor;
487 _wxvtk3Dbaseview->GetRenderer()->AddActor( _actualActor );
492 //-------------------------------------------------------------------
493 void wxPanelCuttingImageData::RefreshOpacity()
495 double op= _opacityFig->GetValue()/100.0;
496 _actualActor->GetProperty()->SetOpacity( op );
499 //-------------------------------------------------------------------
500 void wxPanelCuttingImageData::OnOpacityFig(wxScrollEvent& event)
505 //-------------------------------------------------------------------
506 void wxPanelCuttingImageData::RefreshView()
508 SetParamsOfTransformation( );
511 //-------------------------------------------------------------------
512 void wxPanelCuttingImageData::Refresh()
514 _wxvtk3Dbaseview->Refresh();
517 //-------------------------------------------------------------------
518 void wxPanelCuttingImageData::SetParamsOfTransformation( )
521 vtkImageData *vtkimagedata = _vtkmprbasedata->GetImageData();
522 vtkimagedata->GetSpacing(spc);
523 int px = (int) (_vtkmprbasedata->GetX() );
524 int py = (int) (_vtkmprbasedata->GetY() );
525 int pz = (int) (_vtkmprbasedata->GetZ() );
526 int sx = (int) (_scaleX->GetValue() * spc[0] );
527 int sy = (int) (_scaleY->GetValue() * spc[1] );
528 int sz = (int) (_scaleZ->GetValue() * spc[2] );
529 _actualCuttingModel -> SetScale ( sx , sy , sz );
530 _actualCuttingModel -> SetPosition ( px , py , pz );
531 _actualCuttingModel -> SetRotation ( _rotationX->GetValue() , _rotationY->GetValue() , _rotationZ->GetValue() );
532 _actualCuttingModel -> SetSpacing ( spc[0] , spc[1] , spc[2] );
533 _actualCuttingModel -> CalculeMatrix();
536 //-------------------------------------------------------------------
538 void wxPanelCuttingImageData::OnTransform(wxScrollEvent& event)
543 //-------------------------------------------------------------------
544 void wxPanelCuttingImageData::SetVtkMPRBaseData( vtkMPRBaseData *vtkmprbasedata )
546 _vtkmprbasedata = vtkmprbasedata;
547 _imageData = _vtkmprbasedata->GetImageData();
550 //-------------------------------------------------------------------
552 void wxPanelCuttingImageData::Configure()
554 _actualCuttingModel=_modelCylinder;
555 _actualActor=_cylinderActor;
556 _wxvtk3Dbaseview->GetRenderer()->AddActor( _actualActor );
557 SetParamsOfTransformation();