]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Interaction/SphereWidget.cxx
...
[cpPlugins.git] / lib / cpExtensions / Interaction / SphereWidget.cxx
1 #include <cpExtensions/Interaction/SphereWidget.h>
2
3 #include <vtkCallbackCommand.h>
4 #include <vtkCommand.h>
5 #include <vtkRenderWindowInteractor.h>
6 #include <vtkSeedRepresentation.h>
7 #include <vtkWidgetEventTranslator.h>
8 #include <vtkWidgetRepresentation.h>
9 #include <vtkActor.h>
10 #include <vtkSphereRepresentation.h>
11 #include <vtkSmartPointer.h>
12 #include <vtkSphere.h>
13 #include <vtkProperty.h>
14
15 // -------------------------------------------------------------------------
16 cpExtensions::Interaction::SphereWidget::
17 Self* cpExtensions::Interaction::SphereWidget::
18 New()
19 {
20   return(new Self);
21 }
22
23 // -------------------------------------------------------------------------
24 void cpExtensions::Interaction::SphereWidget::
25 SetInteractor(vtkRenderWindowInteractor* rwi)
26 {
27   this->Superclass::SetInteractor(rwi);
28   TBaseStyle* s = dynamic_cast<TBaseStyle*>(rwi->GetInteractorStyle());
29   if (s != NULL)
30   {    
31     s->AddKeyCommand(Self::_KeyPress, this);
32     s->AddMouseMoveCommand(Self::_MouseMove, this);
33
34   } // fi
35 }
36
37 // -------------------------------------------------------------------------
38 cpExtensions::Interaction::SphereWidget::
39 SphereWidget()
40 : Superclass()
41 {
42   //this->SetAxis(0);
43
44   this->SetTranslation(0);
45   this->SetRadius(7);
46
47   this->SetPhiResolution(10);
48   this->SetThetaResolution(20);
49
50   this->SetRepresentationToWireframe();
51 }
52
53 // -------------------------------------------------------------------------
54 cpExtensions::Interaction::SphereWidget::
55 ~SphereWidget()
56 {
57 }
58
59 // -------------------------------------------------------------------------
60 void cpExtensions::Interaction::SphereWidget::
61 _Click(
62 void* data, const TBaseStyle::ButtonID& button,
63 int* idx, double* pos, bool alt, bool ctr, bool sft
64 )
65 {
66   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
67   self->Superclass::EndInteraction();
68 }
69
70 // -------------------------------------------------------------------------
71 void cpExtensions::Interaction::SphereWidget::
72 _DoubleClick(
73 void* data, const TBaseStyle::ButtonID& button,
74 int* idx, double* pos, bool alt, bool ctr, bool sft
75 )
76 {
77
78 }
79
80 // -------------------------------------------------------------------------
81 void cpExtensions::Interaction::SphereWidget::
82 _MouseMove(
83 void* data, const TBaseStyle::ButtonID& button,
84 int* idx, double* pos, bool alt, bool ctr, bool sft
85 )
86 {
87   /*SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
88   self->Superclass::StartInteraction();*/
89 }
90
91 void cpExtensions::Interaction::SphereWidget::SetAxis(int axis)
92 {
93   this->m_axis = axis;
94 }
95
96
97 void cpExtensions::Interaction::SphereWidget::SetAxis(void* data, int axis)
98 {
99   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
100   self->SetAxis(axis);
101 }
102
103 int cpExtensions::Interaction::SphereWidget::GetAxis()
104 {
105   return this->m_axis;
106 }
107
108 int cpExtensions::Interaction::SphereWidget::GetAxis(void* data)
109 {
110   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
111   return self->GetAxis();
112 }
113
114 double* cpExtensions::Interaction::SphereWidget::GetPosition()
115 {
116   return this->SphereActor->GetPosition();
117 }
118
119 double* cpExtensions::Interaction::SphereWidget::GetPosition(void* data)
120 {
121   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
122   return self->GetPosition();
123 }
124
125 void cpExtensions::Interaction::SphereWidget::SetCenter(double * center)
126 {
127   //this->Superclass::SetCenter(center);
128   this->SphereActor->SetPosition(center);
129   this->SphereActor->Modified();
130   this->Modified();
131   this->GetInteractor()->Render();
132 }
133
134 void cpExtensions::Interaction::SphereWidget::SetCenter(void* data, double * center)
135 {
136   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
137   
138   //self->Superclass::SetCenter(center);
139   self->SphereActor->SetPosition(center);
140   self->SphereActor->Modified();
141   self->Modified();
142   self->GetInteractor()->Render();
143
144   std::cout << "sphere: {" << center[0] << " , " << center[1] << " , " << center[2] << "} " << std::endl;
145 }
146
147 // -------------------------------------------------------------------------
148 void cpExtensions::Interaction::SphereWidget::_KeyPress(void* data, const char& key)
149 {
150   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
151   self->Superclass::StartInteraction();
152
153   double * center = self->SphereActor->GetPosition();
154
155   auto radius = self->GetRadius();
156
157   auto axis = self->GetAxis();
158
159   switch (key)
160   {
161   case 122: // Z & z
162   case 90:
163     radius += 0.1;
164    
165     break;
166   case 88:  // X & x
167   case 120:
168     radius -= 0.1;
169    
170     break;
171   case 119: // W & w
172   case 87:
173     if (axis == 0)
174     {
175       center[2] += 1;
176     } else if (axis == 1)
177     {
178       center[2] += 1;
179     } else
180     {
181       center[1] -= 1;
182     }
183    
184    
185     break;
186   case 83: // S & s
187   case 115:
188     if (axis == 0)
189     {
190       center[2] -= 1;
191     }
192     else if (axis == 1)
193     {
194       center[2] -= 1;
195     }
196     else
197     {
198       center[1] += 1;
199     }
200    
201     break;
202   case 65: // A & a
203   case 97:
204     if (axis == 0)
205     {
206       center[1] -= 1;
207     }
208     else if (axis == 1)
209     {
210       center[0] -= 1;
211     }
212     else
213     {
214       center[0] += 1;
215     }
216    
217     break;
218   case 100: // D & d
219   case 68:
220     if (axis == 0)
221     {
222       center[1] += 1;
223     }
224     else if (axis == 1)
225     {
226       center[0] += 1;
227     }
228     else
229     {
230       center[0] -= 1;
231     }
232    
233     break;
234   //case 81: // Q & q
235   //case 113:
236   //  center[2] += 1;
237   //
238   //  break;
239   //case 69: // E & e
240   //case 101:
241   //  center[2] -= 1;
242   //
243   //  break;
244
245   default:
246     break;
247   }
248
249
250   self->SetRadius(radius);
251   self->SetCenter(data, center);
252   self->Modified();
253   self->GetInteractor()->Render();
254   self->EndInteraction();
255 }
256
257
258
259
260
261
262
263 // eof - $RCSfile$