]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuShowNPoints.cxx
.
[creaMaracasVisu.git] / bbtk / src / bbmaracasvisuShowNPoints.cxx
1 #include "bbmaracasvisuShowNPoints.h"
2 #include "bbcreaMaracasVisuPackage.h"
3
4 #include "vtkProperty.h"
5 #include "vtkSphereSource.h"
6 #include "vtkPolyDataMapper.h"
7 #include "vtkRenderWindow.h"
8 #include "vtkTextActor3D.h"
9
10 namespace bbcreaMaracasVisu
11 {
12
13
14 //----------------------------------------------------------------------
15         WidgetShowNPoints::WidgetShowNPoints(wxWindow *parent,  bbcreaMaracasVisu::ShowNPoints *box)
16     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
17   {
18     mbbShowNPoints                              = box;
19     this->renderer                              = NULL;
20         wxPanel *panel                          = this;
21     wxSizer *sizer                              = NULL;
22         
23
24         // Widget interface
25         textCtrl                                                = new wxTextCtrl(panel, -1);
26         wxButton *btnAddPoint                   = new wxButton( panel, -1, _T("Add 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
31
32         wxFlexGridSizer *sizer1 = new wxFlexGridSizer(1); 
33 //    sizer1->Add(new wxStaticText(panel,-1,_T("  ")));
34
35     Connect(btnAddPoint->GetId()                , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnAddPoint);
36     Connect(btnEraseLastPoint->GetId()  , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnEraseLastPoint);
37         Connect(btnErasePoint->GetId()          , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnErasePoint);
38     Connect(btnDeleteAllPoints->GetId() , wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &WidgetShowNPoints::OnDeleteAllPoints);
39
40     sizer1->Add(textCtrl);
41         sizer1->Add(btnAddPoint);
42         sizer1->Add(btnErasePoint);
43     sizer1->Add(btnEraseLastPoint);
44     sizer1->Add(btnDeleteAllPoints);
45
46         sizer = sizer1;
47         panel   ->      SetSizer(sizer);
48     panel       ->      SetAutoLayout(true);
49     panel       ->      Layout();  
50         
51 }
52
53
54 //------------------------------------------------------------------------
55 WidgetShowNPoints::~WidgetShowNPoints()
56 {
57 }
58
59 void WidgetShowNPoints::SetRadio(double radio)
60 {
61         mradio=radio;
62 }
63
64 //------------------------------------------------------------------------
65 std::vector<int> WidgetShowNPoints::GetLstPointsX()
66 {
67         return lstPointsX;
68 }
69
70 //------------------------------------------------------------------------
71 std::vector<int> WidgetShowNPoints::GetLstPointsY()
72 {
73         return lstPointsY;
74 }
75
76 //------------------------------------------------------------------------
77 std::vector<int> WidgetShowNPoints::GetLstPointsZ()
78 {
79         return lstPointsZ;
80 }
81
82 //------------------------------------------------------------------------
83         std::vector<std::string> WidgetShowNPoints::GetLstLabels()
84 {
85         return lstLabels;
86 }
87         
88         
89
90
91 //------------------------------------------------------------------------
92 void WidgetShowNPoints::SetPoint(std::vector<int> ppoint)
93 {
94         mpoint = ppoint;
95 }
96
97 //------------------------------------------------------------------------
98 void WidgetShowNPoints::SetColour(std::vector<double> colour)
99 {
100         this->mcolour = colour;
101 }
102
103 //------------------------------------------------------------------------
104 void WidgetShowNPoints::SetOpacity(double opacity)
105 {
106         this->mopacity=opacity;
107 }
108
109 //------------------------------------------------------------------------
110 void WidgetShowNPoints::SetImage(vtkImageData *image)
111 {
112         this->mimage=image;
113 }
114
115 //------------------------------------------------------------------------
116 void  WidgetShowNPoints::SetRenderer(vtkRenderer *renderer)
117 {
118         this->renderer  = renderer;
119 }
120
121
122
123 //------------------------------------------------------------------------
124 void WidgetShowNPoints::OnAddPoint (wxCommandEvent& event)
125 {
126         if (this->renderer!=NULL){ 
127                 if (mpoint.size()==3){
128                         lstPointsX.push_back( mpoint[0] );
129                         lstPointsY.push_back( mpoint[1] );
130                         lstPointsZ.push_back( mpoint[2] );
131                         lstLabels.push_back( (const char*) ( textCtrl->GetValue().mb_str() )  );
132
133                         // Sphere
134                         vtkSphereSource *vtksphere              = vtkSphereSource::New();
135                         vtksphere->SetThetaResolution (20);
136                         vtksphere->SetPhiResolution (20);
137                         vtksphere->SetRadius( mradio  ); 
138                         vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
139                         sphereMapper->SetInput( vtksphere->GetOutput() );
140                         vtkActor *sphereActor   = vtkActor::New();
141                         sphereActor->SetMapper(sphereMapper);
142                         sphereActor->SetOrigin(0, 0, 0);
143                         double spc[3];
144                         mimage->GetSpacing(spc);
145                         sphereActor->SetPosition( spc[0]*mpoint[0] , spc[1]*mpoint[1] , spc[2]*mpoint[2] );
146                         sphereActor->GetProperty()->SetColor( mcolour[0] , mcolour[1] , mcolour[2] );
147                         sphereActor->GetProperty()->SetOpacity( mopacity );
148
149                         lstActorsSphere.push_back(sphereActor);
150                         renderer->AddActor( sphereActor );
151                         
152                         // Actor
153                         vtkTextActor3D *textActor = vtkTextActor3D::New();
154                         textActor->SetPosition(  mradio+spc[0]*mpoint[0] , spc[1]*mpoint[1] , spc[2]*mpoint[2] );
155                         textActor->SetInput( (const char*) ( textCtrl->GetValue().mb_str() )  );
156                         renderer->AddActor( textActor );
157                         lstActorsText.push_back(textActor);
158                         
159                         renderer->GetRenderWindow()->Render();
160
161                         SetOutputBox(); 
162                         
163                 } else {//mpoint.size
164                         printf("creaMaracasVisu::ShowNPoints (not match point) \n");
165                 }
166         } // renderer
167 }
168
169         void WidgetShowNPoints::SetOutputBox()
170         {
171                 //--BBTK
172                 mbbShowNPoints->bbSetOutputlstPointsX( GetLstPointsX() );
173                 mbbShowNPoints->bbSetOutputlstPointsY( GetLstPointsY() );
174                 mbbShowNPoints->bbSetOutputlstPointsZ( GetLstPointsZ() );
175                 mbbShowNPoints->bbSetOutputlstLabels( GetLstLabels() );
176                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsX"));    
177                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsY"));    
178                 mbbShowNPoints->bbSignalOutputModification(std::string("lstPointsZ"));    
179                 mbbShowNPoints->bbSignalOutputModification(std::string("lstLabels"));    
180         }
181         
182         //------------------------------------------------------------------------
183         void WidgetShowNPoints::ErasePoint(int id)
184         {
185                 if (this->renderer!=NULL){ 
186                         if (id>=0){
187                                 renderer->RemoveActor( lstActorsSphere[id] );
188                                 renderer->RemoveActor( lstActorsText[id] );
189                                  lstActorsSphere[id]->Delete();
190                                  lstActorsText[id]->Delete();
191                                 lstActorsSphere.erase( lstActorsSphere.begin()+id );
192                                 lstActorsText.erase( lstActorsText.begin()+id );
193                                 
194                                 lstPointsX.erase( lstPointsX.begin()+id );
195                                 lstPointsY.erase( lstPointsY.begin()+id );
196                                 lstPointsZ.erase( lstPointsZ.begin()+id );
197                                 lstLabels.erase( lstLabels.begin()+id );
198
199                                 renderer->GetRenderWindow()->Render();
200                                 SetOutputBox(); 
201
202                         } // if id
203                 } // if renderer
204         }
205         
206         //------------------------------------------------------------------------
207         void WidgetShowNPoints::OnErasePoint(wxCommandEvent& event)
208         {
209                 int id=-1;
210                 int i, size=(int)(lstActorsSphere.size());
211                 double spc[3];
212                 mimage->GetSpacing(spc);
213                 
214                 for ( i=0  ; i<size; i++ )
215                 {
216                         double rx =  spc[0]*(mpoint[0] - lstPointsX [i]);
217                         double ry =  spc[1]*(mpoint[1] - lstPointsY [i]);
218                         double rz =  spc[2]*(mpoint[2] - lstPointsZ [i]);
219                         if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
220                         {
221                                 id=i;
222                         }       // if           
223                 } // for
224                 ErasePoint(id);
225         }
226         
227         //------------------------------------------------------------------------
228         void WidgetShowNPoints::OnEraseLastPoint(wxCommandEvent& event)
229         {
230                 ErasePoint(lstActorsSphere.size()-1);
231         }
232         
233         
234 //------------------------------------------------------------------------
235 void WidgetShowNPoints::OnDeleteAllPoints(wxCommandEvent& event)
236 {
237         int id,size=lstActorsSphere.size();
238         for (id=size-1;id>=0;id--)
239         {
240                 ErasePoint(id);
241         }
242                 
243 }
244
245
246
247 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ShowNPoints)
248 BBTK_BLACK_BOX_IMPLEMENTATION(ShowNPoints,bbtk::WxBlackBox);
249
250 void ShowNPoints::Process()
251 {
252         if (mwxwidget!=NULL){
253                 mwxwidget->SetRenderer( bbGetInputRenderer() );   
254                 mwxwidget->SetPoint( bbGetInputIn() );   
255                 mwxwidget->SetImage( bbGetInputImage() );   
256                 mwxwidget->SetColour( bbGetInputColour() );   
257                 mwxwidget->SetOpacity( bbGetInputOpacity() );   
258                 mwxwidget->SetRadio( bbGetInputRadio() );   
259
260                 bbSetOutputlstPointsX( mwxwidget->GetLstPointsX() );
261                 bbSetOutputlstPointsY( mwxwidget->GetLstPointsY() );
262                 bbSetOutputlstPointsZ( mwxwidget->GetLstPointsZ() );
263                 bbSetOutputlstLabels( mwxwidget->GetLstLabels() );
264         } // mwxwidget
265 }
266
267
268 void ShowNPoints::CreateWidget(wxWindow* parent)
269 {
270         mwxwidget = new WidgetShowNPoints( parent ,  this);
271         mwxwidget->SetPoint( bbGetInputIn() );
272
273         if (bbGetInputImage()==NULL)
274         {
275                 printf("Missing Image  (ShowNPoints) \n");
276         }
277
278    bbSetOutputWidget( mwxwidget ); 
279 }
280
281 void ShowNPoints::bbUserSetDefaultValues()
282 {
283         mwxwidget = NULL;
284
285         bbSetInputRadio(0.5);
286         bbSetInputOpacity(1);
287         
288         std::vector<double> colour;
289         colour.push_back(1.0);
290         colour.push_back(0.0);
291         colour.push_back(0.0);
292         bbSetInputColour(colour);
293
294         bbSetInputImage(NULL);
295         bbSetInputRenderer(NULL);
296 }
297         
298         //-----------------------------------------------------------------     
299         void ShowNPoints::bbUserInitializeProcessing()
300         {
301         }
302         
303         //-----------------------------------------------------------------     
304         void ShowNPoints::bbUserFinalizeProcessing()
305         {
306         }
307         
308         //-----------------------------------------------------------------     
309
310 }
311 // EO namespace bbcreaMaracasVisu
312
313