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("Number of points: 0"));
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 )
143 //------------------------------------------------------------------------
144 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
146 if (this->renderer!=NULL){
147 if (mpoint.size()==3){
148 lstPointsX.push_back( mpoint[0] );
149 lstPointsY.push_back( mpoint[1] );
150 lstPointsZ.push_back( mpoint[2] );
152 std::string strLabel = CleanSpaces( (const char*) ( textCtrl->GetValue().mb_str() ) );
154 lstLabels.push_back( strLabel );
157 vtkSphereSource *vtksphere = vtkSphereSource::New();
158 vtksphere->SetThetaResolution (20);
159 vtksphere->SetPhiResolution (20);
160 vtksphere->SetRadius( mradio );
161 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
162 sphereMapper->SetInput( vtksphere->GetOutput() );
163 vtkActor *sphereActor = vtkActor::New();
164 sphereActor->SetMapper(sphereMapper);
165 sphereActor->SetOrigin(0, 0, 0);
167 mimage->GetSpacing(spc);
168 sphereActor->SetPosition( spc[0]*mpoint[0] , spc[1]*mpoint[1] , spc[2]*mpoint[2] );
169 sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
170 sphereActor->GetProperty()->SetOpacity( mopacity );
172 lstActorsSphere.push_back(sphereActor);
173 renderer->AddActor( sphereActor );
176 vtkTextActor3D *textActor = vtkTextActor3D::New();
177 textActor->SetPosition( mradio+spc[0]*mpoint[0] , spc[1]*mpoint[1] , spc[2]*mpoint[2] );
178 textActor->SetInput( strLabel.c_str() );
179 renderer->AddActor( textActor );
180 lstActorsText.push_back(textActor);
184 } else {//mpoint.size
185 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
191 //------------------------------------------------------------------------
192 void WidgetShowNPoints::SetOutputBox()
194 renderer->GetRenderWindow()->Render();
196 strTmp.Printf("Number of points: %d", (int)(lstPointsX.size()) );
197 txtNrPoints->SetLabel( strTmp );
200 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
201 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
202 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
203 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
204 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
205 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
206 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
207 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
210 //------------------------------------------------------------------------
211 void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
214 wxString filename="/Users/davila/Desktop/PDL_2/bbs/test.xls";
215 ff = fopen( (const char*) ( filename.mb_str() ) , "w+" );
218 int i , size = (int) (lstActorsSphere.size());
219 fprintf(ff,"NumberOfPoints %d \n",size);
220 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
223 for (i=0; i<size; i++)
228 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
229 fprintf(ff,"%d\t %d\t %d\t %f\t %s\n", x , y , z , value , lstLabels[i].c_str());
233 printf("WidgetShowNPoints::OnSavePoints ...Error... creating file");
238 //------------------------------------------------------------------------
239 void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
243 int WidgetShowNPoints::GetNearestPoint()
246 int i, size=(int)(lstActorsSphere.size());
248 mimage->GetSpacing(spc);
250 for ( i=0 ; i<size; i++ )
252 double rx = spc[0]*(mpoint[0] - lstPointsX [i]);
253 double ry = spc[1]*(mpoint[1] - lstPointsY [i]);
254 double rz = spc[2]*(mpoint[2] - lstPointsZ [i]);
255 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
264 //------------------------------------------------------------------------
265 void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
267 int id=GetNearestPoint();
270 std::string strLabel = CleanSpaces( (const char*) ( textCtrl->GetValue().mb_str() ) );
271 lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
272 lstActorsText[id]->SetInput( lstLabels[id].c_str() );
277 //------------------------------------------------------------------------
278 void WidgetShowNPoints::ErasePoint(int id)
280 if (this->renderer!=NULL){
282 renderer->RemoveActor( lstActorsSphere[id] );
283 renderer->RemoveActor( lstActorsText[id] );
284 lstActorsSphere[id]->Delete();
285 lstActorsText[id]->Delete();
286 lstActorsSphere.erase( lstActorsSphere.begin()+id );
287 lstActorsText.erase( lstActorsText.begin()+id );
289 lstPointsX.erase( lstPointsX.begin()+id );
290 lstPointsY.erase( lstPointsY.begin()+id );
291 lstPointsZ.erase( lstPointsZ.begin()+id );
292 lstLabels.erase( lstLabels.begin()+id );
300 //------------------------------------------------------------------------
301 void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
303 ErasePoint( GetNearestPoint() );
306 //------------------------------------------------------------------------
307 void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
309 ErasePoint(lstActorsSphere.size()-1);
313 //------------------------------------------------------------------------
314 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
316 int id,size=lstActorsSphere.size();
317 for (id=size-1;id>=0;id--)
326 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
327 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
329 void ShowNPoints::Process()
331 if (mwxwidget!=NULL){
332 mwxwidget->SetRenderer( bbGetInputRenderer() );
333 mwxwidget->SetPoint( bbGetInputIn() );
334 mwxwidget->SetImage( bbGetInputImage() );
335 mwxwidget->SetColour( bbGetInputColour() );
336 mwxwidget->SetOpacity( bbGetInputOpacity() );
337 mwxwidget->SetRadio( bbGetInputRadio() );
339 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
340 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
341 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
342 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
347 void ShowNPoints::CreateWidget(wxWindow* parent)
349 mwxwidget = new WidgetShowNPoints( parent , this);
350 mwxwidget->SetPoint( bbGetInputIn() );
352 if (bbGetInputImage()==NULL)
354 printf("Missing Image (ShowNPoints) \n");
357 bbSetOutputWidget( mwxwidget );
360 void ShowNPoints::bbUserSetDefaultValues()
364 bbSetInputRadio(0.5);
365 bbSetInputOpacity(1);
367 std::vector<double> colour;
368 colour.push_back(1.0);
369 colour.push_back(0.0);
370 colour.push_back(0.0);
371 bbSetInputColour(colour);
373 bbSetInputImage(NULL);
374 bbSetInputRenderer(NULL);
377 //-----------------------------------------------------------------
378 void ShowNPoints::bbUserInitializeProcessing()
382 //-----------------------------------------------------------------
383 void ShowNPoints::bbUserFinalizeProcessing()
387 //-----------------------------------------------------------------
390 // EO namespace bbcreaMaracasVisu