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 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
36 // sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
38 Connect(btnAddPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
39 Connect(btnRenamePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
40 Connect(btnEraseLastPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
41 Connect(btnErasePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
42 Connect(btnDeleteAllPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
43 Connect(btnSavePoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
44 Connect(btnLoadPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
46 sizer1->Add(textCtrl);
47 sizer1->Add(btnAddPoint);
48 sizer1->Add(btnRenamePoint);
49 sizer1->Add(btnErasePoint);
50 sizer1->Add(btnEraseLastPoint);
51 sizer1->Add(btnDeleteAllPoints);
52 sizer1->Add(txtNrPoints);
53 sizer1->Add(btnSavePoints);
54 sizer1->Add(btnLoadPoints);
57 panel -> SetSizer(sizer);
58 panel -> SetAutoLayout(true);
64 //------------------------------------------------------------------------
65 WidgetShowNPoints::~WidgetShowNPoints()
69 void WidgetShowNPoints::SetRadio(double radio)
74 //------------------------------------------------------------------------
75 std::vector<int> WidgetShowNPoints::GetLstPointsX()
80 //------------------------------------------------------------------------
81 std::vector<int> WidgetShowNPoints::GetLstPointsY()
86 //------------------------------------------------------------------------
87 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
92 //------------------------------------------------------------------------
93 std::vector<std::string> WidgetShowNPoints::GetLstLabels()
101 //------------------------------------------------------------------------
102 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
107 //------------------------------------------------------------------------
108 void WidgetShowNPoints::SetColour(std::vector<double> colour)
110 this->mcolour = colour;
113 //------------------------------------------------------------------------
114 void WidgetShowNPoints::SetOpacity(double opacity)
116 this->mopacity=opacity;
119 //------------------------------------------------------------------------
120 void WidgetShowNPoints::SetImage(vtkImageData *image)
125 //------------------------------------------------------------------------
126 void WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
128 this->renderer = renderer;
132 std::string WidgetShowNPoints::CleanSpaces(std::string ss)
135 while( (i=ss.find(32))>=0 )
142 //------------------------------------------------------------------------
143 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
145 lstPointsX.push_back( x );
146 lstPointsY.push_back( y );
147 lstPointsZ.push_back( z );
149 std::string strLabel = CleanSpaces( label );
151 lstLabels.push_back( strLabel );
154 vtkSphereSource *vtksphere = vtkSphereSource::New();
155 vtksphere->SetThetaResolution (20);
156 vtksphere->SetPhiResolution (20);
157 vtksphere->SetRadius( mradio );
158 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
159 sphereMapper->SetInput( vtksphere->GetOutput() );
160 vtkActor *sphereActor = vtkActor::New();
161 sphereActor->SetMapper(sphereMapper);
162 sphereActor->SetOrigin(0, 0, 0);
164 mimage->GetSpacing(spc);
165 sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
166 sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
167 sphereActor->GetProperty()->SetOpacity( mopacity );
169 lstActorsSphere.push_back(sphereActor);
170 renderer->AddActor( sphereActor );
173 vtkTextActor3D *textActor = vtkTextActor3D::New();
174 textActor->SetPosition( mradio+spc[0]*x , spc[1]*y , spc[2]*z );
175 textActor->SetInput( strLabel.c_str() );
176 renderer->AddActor( textActor );
177 lstActorsText.push_back(textActor);
183 //------------------------------------------------------------------------
184 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
186 if (this->renderer!=NULL){
187 if (mpoint.size()==3){
188 AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
189 } else {//mpoint.size
190 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
196 //------------------------------------------------------------------------
197 void WidgetShowNPoints::SetOutputBox()
199 renderer->GetRenderWindow()->Render();
201 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
202 txtNrPoints->SetLabel( strTmp );
205 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
206 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
207 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
208 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
209 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
210 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
211 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
212 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
215 //------------------------------------------------------------------------
216 void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
219 wxFileDialog* FD = new wxFileDialog( 0,
220 _T("Save points .."),
224 wxSAVE | wxOVERWRITE_PROMPT,
228 int result_FD = FD->ShowModal();
230 // This line is need it by windows //EED
231 FD->SetReturnCode( result_FD );
233 if (FD->GetReturnCode()==wxID_OK)
236 std::string filename= (const char*) ( FD->GetPath().mb_str() );
237 ff = fopen( filename.c_str() , "w+" );
240 int i , size = (int) (lstActorsSphere.size());
241 fprintf(ff,"NumberOfPoints %d \n",size);
242 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
245 for (i=0; i<size; i++)
250 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
251 fprintf(ff,"%d\t %d\t %d\t %f\t %s\n", x , y , z , value , lstLabels[i].c_str());
255 printf("WidgetShowNPoints::OnSavePoints ...Error... creating file");
261 //------------------------------------------------------------------------
262 void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
264 wxFileDialog* FD = new wxFileDialog( 0,
265 _T("Load points .."),
269 wxOPEN | wxFILE_MUST_EXIST,
273 int result_FD = FD->ShowModal();
275 // This line is need it by windows //EED
276 FD->SetReturnCode( result_FD );
278 if (FD->GetReturnCode()==wxID_OK)
281 std::string filename= (const char*) ( FD->GetPath().mb_str() );
282 ff = fopen( filename.c_str() , "r+" );
287 fscanf(ff," %s %d",chartmp,&size);
288 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
292 for (i=0; i<size; i++)
294 fscanf(ff," %d %d %d %f %s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
295 AddPoint(x,y,z, chartmp );
299 printf("WidgetShowNPoints::OnLoadPoints ...Error... reading file");
306 int WidgetShowNPoints::GetNearestPoint()
309 int i, size=(int)(lstActorsSphere.size());
311 mimage->GetSpacing(spc);
313 for ( i=0 ; i<size; i++ )
315 double rx = spc[0]*(mpoint[0] - lstPointsX [i]);
316 double ry = spc[1]*(mpoint[1] - lstPointsY [i]);
317 double rz = spc[2]*(mpoint[2] - lstPointsZ [i]);
318 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
327 //------------------------------------------------------------------------
328 void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
330 int id=GetNearestPoint();
333 std::string strLabel = CleanSpaces( (const char*) ( textCtrl->GetValue().mb_str() ) );
334 lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
335 lstActorsText[id]->SetInput( lstLabels[id].c_str() );
340 //------------------------------------------------------------------------
341 void WidgetShowNPoints::ErasePoint(int id)
343 if (this->renderer!=NULL){
345 renderer->RemoveActor( lstActorsSphere[id] );
346 renderer->RemoveActor( lstActorsText[id] );
347 lstActorsSphere[id]->Delete();
348 lstActorsText[id]->Delete();
349 lstActorsSphere.erase( lstActorsSphere.begin()+id );
350 lstActorsText.erase( lstActorsText.begin()+id );
352 lstPointsX.erase( lstPointsX.begin()+id );
353 lstPointsY.erase( lstPointsY.begin()+id );
354 lstPointsZ.erase( lstPointsZ.begin()+id );
355 lstLabels.erase( lstLabels.begin()+id );
363 //------------------------------------------------------------------------
364 void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
366 ErasePoint( GetNearestPoint() );
369 //------------------------------------------------------------------------
370 void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
372 ErasePoint(lstActorsSphere.size()-1);
376 //------------------------------------------------------------------------
377 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
379 int id,size=lstActorsSphere.size();
380 for (id=size-1;id>=0;id--)
389 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
390 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
392 void ShowNPoints::Process()
394 if (mwxwidget!=NULL){
395 mwxwidget->SetRenderer( bbGetInputRenderer() );
396 mwxwidget->SetPoint( bbGetInputIn() );
397 mwxwidget->SetImage( bbGetInputImage() );
398 mwxwidget->SetColour( bbGetInputColour() );
399 mwxwidget->SetOpacity( bbGetInputOpacity() );
400 mwxwidget->SetRadio( bbGetInputRadio() );
402 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
403 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
404 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
405 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
410 void ShowNPoints::CreateWidget(wxWindow* parent)
412 mwxwidget = new WidgetShowNPoints( parent , this);
413 mwxwidget->SetPoint( bbGetInputIn() );
415 if (bbGetInputImage()==NULL)
417 printf("Missing Image (ShowNPoints) \n");
420 bbSetOutputWidget( mwxwidget );
423 void ShowNPoints::bbUserSetDefaultValues()
427 bbSetInputRadio(0.5);
428 bbSetInputOpacity(1);
430 std::vector<double> colour;
431 colour.push_back(1.0);
432 colour.push_back(0.0);
433 colour.push_back(0.0);
434 bbSetInputColour(colour);
436 bbSetInputImage(NULL);
437 bbSetInputRenderer(NULL);
440 //-----------------------------------------------------------------
441 void ShowNPoints::bbUserInitializeProcessing()
445 //-----------------------------------------------------------------
446 void ShowNPoints::bbUserFinalizeProcessing()
450 //-----------------------------------------------------------------
453 // EO namespace bbcreaMaracasVisu