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;
23 askPointLabel = new wxStaticText(panel, -1, _T("Point label :")); // JPR
24 textCtrl = new wxTextCtrl(panel, -1);
25 wxButton *btnAddPoint = new wxButton( panel, -1, _T("Add Point"));
26 wxButton *btnRenamePoint = new wxButton( panel, -1, _T("Rename point"));
27 wxButton *btnEraseLastPoint = new wxButton( panel, -1, _T("Erase Last point"));
28 wxButton *btnErasePoint = new wxButton( panel, -1, _T("Erase point"));
29 wxButton *btnDeleteAllPoints = new wxButton( panel, -1, _T("Delete all points"));
30 wxButton *btnSavePoints = new wxButton( panel, -1, _T("Save points"));
31 wxButton *btnLoadPoints = new wxButton( panel, -1, _T("Load points"));
32 txtNrPoints = new wxStaticText(panel,-1, _T(" "));
34 //NTU: Sliders for opacity and radio change
36 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T(" Points Opacity ")));
37 sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
39 wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T(" Points Radio ")));
40 sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
42 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
43 // sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
45 Connect(btnAddPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
46 Connect(btnRenamePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
47 Connect(btnEraseLastPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
48 Connect(btnErasePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
49 Connect(btnDeleteAllPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
50 Connect(btnSavePoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
51 Connect(btnLoadPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
54 Connect(sdrOpacity->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
55 Connect(sdrRadio->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
57 sizer1->Add(askPointLabel); // JPR
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);
78 //------------------------------------------------------------------------
79 WidgetShowNPoints::~WidgetShowNPoints()
83 void WidgetShowNPoints::SetRadio(double radio)
87 sdrRadio->SetValue(this->mradio);
90 //------------------------------------------------------------------------
91 std::vector<int> WidgetShowNPoints::GetLstPointsX()
96 //------------------------------------------------------------------------
97 std::vector<int> WidgetShowNPoints::GetLstPointsY()
102 //------------------------------------------------------------------------
103 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
108 //------------------------------------------------------------------------
109 std::vector<std::string> WidgetShowNPoints::GetLstLabels()
114 //------------------------------------------------------------------------
115 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
120 //------------------------------------------------------------------------
121 void WidgetShowNPoints::SetColour(std::vector<double> colour)
123 this->mcolour = colour;
126 //------------------------------------------------------------------------
127 void WidgetShowNPoints::SetOpacity(double opacity)
129 this->mopacity=opacity;
131 sdrOpacity->SetValue(this->mopacity*100.0);
134 //------------------------------------------------------------------------
135 void WidgetShowNPoints::SetImage(vtkImageData *image)
140 //------------------------------------------------------------------------
141 void WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
143 this->renderer = renderer;
146 //------------------------------------------------------------------------
148 std::string WidgetShowNPoints::CleanSpaces(std::string ss)
151 while( (i=ss.find(32))>=0 )
158 //------------------------------------------------------------------------
159 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
161 lstPointsX.push_back( x );
162 lstPointsY.push_back( y );
163 lstPointsZ.push_back( z );
165 std::string strLabel = CleanSpaces( label );
167 lstLabels.push_back( strLabel );
170 vtkSphereSource *vtksphere = vtkSphereSource::New();
171 vtksphere->SetThetaResolution (20);
172 vtksphere->SetPhiResolution (20);
173 vtksphere->SetRadius( mradio );
175 //NTU: For updating points
177 lstSourceSphere.push_back(vtksphere);
179 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
180 sphereMapper->SetInput( vtksphere->GetOutput() );
181 vtkActor *sphereActor = vtkActor::New();
182 sphereActor->SetMapper(sphereMapper);
183 sphereActor->SetOrigin(0, 0, 0);
186 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
190 mimage->GetSpacing(spc);
191 sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
192 sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
193 sphereActor->GetProperty()->SetOpacity( mopacity );
195 lstActorsSphere.push_back(sphereActor);
197 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
201 renderer->AddActor( sphereActor );
204 vtkTextActor3D *textActor = vtkTextActor3D::New();
205 textActor->SetPosition( mradio+spc[0]*x , spc[1]*y , spc[2]*z );
206 textActor->SetInput( strLabel.c_str() );
207 renderer->AddActor( textActor );
208 lstActorsText.push_back(textActor);
213 //------------------------------------------------------------------------
214 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
216 if (this->renderer!=NULL){
217 if (mpoint.size()==3){
218 AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
219 } else {//mpoint.size
220 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
225 //------------------------------------------------------------------------
226 void WidgetShowNPoints::SetOutputBox()
228 renderer->GetRenderWindow()->Render();
230 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
231 txtNrPoints->SetLabel( strTmp );
234 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
235 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
236 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
237 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
238 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
239 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
240 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
241 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
244 //------------------------------------------------------------------------
245 void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
247 wxFileDialog* FD = new wxFileDialog( 0,
248 _T("Save points .."),
252 wxSAVE | wxOVERWRITE_PROMPT,
256 int result_FD = FD->ShowModal();
258 // This line is need it by windows //EED
259 FD->SetReturnCode( result_FD );
261 if (FD->GetReturnCode()==wxID_OK)
264 std::string filename= (const char*) ( FD->GetPath().mb_str() );
265 ff = fopen( filename.c_str() , "w+" );
268 int i , size = (int) (lstActorsSphere.size());
269 fprintf(ff,"NumberOfPoints %d \n",size);
270 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
273 for (i=0; i<size; i++)
278 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
279 fprintf(ff,"%d\t %d\t %d\t %f\t %s\n", x , y , z , value , lstLabels[i].c_str());
283 printf("WidgetShowNPoints::OnSavePoints ...Error... creating file");
288 //------------------------------------------------------------------------
289 void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
291 wxFileDialog* FD = new wxFileDialog( 0,
292 _T("Load points .."),
296 wxOPEN | wxFILE_MUST_EXIST,
300 int result_FD = FD->ShowModal();
302 // This line is need it by windows //EED
303 FD->SetReturnCode( result_FD );
305 if (FD->GetReturnCode()==wxID_OK)
308 std::string filename= (const char*) ( FD->GetPath().mb_str() );
309 ff = fopen( filename.c_str() , "r+" );
314 fscanf(ff," %s %d",chartmp,&size);
315 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
319 for (i=0; i<size; i++)
321 fscanf(ff," %d %d %d %f %s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
322 AddPoint(x,y,z, chartmp );
326 printf("WidgetShowNPoints::OnLoadPoints ...Error... reading file");
333 //------------------------------------------------------------------------
335 int WidgetShowNPoints::GetNearestPoint()
338 int i, size=(int)(lstActorsSphere.size());
342 wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
347 mimage->GetSpacing(spc);
349 for ( i=0 ; i<size; i++ )
351 double rx = spc[0]*(mpoint[0] - lstPointsX [i]);
352 double ry = spc[1]*(mpoint[1] - lstPointsY [i]);
353 double rz = spc[2]*(mpoint[2] - lstPointsZ [i]);
354 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
364 //------------------------------------------------------------------------
365 void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
367 int id=GetNearestPoint();
370 std::string strLabel = CleanSpaces( (const char*) ( textCtrl->GetValue().mb_str() ) );
371 lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
372 lstActorsText[id]->SetInput( lstLabels[id].c_str() );
377 //------------------------------------------------------------------------
378 void WidgetShowNPoints::ErasePoint(int id)
380 if (this->renderer!=NULL){
382 renderer->RemoveActor( lstActorsSphere[id] );
383 renderer->RemoveActor( lstActorsText[id] );
384 lstActorsSphere[id]->Delete();
385 lstActorsText[id]->Delete();
386 lstSourceSphere[id]->Delete();
387 lstActorsSphere.erase( lstActorsSphere.begin()+id );
388 lstActorsText.erase( lstActorsText.begin()+id );
389 lstSourceSphere.erase( lstSourceSphere.begin()+id );
391 lstPointsX.erase( lstPointsX.begin()+id );
392 lstPointsY.erase( lstPointsY.begin()+id );
393 lstPointsZ.erase( lstPointsZ.begin()+id );
394 lstLabels.erase( lstLabels.begin()+id );
402 //------------------------------------------------------------------------
403 void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
405 ErasePoint( GetNearestPoint() );
408 //------------------------------------------------------------------------
409 void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
411 ErasePoint(lstActorsSphere.size()-1);
414 //------------------------------------------------------------------------
415 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
417 int id,size=lstActorsSphere.size();
418 for (id=size-1;id>=0;id--)
424 //NTU: Method for updating points opacity and Radio
426 //------------------------------------------------------------------------
427 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
429 //Difference in Radio for text placement
430 int difradio = sdrRadio->GetValue() - this->mradio;
432 this->mopacity = sdrOpacity->GetValue()/100.0;
433 this->mradio = sdrRadio->GetValue();
435 //NTU refresh the inputs
436 mbbShowNPoints->bbSetInputOpacity(this->mopacity);
437 mbbShowNPoints->bbSetInputRadio(this->mradio);
439 int size = (int) this->lstActorsSphere.size();
441 for(i = 0; i < size; i ++)
443 this->lstSourceSphere[i]->SetRadius(mradio);
444 this->lstActorsSphere[i]->GetProperty()->SetOpacity(mopacity);
445 this->lstActorsText[i]->SetPosition(this->lstActorsText[i]->GetPosition()[0]+difradio,this->lstActorsText[i]->GetPosition()[1], this->lstActorsText[i]->GetPosition()[2]);
452 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
453 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
455 void ShowNPoints::Process()
457 if (mwxwidget!=NULL){
458 mwxwidget->SetRenderer( bbGetInputRenderer() );
459 mwxwidget->SetPoint( bbGetInputIn() );
460 mwxwidget->SetImage( bbGetInputImage() );
461 mwxwidget->SetColour( bbGetInputColour() );
462 mwxwidget->SetOpacity( bbGetInputOpacity() );
463 mwxwidget->SetRadio( bbGetInputRadio() );
465 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
466 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
467 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
468 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
472 void ShowNPoints::CreateWidget(wxWindow* parent)
474 mwxwidget = new WidgetShowNPoints( parent, this);
475 mwxwidget->SetPoint( bbGetInputIn() );
477 if (bbGetInputImage()==NULL)
479 printf("Missing Image (ShowNPoints) \n");
482 bbSetOutputWidget( mwxwidget );
485 void ShowNPoints::bbUserSetDefaultValues()
490 bbSetInputOpacity(1);
492 std::vector<double> colour;
493 colour.push_back(1.0);
494 colour.push_back(0.0);
495 colour.push_back(0.0);
496 bbSetInputColour(colour);
498 bbSetInputImage(NULL);
499 bbSetInputRenderer(NULL);
502 //-----------------------------------------------------------------
503 void ShowNPoints::bbUserInitializeProcessing()
507 //-----------------------------------------------------------------
508 void ShowNPoints::bbUserFinalizeProcessing()
512 //-----------------------------------------------------------------
515 // EO namespace bbcreaMaracasVisu