2 // C++ Implementation: egvtkinteractorstyle
7 // Author: Mike Taverne <mtaverne@engits.com>, (C) 2009
9 // Copyright: See COPYING file that comes with this distribution
12 #include "egvtkinteractorstyle.h"
14 #include "vtkInteractorStyleUser.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];
44 this->FindPokedRenderer(x
, y
);
46 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
50 this->FindPokedRenderer(x
, y
);
52 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
56 this->FindPokedRenderer(x
, y
);
58 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
62 this->FindPokedRenderer(x
, y
);
64 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
69 //----------------------------------------------------------------------------
70 void egvtkInteractorStyle::OnLeftButtonDown()
72 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
73 this->Interactor
->GetEventPosition()[1]);
74 if (this->CurrentRenderer
== NULL
)
79 this->GrabFocus(this->EventCallbackCommand
);
80 if (this->Interactor
->GetShiftKey())
82 if (this->Interactor
->GetControlKey())
93 if (this->Interactor
->GetControlKey())
104 //----------------------------------------------------------------------------
105 void egvtkInteractorStyle::OnLeftButtonUp()
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
)
142 this->GrabFocus(this->EventCallbackCommand
);
146 //----------------------------------------------------------------------------
147 void egvtkInteractorStyle::OnMiddleButtonUp()
153 if ( this->Interactor
)
155 this->ReleaseFocus();
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
)
175 this->GrabFocus(this->EventCallbackCommand
);
179 //----------------------------------------------------------------------------
180 void egvtkInteractorStyle::OnRightButtonUp()
187 if ( this->Interactor
)
189 this->ReleaseFocus();
195 //----------------------------------------------------------------------------
196 void egvtkInteractorStyle::OnMouseWheelForward()
198 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
199 this->Interactor
->GetEventPosition()[1]);
200 if (this->CurrentRenderer
== NULL
)
205 this->GrabFocus(this->EventCallbackCommand
);
207 double factor
= this->MotionFactor
* 0.2 * this->MouseWheelMotionFactor
;
208 this->Dolly(pow(1.1, factor
));
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
)
223 this->GrabFocus(this->EventCallbackCommand
);
225 double factor
= this->MotionFactor
* -0.2 * this->MouseWheelMotionFactor
;
226 this->Dolly(pow(1.1, factor
));
228 this->ReleaseFocus();
231 //----------------------------------------------------------------------------
232 void egvtkInteractorStyle::Rotate()
234 if (this->CurrentRenderer
== NULL
)
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();
270 //----------------------------------------------------------------------------
271 void egvtkInteractorStyle::Spin()
273 if ( this->CurrentRenderer
== NULL
)
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();
293 //----------------------------------------------------------------------------
294 void egvtkInteractorStyle::Pan()
296 if (this->CurrentRenderer
== NULL
)
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],
312 focalDepth
= viewFocus
[2];
314 this->ComputeDisplayToWorld(rwi
->GetEventPosition()[0],
315 rwi
->GetEventPosition()[1],
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],
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();
351 //----------------------------------------------------------------------------
352 void egvtkInteractorStyle::Dolly()
354 if (this->CurrentRenderer
== NULL
)
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
)
374 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
375 if (camera
->GetParallelProjection())
377 camera
->SetParallelScale(camera
->GetParallelScale() / factor
);
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())
413 cout
<<"pick node by mouse"<<endl
;
416 cout
<<"pick node by ID"<<endl
;
420 cout
<<"pick cell by mouse"<<endl
;
423 cout
<<"pick cell by ID"<<endl
;
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
);
438 this->vtkInteractorStyle::OnChar();