]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuShowNPoints.cxx
ViewerNV 2D with color Transfer Function
[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         if(mimage==NULL){
165                 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
166                 dialog.ShowModal();
167                 return;
168         }
169         mimage->GetSpacing(spc);
170         sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
171         sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
172         sphereActor->GetProperty()->SetOpacity( mopacity );
173
174         lstActorsSphere.push_back(sphereActor);
175         if(renderer==NULL){
176                 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
177                 dialog.ShowModal();             
178                 return;
179         }
180         renderer->AddActor( sphereActor );
181
182         // Actor
183         vtkTextActor3D *textActor = vtkTextActor3D::New();
184         textActor->SetPosition(  mradio+spc[0]*x , spc[1]*y , spc[2]*z );
185         textActor->SetInput( strLabel.c_str()  );
186         renderer->AddActor( textActor );
187         lstActorsText.push_back(textActor);
188
189         SetOutputBox();
190
191 }
192
193 //------------------------------------------------------------------------
194 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
195 {
196         if (this->renderer!=NULL){
197                 if (mpoint.size()==3){
198                         AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
199                 } else {//mpoint.size
200                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
201                 }
202         } // renderer
203 }
204
205
206         //------------------------------------------------------------------------
207         void WidgetShowNPoints::SetOutputBox()
208         {
209                 renderer->GetRenderWindow()->Render();
210                 wxString strTmp;
211                 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
212                 txtNrPoints->SetLabel(  strTmp );
213
214                 //--BBTK
215                 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
216                 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
217                 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
218                 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
219                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
220                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
221                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
222                 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
223         }
224
225         //------------------------------------------------------------------------
226         void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
227         {
228
229                 wxFileDialog* FD = new wxFileDialog( 0,
230                                                                                         _T("Save points .."),
231                                                                                         _T(""),
232                                                                                         _T(""),
233                                                                                         _T("(*.xls)|*.xls"),
234                                                                                          wxSAVE | wxOVERWRITE_PROMPT,
235                                                                                         wxDefaultPosition);
236                 //EED
237
238                 int result_FD = FD->ShowModal();
239
240                 // This line is need it by windows //EED
241                 FD->SetReturnCode( result_FD );
242
243                 if (FD->GetReturnCode()==wxID_OK)
244                 {
245                         FILE *ff;
246                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
247                         ff = fopen( filename.c_str() , "w+" );
248                         if (ff!=NULL)
249                         {
250                                 int i , size = (int) (lstActorsSphere.size());
251                                 fprintf(ff,"NumberOfPoints %d \n",size);
252                                 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
253                                 int x, y, z;
254                                 double value;
255                                 for (i=0; i<size; i++)
256                                 {
257                                         x=lstPointsX[i];
258                                         y=lstPointsY[i];
259                                         z=lstPointsZ[i];
260                                         value= mimage->GetScalarComponentAsDouble(x,y,z,0);
261                                         fprintf(ff,"%d\t %d\t %d\t %f\t %s\n", x , y , z , value  , lstLabels[i].c_str());
262                                 } // for
263                                 fclose(ff);
264                         } else {   // else ff
265                                 printf("WidgetShowNPoints::OnSavePoints  ...Error... creating file");
266                         } //ff
267                 }       // dialog box
268
269         }
270
271         //------------------------------------------------------------------------
272         void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
273         {
274                 wxFileDialog* FD = new wxFileDialog( 0,
275                                                                                         _T("Load points .."),
276                                                                                         _T(""),
277                                                                                         _T(""),
278                                                                                         _T("(*.xls)|*.xls"),
279                                                                                         wxOPEN | wxFILE_MUST_EXIST,
280                                                                                         wxDefaultPosition);
281                 //EED
282
283                 int result_FD = FD->ShowModal();
284
285                 // This line is need it by windows //EED
286                 FD->SetReturnCode( result_FD );
287
288                 if (FD->GetReturnCode()==wxID_OK)
289                 {
290                         FILE *ff;
291                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
292                         ff = fopen( filename.c_str() , "r+" );
293                         if (ff!=NULL)
294                         {
295                                 int i,size;
296                                 char chartmp[256];
297                                 fscanf(ff," %s %d",chartmp,&size);
298                                 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
299
300                                 float value;
301                                 int x,y,z;
302                                 for (i=0; i<size; i++)
303                                 {
304                                         fscanf(ff," %d %d %d %f %s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
305                                         AddPoint(x,y,z, chartmp );
306                                 }
307                                 fclose(ff);
308                         } else {   // else ff
309                                 printf("WidgetShowNPoints::OnLoadPoints  ...Error... reading file");
310                         } //ff
311                 }       // dialog box
312
313
314         }
315
316         int WidgetShowNPoints::GetNearestPoint()
317         {
318                 int id=-1;
319                 int i, size=(int)(lstActorsSphere.size());
320                 double spc[3];
321
322                 if(mimage ==NULL){                      
323                         wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
324                         diag->ShowModal();
325                         return -1;
326                 }else{
327
328                         mimage->GetSpacing(spc);
329
330                         for ( i=0  ; i<size; i++ )
331                         {
332                                 double rx =  spc[0]*(mpoint[0] - lstPointsX [i]);
333                                 double ry =  spc[1]*(mpoint[1] - lstPointsY [i]);
334                                 double rz =  spc[2]*(mpoint[2] - lstPointsZ [i]);
335                                 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
336                                 {
337                                         id=i;
338                                 }       // if
339                         } // for
340
341                         return id;
342                 }
343         }
344
345         //------------------------------------------------------------------------
346         void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
347         {
348                 int id=GetNearestPoint();
349                 if (id>=0)
350                 {
351                         std::string strLabel = CleanSpaces(  (const char*) ( textCtrl->GetValue().mb_str() ) );
352                         lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
353                         lstActorsText[id]->SetInput(  lstLabels[id].c_str() );
354                         SetOutputBox();
355                 }
356         }
357
358         //------------------------------------------------------------------------
359         void WidgetShowNPoints::ErasePoint(int id)
360         {
361                 if (this->renderer!=NULL){
362                         if (id>=0){
363                                 renderer->RemoveActor( lstActorsSphere[id] );
364                                 renderer->RemoveActor( lstActorsText[id] );
365                                  lstActorsSphere[id]->Delete();
366                                  lstActorsText[id]->Delete();
367                                 lstActorsSphere.erase( lstActorsSphere.begin()+id );
368                                 lstActorsText.erase( lstActorsText.begin()+id );
369
370                                 lstPointsX.erase( lstPointsX.begin()+id );
371                                 lstPointsY.erase( lstPointsY.begin()+id );
372                                 lstPointsZ.erase( lstPointsZ.begin()+id );
373                                 lstLabels.erase( lstLabels.begin()+id );
374
375                                 SetOutputBox();
376
377                         } // if id
378                 } // if renderer
379         }
380
381         //------------------------------------------------------------------------
382         void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
383         {
384                 ErasePoint( GetNearestPoint() );
385         }
386
387         //------------------------------------------------------------------------
388         void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
389         {
390                 ErasePoint(lstActorsSphere.size()-1);
391         }
392
393
394 //------------------------------------------------------------------------
395 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
396 {
397         int id,size=lstActorsSphere.size();
398         for (id=size-1;id>=0;id--)
399         {
400                 ErasePoint(id);
401         }
402
403 }
404
405
406
407 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
408 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
409
410 void ShowNPoints::Process()
411 {
412         if (mwxwidget!=NULL){
413                 mwxwidget->SetRenderer( bbGetInputRenderer() );
414                 mwxwidget->SetPoint( bbGetInputIn() );
415                 mwxwidget->SetImage( bbGetInputImage() );
416                 mwxwidget->SetColour( bbGetInputColour() );
417                 mwxwidget->SetOpacity( bbGetInputOpacity() );
418                 mwxwidget->SetRadio( bbGetInputRadio() );
419
420                 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
421                 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
422                 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
423                 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
424         } // mwxwidget
425 }
426
427
428 void ShowNPoints::CreateWidget(wxWindow* parent)
429 {
430         mwxwidget = new WidgetShowNPoints( parent ,  this);
431         mwxwidget->SetPoint( bbGetInputIn() );
432
433         if (bbGetInputImage()==NULL)
434         {
435                 printf("Missing Image  (ShowNPoints) \n");
436         }
437
438    bbSetOutputWidget( mwxwidget );
439 }
440
441 void ShowNPoints::bbUserSetDefaultValues()
442 {
443         mwxwidget = NULL;
444
445         bbSetInputRadio(1);
446         bbSetInputOpacity(1);
447
448         std::vector<double> colour;
449         colour.push_back(1.0);
450         colour.push_back(0.0);
451         colour.push_back(0.0);
452         bbSetInputColour(colour);
453
454         bbSetInputImage(NULL);
455         bbSetInputRenderer(NULL);
456 }
457
458         //-----------------------------------------------------------------
459         void ShowNPoints::bbUserInitializeProcessing()
460         {
461         }
462
463         //-----------------------------------------------------------------
464         void ShowNPoints::bbUserFinalizeProcessing()
465         {
466         }
467
468         //-----------------------------------------------------------------
469
470 }
471 // EO namespace bbcreaMaracasVisu
472
473