tmp commit
[engrid.git] / egvtkinteractorstyle.cpp
bloba4d6246c13920ca79e278aa7d2555a6fe358df40
1 //
2 // C++ Implementation: egvtkinteractorstyle
3 //
4 // Description:
5 //
6 //
7 // Author: Mike Taverne <mtaverne@engits.com>, (C) 2009
8 //
9 // Copyright: See COPYING file that comes with this distribution
12 #include "egvtkinteractorstyle.h"
14 #include "vtkInteractorStyleUser.h"
15 #include "vtkMath.h"
16 #include "vtkCellPicker.h"
17 #include "vtkRenderWindowInteractor.h"
18 #include "vtkObjectFactory.h"
19 #include "vtkCommand.h"
21 vtkCxxRevisionMacro(egvtkInteractorStyle, "$Revision: 1.37 $");
22 vtkStandardNewMacro(egvtkInteractorStyle);
24 //----------------------------------------------------------------------------
25 egvtkInteractorStyle::egvtkInteractorStyle()
27 this->MotionFactor = 10.0;
30 //----------------------------------------------------------------------------
31 egvtkInteractorStyle::~egvtkInteractorStyle()
35 //----------------------------------------------------------------------------
36 void egvtkInteractorStyle::OnMouseMove()
38 int x = this->Interactor->GetEventPosition()[0];
39 int y = this->Interactor->GetEventPosition()[1];
41 switch (this->State)
43 case VTKIS_ROTATE:
44 this->FindPokedRenderer(x, y);
45 this->Rotate();
46 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
47 break;
49 case VTKIS_PAN:
50 this->FindPokedRenderer(x, y);
51 this->Pan();
52 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
53 break;
55 case VTKIS_DOLLY:
56 this->FindPokedRenderer(x, y);
57 this->Dolly();
58 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
59 break;
61 case VTKIS_SPIN:
62 this->FindPokedRenderer(x, y);
63 this->Spin();
64 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
65 break;
69 //----------------------------------------------------------------------------
70 void egvtkInteractorStyle::OnLeftButtonDown()
72 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
73 this->Interactor->GetEventPosition()[1]);
74 if (this->CurrentRenderer == NULL)
76 return;
79 this->GrabFocus(this->EventCallbackCommand);
80 if (this->Interactor->GetShiftKey())
82 if (this->Interactor->GetControlKey())
84 this->StartDolly();
86 else
88 this->StartPan();
91 else
93 if (this->Interactor->GetControlKey())
95 this->StartSpin();
97 else
99 this->StartRotate();
104 //----------------------------------------------------------------------------
105 void egvtkInteractorStyle::OnLeftButtonUp()
107 switch (this->State)
109 case VTKIS_DOLLY:
110 this->EndDolly();
111 break;
113 case VTKIS_PAN:
114 this->EndPan();
115 break;
117 case VTKIS_SPIN:
118 this->EndSpin();
119 break;
121 case VTKIS_ROTATE:
122 this->EndRotate();
123 break;
126 if ( this->Interactor )
128 this->ReleaseFocus();
132 //----------------------------------------------------------------------------
133 void egvtkInteractorStyle::OnMiddleButtonDown()
135 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
136 this->Interactor->GetEventPosition()[1]);
137 if (this->CurrentRenderer == NULL)
139 return;
142 this->GrabFocus(this->EventCallbackCommand);
143 this->StartPan();
146 //----------------------------------------------------------------------------
147 void egvtkInteractorStyle::OnMiddleButtonUp()
149 switch (this->State)
151 case VTKIS_PAN:
152 this->EndPan();
153 if ( this->Interactor )
155 this->ReleaseFocus();
157 break;
161 //----------------------------------------------------------------------------
162 void egvtkInteractorStyle::OnRightButtonDown()
164 int X = this->Interactor->GetEventPosition()[0];
165 int Y = this->Interactor->GetEventPosition()[1];
166 cout<<"You clicked at ("<<X<<","<<Y<<")"<<endl;
168 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
169 this->Interactor->GetEventPosition()[1]);
170 if (this->CurrentRenderer == NULL)
172 return;
175 this->GrabFocus(this->EventCallbackCommand);
176 this->StartDolly();
179 //----------------------------------------------------------------------------
180 void egvtkInteractorStyle::OnRightButtonUp()
182 switch (this->State)
184 case VTKIS_DOLLY:
185 this->EndDolly();
187 if ( this->Interactor )
189 this->ReleaseFocus();
191 break;
195 //----------------------------------------------------------------------------
196 void egvtkInteractorStyle::OnMouseWheelForward()
198 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
199 this->Interactor->GetEventPosition()[1]);
200 if (this->CurrentRenderer == NULL)
202 return;
205 this->GrabFocus(this->EventCallbackCommand);
206 this->StartDolly();
207 double factor = this->MotionFactor * 0.2 * this->MouseWheelMotionFactor;
208 this->Dolly(pow(1.1, factor));
209 this->EndDolly();
210 this->ReleaseFocus();
213 //----------------------------------------------------------------------------
214 void egvtkInteractorStyle::OnMouseWheelBackward()
216 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
217 this->Interactor->GetEventPosition()[1]);
218 if (this->CurrentRenderer == NULL)
220 return;
223 this->GrabFocus(this->EventCallbackCommand);
224 this->StartDolly();
225 double factor = this->MotionFactor * -0.2 * this->MouseWheelMotionFactor;
226 this->Dolly(pow(1.1, factor));
227 this->EndDolly();
228 this->ReleaseFocus();
231 //----------------------------------------------------------------------------
232 void egvtkInteractorStyle::Rotate()
234 if (this->CurrentRenderer == NULL)
236 return;
239 vtkRenderWindowInteractor *rwi = this->Interactor;
241 int dx = rwi->GetEventPosition()[0] - rwi->GetLastEventPosition()[0];
242 int dy = rwi->GetEventPosition()[1] - rwi->GetLastEventPosition()[1];
244 int *size = this->CurrentRenderer->GetRenderWindow()->GetSize();
246 double delta_elevation = -20.0 / size[1];
247 double delta_azimuth = -20.0 / size[0];
249 double rxf = dx * delta_azimuth * this->MotionFactor;
250 double ryf = dy * delta_elevation * this->MotionFactor;
252 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
253 camera->Azimuth(rxf);
254 camera->Elevation(ryf);
255 camera->OrthogonalizeViewUp();
257 if (this->AutoAdjustCameraClippingRange)
259 this->CurrentRenderer->ResetCameraClippingRange();
262 if (rwi->GetLightFollowCamera())
264 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
267 rwi->Render();
270 //----------------------------------------------------------------------------
271 void egvtkInteractorStyle::Spin()
273 if ( this->CurrentRenderer == NULL )
275 return;
278 vtkRenderWindowInteractor *rwi = this->Interactor;
280 double *center = this->CurrentRenderer->GetCenter();
282 double newAngle = atan2( rwi->GetEventPosition()[1] - center[1], rwi->GetEventPosition()[0] - center[0] ) * vtkMath::RadiansToDegrees();
284 double oldAngle = atan2( rwi->GetLastEventPosition()[1] - center[1], rwi->GetLastEventPosition()[0] - center[0] ) * vtkMath::RadiansToDegrees();
286 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
287 camera->Roll( newAngle - oldAngle );
288 camera->OrthogonalizeViewUp();
290 rwi->Render();
293 //----------------------------------------------------------------------------
294 void egvtkInteractorStyle::Pan()
296 if (this->CurrentRenderer == NULL)
298 return;
301 vtkRenderWindowInteractor *rwi = this->Interactor;
303 double viewFocus[4], focalDepth, viewPoint[3];
304 double newPickPoint[4], oldPickPoint[4], motionVector[3];
306 // Calculate the focal depth since we'll be using it a lot
308 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
309 camera->GetFocalPoint(viewFocus);
310 this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2],
311 viewFocus);
312 focalDepth = viewFocus[2];
314 this->ComputeDisplayToWorld(rwi->GetEventPosition()[0],
315 rwi->GetEventPosition()[1],
316 focalDepth,
317 newPickPoint);
319 // Has to recalc old mouse point since the viewport has moved,
320 // so can't move it outside the loop
322 this->ComputeDisplayToWorld(rwi->GetLastEventPosition()[0],
323 rwi->GetLastEventPosition()[1],
324 focalDepth,
325 oldPickPoint);
327 // Camera motion is reversed
329 motionVector[0] = oldPickPoint[0] - newPickPoint[0];
330 motionVector[1] = oldPickPoint[1] - newPickPoint[1];
331 motionVector[2] = oldPickPoint[2] - newPickPoint[2];
333 camera->GetFocalPoint(viewFocus);
334 camera->GetPosition(viewPoint);
335 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
336 motionVector[1] + viewFocus[1],
337 motionVector[2] + viewFocus[2]);
339 camera->SetPosition(motionVector[0] + viewPoint[0],
340 motionVector[1] + viewPoint[1],
341 motionVector[2] + viewPoint[2]);
343 if (rwi->GetLightFollowCamera())
345 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
348 rwi->Render();
351 //----------------------------------------------------------------------------
352 void egvtkInteractorStyle::Dolly()
354 if (this->CurrentRenderer == NULL)
356 return;
359 vtkRenderWindowInteractor *rwi = this->Interactor;
360 double *center = this->CurrentRenderer->GetCenter();
361 int dy = rwi->GetEventPosition()[1] - rwi->GetLastEventPosition()[1];
362 double dyf = this->MotionFactor * dy / center[1];
363 this->Dolly(pow(1.1, dyf));
366 //----------------------------------------------------------------------------
367 void egvtkInteractorStyle::Dolly(double factor)
369 if (this->CurrentRenderer == NULL)
371 return;
374 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
375 if (camera->GetParallelProjection())
377 camera->SetParallelScale(camera->GetParallelScale() / factor);
379 else
381 camera->Dolly(factor);
382 if (this->AutoAdjustCameraClippingRange)
384 this->CurrentRenderer->ResetCameraClippingRange();
388 if (this->Interactor->GetLightFollowCamera())
390 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
393 this->Interactor->Render();
396 //----------------------------------------------------------------------------
397 void egvtkInteractorStyle::PrintSelf(ostream& os, vtkIndent indent)
399 this->Superclass::PrintSelf(os,indent);
400 os << indent << "MotionFactor: " << this->MotionFactor << "\n";
403 void egvtkInteractorStyle::OnChar()
405 cout<<"OnChar "<<this->Interactor->GetKeyCode()<<endl;
406 this->EventCallbackCommand->SetAbortFlag(1);
408 vtkRenderWindowInteractor *rwi = this->Interactor;
410 switch (rwi->GetKeyCode())
412 case 'n' :
413 cout<<"pick node by mouse"<<endl;
414 break;
415 case 'N' :
416 cout<<"pick node by ID"<<endl;
417 break;
419 case 'c' :
420 cout<<"pick cell by mouse"<<endl;
421 break;
422 case 'C' :
423 cout<<"pick cell by ID"<<endl;
424 break;
427 // otherwise pass the OnChar to the vtkInteractorStyle.
428 if (this->HasObserver(vtkCommand::CharEvent))
430 this->ShiftKey = this->Interactor->GetShiftKey();
431 this->ControlKey = this->Interactor->GetControlKey();
432 this->KeyCode = this->Interactor->GetKeyCode();
434 this->InvokeEvent(vtkCommand::CharEvent,NULL);
436 else
438 this->vtkInteractorStyle::OnChar();