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 int X
= this->Interactor
->GetEventPosition()[0];
85 int Y
= this->Interactor
->GetEventPosition()[1];
86 cout
<<"You clicked at ("<<X
<<","<<Y
<<")"<<endl
;
88 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
89 this->Interactor
->GetEventPosition()[1]);
90 if (this->CurrentRenderer
== NULL
)
95 this->GrabFocus(this->EventCallbackCommand
);
96 if (this->Interactor
->GetShiftKey())
98 if (this->Interactor
->GetControlKey())
109 if (this->Interactor
->GetControlKey())
120 //----------------------------------------------------------------------------
121 void egvtkInteractorStyle::OnLeftButtonUp()
142 if ( this->Interactor
)
144 this->ReleaseFocus();
148 //----------------------------------------------------------------------------
149 void egvtkInteractorStyle::OnMiddleButtonDown()
151 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
152 this->Interactor
->GetEventPosition()[1]);
153 if (this->CurrentRenderer
== NULL
)
158 this->GrabFocus(this->EventCallbackCommand
);
162 //----------------------------------------------------------------------------
163 void egvtkInteractorStyle::OnMiddleButtonUp()
169 if ( this->Interactor
)
171 this->ReleaseFocus();
177 //----------------------------------------------------------------------------
178 void egvtkInteractorStyle::OnRightButtonDown()
180 int X
= this->Interactor
->GetEventPosition()[0];
181 int Y
= this->Interactor
->GetEventPosition()[1];
182 //cout<<"You clicked at ("<<X<<","<<Y<<")"<<endl;
184 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
185 this->Interactor
->GetEventPosition()[1]);
186 if (this->CurrentRenderer
== NULL
)
191 this->GrabFocus(this->EventCallbackCommand
);
195 //----------------------------------------------------------------------------
196 void egvtkInteractorStyle::OnRightButtonUp()
203 if ( this->Interactor
)
205 this->ReleaseFocus();
211 //----------------------------------------------------------------------------
212 void egvtkInteractorStyle::OnMouseWheelForward()
214 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
215 this->Interactor
->GetEventPosition()[1]);
216 if (this->CurrentRenderer
== NULL
)
221 this->GrabFocus(this->EventCallbackCommand
);
223 double factor
= this->MotionFactor
* 0.2 * this->MouseWheelMotionFactor
;
224 this->Dolly(pow(1.1, factor
));
226 this->ReleaseFocus();
229 //----------------------------------------------------------------------------
230 void egvtkInteractorStyle::OnMouseWheelBackward()
232 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
233 this->Interactor
->GetEventPosition()[1]);
234 if (this->CurrentRenderer
== NULL
)
239 this->GrabFocus(this->EventCallbackCommand
);
241 double factor
= this->MotionFactor
* -0.2 * this->MouseWheelMotionFactor
;
242 this->Dolly(pow(1.1, factor
));
244 this->ReleaseFocus();
247 //----------------------------------------------------------------------------
248 void egvtkInteractorStyle::Rotate()
250 if (this->CurrentRenderer
== NULL
)
255 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
257 int dx
= rwi
->GetEventPosition()[0] - rwi
->GetLastEventPosition()[0];
258 int dy
= rwi
->GetEventPosition()[1] - rwi
->GetLastEventPosition()[1];
260 int *size
= this->CurrentRenderer
->GetRenderWindow()->GetSize();
262 double delta_elevation
= -20.0 / size
[1];
263 double delta_azimuth
= -20.0 / size
[0];
265 double rxf
= dx
* delta_azimuth
* this->MotionFactor
;
266 double ryf
= dy
* delta_elevation
* this->MotionFactor
;
268 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
269 camera
->Azimuth(rxf
);
270 camera
->Elevation(ryf
);
271 camera
->OrthogonalizeViewUp();
273 if (this->AutoAdjustCameraClippingRange
)
275 this->CurrentRenderer
->ResetCameraClippingRange();
278 if (rwi
->GetLightFollowCamera())
280 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
286 //----------------------------------------------------------------------------
287 void egvtkInteractorStyle::Spin()
289 if ( this->CurrentRenderer
== NULL
)
294 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
296 double *center
= this->CurrentRenderer
->GetCenter();
298 double newAngle
= vtkMath::DegreesFromRadians( atan2( rwi
->GetEventPosition()[1] - center
[1], rwi
->GetEventPosition()[0] - center
[0] ) );
300 double oldAngle
= vtkMath::DegreesFromRadians( atan2( rwi
->GetLastEventPosition()[1] - center
[1], rwi
->GetLastEventPosition()[0] - center
[0] ) );
302 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
303 camera
->Roll( newAngle
- oldAngle
);
304 camera
->OrthogonalizeViewUp();
309 //----------------------------------------------------------------------------
310 void egvtkInteractorStyle::Pan()
312 if (this->CurrentRenderer
== NULL
)
317 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
319 double viewFocus
[4], focalDepth
, viewPoint
[3];
320 double newPickPoint
[4], oldPickPoint
[4], motionVector
[3];
322 // Calculate the focal depth since we'll be using it a lot
324 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
325 camera
->GetFocalPoint(viewFocus
);
326 this->ComputeWorldToDisplay(viewFocus
[0], viewFocus
[1], viewFocus
[2],
328 focalDepth
= viewFocus
[2];
330 this->ComputeDisplayToWorld(rwi
->GetEventPosition()[0],
331 rwi
->GetEventPosition()[1],
335 // Has to recalc old mouse point since the viewport has moved,
336 // so can't move it outside the loop
338 this->ComputeDisplayToWorld(rwi
->GetLastEventPosition()[0],
339 rwi
->GetLastEventPosition()[1],
343 // Camera motion is reversed
345 motionVector
[0] = oldPickPoint
[0] - newPickPoint
[0];
346 motionVector
[1] = oldPickPoint
[1] - newPickPoint
[1];
347 motionVector
[2] = oldPickPoint
[2] - newPickPoint
[2];
349 camera
->GetFocalPoint(viewFocus
);
350 camera
->GetPosition(viewPoint
);
351 camera
->SetFocalPoint(motionVector
[0] + viewFocus
[0],
352 motionVector
[1] + viewFocus
[1],
353 motionVector
[2] + viewFocus
[2]);
355 camera
->SetPosition(motionVector
[0] + viewPoint
[0],
356 motionVector
[1] + viewPoint
[1],
357 motionVector
[2] + viewPoint
[2]);
359 if (rwi
->GetLightFollowCamera())
361 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
367 //----------------------------------------------------------------------------
368 void egvtkInteractorStyle::Dolly()
370 if (this->CurrentRenderer
== NULL
)
375 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
376 double *center
= this->CurrentRenderer
->GetCenter();
377 int dy
= rwi
->GetEventPosition()[1] - rwi
->GetLastEventPosition()[1];
378 double dyf
= this->MotionFactor
* dy
/ center
[1];
379 this->Dolly(pow(1.1, dyf
));
382 //----------------------------------------------------------------------------
383 void egvtkInteractorStyle::Dolly(double factor
)
385 if (this->CurrentRenderer
== NULL
)
390 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
391 if (camera
->GetParallelProjection())
393 camera
->SetParallelScale(camera
->GetParallelScale() / factor
);
397 camera
->Dolly(factor
);
398 if (this->AutoAdjustCameraClippingRange
)
400 this->CurrentRenderer
->ResetCameraClippingRange();
404 if (this->Interactor
->GetLightFollowCamera())
406 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
409 this->Interactor
->Render();
412 //----------------------------------------------------------------------------
413 void egvtkInteractorStyle::PrintSelf(ostream
& os
, vtkIndent indent
)
415 this->Superclass::PrintSelf(os
,indent
);
416 os
<< indent
<< "MotionFactor: " << this->MotionFactor
<< "\n";
419 void egvtkInteractorStyle::OnChar()
421 cout
<<"OnChar "<<this->Interactor
->GetKeyCode()<<endl
;
422 this->EventCallbackCommand
->SetAbortFlag(1);
424 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
425 char key
=rwi
->GetKeyCode();
428 cout
<<"pick node by mouse"<<endl
;
430 /* else if(key=='N') {
431 cout<<"pick node by ID"<<endl;
435 cout
<<"pick cell by mouse"<<endl
;
437 /* else if(key=='C') {
438 cout<<"pick cell by ID"<<endl;
441 /* else if(key=='b') {
442 cout<<"box select"<<endl;
445 /* else if(key=='d') {
446 cout<<"Delete picked point"<<endl;
447 // EG_STDINTERSLOT(DeletePickedPoint);
448 DeletePickedPoint deletepickedpoint;
450 OPER *oper = new OPER(); \
453 if(grid->GetNumberOfPoints()) updateBoundaryCodes(false);
459 // otherwise pass the OnChar to the vtkInteractorStyle.
460 if (this->HasObserver(vtkCommand::CharEvent
)) {
461 this->ShiftKey
= this->Interactor
->GetShiftKey();
462 this->ControlKey
= this->Interactor
->GetControlKey();
463 this->KeyCode
= this->Interactor
->GetKeyCode();
465 this->InvokeEvent(vtkCommand::CharEvent
,NULL
);
468 this->vtkInteractorStyle::OnChar();