]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Interaction/SphereWidget.cxx
07d778f84041a39e58e8308ff12dcf697c38b9aa
[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
14 // -------------------------------------------------------------------------
15 cpExtensions::Interaction::SphereWidget::
16 Self* cpExtensions::Interaction::SphereWidget::
17 New()
18 {
19   return(new Self);
20 }
21
22 // -------------------------------------------------------------------------
23 void cpExtensions::Interaction::SphereWidget::
24 SetInteractor(vtkRenderWindowInteractor* rwi)
25 {
26   this->Superclass::SetInteractor(rwi);
27   TBaseStyle* s = dynamic_cast<TBaseStyle*>(rwi->GetInteractorStyle());
28   if (s != NULL)
29   {
30     s->AddMouseClickCommand(Self::_Click, this);
31     //s->AddMouseDoubleClickCommand( Self::_DoubleClick, this );
32     s->AddKeyCommand(Self::_KeyPress, this);
33   } // fi
34 }
35
36 // -------------------------------------------------------------------------
37 cpExtensions::Interaction::SphereWidget::
38 SphereWidget()
39 : Superclass()
40 {
41   /*vtkSmartPointer<vtkself> self =
42     vtkself::SafeDownCast(this->GetRepresentation());*/
43
44   //self->SetHandleSize(0);
45   //self->SetRadialLine(2);
46   this->SetRadius(7);
47   
48   this->SetPhiResolution(10);
49   this->SetThetaResolution(20);
50
51   //self->HandleVisibilityOff();
52   //self->SetHandleText(0);
53   this->SetRepresentationToWireframe();
54
55   // Remove default translations
56   /* vtkWidgetEventTranslator* t = this->GetEventTranslator();
57    t->RemoveTranslation(vtkCommand::LeftButtonPressEvent);
58    t->RemoveTranslation(vtkCommand::MiddleButtonPressEvent);
59    t->RemoveTranslation(vtkCommand::RightButtonPressEvent);*/
60 }
61
62 // -------------------------------------------------------------------------
63 cpExtensions::Interaction::SphereWidget::
64 ~SphereWidget()
65 {
66 }
67
68 // -------------------------------------------------------------------------
69 void cpExtensions::Interaction::SphereWidget::
70 _Click(
71 void* data, const TBaseStyle::ButtonID& button,
72 int* idx, double* pos, bool alt, bool ctr, bool sft
73 )
74 {
75   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
76   self->Superclass::StartInteraction();
77   self->InvokeEvent(vtkCommand::LeftButtonPressEvent, NULL);
78   //if( self->WidgetState == vtkSphereWidget2::MovingSeed )
79   //  return;
80
81   //int st = self->WidgetRep->ComputeInteractionState( idx[ 0 ], idx[ 1 ] );
82   //if( st == vtkSeedRepresentation::NearSeed )
83   //{
84   //  self->WidgetState = vtkSphereWidget2::MovingSeed;
85
86   //  // Invoke an event on ourself for the handles
87   //  self->InvokeEvent( vtkCommand::LeftButtonPressEvent, NULL );
88   //  self->Superclass::StartInteraction( );
89   //  self->InvokeEvent( vtkCommand::StartInteractionEvent, NULL );
90   //  self->EventCallbackCommand->SetAbortFlag( 1 );
91   //  self->Render( );
92
93   //} // fi
94 }
95
96 // -------------------------------------------------------------------------
97 void cpExtensions::Interaction::SphereWidget::
98 _DoubleClick(
99 void* data, const TBaseStyle::ButtonID& button,
100 int* idx, double* pos, bool alt, bool ctr, bool sft
101 )
102 {
103
104 }
105
106 // -------------------------------------------------------------------------
107 void cpExtensions::Interaction::SphereWidget::_KeyPress(void* data, const char& key)
108 {
109   SphereWidget* self = reinterpret_cast<SphereWidget*>(data);
110
111   double handlePosition[3];
112  /* vtkself* self =
113     vtkself::SafeDownCast(self->GetRepresentation());
114   */
115   auto center = self->GetCenter();
116   auto center2 = self->GetCenter();
117   auto radius = self->GetRadius();
118
119   self->Superclass::StartInteraction();
120   switch (key)
121   {
122   case 122: // Z & z
123   case 90:
124     radius += 0.1;
125     //self->SetRadius(radius);
126     break;
127   case 88:  // X & x
128   case 120:
129     radius -= 0.1;
130     //self->SetRadius(radius);
131     break;
132   case 119: // W & w
133   case 87:
134     center[0] += 1;
135     self->Translate(center, center2);
136     break;
137   case 83: // S & s
138   case 115: 
139     center[0] -= 1;
140     self->Translate(center, center2);
141     break;
142   case 65: // A & a
143   case 97:
144     center[1] += 1;
145     self->SetCenter(center);
146     break;
147   case 100: // D & d
148   case 68:
149     center[1] -= 1;
150     self->SetCenter(center);
151     break;
152   case 81: // Q & q
153   case 113:
154     center[2] += 1;
155     self->SetCenter(center);
156     break;
157   case 69: // E & e
158   case 101:
159     center[2] -= 1;
160     self->SetCenter(center);
161     break;
162
163   default:
164     break;
165   }
166
167   self->SetRadius(radius);
168   self->Translate(center, center2);
169
170   vtkSmartPointer<vtkSphere> sphere = vtkSmartPointer<vtkSphere>::New();
171   
172
173   self->InvokeEvent(vtkCommand::ModifiedEvent, NULL);
174   
175   self->Modified();
176   self->GetInteractor()->Render();
177   self->ReleaseFocus();
178 }
179
180
181
182
183
184 // eof - $RCSfile$