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
13 //----------------------------------------------------------------------
14 WidgetShowNPoints::WidgetShowNPoints(wxWindow *parent, bbcreaMaracasVisu::ShowNPoints *box)
15 : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
18 this->renderer = NULL;
19 wxPanel *panel = this;
20 wxSizer *sizer = NULL;
22 if (mbbShowNPoints->bbGetInputType()==0)
25 askPointLabel = new wxStaticText(panel, -1, _T("Point label :")); // JPR
26 textCtrl = new wxTextCtrl(panel, -1);
27 wxButton *btnAddPoint = new wxButton( panel, -1, _T("Add Point"));
28 wxButton *btnSetPositionPoint = new wxButton( panel, -1, _T("Set nearest point"));
29 wxButton *btnRenamePoint = new wxButton( panel, -1, _T("Rename point"));
30 wxButton *btnEraseLastPoint = new wxButton( panel, -1, _T("Erase Last point"));
31 wxButton *btnErasePoint = new wxButton( panel, -1, _T("Erase point"));
32 wxButton *btnDeleteAllPoints = new wxButton( panel, -1, _T("Delete all points"));
33 wxButton *btnSavePoints = new wxButton( panel, -1, _T("Save points"));
34 wxButton *btnLoadPoints = new wxButton( panel, -1, _T("Load points"));
35 txtNrPoints = new wxStaticText(panel,-1, _T(" "));
37 //NTU: Sliders for opacity and radio change
38 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T(" Points Opacity ")));
39 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(btnSetPositionPoint->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
48 Connect(btnRenamePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
49 Connect(btnEraseLastPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
50 Connect(btnErasePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
51 Connect(btnDeleteAllPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
52 Connect(btnSavePoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
53 Connect(btnLoadPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
56 Connect(sdrOpacity->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
57 Connect(sdrRadio->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
59 sizer1->Add(askPointLabel); // JPR
60 sizer1->Add(textCtrl);
61 sizer1->Add(btnAddPoint);
62 sizer1->Add(btnSetPositionPoint);
63 sizer1->Add(btnRenamePoint);
64 sizer1->Add(btnErasePoint);
65 sizer1->Add(btnEraseLastPoint);
66 sizer1->Add(btnDeleteAllPoints);
67 sizer1->Add(txtNrPoints);
68 sizer1->Add(txOpacity);
69 sizer1->Add(sdrOpacity,1,wxGROW );
71 sizer1->Add(sdrRadio,1,wxGROW );
72 sizer1->Add(btnSavePoints);
73 sizer1->Add(btnLoadPoints);
77 if (mbbShowNPoints->bbGetInputType()==1)
80 wxButton *btnSetPositionPoint = new wxButton( panel, -1, _T("Set nearest point"));
81 txtNrPoints = new wxStaticText(panel,-1, _T(" "));
83 //NTU: Sliders for opacity and radio change
84 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T(" Points Opacity ")));
85 sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
86 wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T(" Points Radio ")));
87 sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
88 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
89 Connect(btnSetPositionPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
90 Connect(sdrOpacity->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
91 Connect(sdrRadio->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
93 sizer1->Add(btnSetPositionPoint);
94 sizer1->Add(txtNrPoints);
95 sizer1->Add(txOpacity);
96 sizer1->Add(sdrOpacity,1,wxGROW );
98 sizer1->Add(sdrRadio,1,wxGROW );
104 panel->SetSizer(sizer);
105 panel->SetAutoLayout(true);
110 //------------------------------------------------------------------------
111 WidgetShowNPoints::~WidgetShowNPoints()
115 void WidgetShowNPoints::SetRadio(double radio)
119 sdrRadio->SetValue(this->mradio);
122 //------------------------------------------------------------------------
123 std::vector<int> WidgetShowNPoints::GetLstPointsX()
128 //------------------------------------------------------------------------
129 std::vector<int> WidgetShowNPoints::GetLstPointsY()
134 //------------------------------------------------------------------------
135 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
140 //------------------------------------------------------------------------
141 std::vector<std::string> WidgetShowNPoints::GetLstLabels()
146 //------------------------------------------------------------------------
147 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
152 //------------------------------------------------------------------------
153 void WidgetShowNPoints::SetColour(std::vector<double> colour)
155 this->mcolour = colour;
158 //------------------------------------------------------------------------
159 void WidgetShowNPoints::SetOpacity(double opacity)
161 this->mopacity=opacity;
163 sdrOpacity->SetValue(this->mopacity/100.0);
166 //------------------------------------------------------------------------
167 void WidgetShowNPoints::SetImage(vtkImageData *image)
172 //------------------------------------------------------------------------
173 void WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
175 this->renderer = renderer;
178 //------------------------------------------------------------------------
180 std::string WidgetShowNPoints::CleanSpaces(std::string ss)
183 while( (i=ss.find(32))>=0 )
191 //------------------------------------------------------------------------
193 void WidgetShowNPoints::RefreshPoint(int id)
195 int x = lstPointsX[id];
196 int y = lstPointsY[id];
197 int z = lstPointsZ[id];
200 mimage->GetSpacing(spc);
202 lstActorsSphere[id]->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
203 lstActorsSphere[id]->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
204 lstActorsSphere[id]->GetProperty()->SetOpacity( mopacity );
206 lstActorsText[id]->SetPosition( mradio+spc[0]*x , spc[1]*y , spc[2]*z );
210 //------------------------------------------------------------------------
211 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
213 lstPointsX.push_back( x );
214 lstPointsY.push_back( y );
215 lstPointsZ.push_back( z );
217 std::string strLabel = CleanSpaces( label );
219 lstLabels.push_back( strLabel );
222 vtkSphereSource *vtksphere = vtkSphereSource::New();
223 vtksphere->SetThetaResolution (20);
224 vtksphere->SetPhiResolution (20);
225 vtksphere->SetRadius( mradio );
227 //NTU: For updating points
229 lstSourceSphere.push_back(vtksphere);
231 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
232 sphereMapper->SetInput( vtksphere->GetOutput() );
233 vtkActor *sphereActor = vtkActor::New();
234 sphereActor->SetMapper(sphereMapper);
235 sphereActor->SetOrigin(0, 0, 0);
241 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
245 mimage->GetSpacing(spc);
246 sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
247 sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
248 sphereActor->GetProperty()->SetOpacity( mopacity );
251 lstActorsSphere.push_back(sphereActor);
253 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
257 renderer->AddActor( sphereActor );
260 vtkTextActor3D *textActor = vtkTextActor3D::New();
262 textActor->SetPosition( mradio+spc[0]*x , spc[1]*y , spc[2]*z );
264 textActor->SetInput( strLabel.c_str() );
265 renderer->AddActor( textActor );
266 lstActorsText.push_back(textActor);
268 RefreshPoint(lstPointsX.size()-1);
272 //------------------------------------------------------------------------
273 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
276 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
281 if (this->renderer!=NULL){
282 if (mpoint.size()==3){
283 AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
284 } else {//mpoint.size
285 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
290 //------------------------------------------------------------------------
291 void WidgetShowNPoints::SetOutputBox()
293 renderer->GetRenderWindow()->Render();
295 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
296 txtNrPoints->SetLabel( strTmp );
299 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
300 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
301 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
302 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
303 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
304 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
305 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
306 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
309 //------------------------------------------------------------------------
310 void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
312 wxFileDialog* FD = new wxFileDialog( 0,
313 _T("Save points .."),
317 wxSAVE | wxOVERWRITE_PROMPT,
321 int result_FD = FD->ShowModal();
323 // This line is need it by windows //EED
324 FD->SetReturnCode( result_FD );
326 if (FD->GetReturnCode()==wxID_OK)
329 std::string filename= (const char*) ( FD->GetPath().mb_str() );
330 ff = fopen( filename.c_str() , "w+" );
333 int i , size = (int) (lstActorsSphere.size());
334 fprintf(ff,"NumberOfPoints %d \n",size);
335 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
338 for (i=0; i<size; i++)
343 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
344 fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value , lstLabels[i].c_str());
348 printf("WidgetShowNPoints::OnSavePoints ...Error... creating file");
353 //------------------------------------------------------------------------
354 void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
356 wxFileDialog* FD = new wxFileDialog( 0,
357 _T("Load points .."),
361 wxOPEN | wxFILE_MUST_EXIST,
365 int result_FD = FD->ShowModal();
367 // This line is need it by windows //EED
368 FD->SetReturnCode( result_FD );
370 if (FD->GetReturnCode()==wxID_OK)
373 std::string filename= (const char*) ( FD->GetPath().mb_str() );
374 ff = fopen( filename.c_str() , "r+" );
379 fscanf(ff," %s %d",chartmp,&size);
380 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
384 for (i=0; i<size; i++)
386 fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
387 AddPoint(x,y,z, chartmp );
391 printf("WidgetShowNPoints::OnLoadPoints ...Error... reading file");
398 //------------------------------------------------------------------------
400 int WidgetShowNPoints::GetNearestPoint()
403 int i, size=(int)(lstActorsSphere.size());
404 double radioMin=10000000;
407 wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
412 for ( i=0 ; i<size; i++ )
414 double rx = mpoint[0] - lstPointsX [i];
415 double ry = mpoint[1] - lstPointsY [i];
416 double rz = mpoint[2] - lstPointsZ [i];
417 double radio = rx*rx + ry*ry + rz*rz;
418 if ( radio <= radioMin)
430 //------------------------------------------------------------------------
431 void WidgetShowNPoints::OnSetPoint(wxCommandEvent& event)
433 int id=GetNearestPoint();
434 if((id==-1) && (mbbShowNPoints->bbGetInputType()==1))
443 printf("WidgetShowNPoints::OnSetPoint \n");
444 lstPointsX[id] = mpoint[0];
445 lstPointsY[id] = mpoint[1];
446 lstPointsZ[id] = mpoint[2];
449 this->lstActorsSphere[id]->SetPosition( spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
450 this->lstActorsText[id]->SetPosition( spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
457 //------------------------------------------------------------------------
459 int WidgetShowNPoints::IdInsidePoint()
462 int i, size=(int)(lstActorsSphere.size());
466 wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
471 mimage->GetSpacing(spc);
473 for ( i=0 ; i<size; i++ )
475 double rx = spc[0]*(mpoint[0] - lstPointsX [i]);
476 double ry = spc[1]*(mpoint[1] - lstPointsY [i]);
477 double rz = spc[2]*(mpoint[2] - lstPointsZ [i]);
478 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
489 //------------------------------------------------------------------------
490 void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
492 int id=IdInsidePoint();
495 std::string strLabel = CleanSpaces( (const char*) ( textCtrl->GetValue().mb_str() ) );
496 lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
497 lstActorsText[id]->SetInput( lstLabels[id].c_str() );
502 //------------------------------------------------------------------------
503 void WidgetShowNPoints::ErasePoint(int id)
505 if (this->renderer!=NULL){
507 renderer->RemoveActor( lstActorsSphere[id] );
508 renderer->RemoveActor( lstActorsText[id] );
509 lstActorsSphere[id]->Delete();
510 lstActorsText[id]->Delete();
511 lstSourceSphere[id]->Delete();
512 lstActorsSphere.erase( lstActorsSphere.begin()+id );
513 lstActorsText.erase( lstActorsText.begin()+id );
514 lstSourceSphere.erase( lstSourceSphere.begin()+id );
516 lstPointsX.erase( lstPointsX.begin()+id );
517 lstPointsY.erase( lstPointsY.begin()+id );
518 lstPointsZ.erase( lstPointsZ.begin()+id );
519 lstLabels.erase( lstLabels.begin()+id );
527 //------------------------------------------------------------------------
528 void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
530 ErasePoint( IdInsidePoint() );
533 //------------------------------------------------------------------------
534 void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
536 ErasePoint(lstActorsSphere.size()-1);
539 //------------------------------------------------------------------------
540 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
542 int id,size=lstActorsSphere.size();
543 for (id=size-1;id>=0;id--)
549 //NTU: Method for updating points opacity and Radio
551 //------------------------------------------------------------------------
552 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
554 //Difference in Radio for text placement
555 int difradio = sdrRadio->GetValue() - this->mradio;
557 this->mopacity = sdrOpacity->GetValue()/100.0;
558 this->mradio = sdrRadio->GetValue();
560 //NTU refresh the inputs
561 mbbShowNPoints->bbSetInputOpacity(this->mopacity);
562 mbbShowNPoints->bbSetInputRadio(this->mradio);
564 int size = (int) this->lstActorsSphere.size();
566 for(i = 0; i < size; i ++)
568 this->lstSourceSphere[i]->SetRadius(mradio);
569 this->lstActorsSphere[i]->GetProperty()->SetOpacity(mopacity);
570 this->lstActorsText[i]->SetPosition(this->lstActorsText[i]->GetPosition()[0]+difradio,this->lstActorsText[i]->GetPosition()[1], this->lstActorsText[i]->GetPosition()[2]);
577 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
578 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
580 void ShowNPoints::Process()
584 mwxwidget->SetRenderer( bbGetInputRenderer() );
585 mwxwidget->SetPoint( bbGetInputIn() );
586 mwxwidget->SetImage( bbGetInputImage() );
587 mwxwidget->SetColour( bbGetInputColour() );
588 mwxwidget->SetOpacity( bbGetInputOpacity() );
589 mwxwidget->SetRadio( bbGetInputRadio() );
591 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
592 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
593 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
594 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
598 void ShowNPoints::CreateWidget(wxWindow* parent)
600 mwxwidget = new WidgetShowNPoints( parent, this);
601 bbSetOutputWidget( mwxwidget );
604 void ShowNPoints::bbUserSetDefaultValues()
609 bbSetInputOpacity(1);
611 std::vector<double> colour;
612 colour.push_back(1.0);
613 colour.push_back(0.0);
614 colour.push_back(0.0);
615 bbSetInputColour(colour);
617 bbSetInputImage(NULL);
619 bbSetInputRenderer(NULL);
622 //-----------------------------------------------------------------
623 void ShowNPoints::bbUserInitializeProcessing()
627 //-----------------------------------------------------------------
628 void ShowNPoints::bbUserFinalizeProcessing()
632 //-----------------------------------------------------------------
635 // EO namespace bbcreaMaracasVisu