]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuShowNPoints.cxx
2392 creaMaracasVisu Feature New Normal InsertPoint in showNPoints 2014-06-13 11:13
[creaMaracasVisu.git] / bbtk / src / bbmaracasvisuShowNPoints.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "bbmaracasvisuShowNPoints.h"
27 #include "bbcreaMaracasVisuPackage.h"
28
29 #include "vtkProperty.h"
30 #include "vtkSphereSource.h"
31 #include "vtkPolyDataMapper.h"
32 #include "vtkRenderWindow.h"
33 #include "vtkTextActor3D.h"
34 #include <vtkTextProperty.h>
35
36 namespace bbcreaMaracasVisu
37 {
38
39 //----------------------------------------------------------------------
40   WidgetShowNPoints::WidgetShowNPoints(wxWindow *parent,  bbcreaMaracasVisu::ShowNPoints *box)
41     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
42   {
43     mbbShowNPoints                                      = box;
44     this->renderer                                      = NULL;
45     wxPanel     *panel                                  = this;
46     wxSizer *sizer                                      = NULL;
47
48         if (mbbShowNPoints->bbGetInputType()==0)
49         {
50                 // Widget interface
51                 askPointLabel                                   = new wxStaticText(panel, -1, _T("Point label :")); // JPR
52                 textCtrl                                                = new wxTextCtrl(panel, -1);
53                 wxButton *btnAddPoint                   = new wxButton( panel, -1, _T("Add Point"));
54                 wxButton *btnInsertPoint                        = new wxButton( panel, -1, _T("Insert Point"));//CFT
55                 wxButton *btnSetPositionPoint   = new wxButton( panel, -1, _T("Set nearest point"));
56                 wxButton *btnRenamePoint                = new wxButton( panel, -1, _T("Rename point"));
57                 wxButton *btnEraseLastPoint             = new wxButton( panel, -1, _T("Erase Last point"));
58                 wxButton *btnErasePoint                 = new wxButton( panel, -1, _T("Erase point"));
59                 wxButton *btnDeleteAllPoints    = new wxButton( panel, -1, _T("Delete all points"));
60                 wxButton *btnSavePoints                 = new wxButton( panel, -1, _T("Save points"));
61                 wxButton *btnLoadPoints                 = new wxButton( panel, -1, _T("Load points"));
62                 txtNrPoints                                             = new wxStaticText(panel,-1, _T(" "));
63
64                 //NTU: Sliders for opacity and radio change
65                 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T("  Points Opacity  ")));
66                 sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
67                 wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T("  Points Radio  ")));
68                 sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
69
70                 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
71                 //    sizer1->Add(new wxStaticText(panel,-1,_T("  ")));
72
73                 Connect(btnAddPoint->GetId()            , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
74                 Connect(btnInsertPoint->GetId()         , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnInsertPoint);//CFT
75                 Connect(btnSetPositionPoint->GetId(), wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
76                 Connect(btnRenamePoint->GetId()         , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
77                 Connect(btnEraseLastPoint->GetId()      , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
78                 Connect(btnErasePoint->GetId()          , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
79                 Connect(btnDeleteAllPoints->GetId()     , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
80                 Connect(btnSavePoints->GetId()          , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
81                 Connect(btnLoadPoints->GetId()          , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
82
83                 //NTU: Slider events
84                 Connect(sdrOpacity->GetId()                     , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
85                 Connect(sdrRadio->GetId()                       , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
86
87                 sizer1->Add(askPointLabel); // JPR
88                 sizer1->Add(textCtrl);
89                 sizer1->Add(btnAddPoint);
90                 sizer1->Add(btnInsertPoint);//CFT
91                 sizer1->Add(btnSetPositionPoint);
92                 sizer1->Add(btnRenamePoint);
93                 sizer1->Add(btnErasePoint);
94                 sizer1->Add(btnEraseLastPoint);
95                 sizer1->Add(btnDeleteAllPoints);
96                 sizer1->Add(txtNrPoints);
97                 sizer1->Add(txOpacity);
98                 sizer1->Add(sdrOpacity,1,wxGROW );
99                 sizer1->Add(txRadio);
100                 sizer1->Add(sdrRadio,1,wxGROW );
101                 sizer1->Add(btnSavePoints);
102                 sizer1->Add(btnLoadPoints);
103                 sizer = sizer1;
104         }
105
106           if (mbbShowNPoints->bbGetInputType()==1)
107           {
108                   // Widget interface
109                   wxButton *btnSetPositionPoint         = new wxButton( panel, -1, _T("Set nearest point"));
110                   txtNrPoints                                           = new wxStaticText(panel,-1, _T(" "));
111
112                   //NTU: Sliders for opacity and radio change
113                   wxStaticText* txOpacity               = new wxStaticText(this, -1, wxString(_T("  Points Opacity  ")));
114                   sdrOpacity                                    = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
115                   wxStaticText* txRadio                 = new wxStaticText(this, -1, wxString(_T("  Points Radio  ")));
116                   sdrRadio                                              = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
117                   wxFlexGridSizer *sizer1               = new wxFlexGridSizer(1);
118                   Connect(btnSetPositionPoint->GetId()  , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
119                   Connect(sdrOpacity->GetId()   , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
120                   Connect(sdrRadio->GetId()     , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
121
122                   sizer1->Add(btnSetPositionPoint);
123                   sizer1->Add(txtNrPoints);
124                   sizer1->Add(txOpacity);
125                   sizer1->Add(sdrOpacity,1,wxGROW );
126                   sizer1->Add(txRadio);
127                   sizer1->Add(sdrRadio,1,wxGROW );
128                   sizer = sizer1;
129           }
130     
131         if (mbbShowNPoints->bbGetInputType() == 2) {
132             askPointLabel = new wxStaticText(panel, -1, _T("\nPOINT CONTROLS:")); // JPR
133             
134             wxButton *btnAddPoint = new wxButton(panel, -1,        _T("      Add  Point      "));
135             wxButton *btnDeleteAllPoints = new wxButton(panel, -1, _T("      Delete All      "));
136             wxStaticText* spacer = new wxStaticText(panel, -1, _T("\n")); // JPR
137             textCtrl = new wxTextCtrl(panel, -1);
138             
139             wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
140
141             Connect(btnAddPoint->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) & WidgetShowNPoints::OnAddPoint);
142             Connect(btnDeleteAllPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) & WidgetShowNPoints::OnDeleteAllPoints);
143             
144             sizer1->Add(askPointLabel); 
145             sizer1->Add(btnAddPoint);
146             sizer1->Add(btnDeleteAllPoints);
147             sizer1->Add(spacer);
148             sizer1->Add(textCtrl);
149             
150             sdrOpacity = new wxSlider();
151             sdrRadio = new wxSlider();
152             
153             txtNrPoints = new wxStaticText(panel, -1, _T("\n\n\n"));  
154             
155             sizer = sizer1;
156         } 
157           if (sizer!=NULL)
158           {
159                   panel->SetSizer(sizer);
160                   panel->SetAutoLayout(true);
161                   panel->Layout();
162           }
163 }
164
165 //------------------------------------------------------------------------
166 WidgetShowNPoints::~WidgetShowNPoints()
167 {
168 }
169
170 //------------------------------------------------------------------------
171 void WidgetShowNPoints::SetRadio(double radio)
172 {
173         this->mradio=radio;
174         //NTU: For Slider
175         sdrRadio->SetValue(this->mradio);
176 }
177
178 //------------------------------------------------------------------------
179 std::vector<int> WidgetShowNPoints::GetLstPointsX()
180 {
181         return lstPointsX;
182 }
183
184 //------------------------------------------------------------------------
185 std::vector<int> WidgetShowNPoints::GetLstPointsY()
186 {
187         return lstPointsY;
188 }
189
190 //------------------------------------------------------------------------
191 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
192 {
193         return lstPointsZ;
194 }
195
196 //------------------------------------------------------------------------
197         std::vector<std::string> WidgetShowNPoints::GetLstLabels()
198 {
199         return lstLabels;
200 }
201
202 //------------------------------------------------------------------------
203 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
204 {
205         mpoint = ppoint;
206 }
207
208 //------------------------------------------------------------------------
209 void WidgetShowNPoints::SetColour(std::vector<double> colour)
210 {
211         this->mcolour = colour;
212 }
213
214 //------------------------------------------------------------------------
215 void WidgetShowNPoints::SetOpacity(double opacity)
216 {
217         this->mopacity=opacity;
218         //NTU: For Slider
219         sdrOpacity->SetValue(this->mopacity*100.0);
220 }
221
222 //------------------------------------------------------------------------
223 void WidgetShowNPoints::SetImage(vtkImageData *image)
224 {
225         this->mimage=image;
226 }
227
228 //------------------------------------------------------------------------
229 void  WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
230 {
231         this->renderer  = renderer;
232 }
233
234 //------------------------------------------------------------------------
235
236 std::string WidgetShowNPoints::CleanSpaces(std::string ss)
237 {
238         int i;
239         while( (i=ss.find(32))>=0 )
240         {
241                 ss.replace(i,1,"_");
242         }
243         return ss;
244 }
245
246         
247 //------------------------------------------------------------------------
248         
249 void WidgetShowNPoints::RefreshPoint(int id)
250 {
251         int x = lstPointsX[id];
252         int y = lstPointsY[id];
253         int z = lstPointsZ[id];
254         
255         double spc[3];
256         mimage->GetSpacing(spc);
257         
258         lstActorsSphere[id]->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
259         lstActorsSphere[id]->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
260         lstActorsSphere[id]->GetProperty()->SetOpacity( mopacity );
261         
262         lstActorsText[id]->SetPosition(  mradio+spc[0]*x , spc[1]*y , spc[2]*z );
263 }
264         
265         
266 //------------------------------------------------------------------------
267 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
268 {
269         lstPointsX.push_back( x );
270         lstPointsY.push_back( y );
271         lstPointsZ.push_back( z );
272
273         std::string strLabel = CleanSpaces(  label );
274
275         lstLabels.push_back( strLabel );
276
277         // Sphere
278         vtkSphereSource *vtksphere              = vtkSphereSource::New();
279         vtksphere->SetThetaResolution (20);
280         vtksphere->SetPhiResolution (20);
281         vtksphere->SetRadius( mradio  );
282
283         //NTU: For updating points
284
285         lstSourceSphere.push_back(vtksphere);
286
287         vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
288         sphereMapper->SetInput( vtksphere->GetOutput() );
289         vtkActor *sphereActor   = vtkActor::New();
290         sphereActor->SetMapper(sphereMapper);
291         sphereActor->SetOrigin(0, 0, 0);
292
293 /*EED Borrame
294         double spc[3];
295         if(mimage==NULL){
296                 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
297                 dialog.ShowModal();
298                 return;
299         }
300         mimage->GetSpacing(spc);
301         sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
302         sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
303         sphereActor->GetProperty()->SetOpacity( mopacity );
304 */
305
306         lstActorsSphere.push_back(sphereActor);
307         if(renderer==NULL){
308                 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
309                 dialog.ShowModal();             
310                 return;
311         }
312         renderer->AddActor( sphereActor );
313
314         // Actor
315         vtkTextActor3D *textActor = vtkTextActor3D::New();
316 /* EED Borrame  
317         textActor->SetPosition(  mradio+spc[0]*x , spc[1]*y , spc[2]*z );
318 */ 
319         textActor->SetInput( strLabel.c_str()  );
320         renderer->AddActor( textActor );
321         lstActorsText.push_back(textActor);
322
323         RefreshPoint(lstPointsX.size()-1);
324 }
325
326 //------------------------------------------------------------------------
327 double WidgetShowNPoints::Distance(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
328 {
329     return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
330 }
331
332 //------------------------------------------------------------------------
333 void WidgetShowNPoints::InsertPoint(int x, int y, int z, std::string label)//CFT
334 {
335         std::cout<<"WidgetShowNPoints::InsertPoint begin "<<std::endl;
336         std::vector<int> dTotal;
337         int pos = 0;
338         int a,b,res;
339         //Calcule distance for each pair of points
340         for(int i = 0; i<lstPointsX.size()-1 ; i++)
341         {
342                 a = Distance(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
343                 b = Distance(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
344                 res = a + b;            
345                 dTotal.push_back (res);         
346         }
347         //Gets the smallest distance 
348         int small = dTotal[0];
349         for (int j = 0; j < dTotal.size(); j++)
350   {
351       if(dTotal[j]<small)
352       {
353                       small=dTotal[j];
354                                         pos = j+1;
355       }
356   }
357
358         std::vector<int>::iterator it;
359         //Insert the point in the list of points
360         it = lstPointsX.begin();
361         lstPointsX.insert( it+pos, x );
362         it = lstPointsY.begin();
363         lstPointsY.insert( it+pos, y );
364         it = lstPointsZ.begin();
365         lstPointsZ.insert( it+pos, z );
366
367         std::string strLabel = CleanSpaces(  label );
368         
369         std::vector<std::string>::iterator itS;
370         itS = lstLabels.begin();
371         //Insert Label in list of labels
372         lstLabels.insert( itS+pos, strLabel );
373
374         // Sphere
375         vtkSphereSource *vtksphere              = vtkSphereSource::New();
376         vtksphere->SetThetaResolution (20);
377         vtksphere->SetPhiResolution (20);
378         vtksphere->SetRadius( mradio  );
379
380         //NTU: For updating points
381         std::vector<vtkSphereSource*>::iterator itSS;
382         itSS = lstSourceSphere.begin();
383         lstSourceSphere.insert( itSS+pos, vtksphere);
384
385         vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
386         sphereMapper->SetInput( vtksphere->GetOutput() );
387         vtkActor *sphereActor   = vtkActor::New();
388         sphereActor->SetMapper(sphereMapper);
389         sphereActor->SetOrigin(0, 0, 0);
390
391         std::vector<vtkActor*>::iterator itAS;
392         itAS = lstActorsSphere.begin();
393         lstActorsSphere.insert( itAS+pos, sphereActor);
394         if(renderer==NULL){
395                 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
396                 dialog.ShowModal();             
397                 return;
398         }
399         renderer->AddActor( sphereActor );
400
401         // Actor
402         vtkTextActor3D *textActor = vtkTextActor3D::New();
403
404         textActor->SetInput( strLabel.c_str()  );
405         renderer->AddActor( textActor );
406         std::vector<vtkTextActor3D*>::iterator itTA;
407         itTA = lstActorsText.begin();
408         lstActorsText.insert( itTA+pos , textActor);
409
410         RefreshPoint(pos);
411         std::cout<<"WidgetShowNPoints::InsertPoint end"<<std::endl;
412 }
413
414 //------------------------------------------------------------------------
415 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
416 {
417         if(mimage==NULL){
418                 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
419                 dialog.ShowModal();
420                 return;
421         }
422         
423         if (this->renderer!=NULL){
424                 if (mpoint.size()==3){
425                         AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
426                         SetOutputBox();
427                 } else {//mpoint.size
428                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
429                 }
430         } // renderer
431 }
432
433 //------------------------------------------------------------------------
434 void WidgetShowNPoints::OnInsertPoint (wxCommandEvent& event)//CFT
435 {
436         std::cout<<"WidgetShowNPoints::OnInsertPoint begin"<<std::endl;
437         if(mimage==NULL){
438                 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
439                 dialog.ShowModal();
440                 return;
441         }
442         
443         if (this->renderer!=NULL){
444                 if (mpoint.size()==3){
445                         InsertPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
446                         SetOutputBox();
447                 } else {//mpoint.size
448                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
449                 }
450         } // renderer
451         std::cout<<"WidgetShowNPoints::OnInsertPoint end"<<std::endl;
452 }
453
454
455 //------------------------------------------------------------------------
456         void WidgetShowNPoints::SetOutputBox()
457         {
458                 renderer->GetRenderWindow()->Render();
459                 wxString strTmp;
460                 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
461                 txtNrPoints->SetLabel(  strTmp );
462
463                 //--BBTK
464                 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
465                 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
466                 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
467                 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
468
469                 mbbShowNPoints->bbSignalOutputModification();
470
471 //EED 07/07/2014
472 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
473 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
474 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
475 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
476         }
477
478 //------------------------------------------------------------------------
479         void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
480         {
481                 wxFileDialog* FD = new wxFileDialog( 0,
482                                             _T("Save points .."),
483                                             _T(""),
484                                             _T(""),
485                                             _T("(*.xls)|*.xls"),
486                                             wxSAVE | wxOVERWRITE_PROMPT,
487                                             wxDefaultPosition);
488                 //EED
489
490                 int result_FD = FD->ShowModal();
491                 std::string tmpLabel;
492                 
493                 // This line is need it by windows //EED
494                 FD->SetReturnCode( result_FD );
495
496                 if (FD->GetReturnCode()==wxID_OK)
497                 {
498                         FILE *ff;
499                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
500                         ff = fopen( filename.c_str() , "w+" );
501                         if (ff!=NULL)
502                         {
503                                 int i , size = (int) (lstActorsSphere.size());
504                                 fprintf(ff,"NumberOfPoints %d \n",size);
505                                 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
506                                 int x, y, z;
507                                 double value;
508                                 for (i=0; i<size; i++)
509                                 {
510                                         x=lstPointsX[i];
511                                         y=lstPointsY[i];
512                                         z=lstPointsZ[i];
513                                         value= mimage->GetScalarComponentAsDouble(x,y,z,0);
514                                         if (lstLabels[i]!="") 
515                                         {
516                                                 tmpLabel=lstLabels[i];
517                                         } else{
518                                             tmpLabel="<_VOID_>";
519                                         }
520                                         fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value  , tmpLabel.c_str());
521                                 } // for
522                                 fclose(ff);
523                         } else {   // else ff
524                                 printf("WidgetShowNPoints::OnSavePoints  ...Error... creating file");
525                         } //ff
526                 }       // dialog box
527         }
528
529 //------------------------------------------------------------------------
530         void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
531         {
532                 wxFileDialog* FD = new wxFileDialog( 0,
533                                              _T("Load points .."),
534                                              _T(""),
535                                              _T(""),
536                                              _T("(*.xls)|*.xls"),
537                                              wxOPEN | wxFILE_MUST_EXIST,
538                                              wxDefaultPosition);
539                 //EED
540
541                 int result_FD = FD->ShowModal();
542
543                 // This line is need it by windows //EED
544                 FD->SetReturnCode( result_FD );
545
546                 if (FD->GetReturnCode()==wxID_OK)
547                 {
548                         FILE *ff;
549                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
550                         ff = fopen( filename.c_str() , "r+" );
551                         if (ff!=NULL)
552                         {
553                                 int i,size;
554                                 char chartmp[256];
555                                 fscanf(ff," %s %d",chartmp,&size);
556                                 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
557
558                                 float value;
559                                 int x,y,z;
560                                 for (i=0; i<size; i++)
561                                 {
562                                         fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
563                                         if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
564                                         AddPoint(x,y,z, chartmp );
565                                 }
566                                 SetOutputBox();
567                                 fclose(ff);
568                         } else {   // else ff
569                                 printf("WidgetShowNPoints::OnLoadPoints  ...Error... reading file");
570                         } //ff
571                 }       // dialog box
572
573
574         }
575
576 //------------------------------------------------------------------------
577
578         int WidgetShowNPoints::GetNearestPoint()
579         {
580                 int id=-1;
581                 int i, size=(int)(lstActorsSphere.size());
582                 double radioMin=10000000;
583                 
584                 if(mimage ==NULL){                      
585                         wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
586                         diag->ShowModal();
587                         return -1;
588                 }else{
589                         
590                         for ( i=0  ; i<size; i++ )
591                         {
592                                 double rx =  mpoint[0] - lstPointsX [i];
593                                 double ry =  mpoint[1] - lstPointsY [i];
594                                 double rz =  mpoint[2] - lstPointsZ [i];
595                                 double radio = rx*rx + ry*ry + rz*rz;
596                                 if ( radio <= radioMin)
597                                 {
598                                         radioMin=radio;
599                                         id=i;
600                                 }       // if
601                         } // for
602                         
603                         return id;
604                 }
605         }
606
607         
608         //------------------------------------------------------------------------
609         void WidgetShowNPoints::OnSetPoint(wxCommandEvent& event)
610         {
611                 int id=GetNearestPoint();
612                 if((id==-1) && (mbbShowNPoints->bbGetInputType()==1))
613                 {
614                         id=0;
615                         AddPoint(0,0,0,"");
616                         SetOutputBox();
617                 }
618                 
619                 
620                 if (id>=0)
621                 {
622                         printf("WidgetShowNPoints::OnSetPoint \n");
623                         lstPointsX[id] = mpoint[0];
624                         lstPointsY[id] = mpoint[1];
625                         lstPointsZ[id] = mpoint[2];             
626                         RefreshPoint(id);
627                         /*EED Borrame
628                          this->lstActorsSphere[id]->SetPosition( spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
629                          this->lstActorsText[id]->SetPosition(  spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
630                          */ 
631                 } // if id
632                 SetOutputBox();
633         }
634         
635         
636         //------------------------------------------------------------------------
637         
638         int WidgetShowNPoints::IdInsidePoint()
639         {
640                 int id=-1;
641                 int i, size=(int)(lstActorsSphere.size());
642                 double spc[3];
643
644                 if(mimage ==NULL){                      
645                         wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
646                         diag->ShowModal();
647                         return -1;
648                 }else{
649
650                         mimage->GetSpacing(spc);
651
652                         for ( i=0  ; i<size; i++ )
653                         {
654                                 double rx =  spc[0]*(mpoint[0] - lstPointsX [i]);
655                                 double ry =  spc[1]*(mpoint[1] - lstPointsY [i]);
656                                 double rz =  spc[2]*(mpoint[2] - lstPointsZ [i]);
657                                 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
658                                 {
659                                         id=i;
660                                 }       // if
661                         } // for
662
663                         return id;
664                 }
665         }
666
667 //------------------------------------------------------------------------
668         void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
669         {
670                 int id=IdInsidePoint();
671                 if (id>=0)
672                 {
673                         std::string strLabel = CleanSpaces(  (const char*) ( textCtrl->GetValue().mb_str() ) );
674                         lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
675                         lstActorsText[id]->SetInput(  lstLabels[id].c_str() );
676                         SetOutputBox();
677                 }
678         }
679
680 //------------------------------------------------------------------------
681         void WidgetShowNPoints::ErasePoint(int id)
682         {
683                 if (this->renderer!=NULL){
684                         if (id>=0){
685                                 renderer->RemoveActor( lstActorsSphere[id] );
686                                 renderer->RemoveActor( lstActorsText[id] );
687                                  lstActorsSphere[id]->Delete();
688                                  lstActorsText[id]->Delete();
689                                  lstSourceSphere[id]->Delete();
690                                 lstActorsSphere.erase( lstActorsSphere.begin()+id );
691                                 lstActorsText.erase( lstActorsText.begin()+id );
692                                 lstSourceSphere.erase( lstSourceSphere.begin()+id );
693
694                                 lstPointsX.erase( lstPointsX.begin()+id );
695                                 lstPointsY.erase( lstPointsY.begin()+id );
696                                 lstPointsZ.erase( lstPointsZ.begin()+id );
697                                 lstLabels.erase( lstLabels.begin()+id );
698
699                                 SetOutputBox();
700
701                         } // if id
702                 } // if renderer
703         }
704
705 //------------------------------------------------------------------------
706         void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
707         {
708                 ErasePoint( IdInsidePoint() );
709         }
710
711 //------------------------------------------------------------------------
712         void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
713         {
714                 ErasePoint(lstActorsSphere.size()-1);
715         }
716
717 //------------------------------------------------------------------------
718 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
719 {
720         int id,size=lstActorsSphere.size();
721         for (id=size-1;id>=0;id--)
722         {
723                 ErasePoint(id);
724         }
725 }
726
727 //NTU: Method for updating points opacity and Radio
728
729 //------------------------------------------------------------------------
730 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
731 {
732         //Difference in Radio for text placement
733         int difradio = sdrRadio->GetValue() - this->mradio;
734         
735         this->mopacity = sdrOpacity->GetValue()/100.0;
736         this->mradio = sdrRadio->GetValue();
737         
738         //NTU refresh the inputs
739         mbbShowNPoints->bbSetInputOpacity(this->mopacity);
740         mbbShowNPoints->bbSetInputRadio(this->mradio);
741
742         int size = (int) this->lstActorsSphere.size();
743         int i;
744         for(i = 0; i < size; i ++)
745         {
746                 this->lstSourceSphere[i]->SetRadius(mradio);
747                 this->lstActorsSphere[i]->GetProperty()->SetOpacity(mopacity);
748                 this->lstActorsText[i]->GetTextProperty()->SetOpacity(mopacity);
749                 this->lstActorsText[i]->SetPosition(this->lstActorsText[i]->GetPosition()[0]+difradio,this->lstActorsText[i]->GetPosition()[1], this->lstActorsText[i]->GetPosition()[2]);
750         }
751
752         //NTU: For updating
753 //EED   SetOutputBox();
754         renderer->GetRenderWindow()->Render();
755
756 }
757
758 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
759 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
760
761 //-----------------------------------------------------------------
762 void ShowNPoints::Process()
763 {
764         if (mwxwidget!=NULL)
765         {
766                 mwxwidget->SetRenderer( bbGetInputRenderer() );
767                 mwxwidget->SetPoint( bbGetInputIn() );
768                 mwxwidget->SetImage( bbGetInputImage() );
769                 mwxwidget->SetColour( bbGetInputColour() );
770                 mwxwidget->SetOpacity( bbGetInputOpacity() );
771                 mwxwidget->SetRadio( bbGetInputRadio() );
772
773                 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
774                 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
775                 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
776                 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
777
778         } // mwxwidget
779 }
780
781 //-----------------------------------------------------------------
782 void ShowNPoints::CreateWidget(wxWindow* parent)
783 {
784         mwxwidget = new WidgetShowNPoints( parent, this);
785     bbSetOutputWidget( mwxwidget );
786 }
787
788 //-----------------------------------------------------------------
789 void ShowNPoints::bbUserSetDefaultValues()
790 {
791         mwxwidget = NULL;
792
793         bbSetInputRadio(10);
794         bbSetInputOpacity(1);
795
796         std::vector<double> colour;
797         colour.push_back(1.0);
798         colour.push_back(0.0);
799         colour.push_back(0.0);
800         bbSetInputColour(colour);
801
802         bbSetInputImage(NULL);
803         bbSetInputType(0);
804         bbSetInputRenderer(NULL);
805 }
806
807 //-----------------------------------------------------------------
808         void ShowNPoints::bbUserInitializeProcessing()
809         {
810         }
811
812 //-----------------------------------------------------------------
813         void ShowNPoints::bbUserFinalizeProcessing()
814         {
815         }
816
817 //-----------------------------------------------------------------
818
819 }
820 // EO namespace bbcreaMaracasVisu