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