]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuShowNPoints.cxx
c0edbf2b31ea9774d7f2db40028d46eec4f0ae51
[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
337         if(lstPointsX.size()>1)
338         {
339                 std::vector<int> dTotal;
340                 int pos = 1;
341                 int a,b,res;
342                 
343                 //Calcule distance for each pair of points
344                 for(int i = 0; i<lstPointsX.size()-1 ; i++)
345                 {
346                                 a = Distance(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
347                                 b = Distance(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
348                                 res = a + b;            
349                                 dTotal.push_back (res);         
350                 }
351                 //Gets the smallest distance 
352                 int smallTMP = dTotal[0];
353                 for (int j = 0; j < dTotal.size(); j++)
354                 {
355                           if(dTotal[j]<smallTMP)
356                           {
357                                           smallTMP=dTotal[j];
358                                                 pos = j+1;
359                           }
360                 }
361                 
362                 std::vector<int>::iterator it;
363                 //Insert the point in the list of points
364                 it = lstPointsX.begin();
365                 lstPointsX.insert( it+pos, x );
366                 it = lstPointsY.begin();
367                 lstPointsY.insert( it+pos, y );
368                 it = lstPointsZ.begin();
369                 lstPointsZ.insert( it+pos, z );
370
371                 std::string strLabel = CleanSpaces(  label );
372         
373                 std::vector<std::string>::iterator itS;
374                 itS = lstLabels.begin();
375                 //Insert Label in list of labels
376                 lstLabels.insert( itS+pos, strLabel );
377
378                 // Sphere
379                 vtkSphereSource *vtksphere              = vtkSphereSource::New();
380                 vtksphere->SetThetaResolution (20);
381                 vtksphere->SetPhiResolution (20);
382                 vtksphere->SetRadius( mradio  );
383
384                 //NTU: For updating points
385                 std::vector<vtkSphereSource*>::iterator itSS;
386                 itSS = lstSourceSphere.begin();
387                 lstSourceSphere.insert( itSS+pos, vtksphere);
388
389                 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
390                 sphereMapper->SetInput( vtksphere->GetOutput() );
391                 vtkActor *sphereActor   = vtkActor::New();
392                 sphereActor->SetMapper(sphereMapper);
393                 sphereActor->SetOrigin(0, 0, 0);
394
395                 std::vector<vtkActor*>::iterator itAS;
396                 itAS = lstActorsSphere.begin();
397                 lstActorsSphere.insert( itAS+pos, sphereActor);
398                 if(renderer==NULL){
399                         wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
400                         dialog.ShowModal();             
401                         return;
402                 }
403                 renderer->AddActor( sphereActor );
404
405                 // Actor
406                 vtkTextActor3D *textActor = vtkTextActor3D::New();
407
408                 textActor->SetInput( strLabel.c_str()  );
409                 renderer->AddActor( textActor );
410                 std::vector<vtkTextActor3D*>::iterator itTA;
411                 itTA = lstActorsText.begin();
412                 lstActorsText.insert( itTA+pos , textActor);
413
414                 RefreshPoint(pos);
415         }//end if
416         std::cout<<"WidgetShowNPoints::InsertPoint end"<<std::endl;
417 }
418
419 //------------------------------------------------------------------------
420 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
421 {
422         if(mimage==NULL){
423                 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
424                 dialog.ShowModal();
425                 return;
426         }
427         
428         if (this->renderer!=NULL){
429                 if (mpoint.size()==3){
430                         AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
431                         SetOutputBox();
432                 } else {//mpoint.size
433                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
434                 }
435         } // renderer
436 }
437
438 //------------------------------------------------------------------------
439 void WidgetShowNPoints::OnInsertPoint (wxCommandEvent& event)//CFT
440 {
441         std::cout<<"WidgetShowNPoints::OnInsertPoint begin"<<std::endl;
442         if(mimage==NULL){
443                 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
444                 dialog.ShowModal();
445                 return;
446         }
447         
448         if (this->renderer!=NULL){
449                 if (mpoint.size()==3){
450                         InsertPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
451                         SetOutputBox();
452                 } else {//mpoint.size
453                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
454                 }
455         } // renderer
456         std::cout<<"WidgetShowNPoints::OnInsertPoint end"<<std::endl;
457 }
458
459
460 //------------------------------------------------------------------------
461         void WidgetShowNPoints::SetOutputBox()
462         {
463                 renderer->GetRenderWindow()->Render();
464                 wxString strTmp;
465                 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
466                 txtNrPoints->SetLabel(  strTmp );
467
468                 //--BBTK
469                 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
470                 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
471                 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
472                 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
473
474                 mbbShowNPoints->bbSignalOutputModification();
475
476 //EED 07/07/2014
477 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
478 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
479 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
480 //              mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
481         }
482
483 //------------------------------------------------------------------------
484         void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
485         {
486                 wxFileDialog* FD = new wxFileDialog( 0,
487                                             _T("Save points .."),
488                                             _T(""),
489                                             _T(""),
490                                             _T("(*.xls)|*.xls"),
491                                             wxSAVE | wxOVERWRITE_PROMPT,
492                                             wxDefaultPosition);
493                 //EED
494
495                 int result_FD = FD->ShowModal();
496                 std::string tmpLabel;
497                 
498                 // This line is need it by windows //EED
499                 FD->SetReturnCode( result_FD );
500
501                 if (FD->GetReturnCode()==wxID_OK)
502                 {
503                         FILE *ff;
504                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
505                         ff = fopen( filename.c_str() , "w+" );
506                         if (ff!=NULL)
507                         {
508                                 int i , size = (int) (lstActorsSphere.size());
509                                 fprintf(ff,"NumberOfPoints %d \n",size);
510                                 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
511                                 int x, y, z;
512                                 double value;
513                                 for (i=0; i<size; i++)
514                                 {
515                                         x=lstPointsX[i];
516                                         y=lstPointsY[i];
517                                         z=lstPointsZ[i];
518                                         value= mimage->GetScalarComponentAsDouble(x,y,z,0);
519                                         if (lstLabels[i]!="") 
520                                         {
521                                                 tmpLabel=lstLabels[i];
522                                         } else{
523                                             tmpLabel="<_VOID_>";
524                                         }
525                                         fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value  , tmpLabel.c_str());
526                                 } // for
527                                 fclose(ff);
528                         } else {   // else ff
529                                 printf("WidgetShowNPoints::OnSavePoints  ...Error... creating file");
530                         } //ff
531                 }       // dialog box
532         }
533
534 //------------------------------------------------------------------------
535         void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
536         {
537                 wxFileDialog* FD = new wxFileDialog( 0,
538                                              _T("Load points .."),
539                                              _T(""),
540                                              _T(""),
541                                              _T("(*.xls)|*.xls"),
542                                              wxOPEN | wxFILE_MUST_EXIST,
543                                              wxDefaultPosition);
544                 //EED
545
546                 int result_FD = FD->ShowModal();
547
548                 // This line is need it by windows //EED
549                 FD->SetReturnCode( result_FD );
550
551                 if (FD->GetReturnCode()==wxID_OK)
552                 {
553                         FILE *ff;
554                         std::string filename= (const char*) ( FD->GetPath().mb_str() );
555                         ff = fopen( filename.c_str() , "r+" );
556                         if (ff!=NULL)
557                         {
558                                 int i,size;
559                                 char chartmp[256];
560                                 fscanf(ff," %s %d",chartmp,&size);
561                                 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
562
563                                 float value;
564                                 int x,y,z;
565                                 for (i=0; i<size; i++)
566                                 {
567                                         fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
568                                         if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
569                                         AddPoint(x,y,z, chartmp );
570                                 }
571                                 SetOutputBox();
572                                 fclose(ff);
573                         } else {   // else ff
574                                 printf("WidgetShowNPoints::OnLoadPoints  ...Error... reading file");
575                         } //ff
576                 }       // dialog box
577
578
579         }
580
581 //------------------------------------------------------------------------
582
583         int WidgetShowNPoints::GetNearestPoint()
584         {
585                 int id=-1;
586                 int i, size=(int)(lstActorsSphere.size());
587                 double radioMin=10000000;
588                 
589                 if(mimage ==NULL){                      
590                         wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
591                         diag->ShowModal();
592                         return -1;
593                 }else{
594                         
595                         for ( i=0  ; i<size; i++ )
596                         {
597                                 double rx =  mpoint[0] - lstPointsX [i];
598                                 double ry =  mpoint[1] - lstPointsY [i];
599                                 double rz =  mpoint[2] - lstPointsZ [i];
600                                 double radio = rx*rx + ry*ry + rz*rz;
601                                 if ( radio <= radioMin)
602                                 {
603                                         radioMin=radio;
604                                         id=i;
605                                 }       // if
606                         } // for
607                         
608                         return id;
609                 }
610         }
611
612         
613         //------------------------------------------------------------------------
614         void WidgetShowNPoints::OnSetPoint(wxCommandEvent& event)
615         {
616                 int id=GetNearestPoint();
617                 if((id==-1) && (mbbShowNPoints->bbGetInputType()==1))
618                 {
619                         id=0;
620                         AddPoint(0,0,0,"");
621                         SetOutputBox();
622                 }
623                 
624                 
625                 if (id>=0)
626                 {
627                         printf("WidgetShowNPoints::OnSetPoint \n");
628                         lstPointsX[id] = mpoint[0];
629                         lstPointsY[id] = mpoint[1];
630                         lstPointsZ[id] = mpoint[2];             
631                         RefreshPoint(id);
632                         /*EED Borrame
633                          this->lstActorsSphere[id]->SetPosition( spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
634                          this->lstActorsText[id]->SetPosition(  spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
635                          */ 
636                 } // if id
637                 SetOutputBox();
638         }
639         
640         
641         //------------------------------------------------------------------------
642         
643         int WidgetShowNPoints::IdInsidePoint()
644         {
645                 int id=-1;
646                 int i, size=(int)(lstActorsSphere.size());
647                 double spc[3];
648
649                 if(mimage ==NULL){                      
650                         wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
651                         diag->ShowModal();
652                         return -1;
653                 }else{
654
655                         mimage->GetSpacing(spc);
656
657                         for ( i=0  ; i<size; i++ )
658                         {
659                                 double rx =  spc[0]*(mpoint[0] - lstPointsX [i]);
660                                 double ry =  spc[1]*(mpoint[1] - lstPointsY [i]);
661                                 double rz =  spc[2]*(mpoint[2] - lstPointsZ [i]);
662                                 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
663                                 {
664                                         id=i;
665                                 }       // if
666                         } // for
667
668                         return id;
669                 }
670         }
671
672 //------------------------------------------------------------------------
673         void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
674         {
675                 int id=IdInsidePoint();
676                 if (id>=0)
677                 {
678                         std::string strLabel = CleanSpaces(  (const char*) ( textCtrl->GetValue().mb_str() ) );
679                         lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
680                         lstActorsText[id]->SetInput(  lstLabels[id].c_str() );
681                         SetOutputBox();
682                 }
683         }
684
685 //------------------------------------------------------------------------
686         void WidgetShowNPoints::ErasePoint(int id)
687         {
688                 if (this->renderer!=NULL){
689                         if (id>=0){
690                                 renderer->RemoveActor( lstActorsSphere[id] );
691                                 renderer->RemoveActor( lstActorsText[id] );
692                                  lstActorsSphere[id]->Delete();
693                                  lstActorsText[id]->Delete();
694                                  lstSourceSphere[id]->Delete();
695                                 lstActorsSphere.erase( lstActorsSphere.begin()+id );
696                                 lstActorsText.erase( lstActorsText.begin()+id );
697                                 lstSourceSphere.erase( lstSourceSphere.begin()+id );
698
699                                 lstPointsX.erase( lstPointsX.begin()+id );
700                                 lstPointsY.erase( lstPointsY.begin()+id );
701                                 lstPointsZ.erase( lstPointsZ.begin()+id );
702                                 lstLabels.erase( lstLabels.begin()+id );
703
704                                 SetOutputBox();
705
706                         } // if id
707                 } // if renderer
708         }
709
710 //------------------------------------------------------------------------
711         void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
712         {
713                 ErasePoint( IdInsidePoint() );
714         }
715
716 //------------------------------------------------------------------------
717         void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
718         {
719                 ErasePoint(lstActorsSphere.size()-1);
720         }
721
722 //------------------------------------------------------------------------
723 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
724 {
725         int id,size=lstActorsSphere.size();
726         for (id=size-1;id>=0;id--)
727         {
728                 ErasePoint(id);
729         }
730 }
731
732 //NTU: Method for updating points opacity and Radio
733
734 //------------------------------------------------------------------------
735 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
736 {
737         //Difference in Radio for text placement
738         int difradio = sdrRadio->GetValue() - this->mradio;
739         
740         this->mopacity = sdrOpacity->GetValue()/100.0;
741         this->mradio = sdrRadio->GetValue();
742         
743         //NTU refresh the inputs
744         mbbShowNPoints->bbSetInputOpacity(this->mopacity);
745         mbbShowNPoints->bbSetInputRadio(this->mradio);
746
747         int size = (int) this->lstActorsSphere.size();
748         int i;
749         for(i = 0; i < size; i ++)
750         {
751                 this->lstSourceSphere[i]->SetRadius(mradio);
752                 this->lstActorsSphere[i]->GetProperty()->SetOpacity(mopacity);
753                 this->lstActorsText[i]->GetTextProperty()->SetOpacity(mopacity);
754                 this->lstActorsText[i]->SetPosition(this->lstActorsText[i]->GetPosition()[0]+difradio,this->lstActorsText[i]->GetPosition()[1], this->lstActorsText[i]->GetPosition()[2]);
755         }
756
757         //NTU: For updating
758 //EED   SetOutputBox();
759         renderer->GetRenderWindow()->Render();
760
761 }
762
763 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
764 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
765
766 //-----------------------------------------------------------------
767 void ShowNPoints::Process()
768 {
769         if (mwxwidget!=NULL)
770         {
771                 mwxwidget->SetRenderer( bbGetInputRenderer() );
772                 mwxwidget->SetPoint( bbGetInputIn() );
773                 mwxwidget->SetImage( bbGetInputImage() );
774                 mwxwidget->SetColour( bbGetInputColour() );
775                 mwxwidget->SetOpacity( bbGetInputOpacity() );
776                 mwxwidget->SetRadio( bbGetInputRadio() );
777
778                 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
779                 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
780                 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
781                 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
782
783         } // mwxwidget
784 }
785
786 //-----------------------------------------------------------------
787 void ShowNPoints::CreateWidget(wxWindow* parent)
788 {
789         mwxwidget = new WidgetShowNPoints( parent, this);
790     bbSetOutputWidget( mwxwidget );
791 }
792
793 //-----------------------------------------------------------------
794 void ShowNPoints::bbUserSetDefaultValues()
795 {
796         mwxwidget = NULL;
797
798         bbSetInputRadio(10);
799         bbSetInputOpacity(1);
800
801         std::vector<double> colour;
802         colour.push_back(1.0);
803         colour.push_back(0.0);
804         colour.push_back(0.0);
805         bbSetInputColour(colour);
806
807         bbSetInputImage(NULL);
808         bbSetInputType(0);
809         bbSetInputRenderer(NULL);
810 }
811
812 //-----------------------------------------------------------------
813         void ShowNPoints::bbUserInitializeProcessing()
814         {
815         }
816
817 //-----------------------------------------------------------------
818         void ShowNPoints::bbUserFinalizeProcessing()
819         {
820         }
821
822 //-----------------------------------------------------------------
823
824 }
825 // EO namespace bbcreaMaracasVisu