fixed edge display for volume cells
[engrid-github.git] / src / libengrid / egvtkinteractorstyle.cpp
blob142c4a10160698f6d9b7ed68a8044472cbb7e737
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
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. +
11 // + +
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. +
16 // + +
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/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "egvtkinteractorstyle.h"
23 #include "deletepickedpoint.h"
24 #include "egvtkobject.h"
25 #include "operation.h"
27 #include "vtkInteractorStyleUser.h"
28 #include "vtkMath.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];
53 switch (this->State)
55 case VTKIS_ROTATE:
56 this->FindPokedRenderer(x, y);
57 this->Rotate();
58 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
59 break;
61 case VTKIS_PAN:
62 this->FindPokedRenderer(x, y);
63 this->Pan();
64 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
65 break;
67 case VTKIS_DOLLY:
68 this->FindPokedRenderer(x, y);
69 this->Dolly();
70 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
71 break;
73 case VTKIS_SPIN:
74 this->FindPokedRenderer(x, y);
75 this->Spin();
76 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
77 break;
81 //----------------------------------------------------------------------------
82 void egvtkInteractorStyle::OnLeftButtonDown()
84 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
85 this->Interactor->GetEventPosition()[1]);
86 if (this->CurrentRenderer == NULL)
88 return;
91 this->GrabFocus(this->EventCallbackCommand);
92 if (this->Interactor->GetShiftKey())
94 if (this->Interactor->GetControlKey())
96 this->StartDolly();
98 else
100 this->StartPan();
103 else
105 if (this->Interactor->GetControlKey())
107 this->StartSpin();
109 else
111 this->StartRotate();
116 //----------------------------------------------------------------------------
117 void egvtkInteractorStyle::OnLeftButtonUp()
119 switch (this->State)
121 case VTKIS_DOLLY:
122 this->EndDolly();
123 break;
125 case VTKIS_PAN:
126 this->EndPan();
127 break;
129 case VTKIS_SPIN:
130 this->EndSpin();
131 break;
133 case VTKIS_ROTATE:
134 this->EndRotate();
135 break;
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)
151 return;
154 this->GrabFocus(this->EventCallbackCommand);
155 this->StartPan();
158 //----------------------------------------------------------------------------
159 void egvtkInteractorStyle::OnMiddleButtonUp()
161 switch (this->State)
163 case VTKIS_PAN:
164 this->EndPan();
165 if ( this->Interactor )
167 this->ReleaseFocus();
169 break;
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)
184 return;
187 this->GrabFocus(this->EventCallbackCommand);
188 this->StartDolly();
191 //----------------------------------------------------------------------------
192 void egvtkInteractorStyle::OnRightButtonUp()
194 switch (this->State)
196 case VTKIS_DOLLY:
197 this->EndDolly();
199 if ( this->Interactor )
201 this->ReleaseFocus();
203 break;
207 //----------------------------------------------------------------------------
208 void egvtkInteractorStyle::OnMouseWheelForward()
210 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
211 this->Interactor->GetEventPosition()[1]);
212 if (this->CurrentRenderer == NULL)
214 return;
217 this->GrabFocus(this->EventCallbackCommand);
218 this->StartDolly();
219 double factor = this->MotionFactor * 0.2 * this->MouseWheelMotionFactor;
220 this->Dolly(pow(1.1, factor));
221 this->EndDolly();
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)
232 return;
235 this->GrabFocus(this->EventCallbackCommand);
236 this->StartDolly();
237 double factor = this->MotionFactor * -0.2 * this->MouseWheelMotionFactor;
238 this->Dolly(pow(1.1, factor));
239 this->EndDolly();
240 this->ReleaseFocus();
243 //----------------------------------------------------------------------------
244 void egvtkInteractorStyle::Rotate()
246 if (this->CurrentRenderer == NULL)
248 return;
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();
279 rwi->Render();
282 //----------------------------------------------------------------------------
283 void egvtkInteractorStyle::Spin()
285 if ( this->CurrentRenderer == NULL )
287 return;
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();
302 rwi->Render();
305 //----------------------------------------------------------------------------
306 void egvtkInteractorStyle::Pan()
308 if (this->CurrentRenderer == NULL)
310 return;
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],
323 viewFocus);
324 focalDepth = viewFocus[2];
326 this->ComputeDisplayToWorld(rwi->GetEventPosition()[0],
327 rwi->GetEventPosition()[1],
328 focalDepth,
329 newPickPoint);
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],
336 focalDepth,
337 oldPickPoint);
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();
360 rwi->Render();
363 //----------------------------------------------------------------------------
364 void egvtkInteractorStyle::Dolly()
366 if (this->CurrentRenderer == NULL)
368 return;
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)
383 return;
386 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
387 if (camera->GetParallelProjection())
389 camera->SetParallelScale(camera->GetParallelScale() / factor);
391 else
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();
423 if(key=='n') {
424 cout<<"pick node by mouse"<<endl;
426 /* else if(key=='N') {
427 cout<<"pick node by ID"<<endl;
430 else if(key=='c') {
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;
445 deletepickedpoint();
446 OPER *oper = new OPER(); \
447 (*oper)(); \
448 oper->del(); \
449 if(grid->GetNumberOfPoints()) updateBoundaryCodes(false);
450 updateActors();
454 else {
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);
463 else {
464 this->vtkInteractorStyle::OnChar();