1 #include "bbmaracasvisuShowNPoints.h"
2 #include "bbcreaMaracasVisuPackage.h"
4 #include "vtkProperty.h"
5 #include "vtkSphereSource.h"
6 #include "vtkPolyDataMapper.h"
7 #include "vtkRenderWindow.h"
8 #include "vtkTextActor3D.h"
10 namespace bbcreaMaracasVisu
14 //----------------------------------------------------------------------
15 WidgetShowNPoints::WidgetShowNPoints(wxWindow *parent, bbcreaMaracasVisu::ShowNPoints *box)
16 : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
19 this->renderer = NULL;
20 wxPanel *panel = this;
21 wxSizer *sizer = NULL;
25 textCtrl = new wxTextCtrl(panel, -1);
26 wxButton *btnAddPoint = new wxButton( panel, -1, _T("Add Point"));
27 wxButton *btnRenamePoint = new wxButton( panel, -1, _T("Rename point"));
28 wxButton *btnEraseLastPoint = new wxButton( panel, -1, _T("Erase Last point"));
29 wxButton *btnErasePoint = new wxButton( panel, -1, _T("Erase point"));
30 wxButton *btnDeleteAllPoints = new wxButton( panel, -1, _T("Delete all points"));
31 wxButton *btnSavePoints = new wxButton( panel, -1, _T("Save points"));
32 wxButton *btnLoadPoints = new wxButton( panel, -1, _T("Load points"));
33 txtNrPoints = new wxStaticText(panel,-1, _T(" "));
35 //NTU: Sliders for opacity and radio change
37 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T(" Points Opacity ")));
38 sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
40 wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T(" Points Radio ")));
41 sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
43 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
44 // sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
46 Connect(btnAddPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
47 Connect(btnRenamePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
48 Connect(btnEraseLastPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
49 Connect(btnErasePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
50 Connect(btnDeleteAllPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
51 Connect(btnSavePoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
52 Connect(btnLoadPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
55 Connect(sdrOpacity->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
56 Connect(sdrRadio->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
58 sizer1->Add(textCtrl);
59 sizer1->Add(btnAddPoint);
60 sizer1->Add(btnRenamePoint);
61 sizer1->Add(btnErasePoint);
62 sizer1->Add(btnEraseLastPoint);
63 sizer1->Add(btnDeleteAllPoints);
64 sizer1->Add(txtNrPoints);
65 sizer1->Add(txOpacity);
66 sizer1->Add(sdrOpacity);
68 sizer1->Add(sdrRadio);
69 sizer1->Add(btnSavePoints);
70 sizer1->Add(btnLoadPoints);
73 panel -> SetSizer(sizer);
74 panel -> SetAutoLayout(true);
80 //------------------------------------------------------------------------
81 WidgetShowNPoints::~WidgetShowNPoints()
85 void WidgetShowNPoints::SetRadio(double radio)
89 sdrRadio->SetValue(this->mradio);
92 //------------------------------------------------------------------------
93 std::vector<int> WidgetShowNPoints::GetLstPointsX()
98 //------------------------------------------------------------------------
99 std::vector<int> WidgetShowNPoints::GetLstPointsY()
104 //------------------------------------------------------------------------
105 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
110 //------------------------------------------------------------------------
111 std::vector<std::string> WidgetShowNPoints::GetLstLabels()
119 //------------------------------------------------------------------------
120 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
125 //------------------------------------------------------------------------
126 void WidgetShowNPoints::SetColour(std::vector<double> colour)
128 this->mcolour = colour;
131 //------------------------------------------------------------------------
132 void WidgetShowNPoints::SetOpacity(double opacity)
134 this->mopacity=opacity;
136 sdrOpacity->SetValue(this->mopacity*100.0);
139 //------------------------------------------------------------------------
140 void WidgetShowNPoints::SetImage(vtkImageData *image)
145 //------------------------------------------------------------------------
146 void WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
148 this->renderer = renderer;
152 std::string WidgetShowNPoints::CleanSpaces(std::string ss)
155 while( (i=ss.find(32))>=0 )
162 //------------------------------------------------------------------------
163 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
165 lstPointsX.push_back( x );
166 lstPointsY.push_back( y );
167 lstPointsZ.push_back( z );
169 std::string strLabel = CleanSpaces( label );
171 lstLabels.push_back( strLabel );
174 vtkSphereSource *vtksphere = vtkSphereSource::New();
175 vtksphere->SetThetaResolution (20);
176 vtksphere->SetPhiResolution (20);
177 vtksphere->SetRadius( mradio );
179 //NTU: For updating points
181 lstSourceSphere.push_back(vtksphere);
183 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
184 sphereMapper->SetInput( vtksphere->GetOutput() );
185 vtkActor *sphereActor = vtkActor::New();
186 sphereActor->SetMapper(sphereMapper);
187 sphereActor->SetOrigin(0, 0, 0);
190 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
194 mimage->GetSpacing(spc);
195 sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
196 sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
197 sphereActor->GetProperty()->SetOpacity( mopacity );
199 lstActorsSphere.push_back(sphereActor);
201 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
205 renderer->AddActor( sphereActor );
208 vtkTextActor3D *textActor = vtkTextActor3D::New();
209 textActor->SetPosition( mradio+spc[0]*x , spc[1]*y , spc[2]*z );
210 textActor->SetInput( strLabel.c_str() );
211 renderer->AddActor( textActor );
212 lstActorsText.push_back(textActor);
218 //------------------------------------------------------------------------
219 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
221 if (this->renderer!=NULL){
222 if (mpoint.size()==3){
223 AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
224 } else {//mpoint.size
225 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
231 //------------------------------------------------------------------------
232 void WidgetShowNPoints::SetOutputBox()
234 renderer->GetRenderWindow()->Render();
236 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
237 txtNrPoints->SetLabel( strTmp );
240 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
241 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
242 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
243 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
244 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
245 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
246 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
247 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
250 //------------------------------------------------------------------------
251 void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
254 wxFileDialog* FD = new wxFileDialog( 0,
255 _T("Save points .."),
259 wxSAVE | wxOVERWRITE_PROMPT,
263 int result_FD = FD->ShowModal();
265 // This line is need it by windows //EED
266 FD->SetReturnCode( result_FD );
268 if (FD->GetReturnCode()==wxID_OK)
271 std::string filename= (const char*) ( FD->GetPath().mb_str() );
272 ff = fopen( filename.c_str() , "w+" );
275 int i , size = (int) (lstActorsSphere.size());
276 fprintf(ff,"NumberOfPoints %d \n",size);
277 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
280 for (i=0; i<size; i++)
285 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
286 fprintf(ff,"%d\t %d\t %d\t %f\t %s\n", x , y , z , value , lstLabels[i].c_str());
290 printf("WidgetShowNPoints::OnSavePoints ...Error... creating file");
296 //------------------------------------------------------------------------
297 void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
299 wxFileDialog* FD = new wxFileDialog( 0,
300 _T("Load points .."),
304 wxOPEN | wxFILE_MUST_EXIST,
308 int result_FD = FD->ShowModal();
310 // This line is need it by windows //EED
311 FD->SetReturnCode( result_FD );
313 if (FD->GetReturnCode()==wxID_OK)
316 std::string filename= (const char*) ( FD->GetPath().mb_str() );
317 ff = fopen( filename.c_str() , "r+" );
322 fscanf(ff," %s %d",chartmp,&size);
323 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
327 for (i=0; i<size; i++)
329 fscanf(ff," %d %d %d %f %s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
330 AddPoint(x,y,z, chartmp );
334 printf("WidgetShowNPoints::OnLoadPoints ...Error... reading file");
341 int WidgetShowNPoints::GetNearestPoint()
344 int i, size=(int)(lstActorsSphere.size());
348 wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
353 mimage->GetSpacing(spc);
355 for ( i=0 ; i<size; i++ )
357 double rx = spc[0]*(mpoint[0] - lstPointsX [i]);
358 double ry = spc[1]*(mpoint[1] - lstPointsY [i]);
359 double rz = spc[2]*(mpoint[2] - lstPointsZ [i]);
360 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
370 //------------------------------------------------------------------------
371 void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
373 int id=GetNearestPoint();
376 std::string strLabel = CleanSpaces( (const char*) ( textCtrl->GetValue().mb_str() ) );
377 lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
378 lstActorsText[id]->SetInput( lstLabels[id].c_str() );
383 //------------------------------------------------------------------------
384 void WidgetShowNPoints::ErasePoint(int id)
386 if (this->renderer!=NULL){
388 renderer->RemoveActor( lstActorsSphere[id] );
389 renderer->RemoveActor( lstActorsText[id] );
390 lstActorsSphere[id]->Delete();
391 lstActorsText[id]->Delete();
392 lstSourceSphere[id]->Delete();
393 lstActorsSphere.erase( lstActorsSphere.begin()+id );
394 lstActorsText.erase( lstActorsText.begin()+id );
395 lstSourceSphere.erase( lstSourceSphere.begin()+id );
397 lstPointsX.erase( lstPointsX.begin()+id );
398 lstPointsY.erase( lstPointsY.begin()+id );
399 lstPointsZ.erase( lstPointsZ.begin()+id );
400 lstLabels.erase( lstLabels.begin()+id );
408 //------------------------------------------------------------------------
409 void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
411 ErasePoint( GetNearestPoint() );
414 //------------------------------------------------------------------------
415 void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
417 ErasePoint(lstActorsSphere.size()-1);
421 //------------------------------------------------------------------------
422 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
424 int id,size=lstActorsSphere.size();
425 for (id=size-1;id>=0;id--)
432 //NTU: Method for updating points opacity and Radio
434 //------------------------------------------------------------------------
435 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
437 //Difference in Radio for text placement
438 int difradio = sdrRadio->GetValue() - this->mradio;
440 this->mopacity = sdrOpacity->GetValue()/100.0;
441 this->mradio = sdrRadio->GetValue();
443 //NTU refresh the inputs
444 mbbShowNPoints->bbSetInputOpacity(this->mopacity);
445 mbbShowNPoints->bbSetInputRadio(this->mradio);
447 int size = (int) this->lstActorsSphere.size();
449 for(i = 0; i < size; i ++)
451 this->lstSourceSphere[i]->SetRadius(mradio);
452 this->lstActorsSphere[i]->GetProperty()->SetOpacity(mopacity);
453 this->lstActorsText[i]->SetPosition(this->lstActorsText[i]->GetPosition()[0]+difradio,this->lstActorsText[i]->GetPosition()[1], this->lstActorsText[i]->GetPosition()[2]);
462 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
463 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
465 void ShowNPoints::Process()
467 if (mwxwidget!=NULL){
468 mwxwidget->SetRenderer( bbGetInputRenderer() );
469 mwxwidget->SetPoint( bbGetInputIn() );
470 mwxwidget->SetImage( bbGetInputImage() );
471 mwxwidget->SetColour( bbGetInputColour() );
472 mwxwidget->SetOpacity( bbGetInputOpacity() );
473 mwxwidget->SetRadio( bbGetInputRadio() );
475 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
476 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
477 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
478 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
483 void ShowNPoints::CreateWidget(wxWindow* parent)
485 mwxwidget = new WidgetShowNPoints( parent , this);
486 mwxwidget->SetPoint( bbGetInputIn() );
488 if (bbGetInputImage()==NULL)
490 printf("Missing Image (ShowNPoints) \n");
493 bbSetOutputWidget( mwxwidget );
496 void ShowNPoints::bbUserSetDefaultValues()
501 bbSetInputOpacity(1);
503 std::vector<double> colour;
504 colour.push_back(1.0);
505 colour.push_back(0.0);
506 colour.push_back(0.0);
507 bbSetInputColour(colour);
509 bbSetInputImage(NULL);
510 bbSetInputRenderer(NULL);
513 //-----------------------------------------------------------------
514 void ShowNPoints::bbUserInitializeProcessing()
518 //-----------------------------------------------------------------
519 void ShowNPoints::bbUserFinalizeProcessing()
523 //-----------------------------------------------------------------
526 // EO namespace bbcreaMaracasVisu