]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuShowNPoints.cxx
bbmaracasvisuShowNPoints.cxx
[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("Number of points: 0"));
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 //------------------------------------------------------------------------
144 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
145 {
146         if (this->renderer!=NULL){ 
147                 if (mpoint.size()==3){
148                         lstPointsX.push_back( mpoint[0] );
149                         lstPointsY.push_back( mpoint[1] );
150                         lstPointsZ.push_back( mpoint[2] );
151                         
152                         std::string strLabel = CleanSpaces(  (const char*) ( textCtrl->GetValue().mb_str() ) );
153                         
154                         lstLabels.push_back( strLabel );
155
156                         // Sphere
157                         vtkSphereSource *vtksphere              = vtkSphereSource::New();
158                         vtksphere->SetThetaResolution (20);
159                         vtksphere->SetPhiResolution (20);
160                         vtksphere->SetRadius( mradio  ); 
161                         vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
162                         sphereMapper->SetInput( vtksphere->GetOutput() );
163                         vtkActor *sphereActor   = vtkActor::New();
164                         sphereActor->SetMapper(sphereMapper);
165                         sphereActor->SetOrigin(0, 0, 0);
166                         double spc[3];
167                         mimage->GetSpacing(spc);
168                         sphereActor->SetPosition( spc[0]*mpoint[0] , spc[1]*mpoint[1] , spc[2]*mpoint[2] );
169                         sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
170                         sphereActor->GetProperty()->SetOpacity( mopacity );
171
172                         lstActorsSphere.push_back(sphereActor);
173                         renderer->AddActor( sphereActor );
174                         
175                         // Actor
176                         vtkTextActor3D *textActor = vtkTextActor3D::New();
177                         textActor->SetPosition(  mradio+spc[0]*mpoint[0] , spc[1]*mpoint[1] , spc[2]*mpoint[2] );
178                         textActor->SetInput( strLabel.c_str()  );
179                         renderer->AddActor( textActor );
180                         lstActorsText.push_back(textActor);
181
182                         SetOutputBox(); 
183                         
184                 } else {//mpoint.size
185                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
186                 }
187         } // renderer
188 }
189
190         
191         //------------------------------------------------------------------------
192         void WidgetShowNPoints::SetOutputBox()
193         {
194                 renderer->GetRenderWindow()->Render();
195                 wxString strTmp;
196                 strTmp.Printf("Number of points: %d", (int)(lstPointsX.size()) );
197                 txtNrPoints->SetLabel(  strTmp );
198                 
199                 //--BBTK
200                 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
201                 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
202                 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
203                 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
204                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));    
205                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));    
206                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));    
207                 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));    
208         }
209         
210         //------------------------------------------------------------------------
211         void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
212         {
213                 FILE *ff;
214                 wxString filename="/Users/davila/Desktop/PDL_2/bbs/test.xls";
215                 ff = fopen( (const char*) ( filename.mb_str() ) , "w+" );
216                 if (ff!=NULL)
217                 {
218                         int i , size = (int) (lstActorsSphere.size());
219                         fprintf(ff,"NumberOfPoints %d \n",size);
220                         fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
221                         int x, y, z;
222                         double value;
223                         for (i=0; i<size; i++)
224                         {
225                                 x=lstPointsX[i];
226                                 y=lstPointsY[i];
227                                 z=lstPointsZ[i];
228                                 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
229                                 fprintf(ff,"%d\t %d\t %d\t %f\t %s\n", x , y , z , value  , lstLabels[i].c_str());
230                         } // for
231                         fclose(ff);
232                 } else {   // else ff
233                         printf("WidgetShowNPoints::OnSavePoints  ...Error... creating file");
234                 } //ff
235                 
236         }               
237
238         //------------------------------------------------------------------------
239         void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
240         {
241         }               
242         
243         int WidgetShowNPoints::GetNearestPoint()
244         {
245                 int id=-1;
246                 int i, size=(int)(lstActorsSphere.size());
247                 double spc[3];
248                 mimage->GetSpacing(spc);
249                 
250                 for ( i=0  ; i<size; i++ )
251                 {
252                         double rx =  spc[0]*(mpoint[0] - lstPointsX [i]);
253                         double ry =  spc[1]*(mpoint[1] - lstPointsY [i]);
254                         double rz =  spc[2]*(mpoint[2] - lstPointsZ [i]);
255                         if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
256                         {
257                                 id=i;
258                         }       // if           
259                 } // for
260                 
261                 return id;
262         }
263         
264         //------------------------------------------------------------------------
265         void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
266         {
267                 int id=GetNearestPoint();
268                 if (id>=0)
269                 {
270                         std::string strLabel = CleanSpaces(  (const char*) ( textCtrl->GetValue().mb_str() ) );
271                         lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
272                         lstActorsText[id]->SetInput(  lstLabels[id].c_str() );
273                         SetOutputBox(); 
274                 }
275         }               
276         
277         //------------------------------------------------------------------------
278         void WidgetShowNPoints::ErasePoint(int id)
279         {
280                 if (this->renderer!=NULL){ 
281                         if (id>=0){
282                                 renderer->RemoveActor( lstActorsSphere[id] );
283                                 renderer->RemoveActor( lstActorsText[id] );
284                                  lstActorsSphere[id]->Delete();
285                                  lstActorsText[id]->Delete();
286                                 lstActorsSphere.erase( lstActorsSphere.begin()+id );
287                                 lstActorsText.erase( lstActorsText.begin()+id );
288                                 
289                                 lstPointsX.erase( lstPointsX.begin()+id );
290                                 lstPointsY.erase( lstPointsY.begin()+id );
291                                 lstPointsZ.erase( lstPointsZ.begin()+id );
292                                 lstLabels.erase( lstLabels.begin()+id );
293
294                                 SetOutputBox(); 
295
296                         } // if id
297                 } // if renderer
298         }
299         
300         //------------------------------------------------------------------------
301         void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
302         {
303                 ErasePoint( GetNearestPoint() );
304         }
305         
306         //------------------------------------------------------------------------
307         void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
308         {
309                 ErasePoint(lstActorsSphere.size()-1);
310         }
311         
312         
313 //------------------------------------------------------------------------
314 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
315 {
316         int id,size=lstActorsSphere.size();
317         for (id=size-1;id>=0;id--)
318         {
319                 ErasePoint(id);
320         }
321                 
322 }
323
324
325
326 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
327 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
328
329 void ShowNPoints::Process()
330 {
331         if (mwxwidget!=NULL){
332                 mwxwidget->SetRenderer( bbGetInputRenderer() );   
333                 mwxwidget->SetPoint( bbGetInputIn() );   
334                 mwxwidget->SetImage( bbGetInputImage() );   
335                 mwxwidget->SetColour( bbGetInputColour() );   
336                 mwxwidget->SetOpacity( bbGetInputOpacity() );   
337                 mwxwidget->SetRadio( bbGetInputRadio() );   
338
339                 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
340                 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
341                 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
342                 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
343         } // mwxwidget
344 }
345
346
347 void ShowNPoints::CreateWidget(wxWindow* parent)
348 {
349         mwxwidget = new WidgetShowNPoints( parent ,  this);
350         mwxwidget->SetPoint( bbGetInputIn() );
351
352         if (bbGetInputImage()==NULL)
353         {
354                 printf("Missing Image  (ShowNPoints) \n");
355         }
356
357    bbSetOutputWidget( mwxwidget ); 
358 }
359
360 void ShowNPoints::bbUserSetDefaultValues()
361 {
362         mwxwidget = NULL;
363
364         bbSetInputRadio(0.5);
365         bbSetInputOpacity(1);
366         
367         std::vector<double> colour;
368         colour.push_back(1.0);
369         colour.push_back(0.0);
370         colour.push_back(0.0);
371         bbSetInputColour(colour);
372
373         bbSetInputImage(NULL);
374         bbSetInputRenderer(NULL);
375 }
376         
377         //-----------------------------------------------------------------     
378         void ShowNPoints::bbUserInitializeProcessing()
379         {
380         }
381         
382         //-----------------------------------------------------------------     
383         void ShowNPoints::bbUserFinalizeProcessing()
384         {
385         }
386         
387         //-----------------------------------------------------------------     
388
389 }
390 // EO namespace bbcreaMaracasVisu
391
392