]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuShowNPoints.cxx
#2982 creaMaracasVisu Feature New Normal - ShowNPoints_model
[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 ModelShowNPoints::ModelShowNPoints()
41 {
42 }
43 //----------------------------------------------------------------------
44
45 ModelShowNPoints::~ModelShowNPoints()
46 {
47 }
48
49 //------------------------------------------------------------------------
50 void ModelShowNPoints::SetRadio(double radio)
51 {
52         mradio =  radio;
53 }
54
55
56 //------------------------------------------------------------------------
57 double ModelShowNPoints::GetRadio()
58 {
59         return mradio;
60 }
61
62
63 //------------------------------------------------------------------------
64 std::vector<int> ModelShowNPoints::GetLstPointsX()
65 {
66         return lstPointsX;
67 }
68
69 //------------------------------------------------------------------------
70 std::vector<int> ModelShowNPoints::GetLstPointsY()
71 {
72         return lstPointsY;
73 }
74
75 //------------------------------------------------------------------------
76 std::vector<int> ModelShowNPoints::GetLstPointsZ()
77 {
78         return lstPointsZ;
79 }
80
81 //------------------------------------------------------------------------
82 std::vector<std::string> ModelShowNPoints::GetLstLabels()
83 {
84         return lstLabels;
85 }
86
87 //------------------------------------------------------------------------
88 void ModelShowNPoints::SetReferencePoint(std::vector<int> ppoint)
89 {
90         mReferencePoint = ppoint;
91 }
92
93 //------------------------------------------------------------------------
94 std::vector<int> ModelShowNPoints::GetReferencePoint()
95 {
96         return mReferencePoint;
97 }
98
99 //------------------------------------------------------------------------
100 void ModelShowNPoints::SetImage(vtkImageData *image)
101 {
102         this->mimage=image;
103 }
104
105
106 //------------------------------------------------------------------------
107 std::string ModelShowNPoints::CleanSpaces(std::string ss)
108 {
109         int i;
110         while( (i=ss.find(32))>=0 )
111         {
112                 ss.replace(i,1,"_");
113         }
114         return ss;
115 }
116
117 //------------------------------------------------------------------------
118 void ModelShowNPoints::GetIdPoint(int id, int *x, int *y, int *z)
119 {
120         *x=lstPointsX[id];
121         *y=lstPointsY[id];
122         *z=lstPointsZ[id];
123 }
124
125 //------------------------------------------------------------------------
126 std::string ModelShowNPoints::GetIdLabel(int id)
127 {
128         return lstLabels[id];
129 }
130
131 //------------------------------------------------------------------------
132 vtkImageData *ModelShowNPoints::GetImage()
133 {
134         return mimage;
135 }
136
137 //------------------------------------------------------------------------
138 void ModelShowNPoints::AddPoint(int x, int y, int z, std::string label)
139 {
140         lstPointsX.push_back( x );
141         lstPointsY.push_back( y );
142         lstPointsZ.push_back( z );
143         std::string strLabel = CleanSpaces(  label );
144         lstLabels.push_back( strLabel );
145 }
146
147 //------------------------------------------------------------------------
148 double ModelShowNPoints::Distance(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
149 {
150     return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
151 }
152
153 //------------------------------------------------------------------------
154 int ModelShowNPoints::InsertPoint(int x, int y, int z, std::string label)
155 {
156         if(lstPointsX.size()>1)
157         {
158                 std::vector<int> dTotal;
159                 int pos = 1;
160                 int a,b,res;
161                 
162                 //Calcule distance for each pair of points
163                 for(int i = 0; i<(int)lstPointsX.size()-1 ; i++)
164                 {
165                                 a = Distance(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
166                                 b = Distance(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
167                                 res = a + b;            
168                                 dTotal.push_back (res);         
169                 }
170                 //Gets the smallest distance 
171                 int smallTMP = dTotal[0];
172                 for (int j = 0; j < (int) dTotal.size(); j++)
173                 {
174                           if(dTotal[j]<smallTMP)
175                           {
176                                           smallTMP=dTotal[j];
177                                                 pos = j+1;
178                           }
179                 }
180                 
181                 std::vector<int>::iterator it;
182                 //Insert the point in the list of points
183                 it = lstPointsX.begin();
184                 lstPointsX.insert( it+pos, x );
185                 it = lstPointsY.begin();
186                 lstPointsY.insert( it+pos, y );
187                 it = lstPointsZ.begin();
188                 lstPointsZ.insert( it+pos, z );
189
190                 std::string strLabel = CleanSpaces(  label );
191         
192                 std::vector<std::string>::iterator itS;
193                 itS = lstLabels.begin();
194                 //Insert Label in list of labels
195                 lstLabels.insert( itS+pos, strLabel );
196                 return pos;
197         } else {
198                 return -1;
199         }// if size lst X
200
201 }
202
203
204 //------------------------------------------------------------------------
205 void ModelShowNPoints::SavePoints(std::string filename)
206 {
207         std::string tmpLabel;
208         FILE *ff;
209         ff = fopen( filename.c_str() , "w+" );
210         if (ff!=NULL)
211         {
212                 int i , size = (int) (lstPointsX.size());
213                 fprintf(ff,"NumberOfPoints %d \n",size);
214                 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
215                 int x, y, z;
216                 double value;
217                 for (i=0; i<size; i++)
218                 {
219                         x=lstPointsX[i];
220                         y=lstPointsY[i];
221                         z=lstPointsZ[i];
222                         value= mimage->GetScalarComponentAsDouble(x,y,z,0);
223                         if (lstLabels[i]!="") 
224                         {
225                                 tmpLabel=lstLabels[i];
226                         } else{
227                                 tmpLabel="<_VOID_>";
228                         }
229                         fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value  , tmpLabel.c_str());
230                 } // for
231                 fclose(ff);
232         } else {   // else ff
233                 printf("ModelShowNPoints::SavePoints  ...Error... creating file");
234         } //ff
235 }
236
237 //------------------------------------------------------------------------
238 int ModelShowNPoints::ReadPoints(std::string filename)
239 {
240         int i,size;
241         char chartmp[256];
242         FILE *ff;
243         ff = fopen( filename.c_str() , "r+" );
244         if (ff!=NULL)
245         {
246                 fscanf(ff," %s %d",chartmp,&size);
247                 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
248
249                 float value;
250                 int x,y,z;
251                 for (i=0; i<size; i++)
252                 {
253                         fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
254                         if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
255                         AddPoint(x,y,z, chartmp );
256                 }
257                 fclose(ff);
258         } else {   // else ff
259                 printf("ModelShowNPoints::LoadPoints  ...Error... reading file");
260         } //ff
261         return size;
262 }
263
264 //------------------------------------------------------------------------
265 int ModelShowNPoints::GetNearestPoint()
266 {
267         int id=-1;
268         int i, size=(int)(lstPointsX.size());
269         double radioMin=10000000;       
270         for ( i=0  ; i<size; i++ )
271         {
272                 double rx =  mReferencePoint[0] - lstPointsX [i];
273                 double ry =  mReferencePoint[1] - lstPointsY [i];
274                 double rz =  mReferencePoint[2] - lstPointsZ [i];
275                 double radio = rx*rx + ry*ry + rz*rz;
276                 if ( radio <= radioMin)
277                 {
278                         radioMin=radio;
279                         id=i;
280                 }       // if
281         } // for                        
282         return id;
283 }
284
285 //------------------------------------------------------------------------
286 int ModelShowNPoints::GetLstPointsSize()
287 {
288         return lstPointsX.size();
289 }
290
291 //------------------------------------------------------------------------
292 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
293 {
294         lstPointsX[id] = mReferencePoint[0];
295         lstPointsY[id] = mReferencePoint[1];
296         lstPointsZ[id] = mReferencePoint[2];            
297 }
298
299
300 //------------------------------------------------------------------------      
301 int ModelShowNPoints::IdInsidePoint()
302 {
303         int id=-1;
304         int i, size=(int)(lstPointsX.size());
305         double spc[3];
306         if(mimage ==NULL)
307         {
308                 printf("WidgetShowNPoints::IdInsidePoint  image not set\n");
309                 return -1;
310         }else{
311                 mimage->GetSpacing(spc);
312                 for ( i=0  ; i<size; i++ )
313                 {
314                         double rx =  spc[0]*(mReferencePoint[0] - lstPointsX [i]);
315                         double ry =  spc[1]*(mReferencePoint[1] - lstPointsY [i]);
316                         double rz =  spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
317                         if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
318                         {
319                                 id=i;
320                         }       // if
321                 } // for
322                 return id;
323         } // if
324 }
325
326 //------------------------------------------------------------------------      
327 int ModelShowNPoints::RenamePoint(std::string label)
328 {
329         int id=IdInsidePoint();
330         if (id>=0)
331         {
332                 std::string strLabel = CleanSpaces( label );
333                 lstLabels[id] = strLabel;
334         }
335         return id;
336 }
337
338 //----------------------------------------------------------------------
339 void ModelShowNPoints::ErasePoint(int id)
340 {
341         lstPointsX.erase( lstPointsX.begin()+id );
342         lstPointsY.erase( lstPointsY.begin()+id );
343         lstPointsZ.erase( lstPointsZ.begin()+id );
344         lstLabels.erase( lstLabels.begin()+id );
345 }
346
347
348
349 //----------------------------------------------------------------------
350 //----------------------------------------------------------------------
351 //----------------------------------------------------------------------
352 //----------------------------------------------------------------------
353 //----------------------------------------------------------------------
354   WidgetShowNPoints::WidgetShowNPoints(wxWindow *parent,  bbcreaMaracasVisu::ShowNPoints *box)
355     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
356   {
357         mmodelShowNPoints                               = new ModelShowNPoints();
358     mbbShowNPoints                                      = box;
359     this->renderer                                      = NULL;
360     wxPanel     *panel                                  = this;
361     wxSizer *sizer                                      = NULL;
362
363         if (mbbShowNPoints->bbGetInputType()==0)
364         {
365                 // Widget interface
366                 askPointLabel                                   = new wxStaticText(panel, -1, _T("Point label :")); // JPR
367                 textCtrl                                                = new wxTextCtrl(panel, -1);
368                 wxButton *btnAddPoint                   = new wxButton( panel, -1, _T("Add Point"));
369                 wxButton *btnInsertPoint                = new wxButton( panel, -1, _T("Insert Point"));//CFT
370                 wxButton *btnSetPositionPoint   = new wxButton( panel, -1, _T("Set nearest point"));
371                 wxButton *btnRenamePoint                = new wxButton( panel, -1, _T("Rename point"));
372                 wxButton *btnEraseLastPoint             = new wxButton( panel, -1, _T("Erase Last point"));
373                 wxButton *btnErasePoint                 = new wxButton( panel, -1, _T("Erase point"));
374                 wxButton *btnDeleteAllPoints    = new wxButton( panel, -1, _T("Delete all points"));
375                 wxButton *btnSavePoints                 = new wxButton( panel, -1, _T("Save points"));
376                 wxButton *btnLoadPoints                 = new wxButton( panel, -1, _T("Load points"));
377                 txtNrPoints                                             = new wxStaticText(panel,-1, _T(" "));
378
379                 //NTU: Sliders for opacity and radio change
380                 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T("  Points Opacity  ")));
381                 sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
382                 wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T("  Points Radio  ")));
383                 sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
384
385                 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
386                 //    sizer1->Add(new wxStaticText(panel,-1,_T("  ")));
387
388                 Connect(btnAddPoint->GetId()            , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
389                 Connect(btnInsertPoint->GetId()         , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnInsertPoint);//CFT
390                 Connect(btnSetPositionPoint->GetId(), wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
391                 Connect(btnRenamePoint->GetId()         , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
392                 Connect(btnEraseLastPoint->GetId()      , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
393                 Connect(btnErasePoint->GetId()          , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
394                 Connect(btnDeleteAllPoints->GetId()     , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
395                 Connect(btnSavePoints->GetId()          , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
396                 Connect(btnLoadPoints->GetId()          , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
397
398                 //NTU: Slider events
399                 Connect(sdrOpacity->GetId()                     , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
400                 Connect(sdrRadio->GetId()                       , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
401
402                 sizer1->Add(askPointLabel); // JPR
403                 sizer1->Add(textCtrl);
404                 sizer1->Add(btnAddPoint);
405                 sizer1->Add(btnInsertPoint);//CFT
406                 sizer1->Add(btnSetPositionPoint);
407                 sizer1->Add(btnRenamePoint);
408                 sizer1->Add(btnErasePoint);
409                 sizer1->Add(btnEraseLastPoint);
410                 sizer1->Add(btnDeleteAllPoints);
411                 sizer1->Add(txtNrPoints);
412                 sizer1->Add(txOpacity);
413                 sizer1->Add(sdrOpacity,1,wxGROW );
414                 sizer1->Add(txRadio);
415                 sizer1->Add(sdrRadio,1,wxGROW );
416                 sizer1->Add(btnSavePoints);
417                 sizer1->Add(btnLoadPoints);
418                 sizer = sizer1;
419         }
420
421           if (mbbShowNPoints->bbGetInputType()==1)
422           {
423                   // Widget interface
424                   wxButton *btnSetPositionPoint         = new wxButton( panel, -1, _T("Set nearest point"));
425                   txtNrPoints                                           = new wxStaticText(panel,-1, _T(" "));
426
427                   //NTU: Sliders for opacity and radio change
428                   wxStaticText* txOpacity                       = new wxStaticText(this, -1, wxString(_T("  Points Opacity  ")));
429                   sdrOpacity                                            = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
430                   wxStaticText* txRadio                         = new wxStaticText(this, -1, wxString(_T("  Points Radio  ")));
431                   sdrRadio                                                      = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
432                   wxFlexGridSizer *sizer1                       = new wxFlexGridSizer(1);
433                   Connect(btnSetPositionPoint->GetId()  , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
434                   Connect(sdrOpacity->GetId()   , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
435                   Connect(sdrRadio->GetId()     , wxEVT_COMMAND_SLIDER_UPDATED  , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
436
437                   sizer1->Add(btnSetPositionPoint);
438                   sizer1->Add(txtNrPoints);
439                   sizer1->Add(txOpacity);
440                   sizer1->Add(sdrOpacity,1,wxGROW );
441                   sizer1->Add(txRadio);
442                   sizer1->Add(sdrRadio,1,wxGROW );
443                   sizer = sizer1;
444           }
445     
446     if (mbbShowNPoints->bbGetInputType() == 2) 
447         {
448                 askPointLabel                                           = new wxStaticText(panel, -1, _T("\nPOINT CONTROLS:")); // JPR
449         wxButton                *btnAddPoint            = new wxButton(panel, -1,        _T("      Add  Point      "));
450         wxButton                *btnDeleteAllPoints = new wxButton(panel, -1, _T("      Delete All      "));
451         wxStaticText    *spacer                         = new wxStaticText(panel, -1, _T("\n")); // JPR
452                                          textCtrl                       = new wxTextCtrl(panel, -1);
453         wxFlexGridSizer *sizer1                         = new wxFlexGridSizer(1);
454         Connect(btnAddPoint->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) & WidgetShowNPoints::OnAddPoint);
455         Connect(btnDeleteAllPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) & WidgetShowNPoints::OnDeleteAllPoints);    
456         sizer1->Add(askPointLabel); 
457         sizer1->Add(btnAddPoint);
458         sizer1->Add(btnDeleteAllPoints);
459         sizer1->Add(spacer);
460         sizer1->Add(textCtrl);
461         sdrOpacity = new wxSlider();
462         sdrRadio = new wxSlider();
463         txtNrPoints = new wxStaticText(panel, -1, _T("\n\n\n"));
464         sizer = sizer1;
465         } // bbGetInputType 2
466
467         if (sizer!=NULL)
468         {
469                 panel->SetSizer(sizer);
470                 panel->SetAutoLayout(true);
471                 panel->Layout();
472         } // if sizer
473 }
474
475 //------------------------------------------------------------------------
476 WidgetShowNPoints::~WidgetShowNPoints()
477 {
478 }
479
480 //------------------------------------------------------------------------
481 ModelShowNPoints* WidgetShowNPoints::GetModelShowNPoints()
482 {
483         return mmodelShowNPoints;
484 }
485
486
487 //------------------------------------------------------------------------
488 void WidgetShowNPoints::SetRadio(double radio)
489 {
490         GetModelShowNPoints()->SetRadio(radio);
491         //NTU: For Slider
492         sdrRadio->SetValue(radio);
493 }
494
495 //------------------------------------------------------------------------
496 void WidgetShowNPoints::SetColour(std::vector<double> colour)
497 {
498         this->mcolour = colour;
499 }
500
501 //------------------------------------------------------------------------
502 void WidgetShowNPoints::SetImage(vtkImageData* image)
503 {
504         GetModelShowNPoints()->SetImage(image);
505 }
506
507 //------------------------------------------------------------------------
508 void WidgetShowNPoints::SetOpacity(double opacity)
509 {
510         this->mopacity=opacity;
511         //NTU: For Slider
512         sdrOpacity->SetValue(this->mopacity*100.0);
513 }
514
515
516 //------------------------------------------------------------------------
517 void  WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
518 {
519         this->renderer  = renderer;
520 }
521         
522 //------------------------------------------------------------------------
523         
524 void WidgetShowNPoints::RefreshPoint(int id)
525 {
526         double spc[3];
527
528 //EED 2016/06/17
529 //      mimage->GetSpacing(spc);
530 //      int x = lstPointsX[id];
531 //      int y = lstPointsY[id];
532 //      int z = lstPointsZ[id];
533         int x,y,z;
534         GetModelShowNPoints()->GetIdPoint(id,&x,&y,&z);
535         GetModelShowNPoints()->GetImage()->GetSpacing(spc);
536         std::string label       = GetModelShowNPoints()->GetIdLabel(id);
537         double radio            = GetModelShowNPoints()->GetRadio();
538
539         lstActorsSphere[id]->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
540         lstActorsSphere[id]->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
541         lstActorsSphere[id]->GetProperty()->SetOpacity( mopacity );
542         lstSourceSphere[id]->SetRadius( radio );
543
544         lstActorsText[id]->SetInput( label.c_str()  );
545         lstActorsText[id]->SetPosition(  radio+spc[0]*x , spc[1]*y , spc[2]*z );
546 }
547
548 //------------------------------------------------------------------------
549 void WidgetShowNPoints::RefreshPoints()
550 {
551         int id,size=lstActorsSphere.size();
552         for (id=0;id<size;id++)
553         {
554                 RefreshPoint(id);
555         } // for
556         renderer->GetRenderWindow()->Render();
557 }
558
559 //------------------------------------------------------------------------
560 void WidgetShowNPoints::AddVtkPoint()
561 {
562         // Sphere
563         vtkSphereSource *vtksphere = vtkSphereSource::New();
564         vtksphere->SetThetaResolution (20);
565         vtksphere->SetPhiResolution (20);
566         vtksphere->SetRadius( 1 );
567         //NTU: For updating points
568         lstSourceSphere.push_back(vtksphere);
569         vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
570         sphereMapper->SetInput( vtksphere->GetOutput() );
571         vtkActor *sphereActor   = vtkActor::New();
572         sphereActor->SetMapper(sphereMapper);
573         sphereActor->SetOrigin(0, 0, 0);
574
575         lstActorsSphere.push_back(sphereActor);
576         if(renderer==NULL){
577                 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
578                 dialog.ShowModal();             
579                 return;
580         }
581         renderer->AddActor( sphereActor );
582         // Actor
583         vtkTextActor3D *textActor = vtkTextActor3D::New();
584 //      textActor->SetInput( strLabel.c_str()  );
585         renderer->AddActor( textActor );
586         lstActorsText.push_back(textActor);
587 }
588
589
590 //------------------------------------------------------------------------
591 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
592 {
593         GetModelShowNPoints()->AddPoint(x,y,z, label );
594         AddVtkPoint();
595         RefreshPoint(lstActorsSphere.size()-1);
596 }
597
598
599
600 //------------------------------------------------------------------------
601 void WidgetShowNPoints::InsertPoint(int x, int y, int z, std::string label)//CFT
602 {
603
604
605 //--
606         if ( GetModelShowNPoints()->InsertPoint(x,y,z,label) != -1 )
607         {
608 /*
609                 // Sphere
610                 vtkSphereSource *vtksphere              = vtkSphereSource::New();
611                 vtksphere->SetThetaResolution (20);
612                 vtksphere->SetPhiResolution (20);
613                 vtksphere->SetRadius( mradio  );
614
615                 //NTU: For updating points
616                 std::vector<vtkSphereSource*>::iterator itSS;
617                 itSS = lstSourceSphere.begin();
618                 lstSourceSphere.insert( itSS+pos, vtksphere);
619
620                 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
621                 sphereMapper->SetInput( vtksphere->GetOutput() );
622                 vtkActor *sphereActor   = vtkActor::New();
623                 sphereActor->SetMapper(sphereMapper);
624                 sphereActor->SetOrigin(0, 0, 0);
625
626                 std::vector<vtkActor*>::iterator itAS;
627                 itAS = lstActorsSphere.begin();
628                 lstActorsSphere.insert( itAS+pos, sphereActor);
629                 if(renderer==NULL){
630                         wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
631                         dialog.ShowModal();             
632                         return;
633                 }
634                 renderer->AddActor( sphereActor );
635
636                 // Actor
637                 vtkTextActor3D *textActor = vtkTextActor3D::New();
638
639                 textActor->SetInput( strLabel.c_str()  );
640                 renderer->AddActor( textActor );
641                 std::vector<vtkTextActor3D*>::iterator itTA;
642                 itTA = lstActorsText.begin();
643                 lstActorsText.insert( itTA+pos , textActor);
644 */
645
646                 AddVtkPoint();
647         } else {
648                 AddPoint(x,y,z,label);
649         }
650         RefreshPoints();
651         //end if
652 }
653
654
655 //------------------------------------------------------------------------
656 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
657 {       
658         if (this->renderer==NULL)
659         { 
660                 return;
661         }
662
663         std::vector<int> point = GetModelShowNPoints()->GetReferencePoint();
664         if (point.size()==3)
665         {
666                 AddPoint(point[0],point[1],point[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
667                 SetOutputBox();
668                 renderer->GetRenderWindow()->Render();
669         } else {//mpoint.size
670                 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
671         }
672 }
673
674 //------------------------------------------------------------------------
675 void WidgetShowNPoints::OnInsertPoint (wxCommandEvent& event)//CFT
676 {       
677         if (this->renderer==NULL)
678         { 
679                 return;
680         }
681
682         std::vector<int> point = GetModelShowNPoints()->GetReferencePoint();
683         if (point.size()==3)
684         {
685                 InsertPoint(point[0],point[1],point[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
686                 SetOutputBox();
687         } else {//mpoint.size
688                 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
689         }
690 }
691
692
693 //------------------------------------------------------------------------
694         void WidgetShowNPoints::SetOutputBox()
695         {
696 //EED           renderer->GetRenderWindow()->Render();
697                 wxString strTmp;
698                 strTmp.Printf(_T("Nbr of points: %d"), GetModelShowNPoints()->GetLstPointsSize() );
699                 txtNrPoints->SetLabel(  strTmp );
700                 //--BBTK
701                 mbbShowNPoints->bbSetOutputlstPointsX( GetModelShowNPoints()->GetLstPointsX() );
702                 mbbShowNPoints->bbSetOutputlstPointsY( GetModelShowNPoints()->GetLstPointsY() );
703                 mbbShowNPoints->bbSetOutputlstPointsZ( GetModelShowNPoints()->GetLstPointsZ() );
704                 mbbShowNPoints->bbSetOutputlstLabels( GetModelShowNPoints()->GetLstLabels() );
705                 mbbShowNPoints->bbSignalOutputModification();
706         }
707
708
709 //------------------------------------------------------------------------
710         void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
711         {
712                 wxFileDialog* FD = new wxFileDialog( 0,
713                                             _T("Save points .."),
714                                             _T(""),
715                                             _T(""),
716                                             _T("(*.xls)|*.xls"),
717                                             wxSAVE | wxOVERWRITE_PROMPT,
718                                             wxDefaultPosition);
719                 //EED
720
721                 int result_FD = FD->ShowModal();
722                 
723                 // This line is need it by windows //EED
724                 FD->SetReturnCode( result_FD );
725
726                 if (FD->GetReturnCode()==wxID_OK)
727                 {
728                         std::string filename= (const char*) ( FD->GetPath().mb_str() ); 
729                         GetModelShowNPoints()->SavePoints( filename );
730                 }       // dialog box
731         }
732
733
734 //------------------------------------------------------------------------
735         void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
736         {
737                 wxFileDialog* FD = new wxFileDialog( 0,
738                                              _T("Load points .."),
739                                              _T(""),
740                                              _T(""),
741                                              _T("(*.xls)|*.xls"),
742                                              wxOPEN | wxFILE_MUST_EXIST,
743                                              wxDefaultPosition);
744                 int i;
745                 //EED
746                 int result_FD = FD->ShowModal();
747                 // This line is need it by windows //EED
748                 FD->SetReturnCode( result_FD );
749                 if (FD->GetReturnCode()==wxID_OK)
750                 {
751
752                         std::string filename= (const char*) ( FD->GetPath().mb_str() ); 
753                         int numberPointsRead = GetModelShowNPoints()->ReadPoints( filename );
754                         for (i=0;i<numberPointsRead;i++)
755                         {
756                                 AddVtkPoint();
757                         }// for
758                         SetOutputBox();
759                         RefreshPoints();
760                 }       // dialog box
761
762         }
763
764         //------------------------------------------------------------------------
765         void WidgetShowNPoints::OnSetPoint(wxCommandEvent& event)
766         {
767                 int id=GetModelShowNPoints()->GetNearestPoint();
768                 if((id==-1) && (mbbShowNPoints->bbGetInputType()==1))
769                 {
770                         id=0;
771                         AddPoint(0,0,0,"");
772                         SetOutputBox();
773                 }
774         
775                 if (id>=0)
776                 {
777                         GetModelShowNPoints()->SetPointId_mReferencePoint(id);
778                         RefreshPoint(id);
779                         renderer->GetRenderWindow()->Render();
780                 } // if id
781                 SetOutputBox();
782         }
783         
784 //------------------------------------------------------------------------
785         void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
786         {
787                 int id = GetModelShowNPoints()->RenamePoint( (const char*) ( textCtrl->GetValue().mb_str() ) );
788                 if (id>=0)
789                 {
790                         lstActorsText[id]->SetInput(  (const char*) ( textCtrl->GetValue().mb_str() ) );
791                         SetOutputBox();
792                         renderer->GetRenderWindow()->Render();
793                 }
794         }
795
796 //------------------------------------------------------------------------
797         void WidgetShowNPoints::ErasePoint(int id)
798         {
799                 if (this->renderer!=NULL)
800                 {
801                         if (id>=0)
802                         {
803                                 renderer->RemoveActor( lstActorsSphere[id] );
804                                 renderer->RemoveActor( lstActorsText[id] );
805                                 lstActorsSphere[id]->Delete();
806                                 lstActorsText[id]->Delete();
807                                 lstSourceSphere[id]->Delete();
808                                 lstActorsSphere.erase( lstActorsSphere.begin()+id );
809                                 lstActorsText.erase( lstActorsText.begin()+id );
810                                 lstSourceSphere.erase( lstSourceSphere.begin()+id );
811                                 GetModelShowNPoints()->ErasePoint(id);
812                         } // if id
813                 } // if renderer
814         }
815
816 //------------------------------------------------------------------------
817         void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
818         {
819                 ErasePoint( GetModelShowNPoints()->IdInsidePoint() );
820                 SetOutputBox();
821                 renderer->GetRenderWindow()->Render();
822         }
823
824 //------------------------------------------------------------------------
825         void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
826         {
827                 ErasePoint(lstActorsSphere.size()-1);
828                 SetOutputBox();
829                 renderer->GetRenderWindow()->Render();
830         }
831
832 //------------------------------------------------------------------------
833 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
834 {
835         int id,size=lstActorsSphere.size();
836         for (id=size-1;id>=0;id--)
837         {
838                 ErasePoint(id);
839         }
840         SetOutputBox();
841         renderer->GetRenderWindow()->Render();
842 }
843
844 //NTU: Method for updating points opacity and Radio
845
846 //------------------------------------------------------------------------
847 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
848 {
849         //Difference in Radio for text placement
850         double radio=GetModelShowNPoints()->GetRadio();
851         this->mopacity = sdrOpacity->GetValue()/100.0;
852         GetModelShowNPoints()->SetRadio( sdrRadio->GetValue() ) ;
853         radio = sdrRadio->GetValue();
854         //NTU refresh the inputs
855         mbbShowNPoints->bbSetInputOpacity(this->mopacity);
856         mbbShowNPoints->bbSetInputRadio( radio );
857         // EED 
858         RefreshPoints();
859 }
860
861 //------------------------------------------------------------------------
862 void  WidgetShowNPoints::SetReferencePoint(std::vector<int> point)
863 {
864         GetModelShowNPoints()->SetReferencePoint(point);
865 }
866
867 //------------------------------------------------------------------------
868 void  WidgetShowNPoints::SetInitLstPoints( std::vector<int> initLstPointsX,  std::vector<int> initLstPointsY, std::vector<int> initLstPointsZ, std::vector<std::string> initLstLabels )
869 {
870         if (this->renderer==NULL)
871         { 
872                 return;
873         }
874
875         int i,sizeX,sizeY,sizeZ,sizeLabels;
876         sizeX=(int)initLstPointsX.size();
877         sizeY=(int)initLstPointsY.size();
878         sizeZ=(int)initLstPointsZ.size();
879         sizeLabels=(int)initLstLabels.size();
880
881         int x,y,z;
882         std::string label;
883
884         if ( (sizeX==sizeY) && (sizeX==sizeZ) )
885         {
886
887                 for (i=0;i<sizeX;i++)
888                 {
889                         x               = initLstPointsX[i];
890                         y               = initLstPointsY[i];
891                         z               = initLstPointsZ[i];
892                         if (i<sizeLabels) 
893                         {
894                                 label   = initLstLabels[i];
895                         } else {
896                                 label="";
897                         }
898                         AddPoint( x,y,z,label );
899                 } // for i
900                 //      SetOutputBox(); 
901                 //      renderer->GetRenderWindow()->Render();
902
903         } // if size
904 }
905
906
907
908
909 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
910 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
911
912 //-----------------------------------------------------------------
913 void ShowNPoints::Process()
914 {
915         if (mwxwidget!=NULL)
916         {
917                 mwxwidget->SetRenderer( bbGetInputRenderer() );
918                 mwxwidget->SetReferencePoint( bbGetInputIn() );
919                 mwxwidget->SetImage( bbGetInputImage() );
920                 mwxwidget->SetColour( bbGetInputColour() );
921                 mwxwidget->SetOpacity( bbGetInputOpacity() );
922                 mwxwidget->SetRadio( bbGetInputRadio() );
923
924                 if (firsttime==true)
925                 {
926                         firsttime=false;
927                         mwxwidget->SetInitLstPoints( bbGetInputInitLstPointsX() , bbGetInputInitLstPointsY() , bbGetInputInitLstPointsZ() , bbGetInputInitLstLabels() );
928                 }
929
930                 bbSetOutputlstPointsX( mwxwidget->GetModelShowNPoints()->GetLstPointsX() );
931                 bbSetOutputlstPointsY( mwxwidget->GetModelShowNPoints()->GetLstPointsY() );
932                 bbSetOutputlstPointsZ( mwxwidget->GetModelShowNPoints()->GetLstPointsZ() );
933                 bbSetOutputlstLabels( mwxwidget->GetModelShowNPoints()->GetLstLabels() );
934         } // mwxwidget
935 }
936
937 //-----------------------------------------------------------------
938 void ShowNPoints::CreateWidget(wxWindow* parent)
939 {
940         mwxwidget = new WidgetShowNPoints( parent, this);
941     bbSetOutputWidget( mwxwidget );
942 }
943
944 //-----------------------------------------------------------------
945 void ShowNPoints::bbUserSetDefaultValues()
946 {
947         firsttime = true;
948         mwxwidget = NULL;
949
950         bbSetInputRadio(10);
951         bbSetInputOpacity(1);
952
953         std::vector<double> colour;
954         colour.push_back(1.0);
955         colour.push_back(0.0);
956         colour.push_back(0.0);
957         bbSetInputColour(colour);
958
959         bbSetInputImage(NULL);
960         bbSetInputType(0);
961         bbSetInputRenderer(NULL);
962 }
963
964 //-----------------------------------------------------------------
965         void ShowNPoints::bbUserInitializeProcessing()
966         {
967         }
968
969 //-----------------------------------------------------------------
970         void ShowNPoints::bbUserFinalizeProcessing()
971         {
972         }
973
974 //-----------------------------------------------------------------
975
976 }
977 // EO namespace bbcreaMaracasVisu