1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "egvtkinteractorstyle.h"
23 #include "deletepickedpoint.h"
24 #include "egvtkobject.h"
25 #include "operation.h"
27 #include "vtkInteractorStyleUser.h"
29 #include "vtkCellPicker.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkObjectFactory.h"
32 #include "vtkCommand.h"
34 vtkStandardNewMacro(egvtkInteractorStyle
);
36 //----------------------------------------------------------------------------
37 egvtkInteractorStyle::egvtkInteractorStyle()
39 this->MotionFactor
= 10.0;
42 //----------------------------------------------------------------------------
43 egvtkInteractorStyle::~egvtkInteractorStyle()
47 //----------------------------------------------------------------------------
48 void egvtkInteractorStyle::OnMouseMove()
50 int x
= this->Interactor
->GetEventPosition()[0];
51 int y
= this->Interactor
->GetEventPosition()[1];
56 this->FindPokedRenderer(x
, y
);
58 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
62 this->FindPokedRenderer(x
, y
);
64 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
68 this->FindPokedRenderer(x
, y
);
70 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
74 this->FindPokedRenderer(x
, y
);
76 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
81 //----------------------------------------------------------------------------
82 void egvtkInteractorStyle::OnLeftButtonDown()
84 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
85 this->Interactor
->GetEventPosition()[1]);
86 if (this->CurrentRenderer
== NULL
)
91 this->GrabFocus(this->EventCallbackCommand
);
92 if (this->Interactor
->GetShiftKey())
94 if (this->Interactor
->GetControlKey())
105 if (this->Interactor
->GetControlKey())
116 //----------------------------------------------------------------------------
117 void egvtkInteractorStyle::OnLeftButtonUp()
138 if ( this->Interactor
)
140 this->ReleaseFocus();
144 //----------------------------------------------------------------------------
145 void egvtkInteractorStyle::OnMiddleButtonDown()
147 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
148 this->Interactor
->GetEventPosition()[1]);
149 if (this->CurrentRenderer
== NULL
)
154 this->GrabFocus(this->EventCallbackCommand
);
158 //----------------------------------------------------------------------------
159 void egvtkInteractorStyle::OnMiddleButtonUp()
165 if ( this->Interactor
)
167 this->ReleaseFocus();
173 //----------------------------------------------------------------------------
174 void egvtkInteractorStyle::OnRightButtonDown()
176 int X
= this->Interactor
->GetEventPosition()[0];
177 int Y
= this->Interactor
->GetEventPosition()[1];
178 cout
<<"You clicked at ("<<X
<<","<<Y
<<")"<<endl
;
180 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
181 this->Interactor
->GetEventPosition()[1]);
182 if (this->CurrentRenderer
== NULL
)
187 this->GrabFocus(this->EventCallbackCommand
);
191 //----------------------------------------------------------------------------
192 void egvtkInteractorStyle::OnRightButtonUp()
199 if ( this->Interactor
)
201 this->ReleaseFocus();
207 //----------------------------------------------------------------------------
208 void egvtkInteractorStyle::OnMouseWheelForward()
210 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
211 this->Interactor
->GetEventPosition()[1]);
212 if (this->CurrentRenderer
== NULL
)
217 this->GrabFocus(this->EventCallbackCommand
);
219 double factor
= this->MotionFactor
* 0.2 * this->MouseWheelMotionFactor
;
220 this->Dolly(pow(1.1, factor
));
222 this->ReleaseFocus();
225 //----------------------------------------------------------------------------
226 void egvtkInteractorStyle::OnMouseWheelBackward()
228 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
229 this->Interactor
->GetEventPosition()[1]);
230 if (this->CurrentRenderer
== NULL
)
235 this->GrabFocus(this->EventCallbackCommand
);
237 double factor
= this->MotionFactor
* -0.2 * this->MouseWheelMotionFactor
;
238 this->Dolly(pow(1.1, factor
));
240 this->ReleaseFocus();
243 //----------------------------------------------------------------------------
244 void egvtkInteractorStyle::Rotate()
246 if (this->CurrentRenderer
== NULL
)
251 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
253 int dx
= rwi
->GetEventPosition()[0] - rwi
->GetLastEventPosition()[0];
254 int dy
= rwi
->GetEventPosition()[1] - rwi
->GetLastEventPosition()[1];
256 int *size
= this->CurrentRenderer
->GetRenderWindow()->GetSize();
258 double delta_elevation
= -20.0 / size
[1];
259 double delta_azimuth
= -20.0 / size
[0];
261 double rxf
= dx
* delta_azimuth
* this->MotionFactor
;
262 double ryf
= dy
* delta_elevation
* this->MotionFactor
;
264 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
265 camera
->Azimuth(rxf
);
266 camera
->Elevation(ryf
);
267 camera
->OrthogonalizeViewUp();
269 if (this->AutoAdjustCameraClippingRange
)
271 this->CurrentRenderer
->ResetCameraClippingRange();
274 if (rwi
->GetLightFollowCamera())
276 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
282 //----------------------------------------------------------------------------
283 void egvtkInteractorStyle::Spin()
285 if ( this->CurrentRenderer
== NULL
)
290 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
292 double *center
= this->CurrentRenderer
->GetCenter();
294 double newAngle
= vtkMath::DegreesFromRadians( atan2( rwi
->GetEventPosition()[1] - center
[1], rwi
->GetEventPosition()[0] - center
[0] ) );
296 double oldAngle
= vtkMath::DegreesFromRadians( atan2( rwi
->GetLastEventPosition()[1] - center
[1], rwi
->GetLastEventPosition()[0] - center
[0] ) );
298 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
299 camera
->Roll( newAngle
- oldAngle
);
300 camera
->OrthogonalizeViewUp();
305 //----------------------------------------------------------------------------
306 void egvtkInteractorStyle::Pan()
308 if (this->CurrentRenderer
== NULL
)
313 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
315 double viewFocus
[4], focalDepth
, viewPoint
[3];
316 double newPickPoint
[4], oldPickPoint
[4], motionVector
[3];
318 // Calculate the focal depth since we'll be using it a lot
320 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
321 camera
->GetFocalPoint(viewFocus
);
322 this->ComputeWorldToDisplay(viewFocus
[0], viewFocus
[1], viewFocus
[2],
324 focalDepth
= viewFocus
[2];
326 this->ComputeDisplayToWorld(rwi
->GetEventPosition()[0],
327 rwi
->GetEventPosition()[1],
331 // Has to recalc old mouse point since the viewport has moved,
332 // so can't move it outside the loop
334 this->ComputeDisplayToWorld(rwi
->GetLastEventPosition()[0],
335 rwi
->GetLastEventPosition()[1],
339 // Camera motion is reversed
341 motionVector
[0] = oldPickPoint
[0] - newPickPoint
[0];
342 motionVector
[1] = oldPickPoint
[1] - newPickPoint
[1];
343 motionVector
[2] = oldPickPoint
[2] - newPickPoint
[2];
345 camera
->GetFocalPoint(viewFocus
);
346 camera
->GetPosition(viewPoint
);
347 camera
->SetFocalPoint(motionVector
[0] + viewFocus
[0],
348 motionVector
[1] + viewFocus
[1],
349 motionVector
[2] + viewFocus
[2]);
351 camera
->SetPosition(motionVector
[0] + viewPoint
[0],
352 motionVector
[1] + viewPoint
[1],
353 motionVector
[2] + viewPoint
[2]);
355 if (rwi
->GetLightFollowCamera())
357 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
363 //----------------------------------------------------------------------------
364 void egvtkInteractorStyle::Dolly()
366 if (this->CurrentRenderer
== NULL
)
371 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
372 double *center
= this->CurrentRenderer
->GetCenter();
373 int dy
= rwi
->GetEventPosition()[1] - rwi
->GetLastEventPosition()[1];
374 double dyf
= this->MotionFactor
* dy
/ center
[1];
375 this->Dolly(pow(1.1, dyf
));
378 //----------------------------------------------------------------------------
379 void egvtkInteractorStyle::Dolly(double factor
)
381 if (this->CurrentRenderer
== NULL
)
386 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
387 if (camera
->GetParallelProjection())
389 camera
->SetParallelScale(camera
->GetParallelScale() / factor
);
393 camera
->Dolly(factor
);
394 if (this->AutoAdjustCameraClippingRange
)
396 this->CurrentRenderer
->ResetCameraClippingRange();
400 if (this->Interactor
->GetLightFollowCamera())
402 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
405 this->Interactor
->Render();
408 //----------------------------------------------------------------------------
409 void egvtkInteractorStyle::PrintSelf(ostream
& os
, vtkIndent indent
)
411 this->Superclass::PrintSelf(os
,indent
);
412 os
<< indent
<< "MotionFactor: " << this->MotionFactor
<< "\n";
415 void egvtkInteractorStyle::OnChar()
417 cout
<<"OnChar "<<this->Interactor
->GetKeyCode()<<endl
;
418 this->EventCallbackCommand
->SetAbortFlag(1);
420 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
421 char key
=rwi
->GetKeyCode();
424 cout
<<"pick node by mouse"<<endl
;
426 /* else if(key=='N') {
427 cout<<"pick node by ID"<<endl;
431 cout
<<"pick cell by mouse"<<endl
;
433 /* else if(key=='C') {
434 cout<<"pick cell by ID"<<endl;
437 /* else if(key=='b') {
438 cout<<"box select"<<endl;
441 /* else if(key=='d') {
442 cout<<"Delete picked point"<<endl;
443 // EG_STDINTERSLOT(DeletePickedPoint);
444 DeletePickedPoint deletepickedpoint;
446 OPER *oper = new OPER(); \
449 if(grid->GetNumberOfPoints()) updateBoundaryCodes(false);
455 // otherwise pass the OnChar to the vtkInteractorStyle.
456 if (this->HasObserver(vtkCommand::CharEvent
)) {
457 this->ShiftKey
= this->Interactor
->GetShiftKey();
458 this->ControlKey
= this->Interactor
->GetControlKey();
459 this->KeyCode
= this->Interactor
->GetKeyCode();
461 this->InvokeEvent(vtkCommand::CharEvent
,NULL
);
464 this->vtkInteractorStyle::OnChar();