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