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"
9 #include <vtkTextProperty.h>
11 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;
23 if (mbbShowNPoints->bbGetInputType()==0)
26 askPointLabel = new wxStaticText(panel, -1, _T("Point label :")); // JPR
27 textCtrl = new wxTextCtrl(panel, -1);
28 wxButton *btnAddPoint = new wxButton( panel, -1, _T("Add Point"));
29 wxButton *btnSetPositionPoint = new wxButton( panel, -1, _T("Set nearest point"));
30 wxButton *btnRenamePoint = new wxButton( panel, -1, _T("Rename point"));
31 wxButton *btnEraseLastPoint = new wxButton( panel, -1, _T("Erase Last point"));
32 wxButton *btnErasePoint = new wxButton( panel, -1, _T("Erase point"));
33 wxButton *btnDeleteAllPoints = new wxButton( panel, -1, _T("Delete all points"));
34 wxButton *btnSavePoints = new wxButton( panel, -1, _T("Save points"));
35 wxButton *btnLoadPoints = new wxButton( panel, -1, _T("Load points"));
36 txtNrPoints = new wxStaticText(panel,-1, _T(" "));
38 //NTU: Sliders for opacity and radio change
39 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T(" Points Opacity ")));
40 sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
41 wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T(" Points Radio ")));
42 sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
44 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
45 // sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
47 Connect(btnAddPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
48 Connect(btnSetPositionPoint->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
49 Connect(btnRenamePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnRenamePoint);
50 Connect(btnEraseLastPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
51 Connect(btnErasePoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
52 Connect(btnDeleteAllPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
53 Connect(btnSavePoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSavePoints);
54 Connect(btnLoadPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnLoadPoints);
57 Connect(sdrOpacity->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
58 Connect(sdrRadio->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
60 sizer1->Add(askPointLabel); // JPR
61 sizer1->Add(textCtrl);
62 sizer1->Add(btnAddPoint);
63 sizer1->Add(btnSetPositionPoint);
64 sizer1->Add(btnRenamePoint);
65 sizer1->Add(btnErasePoint);
66 sizer1->Add(btnEraseLastPoint);
67 sizer1->Add(btnDeleteAllPoints);
68 sizer1->Add(txtNrPoints);
69 sizer1->Add(txOpacity);
70 sizer1->Add(sdrOpacity,1,wxGROW );
72 sizer1->Add(sdrRadio,1,wxGROW );
73 sizer1->Add(btnSavePoints);
74 sizer1->Add(btnLoadPoints);
78 if (mbbShowNPoints->bbGetInputType()==1)
81 wxButton *btnSetPositionPoint = new wxButton( panel, -1, _T("Set nearest point"));
82 txtNrPoints = new wxStaticText(panel,-1, _T(" "));
84 //NTU: Sliders for opacity and radio change
85 wxStaticText* txOpacity = new wxStaticText(this, -1, wxString(_T(" Points Opacity ")));
86 sdrOpacity = new wxSlider(this, -1, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
87 wxStaticText* txRadio = new wxStaticText(this, -1, wxString(_T(" Points Radio ")));
88 sdrRadio = new wxSlider(this, -1, 0, 1, 50, wxDefaultPosition, wxDefaultSize, wxSL_LABELS);
89 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
90 Connect(btnSetPositionPoint->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WidgetShowNPoints::OnSetPoint);
91 Connect(sdrOpacity->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
92 Connect(sdrRadio->GetId() , wxEVT_COMMAND_SLIDER_UPDATED , (wxObjectEventFunction) &WidgetShowNPoints::UpdatePoints);
94 sizer1->Add(btnSetPositionPoint);
95 sizer1->Add(txtNrPoints);
96 sizer1->Add(txOpacity);
97 sizer1->Add(sdrOpacity,1,wxGROW );
99 sizer1->Add(sdrRadio,1,wxGROW );
103 if (mbbShowNPoints->bbGetInputType() == 2) {
104 askPointLabel = new wxStaticText(panel, -1, _T("\nPOINT CONTROLS:")); // JPR
106 wxButton *btnAddPoint = new wxButton(panel, -1, _T(" Add Point "));
107 wxButton *btnDeleteAllPoints = new wxButton(panel, -1, _T(" Delete All "));
108 wxStaticText* spacer = new wxStaticText(panel, -1, _T("\n")); // JPR
109 textCtrl = new wxTextCtrl(panel, -1);
111 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1);
113 Connect(btnAddPoint->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) & WidgetShowNPoints::OnAddPoint);
114 Connect(btnDeleteAllPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction) & WidgetShowNPoints::OnDeleteAllPoints);
116 sizer1->Add(askPointLabel);
117 sizer1->Add(btnAddPoint);
118 sizer1->Add(btnDeleteAllPoints);
120 sizer1->Add(textCtrl);
122 sdrOpacity = new wxSlider();
123 sdrRadio = new wxSlider();
125 txtNrPoints = new wxStaticText(panel, -1, _T("\n\n\n"));
131 panel->SetSizer(sizer);
132 panel->SetAutoLayout(true);
137 //------------------------------------------------------------------------
138 WidgetShowNPoints::~WidgetShowNPoints()
142 void WidgetShowNPoints::SetRadio(double radio)
146 sdrRadio->SetValue(this->mradio);
149 //------------------------------------------------------------------------
150 std::vector<int> WidgetShowNPoints::GetLstPointsX()
155 //------------------------------------------------------------------------
156 std::vector<int> WidgetShowNPoints::GetLstPointsY()
161 //------------------------------------------------------------------------
162 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
167 //------------------------------------------------------------------------
168 std::vector<std::string> WidgetShowNPoints::GetLstLabels()
173 //------------------------------------------------------------------------
174 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
179 //------------------------------------------------------------------------
180 void WidgetShowNPoints::SetColour(std::vector<double> colour)
182 this->mcolour = colour;
185 //------------------------------------------------------------------------
186 void WidgetShowNPoints::SetOpacity(double opacity)
188 this->mopacity=opacity;
190 sdrOpacity->SetValue(this->mopacity/100.0);
193 //------------------------------------------------------------------------
194 void WidgetShowNPoints::SetImage(vtkImageData *image)
199 //------------------------------------------------------------------------
200 void WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
202 this->renderer = renderer;
205 //------------------------------------------------------------------------
207 std::string WidgetShowNPoints::CleanSpaces(std::string ss)
210 while( (i=ss.find(32))>=0 )
218 //------------------------------------------------------------------------
220 void WidgetShowNPoints::RefreshPoint(int id)
222 int x = lstPointsX[id];
223 int y = lstPointsY[id];
224 int z = lstPointsZ[id];
227 mimage->GetSpacing(spc);
229 lstActorsSphere[id]->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
230 lstActorsSphere[id]->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
231 lstActorsSphere[id]->GetProperty()->SetOpacity( mopacity );
233 lstActorsText[id]->SetPosition( mradio+spc[0]*x , spc[1]*y , spc[2]*z );
237 //------------------------------------------------------------------------
238 void WidgetShowNPoints::AddPoint(int x, int y, int z, std::string label)
240 lstPointsX.push_back( x );
241 lstPointsY.push_back( y );
242 lstPointsZ.push_back( z );
244 std::string strLabel = CleanSpaces( label );
246 lstLabels.push_back( strLabel );
249 vtkSphereSource *vtksphere = vtkSphereSource::New();
250 vtksphere->SetThetaResolution (20);
251 vtksphere->SetPhiResolution (20);
252 vtksphere->SetRadius( mradio );
254 //NTU: For updating points
256 lstSourceSphere.push_back(vtksphere);
258 vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
259 sphereMapper->SetInput( vtksphere->GetOutput() );
260 vtkActor *sphereActor = vtkActor::New();
261 sphereActor->SetMapper(sphereMapper);
262 sphereActor->SetOrigin(0, 0, 0);
267 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
271 mimage->GetSpacing(spc);
272 sphereActor->SetPosition( spc[0]*x , spc[1]*y , spc[2]*z );
273 sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
274 sphereActor->GetProperty()->SetOpacity( mopacity );
277 lstActorsSphere.push_back(sphereActor);
279 wxMessageDialog dialog(this, _T("Renderer Not Set"),_T("Renderer Not Set"),wxICON_ERROR);
283 renderer->AddActor( sphereActor );
286 vtkTextActor3D *textActor = vtkTextActor3D::New();
288 textActor->SetPosition( mradio+spc[0]*x , spc[1]*y , spc[2]*z );
290 textActor->SetInput( strLabel.c_str() );
291 renderer->AddActor( textActor );
292 lstActorsText.push_back(textActor);
294 RefreshPoint(lstPointsX.size()-1);
298 //------------------------------------------------------------------------
299 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
302 wxMessageDialog dialog(this, _T("Image Not Set"),_T("Image Not Set"),wxICON_ERROR);
307 if (this->renderer!=NULL){
308 if (mpoint.size()==3){
309 AddPoint(mpoint[0],mpoint[1],mpoint[2], (const char*) ( textCtrl->GetValue().mb_str() ) );
310 } else {//mpoint.size
311 printf("creaMaracasVisu::ShowNPoints (not match point) \n");
316 //------------------------------------------------------------------------
317 void WidgetShowNPoints::SetOutputBox()
319 renderer->GetRenderWindow()->Render();
321 strTmp.Printf(_T("Nbr of points: %d"), (int)(lstPointsX.size()) );
322 txtNrPoints->SetLabel( strTmp );
325 mbbShowNPoints->bbSetOutputlstPointsX( lstPointsX );
326 mbbShowNPoints->bbSetOutputlstPointsY( lstPointsY );
327 mbbShowNPoints->bbSetOutputlstPointsZ( lstPointsZ );
328 mbbShowNPoints->bbSetOutputlstLabels( lstLabels );
329 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));
330 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));
331 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));
332 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));
335 //------------------------------------------------------------------------
336 void WidgetShowNPoints::OnSavePoints(wxCommandEvent& event)
338 wxFileDialog* FD = new wxFileDialog( 0,
339 _T("Save points .."),
343 wxSAVE | wxOVERWRITE_PROMPT,
347 int result_FD = FD->ShowModal();
348 std::string tmpLabel;
350 // This line is need it by windows //EED
351 FD->SetReturnCode( result_FD );
353 if (FD->GetReturnCode()==wxID_OK)
356 std::string filename= (const char*) ( FD->GetPath().mb_str() );
357 ff = fopen( filename.c_str() , "w+" );
360 int i , size = (int) (lstActorsSphere.size());
361 fprintf(ff,"NumberOfPoints %d \n",size);
362 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
365 for (i=0; i<size; i++)
370 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
371 if (lstLabels[i]!="")
373 tmpLabel=lstLabels[i];
377 fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value , tmpLabel.c_str());
381 printf("WidgetShowNPoints::OnSavePoints ...Error... creating file");
386 //------------------------------------------------------------------------
387 void WidgetShowNPoints::OnLoadPoints(wxCommandEvent& event)
389 wxFileDialog* FD = new wxFileDialog( 0,
390 _T("Load points .."),
394 wxOPEN | wxFILE_MUST_EXIST,
398 int result_FD = FD->ShowModal();
400 // This line is need it by windows //EED
401 FD->SetReturnCode( result_FD );
403 if (FD->GetReturnCode()==wxID_OK)
406 std::string filename= (const char*) ( FD->GetPath().mb_str() );
407 ff = fopen( filename.c_str() , "r+" );
412 fscanf(ff," %s %d",chartmp,&size);
413 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
417 for (i=0; i<size; i++)
419 fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
420 if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
421 AddPoint(x,y,z, chartmp );
425 printf("WidgetShowNPoints::OnLoadPoints ...Error... reading file");
432 //------------------------------------------------------------------------
434 int WidgetShowNPoints::GetNearestPoint()
437 int i, size=(int)(lstActorsSphere.size());
438 double radioMin=10000000;
441 wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
446 for ( i=0 ; i<size; i++ )
448 double rx = mpoint[0] - lstPointsX [i];
449 double ry = mpoint[1] - lstPointsY [i];
450 double rz = mpoint[2] - lstPointsZ [i];
451 double radio = rx*rx + ry*ry + rz*rz;
452 if ( radio <= radioMin)
464 //------------------------------------------------------------------------
465 void WidgetShowNPoints::OnSetPoint(wxCommandEvent& event)
467 int id=GetNearestPoint();
468 if((id==-1) && (mbbShowNPoints->bbGetInputType()==1))
477 printf("WidgetShowNPoints::OnSetPoint \n");
478 lstPointsX[id] = mpoint[0];
479 lstPointsY[id] = mpoint[1];
480 lstPointsZ[id] = mpoint[2];
483 this->lstActorsSphere[id]->SetPosition( spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
484 this->lstActorsText[id]->SetPosition( spc[0]*mpoint[0]+difradio, spc[1]*mpoint[1], spc[2]* mpoint[2] );
491 //------------------------------------------------------------------------
493 int WidgetShowNPoints::IdInsidePoint()
496 int i, size=(int)(lstActorsSphere.size());
500 wxMessageDialog* diag = new wxMessageDialog(this, _T("Image not set"), _T("Image Not Set"), wxICON_ERROR);
505 mimage->GetSpacing(spc);
507 for ( i=0 ; i<size; i++ )
509 double rx = spc[0]*(mpoint[0] - lstPointsX [i]);
510 double ry = spc[1]*(mpoint[1] - lstPointsY [i]);
511 double rz = spc[2]*(mpoint[2] - lstPointsZ [i]);
512 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
522 //------------------------------------------------------------------------
523 void WidgetShowNPoints::OnRenamePoint(wxCommandEvent& event)
525 int id=IdInsidePoint();
528 std::string strLabel = CleanSpaces( (const char*) ( textCtrl->GetValue().mb_str() ) );
529 lstLabels[id] = CleanSpaces( (const char*) (textCtrl->GetValue().mb_str()) ) ;
530 lstActorsText[id]->SetInput( lstLabels[id].c_str() );
535 //------------------------------------------------------------------------
536 void WidgetShowNPoints::ErasePoint(int id)
538 if (this->renderer!=NULL){
540 renderer->RemoveActor( lstActorsSphere[id] );
541 renderer->RemoveActor( lstActorsText[id] );
542 lstActorsSphere[id]->Delete();
543 lstActorsText[id]->Delete();
544 lstSourceSphere[id]->Delete();
545 lstActorsSphere.erase( lstActorsSphere.begin()+id );
546 lstActorsText.erase( lstActorsText.begin()+id );
547 lstSourceSphere.erase( lstSourceSphere.begin()+id );
549 lstPointsX.erase( lstPointsX.begin()+id );
550 lstPointsY.erase( lstPointsY.begin()+id );
551 lstPointsZ.erase( lstPointsZ.begin()+id );
552 lstLabels.erase( lstLabels.begin()+id );
560 //------------------------------------------------------------------------
561 void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
563 ErasePoint( IdInsidePoint() );
566 //------------------------------------------------------------------------
567 void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
569 ErasePoint(lstActorsSphere.size()-1);
572 //------------------------------------------------------------------------
573 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
575 int id,size=lstActorsSphere.size();
576 for (id=size-1;id>=0;id--)
582 //NTU: Method for updating points opacity and Radio
584 //------------------------------------------------------------------------
585 void WidgetShowNPoints::UpdatePoints(wxCommandEvent &event)
587 //Difference in Radio for text placement
588 int difradio = sdrRadio->GetValue() - this->mradio;
590 this->mopacity = sdrOpacity->GetValue()/100.0;
591 this->mradio = sdrRadio->GetValue();
593 //NTU refresh the inputs
594 mbbShowNPoints->bbSetInputOpacity(this->mopacity);
595 mbbShowNPoints->bbSetInputRadio(this->mradio);
597 int size = (int) this->lstActorsSphere.size();
599 for(i = 0; i < size; i ++)
601 this->lstSourceSphere[i]->SetRadius(mradio);
602 this->lstActorsSphere[i]->GetProperty()->SetOpacity(mopacity);
603 this->lstActorsText[i]->GetTextProperty()->SetOpacity(mopacity);
604 this->lstActorsText[i]->SetPosition(this->lstActorsText[i]->GetPosition()[0]+difradio,this->lstActorsText[i]->GetPosition()[1], this->lstActorsText[i]->GetPosition()[2]);
611 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
612 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
614 void ShowNPoints::Process()
618 mwxwidget->SetRenderer( bbGetInputRenderer() );
619 mwxwidget->SetPoint( bbGetInputIn() );
620 mwxwidget->SetImage( bbGetInputImage() );
621 mwxwidget->SetColour( bbGetInputColour() );
622 mwxwidget->SetOpacity( bbGetInputOpacity() );
623 mwxwidget->SetRadio( bbGetInputRadio() );
625 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
626 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
627 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
628 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
632 void ShowNPoints::CreateWidget(wxWindow* parent)
634 mwxwidget = new WidgetShowNPoints( parent, this);
635 bbSetOutputWidget( mwxwidget );
638 void ShowNPoints::bbUserSetDefaultValues()
643 bbSetInputOpacity(1);
645 std::vector<double> colour;
646 colour.push_back(1.0);
647 colour.push_back(0.0);
648 colour.push_back(0.0);
649 bbSetInputColour(colour);
651 bbSetInputImage(NULL);
653 bbSetInputRenderer(NULL);
656 //-----------------------------------------------------------------
657 void ShowNPoints::bbUserInitializeProcessing()
661 //-----------------------------------------------------------------
662 void ShowNPoints::bbUserFinalizeProcessing()
666 //-----------------------------------------------------------------
669 // EO namespace bbcreaMaracasVisu