]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuShowNPoints.cxx
NTU: Added functional Radio and Opacity sliders for showNpoints
[creaMaracasVisu.git] / bbtk / src / bbmaracasvisuShowNPoints.cxx
1 #include "bbmaracasvisuShowNPoints.h"
2 #include "bbcreaMaracasVisuPackage.h"
3
4 #include "vtkProperty.h"
5 #include "vtkSphereSource.h"
6 #include "vtkPolyDataMapper.h"
7 #include "vtkRenderWindow.h"
8 #include "vtkTextActor3D.h"
9
10 namespace bbcreaMaracasVisu
11 {
12
13
14 //----------------------------------------------------------------------
15         WidgetShowNPoints::WidgetShowNPoints(wxWindow *parent,  bbcreaMaracasVisu::ShowNPoints *box)
16     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
17   {
18     mbbShowNPoints                              = box;
19     this->renderer                              = NULL;
20         wxPanel *panel                          = this;
21     wxSizer *sizer                              = NULL;
22
23
24         // Widget interface
25         textCtrl                                                = new wxTextCtrl(panel, -1);
26         wxButton *btnAddPoint                   = new wxButton( panel, -1, _T("Add Point"));
27         wxButton *btnRenamePoint                = new wxButton( panel, -1, _T("Rename point"));
28         wxButton *btnEraseLastPoint             = new wxButton( panel, -1, _T("Erase Last point"));
29         wxButton *btnErasePoint                 = new wxButton( panel, -1, _T("Erase point"));
30         wxButton *btnDeleteAllPoints    = new wxButton( panel, -1, _T("Delete all points"));
31         wxButton *btnSavePoints                 = new wxButton( panel, -1, _T("Save points"));
32         wxButton *btnLoadPoints                 = new wxButton( panel, -1, _T("Load points"));
33         txtNrPoints                                             = new wxStaticText(panel,-1, _T(" "));
34
35         //NTU: Sliders for opacity and radio change
36
37         wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T("  Points Opacity  ")));
38         sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
39
40         wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T("  Points Radio  ")));
41         sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
42
43         wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
44 //    sizer1->Add(new wxStaticText(panel,-1,_T("  ")));
45
46           Connect(btnAddPoint->GetId()                  , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
47           Connect(btnRenamePoint->GetId()               , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
48           Connect(btnEraseLastPoint->GetId()    , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
49           Connect(btnErasePoint->GetId()                , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
50           Connect(btnDeleteAllPoints->GetId()   , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
51           Connect(btnSavePoints->GetId()                , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
52           Connect(btnLoadPoints->GetId()                , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
53
54           //NTU: Slider events
55           Connect(sdrOpacity->GetId()                   , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
56           Connect(sdrRadio->GetId()                             , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
57
58           sizer1->Add(textCtrl);
59           sizer1->Add(btnAddPoint);
60           sizer1->Add(btnRenamePoint);
61           sizer1->Add(btnErasePoint);
62           sizer1->Add(btnEraseLastPoint);
63           sizer1->Add(btnDeleteAllPoints);
64           sizer1->Add(txtNrPoints);
65           sizer1->Add(txOpacity);
66           sizer1->Add(sdrOpacity);
67           sizer1->Add(txRadio);
68           sizer1->Add(sdrRadio);
69           sizer1->Add(btnSavePoints);
70           sizer1->Add(btnLoadPoints);
71
72           sizer = sizer1;
73           panel ->      SetSizer(sizer);
74           panel ->      SetAutoLayout(true);
75           panel ->      Layout();
76
77 }
78
79
80 //------------------------------------------------------------------------
81 WidgetShowNPoints::~WidgetShowNPoints()
82 {
83 }
84
85 void WidgetShowNPoints::SetRadio(double radio)
86 {
87         this->mradio=radio;
88         //NTU: For Slider
89         sdrRadio->SetValue(this->mradio);
90 }
91
92 //------------------------------------------------------------------------
93 std::vector<int> WidgetShowNPoints::GetLstPointsX()
94 {
95         return lstPointsX;
96 }
97
98 //------------------------------------------------------------------------
99 std::vector<int> WidgetShowNPoints::GetLstPointsY()
100 {
101         return lstPointsY;
102 }
103
104 //------------------------------------------------------------------------
105 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
106 {
107         return lstPointsZ;
108 }
109
110 //------------------------------------------------------------------------
111         std::vector<std::string> WidgetShowNPoints::GetLstLabels()
112 {
113         return lstLabels;
114 }
115
116
117
118
119 //------------------------------------------------------------------------
120 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
121 {
122         mpoint = ppoint;
123 }
124
125 //------------------------------------------------------------------------
126 void WidgetShowNPoints::SetColour(std::vector<double> colour)
127 {
128         this->mcolour = colour;
129 }
130
131 //------------------------------------------------------------------------
132 void WidgetShowNPoints::SetOpacity(double opacity)
133 {
134         this->mopacity=opacity;
135         //NTU: For Slider
136         sdrOpacity->SetValue(this->mopacity*100.0);
137 }
138
139 //------------------------------------------------------------------------
140 void WidgetShowNPoints::SetImage(vtkImageData *image)
141 {
142         this->mimage=image;
143 }
144
145 //------------------------------------------------------------------------
146 void  WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
147 {
148         this->renderer  = renderer;
149 }
150
151
152 std::string WidgetShowNPoints::CleanSpaces(std::string ss)
153 {
154         int i;
155         while( (i=ss.find(32))>=0 )
156         {
157                 ss.replace(i,1,"_");
158         }
159         return ss;
160 }
161
162 //------------------------------------------------------------------------
163 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
164 {
165         lstPointsX.push_back( x );
166         lstPointsY.push_back( y );
167         lstPointsZ.push_back( z );
168
169         std::string strLabel = CleanSpaces(  label );
170
171         lstLabels.push_back( strLabel );
172
173         // Sphere
174         vtkSphereSource *vtksphere              = vtkSphereSource::New();
175         vtksphere->SetThetaResolution (20);
176         vtksphere->SetPhiResolution (20);
177         vtksphere->SetRadius( mradio  );
178
179         //NTU: For updating points
180
181         lstSourceSphere.push_back(vtksphere);
182
183         vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
184         sphereMapper->SetInput( vtksphere->GetOutput() );
185         vtkActor *sphereActor   = vtkActor::New();
186         sphereActor->SetMapper(sphereMapper);
187         sphereActor->SetOrigin(0, 0, 0);
188         double spc[3];
189         if(mimage==NULL){
190                 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
191                 dialog.ShowModal();
192                 return;
193         }
194         mimage->GetSpacing(spc);
195         sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
196         sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
197         sphereActor->GetProperty()->SetOpacity( mopacity );
198
199         lstActorsSphere.push_back(sphereActor);
200         if(renderer==NULL){
201                 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
202                 dialog.ShowModal();             
203                 return;
204         }
205         renderer->AddActor( sphereActor );
206
207         // Actor
208         vtkTextActor3D *textActor = vtkTextActor3D::New();
209         textActor->SetPosition(  mradio+spc[0]*x , spc[1]*y , spc[2]*z );
210         textActor->SetInput( strLabel.c_str()  );
211         renderer->AddActor( textActor );
212         lstActorsText.push_back(textActor);
213
214         SetOutputBox();
215
216 }
217
218 //------------------------------------------------------------------------
219 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
220 {
221         if (this->renderer!=NULL){
222                 if (mpoint.size()==3){
223                         AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
224                 } else {//mpoint.size
225                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
226                 }
227         } // renderer
228 }
229
230
231         //------------------------------------------------------------------------
232         void WidgetShowNPoints::SetOutputBox()
233         {
234                 renderer->GetRenderWindow()->Render();
235                 wxString strTmp;
236                 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
237                 txtNrPoints->SetLabel(  strTmp );
238
239                 //--BBTK
240                 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
241                 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
242                 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
243                 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
244                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
245                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
246                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
247                 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
248         }
249
250         //------------------------------------------------------------------------
251         void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
252         {
253
254                 wxFileDialog* FD = new wxFileDialog( 0,
255                                                                                         _T("Save points .."),
256                                                                                         _T(""),
257                                                                                         _T(""),
258                                                                                         _T("(*.xls)|*.xls"),
259                                                                                          wxSAVE | wxOVERWRITE_PROMPT,
260                                                                                         wxDefaultPosition);
261                 //EED
262
263                 int result_FD = FD->ShowModal();
264
265                 // This line is need it by windows //EED
266                 FD->SetReturnCode( result_FD );
267
268                 if (FD->GetReturnCode()==wxID_OK)
269                 {
270                         FILE *ff;
271                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
272                         ff = fopen( filename.c_str() , "w+" );
273                         if (ff!=NULL)
274                         {
275                                 int i , size = (int) (lstActorsSphere.size());
276                                 fprintf(ff,"NumberOfPoints %d \n",size);
277                                 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
278                                 int x, y, z;
279                                 double value;
280                                 for (i=0; i<size; i++)
281                                 {
282                                         x=lstPointsX[i];
283                                         y=lstPointsY[i];
284                                         z=lstPointsZ[i];
285                                         value= mimage->GetScalarComponentAsDouble(x,y,z,0);
286                                         fprintf(ff,"%d\t %d\t %d\t %f\t %s\n", x , y , z , value  , lstLabels[i].c_str());
287                                 } // for
288                                 fclose(ff);
289                         } else {   // else ff
290                                 printf("WidgetShowNPoints::OnSavePoints  ...Error... creating file");
291                         } //ff
292                 }       // dialog box
293
294         }
295
296         //------------------------------------------------------------------------
297         void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
298         {
299                 wxFileDialog* FD = new wxFileDialog( 0,
300                                                                                         _T("Load points .."),
301                                                                                         _T(""),
302                                                                                         _T(""),
303                                                                                         _T("(*.xls)|*.xls"),
304                                                                                         wxOPEN | wxFILE_MUST_EXIST,
305                                                                                         wxDefaultPosition);
306                 //EED
307
308                 int result_FD = FD->ShowModal();
309
310                 // This line is need it by windows //EED
311                 FD->SetReturnCode( result_FD );
312
313                 if (FD->GetReturnCode()==wxID_OK)
314                 {
315                         FILE *ff;
316                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
317                         ff = fopen( filename.c_str() , "r+" );
318                         if (ff!=NULL)
319                         {
320                                 int i,size;
321                                 char chartmp[256];
322                                 fscanf(ff," %s %d",chartmp,&size);
323                                 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
324
325                                 float value;
326                                 int x,y,z;
327                                 for (i=0; i<size; i++)
328                                 {
329                                         fscanf(ff," %d %d %d %f %s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
330                                         AddPoint(x,y,z, chartmp );
331                                 }
332                                 fclose(ff);
333                         } else {   // else ff
334                                 printf("WidgetShowNPoints::OnLoadPoints  ...Error... reading file");
335                         } //ff
336                 }       // dialog box
337
338
339         }
340
341         int WidgetShowNPoints::GetNearestPoint()
342         {
343                 int id=-1;
344                 int i, size=(int)(lstActorsSphere.size());
345                 double spc[3];
346
347                 if(mimage ==NULL){                      
348                         wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
349                         diag->ShowModal();
350                         return -1;
351                 }else{
352
353                         mimage->GetSpacing(spc);
354
355                         for ( i=0  ; i<size; i++ )
356                         {
357                                 double rx =  spc[0]*(mpoint[0] - lstPointsX [i]);
358                                 double ry =  spc[1]*(mpoint[1] - lstPointsY [i]);
359                                 double rz =  spc[2]*(mpoint[2] - lstPointsZ [i]);
360                                 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
361                                 {
362                                         id=i;
363                                 }       // if
364                         } // for
365
366                         return id;
367                 }
368         }
369
370         //------------------------------------------------------------------------
371         void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
372         {
373                 int id=GetNearestPoint();
374                 if (id>=0)
375                 {
376                         std::string strLabel = CleanSpaces(  (const char*) ( textCtrl->GetValue().mb_str() ) );
377                         lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
378                         lstActorsText[id]->SetInput(  lstLabels[id].c_str() );
379                         SetOutputBox();
380                 }
381         }
382
383         //------------------------------------------------------------------------
384         void WidgetShowNPoints::ErasePoint(int id)
385         {
386                 if (this->renderer!=NULL){
387                         if (id>=0){
388                                 renderer->RemoveActor( lstActorsSphere[id] );
389                                 renderer->RemoveActor( lstActorsText[id] );
390                                  lstActorsSphere[id]->Delete();
391                                  lstActorsText[id]->Delete();
392                                  lstSourceSphere[id]->Delete();
393                                 lstActorsSphere.erase( lstActorsSphere.begin()+id );
394                                 lstActorsText.erase( lstActorsText.begin()+id );
395                                 lstSourceSphere.erase( lstSourceSphere.begin()+id );
396
397                                 lstPointsX.erase( lstPointsX.begin()+id );
398                                 lstPointsY.erase( lstPointsY.begin()+id );
399                                 lstPointsZ.erase( lstPointsZ.begin()+id );
400                                 lstLabels.erase( lstLabels.begin()+id );
401
402                                 SetOutputBox();
403
404                         } // if id
405                 } // if renderer
406         }
407
408         //------------------------------------------------------------------------
409         void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
410         {
411                 ErasePoint( GetNearestPoint() );
412         }
413
414         //------------------------------------------------------------------------
415         void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
416         {
417                 ErasePoint(lstActorsSphere.size()-1);
418         }
419
420
421 //------------------------------------------------------------------------
422 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
423 {
424         int id,size=lstActorsSphere.size();
425         for (id=size-1;id>=0;id--)
426         {
427                 ErasePoint(id);
428         }
429
430 }
431
432 //NTU: Method for updating points opacity and Radio
433
434 //------------------------------------------------------------------------
435 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
436 {
437         //Difference in Radio for text placement
438         int difradio = sdrRadio->GetValue() - this->mradio;
439         
440         this->mopacity = sdrOpacity->GetValue()/100.0;
441         this->mradio = sdrRadio->GetValue();
442         
443         //NTU refresh the inputs
444         mbbShowNPoints->bbSetInputOpacity(this->mopacity);
445         mbbShowNPoints->bbSetInputRadio(this->mradio);
446
447         int size = (int) this->lstActorsSphere.size();
448         int i;
449         for(i = 0; i < size; i ++)
450         {
451                 this->lstSourceSphere[i]->SetRadius(mradio);
452                 this->lstActorsSphere[i]->GetProperty()->SetOpacity(mopacity);
453                 this->lstActorsText[i]->SetPosition(this->lstActorsText[i]->GetPosition()[0]+difradio,this->lstActorsText[i]->GetPosition()[1], this->lstActorsText[i]->GetPosition()[2]);
454         }
455
456         //NTU: For updating
457         SetOutputBox();
458 }
459
460
461
462 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
463 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
464
465 void ShowNPoints::Process()
466 {
467         if (mwxwidget!=NULL){
468                 mwxwidget->SetRenderer( bbGetInputRenderer() );
469                 mwxwidget->SetPoint( bbGetInputIn() );
470                 mwxwidget->SetImage( bbGetInputImage() );
471                 mwxwidget->SetColour( bbGetInputColour() );
472                 mwxwidget->SetOpacity( bbGetInputOpacity() );
473                 mwxwidget->SetRadio( bbGetInputRadio() );
474
475                 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
476                 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
477                 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
478                 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
479         } // mwxwidget
480 }
481
482
483 void ShowNPoints::CreateWidget(wxWindow* parent)
484 {
485         mwxwidget = new WidgetShowNPoints( parent ,  this);
486         mwxwidget->SetPoint( bbGetInputIn() );
487
488         if (bbGetInputImage()==NULL)
489         {
490                 printf("Missing Image  (ShowNPoints) \n");
491         }
492
493    bbSetOutputWidget( mwxwidget );
494 }
495
496 void ShowNPoints::bbUserSetDefaultValues()
497 {
498         mwxwidget = NULL;
499
500         bbSetInputRadio(1);
501         bbSetInputOpacity(1);
502
503         std::vector<double> colour;
504         colour.push_back(1.0);
505         colour.push_back(0.0);
506         colour.push_back(0.0);
507         bbSetInputColour(colour);
508
509         bbSetInputImage(NULL);
510         bbSetInputRenderer(NULL);
511 }
512
513         //-----------------------------------------------------------------
514         void ShowNPoints::bbUserInitializeProcessing()
515         {
516         }
517
518         //-----------------------------------------------------------------
519         void ShowNPoints::bbUserFinalizeProcessing()
520         {
521         }
522
523         //-----------------------------------------------------------------
524
525 }
526 // EO namespace bbcreaMaracasVisu
527
528