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