]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTransformWidget2.cxx
aed6670333cdec8d69585f2f275feafa99599405
[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 // The widget created by the box
10 class TransformWidgetPanel : public wxPanel
11 {
12 public:
13     TransformWidgetPanel(TransformWidget2* box, wxWindow *parent);
14     ~TransformWidgetPanel           ();
15     void OnSetPoint                 (wxCommandEvent& event);
16     void ReleaseAngle               (wxSlider *slider);
17     void TrackAngle                 (double angle ,int idVector);
18     void SetPossiblePoint           (std::vector<double> possiblePoint);
19     void _OnSetPoint                ();
20     void _OnSetPointLocalReference  ();
21     void ReleaseCenter              (wxSlider *slider);
22     void TrackCenter                (double step);
23
24     
25     // Type : Module A
26     void OnSliderTrackX     (wxScrollEvent& event);
27     void OnSliderTrackY     (wxScrollEvent& event);
28     void OnSliderTrackZ     (wxScrollEvent& event);
29     void OnSliderReleaseX   (wxScrollEvent& event);
30     void OnSliderReleaseY   (wxScrollEvent& event);
31     void OnSliderReleaseZ   (wxScrollEvent& event);
32     
33     // Type : Module B
34     void OnRadioButtonX(wxEvent& event);
35     void OnRadioButtonY(wxEvent& event);
36     void OnRadioButtonZ(wxEvent& event);
37     void OnRadioButtonN(wxEvent& event);
38     void OnReset(wxCommandEvent& event);
39     void OnMoveDown(wxCommandEvent& event);
40     void OnMoveUp(wxCommandEvent& event);
41     void OnSliderGeneralTrack(wxScrollEvent& event);
42     void OnSliderGeneralRelease(wxScrollEvent& event);
43     
44 private:
45     vtkTransform            *t;
46     vtkTransform            *tt;
47     std::vector<double>     mttActualPoint;
48     std::vector<double>     mPossiblePoint;
49     std::vector<double>     mLastPossiblePoint_localReference;
50     std::vector<double>     mActualPoint;
51     TransformWidget2        *mBox;
52     
53     // Type : Module A
54     wxSlider                *mSliderRotationX;
55     wxSlider                *mSliderRotationY;
56     wxSlider                *mSliderRotationZ;
57     // Type : Module B
58     char                    mTool;
59     wxSlider                *mSliderGeneral;
60     wxCheckBox              *cbLocalReference;
61 };
62
63 //------------------------------------------------------------------------
64 //------------------------------------------------------------------------
65 //------------------------------------------------------------------------
66
67 TransformWidgetPanel::TransformWidgetPanel(TransformWidget2* box, wxWindow *parent)
68   : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
69   mBox(box)
70 {
71     t                       = vtkTransform::New();
72     tt                      = vtkTransform::New();
73     mttActualPoint.push_back(0);
74     mttActualPoint.push_back(0);
75     mttActualPoint.push_back(0);
76     
77     mActualPoint.push_back(0);
78     mActualPoint.push_back(0);
79     mActualPoint.push_back(0);
80
81     mPossiblePoint.push_back(0);
82     mPossiblePoint.push_back(0);
83     mPossiblePoint.push_back(0);
84
85     wxSize  sizeSliderWidget;
86     wxSize sizeButton;
87     
88     if (mBox->bbGetInputType()==0)
89     {
90         sizeSliderWidget.Set(25,25);
91         sizeButton.Set( 45 , -1 );
92         mSliderRotationX        = new wxSlider( this, -1, 0 , -180, 180     , wxDefaultPosition,sizeSliderWidget, wxSL_HORIZONTAL, wxDefaultValidator                           );
93         mSliderRotationY        = new wxSlider( this, -1, 0 , -180, 180     , wxDefaultPosition,sizeSliderWidget, wxSL_HORIZONTAL, wxDefaultValidator                           );
94         mSliderRotationZ        = new wxSlider( this, -1, 0 , -180, 180     , wxDefaultPosition,sizeSliderWidget, wxSL_HORIZONTAL, wxDefaultValidator                           );
95         wxButton *mBtnSetPoint  = new wxButton( this, -1, _T("Set Point")   , wxDefaultPosition, sizeButton                                                                     );
96         Connect(mBtnSetPoint->GetId()     , wxEVT_COMMAND_BUTTON_CLICKED    , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSetPoint      );
97         Connect( mSliderRotationX->GetId(), wxEVT_SCROLL_THUMBRELEASE       , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderReleaseX);
98         Connect( mSliderRotationX->GetId(), wxEVT_SCROLL_THUMBTRACK         , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackX  );
99         Connect( mSliderRotationX->GetId(), wxEVT_SCROLL_CHANGED            , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackX  );
100         Connect( mSliderRotationY->GetId(), wxEVT_SCROLL_THUMBRELEASE       , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderReleaseY);
101         Connect( mSliderRotationY->GetId(), wxEVT_SCROLL_THUMBTRACK         , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackY  );
102         Connect( mSliderRotationY->GetId(), wxEVT_SCROLL_CHANGED            , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackY  );
103         Connect( mSliderRotationZ->GetId(), wxEVT_SCROLL_THUMBRELEASE       , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderReleaseZ);
104         Connect( mSliderRotationZ->GetId(), wxEVT_SCROLL_THUMBTRACK         , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackZ  );
105         Connect( mSliderRotationZ->GetId(), wxEVT_SCROLL_CHANGED            , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderTrackZ  );
106         wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
107         sizer->Add( mSliderRotationX    , 1 );
108         sizer->Add( mSliderRotationY    , 1 );
109         sizer->Add( mSliderRotationZ    , 1 );
110         sizer->Add( mBtnSetPoint        , 1 );
111         SetSizer(sizer);
112     } // if Type == 0
113     
114     if (mBox->bbGetInputType()==1)
115     {
116         sizeSliderWidget.Set(250,-1     );
117         sizeButton.Set( 45 , -1 );
118         wxRadioButton *rBtnX    = new wxRadioButton( this, -1, _T("ri")         , wxDefaultPosition    , wxDefaultSize , wxRB_GROUP);
119         wxRadioButton *rBtnY    = new wxRadioButton( this, -1, _T("rj")         , wxDefaultPosition    , wxDefaultSize , 0);
120         wxRadioButton *rBtnZ    = new wxRadioButton( this, -1, _T("rk")         , wxDefaultPosition    , wxDefaultSize , 0);
121         wxRadioButton *rBtnC    = new wxRadioButton( this, -1, _T("Cen")        , wxDefaultPosition   , wxDefaultSize , 0);
122         rBtnC->SetValue(true);        mTool = 3;
123         mSliderGeneral          = new wxSlider     ( this, -1, 0 , -180, 180    , wxDefaultPosition,sizeSliderWidget ,wxSL_HORIZONTAL, wxDefaultValidator                                   );
124         cbLocalReference        = new wxCheckBox   ( this,-1, _T("Loc Ref")     , wxDefaultPosition,wxDefaultSize    ,wxCHK_2STATE | wxALIGN_RIGHT                                          );
125         wxButton *mBtnMoveDown  = new wxButton     ( this, -1, _T("-")          , wxDefaultPosition, sizeButton                                                                             );
126         wxButton *mBtnMoveUp    = new wxButton     ( this, -1, _T("+")          , wxDefaultPosition, sizeButton                                                                             );
127         wxButton *mBtnReset     = new wxButton     ( this, -1, _T("Reset")      , wxDefaultPosition, sizeButton                                                                             );
128         Connect( rBtnX->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED    , (wxObjectEventFunction)(void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonX                );
129         Connect( rBtnY->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED    , (wxObjectEventFunction)(void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonY                );
130         Connect( rBtnZ->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED    , (wxObjectEventFunction)(void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonZ                );
131         Connect( rBtnC->GetId()         , wxEVT_COMMAND_RADIOBUTTON_SELECTED    , (wxObjectEventFunction)(void (wxPanel::*)(wxEvent&)) &TransformWidgetPanel::OnRadioButtonN                );
132         Connect(mBtnReset->GetId()      , wxEVT_COMMAND_BUTTON_CLICKED          , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnReset                 );
133         Connect(mBtnMoveDown->GetId()   , wxEVT_COMMAND_BUTTON_CLICKED          , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnMoveDown              );
134         Connect(mBtnMoveUp->GetId()     , wxEVT_COMMAND_BUTTON_CLICKED          , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnMoveUp                );
135         Connect( mSliderGeneral->GetId(), wxEVT_SCROLL_THUMBRELEASE             , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderGeneralRelease  );
136         Connect( mSliderGeneral->GetId(), wxEVT_SCROLL_THUMBTRACK               , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderGeneralTrack    );
137         Connect( mSliderGeneral->GetId(), wxEVT_SCROLL_CHANGED                  , (wxObjectEventFunction)(void (wxPanel::*)(wxScrollEvent&)) &TransformWidgetPanel::OnSliderGeneralTrack    );
138         
139         wxBoxSizer *sizerA = new wxBoxSizer(wxHORIZONTAL);
140         sizerA->Add( rBtnC              , 1 );
141         sizerA->Add( rBtnX              , 1 );
142         sizerA->Add( rBtnY              , 1 );
143         sizerA->Add( rBtnZ              , 1 );
144         wxBoxSizer *sizerB = new wxBoxSizer(wxHORIZONTAL);
145         sizerB->Add( mSliderGeneral     , 0 );
146         wxBoxSizer *sizerC = new wxBoxSizer(wxHORIZONTAL);
147         sizerC->Add( cbLocalReference   , 1 );
148         sizerC->Add( mBtnReset          , 1 );
149         sizerC->Add( mBtnMoveDown       , 1 );
150         sizerC->Add( mBtnMoveUp         , 1 );
151         wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
152         sizer->Add( sizerA              , 0 );
153         sizer->Add( sizerB              , 0 );
154         sizer->Add( sizerC              , 0 );
155
156         SetSizer(sizer);
157     } // if
158     
159 }
160
161 TransformWidgetPanel::~TransformWidgetPanel()
162 {
163 }
164
165 void TransformWidgetPanel::ReleaseAngle( wxSlider * slider )
166 {
167     if (slider!=NULL)
168     {
169         slider->SetValue(0);
170     }
171     // t = t*tt
172     t->PostMultiply();
173     t->Concatenate ( tt->GetMatrix() );    t->Update();
174     tt->GetMatrix()->Identity();  tt->Update();
175     mBox->bbSetOutputOut( t );
176     mBox->bbSignalOutputModification(std::string("Out"));
177 }
178
179 void TransformWidgetPanel::OnSliderReleaseX(wxScrollEvent& event)
180 {
181     ReleaseAngle( mSliderRotationX );
182 }
183
184 void TransformWidgetPanel::OnSliderReleaseY(wxScrollEvent& event)
185 {
186     ReleaseAngle( mSliderRotationY );
187 }
188
189 void TransformWidgetPanel::OnSliderReleaseZ(wxScrollEvent& event)
190 {
191     ReleaseAngle( mSliderRotationZ );
192 }
193
194 void TransformWidgetPanel::TrackAngle( double angle, int idVector)
195 {
196     // out = t*tt
197     if (tt!=NULL)
198     {
199         tt->Delete();
200     }
201     vtkMatrix4x4    *tm     = t->GetMatrix();
202     double          tvx    = tm->GetElement(0,idVector);
203     double          tvy    = tm->GetElement(1,idVector);
204     double          tvz    = tm->GetElement(2,idVector);
205     tt = vtkTransform::New();
206     tt->PostMultiply();
207
208 //    angle = angle/2;
209     tt->RotateWXYZ( angle , tvx,tvy,tvz);  tt->Update();
210     vtkTransform *result=vtkTransform::New();
211     result->PostMultiply();
212     result->Concatenate ( t->GetMatrix() );    result->Update();
213     result->Concatenate ( tt->GetMatrix() );   result->Update();
214     mBox->bbSetOutputOut( result );
215     mBox->bbSignalOutputModification(std::string("Out"));
216 }
217
218 void TransformWidgetPanel::OnSliderTrackX(wxScrollEvent& event)
219 {
220     TrackAngle(mSliderRotationX->GetValue() ,0);
221 }
222
223 void TransformWidgetPanel::OnSliderTrackY(wxScrollEvent& event)
224 {
225     TrackAngle(mSliderRotationY->GetValue() ,1);
226 }
227
228 void TransformWidgetPanel::OnSliderTrackZ(wxScrollEvent& event)
229 {
230     TrackAngle(mSliderRotationZ->GetValue() ,2);
231 }
232
233 void TransformWidgetPanel::SetPossiblePoint(std::vector<double> possiblePoint)
234 {
235     mPossiblePoint = possiblePoint;
236 }
237
238 void TransformWidgetPanel::_OnSetPoint()
239 {
240     mActualPoint                        = mPossiblePoint;
241     mLastPossiblePoint_localReference   = mPossiblePoint;
242     mBox->bbSetOutputActualPoint( mActualPoint );
243     mBox->bbSignalOutputModification(std::string("ActualPoint"));
244 }
245
246 void TransformWidgetPanel::_OnSetPointLocalReference()
247 {
248     mActualPoint        = mLastPossiblePoint_localReference;
249 //    mLastPossiblePoint  = mPossiblePoint;
250     mBox->bbSetOutputActualPoint( mActualPoint );
251     mBox->bbSignalOutputModification(std::string("ActualPoint"));
252 }
253
254
255
256 void TransformWidgetPanel::OnSetPoint(wxCommandEvent& event)
257 {
258     _OnSetPoint();
259 }
260
261 //------------------------------------------------------------------------
262
263 void TransformWidgetPanel::OnRadioButtonX(wxEvent& event)
264 {
265     mTool = 0;
266 }
267
268 void TransformWidgetPanel::OnRadioButtonY(wxEvent& event)
269 {
270     mTool = 1;
271 }
272
273 void TransformWidgetPanel::OnRadioButtonZ(wxEvent& event)
274 {
275     mTool = 2;
276 }
277
278 void TransformWidgetPanel::OnRadioButtonN(wxEvent& event)
279 {
280     mTool = 3;
281 }
282
283 void TransformWidgetPanel::OnReset(wxCommandEvent& event)
284 {
285     if ((mTool>=0) && (mTool<=2))
286     {
287         
288         if (cbLocalReference->GetValue()==false)
289         {
290             tt->GetMatrix()->Identity(); tt->Update();
291             t->GetMatrix()->Identity();  t->Update();
292             if (mTool==0)
293             {
294                 ReleaseAngle( NULL );
295             } // if
296             if (mTool==1)
297             {
298                 t->RotateWXYZ( 90 , 1,0,0);  tt->Update();
299                 ReleaseAngle( NULL );
300             } // if
301             if (mTool==2)
302             {
303                 t->RotateWXYZ( 90 , 0,1,0);  tt->Update();
304                 ReleaseAngle( NULL );
305             } // if
306         } else {
307             TrackAngle(45, mTool );
308             ReleaseAngle( NULL );
309         } // if cbLocalReference
310
311         
312     } // if mTool >=0   <=2
313     if (mTool==3)
314     {
315         if (cbLocalReference->GetValue()==false)
316         {
317             _OnSetPoint();
318         } else {
319             _OnSetPointLocalReference();
320         } // if cbLocalReference
321     } // if mTool == 3
322 }
323
324
325
326 void TransformWidgetPanel::ReleaseCenter( wxSlider * slider )
327 {
328     if (slider!=NULL)
329     {
330         slider->SetValue(0);
331     }
332     mActualPoint[0] = mttActualPoint[0];
333     mActualPoint[1] = mttActualPoint[1];
334     mActualPoint[2] = mttActualPoint[2];
335 }
336
337 void TransformWidgetPanel::TrackCenter( double step )
338 {
339     vtkMatrix4x4    *tm     = t->GetMatrix();
340     mttActualPoint[0]       = mActualPoint[0] + tm->GetElement(0,2)*step;
341     mttActualPoint[1]       = mActualPoint[1] + tm->GetElement(1,2)*step;
342     mttActualPoint[2]       = mActualPoint[2] + tm->GetElement(2,2)*step;
343     mBox->bbSetOutputActualPoint( mttActualPoint );
344     mBox->bbSignalOutputModification(std::string("ActualPoint"));
345     mBox->bbSetOutputOut( t );
346     mBox->bbSignalOutputModification(std::string("Out"));
347 }
348
349 void TransformWidgetPanel::OnMoveDown(wxCommandEvent& event)
350 {
351     if ((mTool>=0) && (mTool<=2))
352     {
353         TrackAngle( -1 , mTool );
354         ReleaseAngle( NULL );
355     }
356     if (mTool==3)
357     {
358         TrackCenter( -1 );
359         ReleaseCenter( NULL );
360     } // if mTool == 3
361 }
362
363 void TransformWidgetPanel::OnMoveUp(wxCommandEvent& event)
364 {
365     if ((mTool>=0) && (mTool<=2))
366     {
367         TrackAngle( 1 , mTool );
368         ReleaseAngle( NULL );
369     } // if mTool == 0 1 2
370     if (mTool==3)
371     {
372         TrackCenter( 1 );
373         ReleaseCenter( NULL );
374     } // if mTool == 3
375 }
376
377 void TransformWidgetPanel::OnSliderGeneralRelease(wxScrollEvent& event)
378 {
379     if ((mTool>=0) && (mTool<=2))
380     {
381         ReleaseAngle( mSliderGeneral );
382     } // if mTool == 0 1 2
383     if (mTool==3)
384     {
385         ReleaseCenter( mSliderGeneral );
386     } // if mTool == 3
387 }
388
389 void TransformWidgetPanel::OnSliderGeneralTrack(wxScrollEvent& event)
390 {
391     if ((mTool>=0) && (mTool<=2))
392     {
393         TrackAngle( mSliderGeneral->GetValue() , mTool );
394     }  // if mTool == 0 1 2
395     if (mTool==3)
396     {
397         TrackCenter( mSliderGeneral->GetValue() );
398     } // if mTool == 3
399 }
400
401
402 //------------------------------------------------------------------------
403 //------------------------------------------------------------------------
404 //------------------------------------------------------------------------
405
406 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TransformWidget2)
407 BBTK_BLACK_BOX_IMPLEMENTATION(TransformWidget2,bbtk::WxBlackBox);
408 //===== 
409 // 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)
410 //===== 
411 void TransformWidget2::Process()
412 {
413     TransformWidgetPanel *w = (TransformWidgetPanel*) bbGetOutputWidget( );
414     w->SetPossiblePoint( bbGetInputPossiblePoint() );
415     if (firsttime==true)
416     {
417         firsttime=false;
418         w->_OnSetPoint();
419     }// if
420 }
421
422 //===== 
423 // 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)
424 //===== 
425 void TransformWidget2::CreateWidget(wxWindow* parent)
426 {
427    TransformWidgetPanel *w =  new TransformWidgetPanel(this,parent);
428    bbSetOutputWidget( w );
429 }
430
431 //===== 
432 // 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)
433 //===== 
434 void TransformWidget2::bbUserSetDefaultValues()
435 {
436     firsttime=true;
437     bbSetInputType(1);
438 }
439
440 //===== 
441 // 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)
442 //===== 
443 void TransformWidget2::bbUserInitializeProcessing()
444 {
445 }
446
447 //===== 
448 // 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)
449 //===== 
450 void TransformWidget2::bbUserFinalizeProcessing()
451 {
452 }
453
454 }// EO namespace bbcreaMaracasVisu
455
456