]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTransformWidget2.cxx
#3524 TransformWidget2
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuTransformWidget2.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaMaracasVisuTransformWidget2.h"
5 #include "bbcreaMaracasVisuPackage.h"
6 namespace bbcreaMaracasVisu
7 {
8
9
10 // The widget created by the box
11 class TransformWidgetPanel : public wxPanel
12 {
13 public:
14     TransformWidgetPanel(TransformWidget2* box, wxWindow *parent);
15     ~TransformWidgetPanel   ();
16     void OnSetPoint         (wxCommandEvent& event);
17     void Release            (wxSlider *slider);
18     void Track              (double angle ,int idVector);
19     void SetPossiblePoint   (std::vector<double> possiblePoint);
20     void _OnSetPoint();
21     
22     // Type : Module A
23     void OnSliderTrackX     (wxScrollEvent& event);
24     void OnSliderTrackY     (wxScrollEvent& event);
25     void OnSliderTrackZ     (wxScrollEvent& event);
26     void OnSliderReleaseX   (wxScrollEvent& event);
27     void OnSliderReleaseY   (wxScrollEvent& event);
28     void OnSliderReleaseZ   (wxScrollEvent& event);
29     
30     // Type : Module B
31     void OnRadioButtonX(wxEvent& event);
32     void OnRadioButtonY(wxEvent& event);
33     void OnRadioButtonZ(wxEvent& event);
34     void OnRadioButtonN(wxEvent& event);
35     void OnReset(wxCommandEvent& event);
36     void OnMoveDown(wxCommandEvent& event);
37     void OnMoveUp(wxCommandEvent& event);
38     void OnSliderGeneralTrack(wxScrollEvent& event);
39     void OnSliderGeneralRelease(wxScrollEvent& event);
40     
41 private:
42     vtkTransform            *t;
43     vtkTransform            *tt;
44     std::vector<double>     mPossiblePoint;
45     std::vector<double>     mActualPoint;
46     TransformWidget2        *mBox;
47     
48     // Type : Module A
49     wxSlider                *mSliderRotationX;
50     wxSlider                *mSliderRotationY;
51     wxSlider                *mSliderRotationZ;
52     // Type : Module B
53     char                    mTool;
54     wxSlider                *mSliderGeneral;
55 };
56
57 //------------------------------------------------------------------------
58 //------------------------------------------------------------------------
59 //------------------------------------------------------------------------
60
61 TransformWidgetPanel::TransformWidgetPanel(TransformWidget2* box, wxWindow *parent)
62   : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
63   mBox(box)
64 {
65     t                       = vtkTransform::New();
66     tt                      = vtkTransform::New();
67     mActualPoint.push_back(0);
68     mActualPoint.push_back(0);
69     mActualPoint.push_back(0);
70
71     mPossiblePoint.push_back(0);
72     mPossiblePoint.push_back(0);
73     mPossiblePoint.push_back(0);
74
75     wxSize  sizeSliderWidget;
76     wxSize sizeButton;
77     
78     if (mBox->bbGetInputType()==0)
79     {
80         sizeSliderWidget.Set(25,25);
81         sizeButton.Set( 45 , -1 );
82         mSliderRotationX        = new wxSlider( this, -1, 0 , -180, 180     , wxDefaultPosition,sizeSliderWidget, wxSL_HORIZONTAL, wxDefaultValidator   );
83         mSliderRotationY        = new wxSlider( this, -1, 0 , -180, 180     , wxDefaultPosition,sizeSliderWidget, wxSL_HORIZONTAL, wxDefaultValidator   );
84         mSliderRotationZ        = new wxSlider( this, -1, 0 , -180, 180     , wxDefaultPosition,sizeSliderWidget, wxSL_HORIZONTAL, wxDefaultValidator   );
85         wxButton *mBtnSetPoint  = new wxButton( this, -1, _T("Set Point")   , wxDefaultPosition, sizeButton                                             );
86         Connect(mBtnSetPoint->GetId()     , wxEVT_COMMAND_BUTTON_CLICKED    ,  (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSetPoint      );
87         Connect( mSliderRotationX->GetId(), wxEVT_SCROLL_THUMBRELEASE       , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderReleaseX );
88         Connect( mSliderRotationX->GetId(), wxEVT_SCROLL_THUMBTRACK         , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackX   );
89         Connect( mSliderRotationX->GetId(), wxEVT_SCROLL_CHANGED            , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackX   );
90         Connect( mSliderRotationY->GetId(), wxEVT_SCROLL_THUMBRELEASE       , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderReleaseY );
91         Connect( mSliderRotationY->GetId(), wxEVT_SCROLL_THUMBTRACK         , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackY   );
92         Connect( mSliderRotationY->GetId(), wxEVT_SCROLL_CHANGED            , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackY   );
93         Connect( mSliderRotationZ->GetId(), wxEVT_SCROLL_THUMBRELEASE       , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderReleaseZ );
94         Connect( mSliderRotationZ->GetId(), wxEVT_SCROLL_THUMBTRACK         , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackZ   );
95         Connect( mSliderRotationZ->GetId(), wxEVT_SCROLL_CHANGED            , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackZ   );
96         wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
97         sizer->Add( mSliderRotationX    , 1 );
98         sizer->Add( mSliderRotationY    , 1 );
99         sizer->Add( mSliderRotationZ    , 1 );
100         sizer->Add( mBtnSetPoint        , 1 );
101         SetSizer(sizer);
102     } // if Type == 0
103     
104     if (mBox->bbGetInputType()==1)
105     {
106         mTool=0;
107         sizeSliderWidget.Set(250,-1     );
108         sizeButton.Set( 65 , -1 );
109         wxRadioButton *rBtnX    = new wxRadioButton( this, -1, _T("X") ,wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
110         wxRadioButton *rBtnY    = new wxRadioButton( this, -1, _T("Y") ,wxDefaultPosition, wxDefaultSize, 0);
111         wxRadioButton *rBtnZ    = new wxRadioButton( this, -1, _T("Z") ,wxDefaultPosition, wxDefaultSize, 0);
112         wxRadioButton *rBtnN    = new wxRadioButton( this, -1, _T("N") ,wxDefaultPosition, wxDefaultSize, 0);
113         mSliderGeneral          = new wxSlider( this, -1, 0 , -180, 180     , wxDefaultPosition,sizeSliderWidget, wxSL_HORIZONTAL, wxDefaultValidator                                   );
114         wxButton *mBtnMoveDown  = new wxButton( this, -1, _T("-")           , wxDefaultPosition, sizeButton                                                                             );
115         wxButton *mBtnMoveUp    = new wxButton( this, -1, _T("+")           , wxDefaultPosition, sizeButton                                                                             );
116         wxButton *mBtnSetPoint  = new wxButton( this, -1, _T("Set")   , wxDefaultPosition, sizeButton                                                                             );
117         wxButton *mBtnReset     = new wxButton( this, -1, _T("Reset")       , wxDefaultPosition, sizeButton                                                                             );
118         Connect( rBtnX->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonX               );
119         Connect( rBtnY->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonY               );
120         Connect( rBtnZ->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonZ               );
121         Connect( rBtnN->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonN               );
122         Connect(mBtnSetPoint->GetId()   , wxEVT_COMMAND_BUTTON_CLICKED      ,  (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSetPoint             );
123         Connect(mBtnReset->GetId()      , wxEVT_COMMAND_BUTTON_CLICKED      ,  (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnReset                );
124         Connect(mBtnMoveDown->GetId()   , wxEVT_COMMAND_BUTTON_CLICKED      ,  (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnMoveDown             );
125         Connect(mBtnMoveUp->GetId()      , wxEVT_COMMAND_BUTTON_CLICKED     ,  (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnMoveUp               );
126         Connect( mSliderGeneral->GetId(), wxEVT_SCROLL_THUMBRELEASE       , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderGeneralRelease    );
127         Connect( mSliderGeneral->GetId(), wxEVT_SCROLL_THUMBTRACK         , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderGeneralTrack      );
128         Connect( mSliderGeneral->GetId(), wxEVT_SCROLL_CHANGED            , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderGeneralTrack      );
129         rBtnX->SetValue(true);
130         
131         wxBoxSizer *sizerD = new wxBoxSizer(wxHORIZONTAL);
132         sizerD->Add( mBtnSetPoint   , 1 );
133         sizerD->Add( mBtnReset      , 1 );
134         wxBoxSizer *sizerA = new wxBoxSizer(wxHORIZONTAL);
135         sizerA->Add( rBtnX , 1 );
136         sizerA->Add( rBtnY , 1 );
137         sizerA->Add( rBtnZ , 1 );
138         sizerA->Add( rBtnN , 1 );
139         wxBoxSizer *sizerB = new wxBoxSizer(wxHORIZONTAL);
140         sizerB->Add( mSliderGeneral , 0 );
141         wxBoxSizer *sizerC = new wxBoxSizer(wxHORIZONTAL);
142         sizerC->Add( mBtnMoveDown   , 1 );
143         sizerC->Add( mBtnMoveUp     , 1 );
144         wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
145         sizer->Add( sizerD          , 0 );
146         sizer->Add( sizerA          , 0 );
147         sizer->Add( sizerB          , 0 );
148         sizer->Add( sizerC          , 0 );
149
150         SetSizer(sizer);
151     } // if
152     
153 }
154
155 TransformWidgetPanel::~TransformWidgetPanel()
156 {
157 }
158
159 void TransformWidgetPanel::Release( wxSlider * slider )
160 {
161     if (slider!=NULL)
162     {
163         slider->SetValue(0);
164     }
165     // t = t*tt
166     t->PostMultiply();
167     t->Concatenate ( tt->GetMatrix() );    t->Update();
168     tt->GetMatrix()->Identity();  tt->Update();
169     mBox->bbSetOutputOut( t );
170     mBox->bbSignalOutputModification(std::string("Out"));
171 }
172
173 void TransformWidgetPanel::OnSliderReleaseX(wxScrollEvent& event)
174 {
175     Release( mSliderRotationX );
176 }
177
178 void TransformWidgetPanel::OnSliderReleaseY(wxScrollEvent& event)
179 {
180     Release( mSliderRotationY );
181 }
182
183 void TransformWidgetPanel::OnSliderReleaseZ(wxScrollEvent& event)
184 {
185     Release( mSliderRotationZ );
186 }
187
188 void TransformWidgetPanel::Track( double angle, int idVector)
189 {
190     // out = t*tt
191     if (tt!=NULL)
192     {
193         tt->Delete();
194     }
195     vtkMatrix4x4    *tm     = t->GetMatrix();
196     double          tvx    = tm->GetElement(0,idVector);
197     double          tvy    = tm->GetElement(1,idVector);
198     double          tvz    = tm->GetElement(2,idVector);
199     tt = vtkTransform::New();
200     tt->PostMultiply();
201
202     angle = angle/2;
203     tt->RotateWXYZ( angle , tvx,tvy,tvz);  tt->Update();
204     vtkTransform *result=vtkTransform::New();
205     result->PostMultiply();
206     result->Concatenate ( t->GetMatrix() );    result->Update();
207     result->Concatenate ( tt->GetMatrix() );   result->Update();
208     mBox->bbSetOutputOut( result );
209     mBox->bbSignalOutputModification(std::string("Out"));
210 }
211
212 void TransformWidgetPanel::OnSliderTrackX(wxScrollEvent& event)
213 {
214     Track(mSliderRotationX->GetValue() ,0);
215 }
216
217 void TransformWidgetPanel::OnSliderTrackY(wxScrollEvent& event)
218 {
219     Track(mSliderRotationY->GetValue() ,1);
220 }
221
222 void TransformWidgetPanel::OnSliderTrackZ(wxScrollEvent& event)
223 {
224     Track(mSliderRotationZ->GetValue() ,2);
225 }
226
227 void TransformWidgetPanel::SetPossiblePoint(std::vector<double> possiblePoint)
228 {
229     mPossiblePoint = possiblePoint;
230 }
231
232 void TransformWidgetPanel::_OnSetPoint()
233 {
234     mActualPoint = mPossiblePoint;
235     mBox->bbSetOutputActualPoint( mActualPoint );
236     mBox->bbSignalOutputModification(std::string("ActualPoint"));
237 }
238
239 void TransformWidgetPanel::OnSetPoint(wxCommandEvent& event)
240 {
241     _OnSetPoint();
242 }
243
244 //------------------------------------------------------------------------
245
246 void TransformWidgetPanel::OnRadioButtonX(wxEvent& event)
247 {
248     mTool = 0;
249 }
250
251 void TransformWidgetPanel::OnRadioButtonY(wxEvent& event)
252 {
253     mTool = 1;
254 }
255
256 void TransformWidgetPanel::OnRadioButtonZ(wxEvent& event)
257 {
258     mTool = 2;
259 }
260
261 void TransformWidgetPanel::OnRadioButtonN(wxEvent& event)
262 {
263     mTool = 3;
264 }
265
266 void TransformWidgetPanel::OnReset(wxCommandEvent& event)
267 {
268     if ((mTool>=0) && (mTool<=2))
269     {
270         tt->GetMatrix()->Identity(); tt->Update();
271         t->GetMatrix()->Identity();  t->Update();
272         if (mTool==0)
273         {
274             Release( NULL );
275         } // if
276         
277         if (mTool==1)
278         {
279             t->RotateWXYZ( 90 , 1,0,0);  tt->Update();
280             Release( NULL );
281         } // if
282         
283         if (mTool==2)
284         {
285             t->RotateWXYZ( 90 , 0,1,0);  tt->Update();
286             Release( NULL );
287         } // if
288     } // if mTool >=0   <=2
289 }
290
291 void TransformWidgetPanel::OnMoveDown(wxCommandEvent& event)
292 {
293     if ((mTool>=0) && (mTool<=2))
294     {
295         Track( -2 , mTool );
296         Release( NULL );
297     }
298 }
299
300 void TransformWidgetPanel::OnMoveUp(wxCommandEvent& event)
301 {
302     if ((mTool>=0) && (mTool<=2))
303     {
304         Track( 2 , mTool );
305         Release( NULL );
306     }
307 }
308
309 void TransformWidgetPanel::OnSliderGeneralRelease(wxScrollEvent& event)
310 {
311     Release( mSliderGeneral );
312 }
313
314 void TransformWidgetPanel::OnSliderGeneralTrack(wxScrollEvent& event)
315 {
316     if ((mTool>=0) && (mTool<=2))
317     {
318         Track( mSliderGeneral->GetValue() , mTool );
319     }
320 }
321
322
323 //------------------------------------------------------------------------
324 //------------------------------------------------------------------------
325 //------------------------------------------------------------------------
326
327 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TransformWidget2)
328 BBTK_BLACK_BOX_IMPLEMENTATION(TransformWidget2,bbtk::WxBlackBox);
329 //===== 
330 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
331 //===== 
332 void TransformWidget2::Process()
333 {
334     TransformWidgetPanel *w = (TransformWidgetPanel*) bbGetOutputWidget( );
335     w->SetPossiblePoint( bbGetInputPossiblePoint() );
336     if (firsttime==true)
337     {
338         firsttime=false;
339         w->_OnSetPoint();
340     }// if
341 }
342
343 //===== 
344 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
345 //===== 
346 void TransformWidget2::CreateWidget(wxWindow* parent)
347 {
348    TransformWidgetPanel *w =  new TransformWidgetPanel(this,parent);
349    bbSetOutputWidget( w );
350 }
351
352 //===== 
353 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
354 //===== 
355 void TransformWidget2::bbUserSetDefaultValues()
356 {
357     firsttime=true;
358     bbSetInputType(0);
359 }
360
361 //===== 
362 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
363 //===== 
364 void TransformWidget2::bbUserInitializeProcessing()
365 {
366 }
367
368 //===== 
369 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
370 //===== 
371 void TransformWidget2::bbUserFinalizeProcessing()
372 {
373 }
374
375 }// EO namespace bbcreaMaracasVisu
376
377