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