added sphere tutorial
[engrid.git] / src / egvtkinteractorstyle.cpp
blob21dcd31a9425542adad71d648660c39cb1be48bc
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2010 enGits GmbH +
7 // + +
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. +
12 // + +
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. +
17 // + +
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/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "egvtkinteractorstyle.h"
25 #include "deletepickedpoint.h"
26 #include "egvtkobject.h"
27 #include "operation.h"
29 #include "vtkInteractorStyleUser.h"
30 #include "vtkMath.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];
55 switch (this->State)
57 case VTKIS_ROTATE:
58 this->FindPokedRenderer(x, y);
59 this->Rotate();
60 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
61 break;
63 case VTKIS_PAN:
64 this->FindPokedRenderer(x, y);
65 this->Pan();
66 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
67 break;
69 case VTKIS_DOLLY:
70 this->FindPokedRenderer(x, y);
71 this->Dolly();
72 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
73 break;
75 case VTKIS_SPIN:
76 this->FindPokedRenderer(x, y);
77 this->Spin();
78 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
79 break;
83 //----------------------------------------------------------------------------
84 void egvtkInteractorStyle::OnLeftButtonDown()
86 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
87 this->Interactor->GetEventPosition()[1]);
88 if (this->CurrentRenderer == NULL)
90 return;
93 this->GrabFocus(this->EventCallbackCommand);
94 if (this->Interactor->GetShiftKey())
96 if (this->Interactor->GetControlKey())
98 this->StartDolly();
100 else
102 this->StartPan();
105 else
107 if (this->Interactor->GetControlKey())
109 this->StartSpin();
111 else
113 this->StartRotate();
118 //----------------------------------------------------------------------------
119 void egvtkInteractorStyle::OnLeftButtonUp()
121 switch (this->State)
123 case VTKIS_DOLLY:
124 this->EndDolly();
125 break;
127 case VTKIS_PAN:
128 this->EndPan();
129 break;
131 case VTKIS_SPIN:
132 this->EndSpin();
133 break;
135 case VTKIS_ROTATE:
136 this->EndRotate();
137 break;
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)
153 return;
156 this->GrabFocus(this->EventCallbackCommand);
157 this->StartPan();
160 //----------------------------------------------------------------------------
161 void egvtkInteractorStyle::OnMiddleButtonUp()
163 switch (this->State)
165 case VTKIS_PAN:
166 this->EndPan();
167 if ( this->Interactor )
169 this->ReleaseFocus();
171 break;
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)
186 return;
189 this->GrabFocus(this->EventCallbackCommand);
190 this->StartDolly();
193 //----------------------------------------------------------------------------
194 void egvtkInteractorStyle::OnRightButtonUp()
196 switch (this->State)
198 case VTKIS_DOLLY:
199 this->EndDolly();
201 if ( this->Interactor )
203 this->ReleaseFocus();
205 break;
209 //----------------------------------------------------------------------------
210 void egvtkInteractorStyle::OnMouseWheelForward()
212 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
213 this->Interactor->GetEventPosition()[1]);
214 if (this->CurrentRenderer == NULL)
216 return;
219 this->GrabFocus(this->EventCallbackCommand);
220 this->StartDolly();
221 double factor = this->MotionFactor * 0.2 * this->MouseWheelMotionFactor;
222 this->Dolly(pow(1.1, factor));
223 this->EndDolly();
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)
234 return;
237 this->GrabFocus(this->EventCallbackCommand);
238 this->StartDolly();
239 double factor = this->MotionFactor * -0.2 * this->MouseWheelMotionFactor;
240 this->Dolly(pow(1.1, factor));
241 this->EndDolly();
242 this->ReleaseFocus();
245 //----------------------------------------------------------------------------
246 void egvtkInteractorStyle::Rotate()
248 if (this->CurrentRenderer == NULL)
250 return;
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();
281 rwi->Render();
284 //----------------------------------------------------------------------------
285 void egvtkInteractorStyle::Spin()
287 if ( this->CurrentRenderer == NULL )
289 return;
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();
304 rwi->Render();
307 //----------------------------------------------------------------------------
308 void egvtkInteractorStyle::Pan()
310 if (this->CurrentRenderer == NULL)
312 return;
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],
325 viewFocus);
326 focalDepth = viewFocus[2];
328 this->ComputeDisplayToWorld(rwi->GetEventPosition()[0],
329 rwi->GetEventPosition()[1],
330 focalDepth,
331 newPickPoint);
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],
338 focalDepth,
339 oldPickPoint);
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();
362 rwi->Render();
365 //----------------------------------------------------------------------------
366 void egvtkInteractorStyle::Dolly()
368 if (this->CurrentRenderer == NULL)
370 return;
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)
385 return;
388 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
389 if (camera->GetParallelProjection())
391 camera->SetParallelScale(camera->GetParallelScale() / factor);
393 else
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();
425 if(key=='n') {
426 cout<<"pick node by mouse"<<endl;
428 /* else if(key=='N') {
429 cout<<"pick node by ID"<<endl;
432 else if(key=='c') {
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;
447 deletepickedpoint();
448 OPER *oper = new OPER(); \
449 (*oper)(); \
450 oper->del(); \
451 if(grid->GetNumberOfPoints()) updateBoundaryCodes(false);
452 updateActors();
456 else {
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);
465 else {
466 this->vtkInteractorStyle::OnChar();