2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2010 enGits GmbH +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "egvtkinteractorstyle.h"
25 #include "deletepickedpoint.h"
26 #include "egvtkobject.h"
27 #include "operation.h"
29 #include "vtkInteractorStyleUser.h"
31 #include "vtkCellPicker.h"
32 #include "vtkRenderWindowInteractor.h"
33 #include "vtkObjectFactory.h"
34 #include "vtkCommand.h"
36 vtkStandardNewMacro(egvtkInteractorStyle
);
38 //----------------------------------------------------------------------------
39 egvtkInteractorStyle::egvtkInteractorStyle()
41 this->MotionFactor
= 10.0;
44 //----------------------------------------------------------------------------
45 egvtkInteractorStyle::~egvtkInteractorStyle()
49 //----------------------------------------------------------------------------
50 void egvtkInteractorStyle::OnMouseMove()
52 int x
= this->Interactor
->GetEventPosition()[0];
53 int y
= this->Interactor
->GetEventPosition()[1];
58 this->FindPokedRenderer(x
, y
);
60 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
64 this->FindPokedRenderer(x
, y
);
66 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
70 this->FindPokedRenderer(x
, y
);
72 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
76 this->FindPokedRenderer(x
, y
);
78 this->InvokeEvent(vtkCommand::InteractionEvent
, NULL
);
83 //----------------------------------------------------------------------------
84 void egvtkInteractorStyle::OnLeftButtonDown()
86 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
87 this->Interactor
->GetEventPosition()[1]);
88 if (this->CurrentRenderer
== NULL
)
93 this->GrabFocus(this->EventCallbackCommand
);
94 if (this->Interactor
->GetShiftKey())
96 if (this->Interactor
->GetControlKey())
107 if (this->Interactor
->GetControlKey())
118 //----------------------------------------------------------------------------
119 void egvtkInteractorStyle::OnLeftButtonUp()
140 if ( this->Interactor
)
142 this->ReleaseFocus();
146 //----------------------------------------------------------------------------
147 void egvtkInteractorStyle::OnMiddleButtonDown()
149 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
150 this->Interactor
->GetEventPosition()[1]);
151 if (this->CurrentRenderer
== NULL
)
156 this->GrabFocus(this->EventCallbackCommand
);
160 //----------------------------------------------------------------------------
161 void egvtkInteractorStyle::OnMiddleButtonUp()
167 if ( this->Interactor
)
169 this->ReleaseFocus();
175 //----------------------------------------------------------------------------
176 void egvtkInteractorStyle::OnRightButtonDown()
178 int X
= this->Interactor
->GetEventPosition()[0];
179 int Y
= this->Interactor
->GetEventPosition()[1];
180 cout
<<"You clicked at ("<<X
<<","<<Y
<<")"<<endl
;
182 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
183 this->Interactor
->GetEventPosition()[1]);
184 if (this->CurrentRenderer
== NULL
)
189 this->GrabFocus(this->EventCallbackCommand
);
193 //----------------------------------------------------------------------------
194 void egvtkInteractorStyle::OnRightButtonUp()
201 if ( this->Interactor
)
203 this->ReleaseFocus();
209 //----------------------------------------------------------------------------
210 void egvtkInteractorStyle::OnMouseWheelForward()
212 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
213 this->Interactor
->GetEventPosition()[1]);
214 if (this->CurrentRenderer
== NULL
)
219 this->GrabFocus(this->EventCallbackCommand
);
221 double factor
= this->MotionFactor
* 0.2 * this->MouseWheelMotionFactor
;
222 this->Dolly(pow(1.1, factor
));
224 this->ReleaseFocus();
227 //----------------------------------------------------------------------------
228 void egvtkInteractorStyle::OnMouseWheelBackward()
230 this->FindPokedRenderer(this->Interactor
->GetEventPosition()[0],
231 this->Interactor
->GetEventPosition()[1]);
232 if (this->CurrentRenderer
== NULL
)
237 this->GrabFocus(this->EventCallbackCommand
);
239 double factor
= this->MotionFactor
* -0.2 * this->MouseWheelMotionFactor
;
240 this->Dolly(pow(1.1, factor
));
242 this->ReleaseFocus();
245 //----------------------------------------------------------------------------
246 void egvtkInteractorStyle::Rotate()
248 if (this->CurrentRenderer
== NULL
)
253 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
255 int dx
= rwi
->GetEventPosition()[0] - rwi
->GetLastEventPosition()[0];
256 int dy
= rwi
->GetEventPosition()[1] - rwi
->GetLastEventPosition()[1];
258 int *size
= this->CurrentRenderer
->GetRenderWindow()->GetSize();
260 double delta_elevation
= -20.0 / size
[1];
261 double delta_azimuth
= -20.0 / size
[0];
263 double rxf
= dx
* delta_azimuth
* this->MotionFactor
;
264 double ryf
= dy
* delta_elevation
* this->MotionFactor
;
266 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
267 camera
->Azimuth(rxf
);
268 camera
->Elevation(ryf
);
269 camera
->OrthogonalizeViewUp();
271 if (this->AutoAdjustCameraClippingRange
)
273 this->CurrentRenderer
->ResetCameraClippingRange();
276 if (rwi
->GetLightFollowCamera())
278 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
284 //----------------------------------------------------------------------------
285 void egvtkInteractorStyle::Spin()
287 if ( this->CurrentRenderer
== NULL
)
292 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
294 double *center
= this->CurrentRenderer
->GetCenter();
296 double newAngle
= vtkMath::DegreesFromRadians( atan2( rwi
->GetEventPosition()[1] - center
[1], rwi
->GetEventPosition()[0] - center
[0] ) );
298 double oldAngle
= vtkMath::DegreesFromRadians( atan2( rwi
->GetLastEventPosition()[1] - center
[1], rwi
->GetLastEventPosition()[0] - center
[0] ) );
300 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
301 camera
->Roll( newAngle
- oldAngle
);
302 camera
->OrthogonalizeViewUp();
307 //----------------------------------------------------------------------------
308 void egvtkInteractorStyle::Pan()
310 if (this->CurrentRenderer
== NULL
)
315 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
317 double viewFocus
[4], focalDepth
, viewPoint
[3];
318 double newPickPoint
[4], oldPickPoint
[4], motionVector
[3];
320 // Calculate the focal depth since we'll be using it a lot
322 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
323 camera
->GetFocalPoint(viewFocus
);
324 this->ComputeWorldToDisplay(viewFocus
[0], viewFocus
[1], viewFocus
[2],
326 focalDepth
= viewFocus
[2];
328 this->ComputeDisplayToWorld(rwi
->GetEventPosition()[0],
329 rwi
->GetEventPosition()[1],
333 // Has to recalc old mouse point since the viewport has moved,
334 // so can't move it outside the loop
336 this->ComputeDisplayToWorld(rwi
->GetLastEventPosition()[0],
337 rwi
->GetLastEventPosition()[1],
341 // Camera motion is reversed
343 motionVector
[0] = oldPickPoint
[0] - newPickPoint
[0];
344 motionVector
[1] = oldPickPoint
[1] - newPickPoint
[1];
345 motionVector
[2] = oldPickPoint
[2] - newPickPoint
[2];
347 camera
->GetFocalPoint(viewFocus
);
348 camera
->GetPosition(viewPoint
);
349 camera
->SetFocalPoint(motionVector
[0] + viewFocus
[0],
350 motionVector
[1] + viewFocus
[1],
351 motionVector
[2] + viewFocus
[2]);
353 camera
->SetPosition(motionVector
[0] + viewPoint
[0],
354 motionVector
[1] + viewPoint
[1],
355 motionVector
[2] + viewPoint
[2]);
357 if (rwi
->GetLightFollowCamera())
359 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
365 //----------------------------------------------------------------------------
366 void egvtkInteractorStyle::Dolly()
368 if (this->CurrentRenderer
== NULL
)
373 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
374 double *center
= this->CurrentRenderer
->GetCenter();
375 int dy
= rwi
->GetEventPosition()[1] - rwi
->GetLastEventPosition()[1];
376 double dyf
= this->MotionFactor
* dy
/ center
[1];
377 this->Dolly(pow(1.1, dyf
));
380 //----------------------------------------------------------------------------
381 void egvtkInteractorStyle::Dolly(double factor
)
383 if (this->CurrentRenderer
== NULL
)
388 vtkCamera
*camera
= this->CurrentRenderer
->GetActiveCamera();
389 if (camera
->GetParallelProjection())
391 camera
->SetParallelScale(camera
->GetParallelScale() / factor
);
395 camera
->Dolly(factor
);
396 if (this->AutoAdjustCameraClippingRange
)
398 this->CurrentRenderer
->ResetCameraClippingRange();
402 if (this->Interactor
->GetLightFollowCamera())
404 this->CurrentRenderer
->UpdateLightsGeometryToFollowCamera();
407 this->Interactor
->Render();
410 //----------------------------------------------------------------------------
411 void egvtkInteractorStyle::PrintSelf(ostream
& os
, vtkIndent indent
)
413 this->Superclass::PrintSelf(os
,indent
);
414 os
<< indent
<< "MotionFactor: " << this->MotionFactor
<< "\n";
417 void egvtkInteractorStyle::OnChar()
419 cout
<<"OnChar "<<this->Interactor
->GetKeyCode()<<endl
;
420 this->EventCallbackCommand
->SetAbortFlag(1);
422 vtkRenderWindowInteractor
*rwi
= this->Interactor
;
423 char key
=rwi
->GetKeyCode();
426 cout
<<"pick node by mouse"<<endl
;
428 /* else if(key=='N') {
429 cout<<"pick node by ID"<<endl;
433 cout
<<"pick cell by mouse"<<endl
;
435 /* else if(key=='C') {
436 cout<<"pick cell by ID"<<endl;
439 /* else if(key=='b') {
440 cout<<"box select"<<endl;
443 /* else if(key=='d') {
444 cout<<"Delete picked point"<<endl;
445 // EG_STDINTERSLOT(DeletePickedPoint);
446 DeletePickedPoint deletepickedpoint;
448 OPER *oper = new OPER(); \
451 if(grid->GetNumberOfPoints()) updateBoundaryCodes(false);
457 // otherwise pass the OnChar to the vtkInteractorStyle.
458 if (this->HasObserver(vtkCommand::CharEvent
)) {
459 this->ShiftKey
= this->Interactor
->GetShiftKey();
460 this->ControlKey
= this->Interactor
->GetControlKey();
461 this->KeyCode
= this->Interactor
->GetKeyCode();
463 this->InvokeEvent(vtkCommand::CharEvent
,NULL
);
466 this->vtkInteractorStyle::OnChar();