limited volume meshing to boundary layer only
[engrid-github.git] / src / libengrid / egvtkinteractorstyle.cpp
blob2420bed3b83b1450ba6280b871c98438a96ea826
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 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)
92 return;
95 this->GrabFocus(this->EventCallbackCommand);
96 if (this->Interactor->GetShiftKey())
98 if (this->Interactor->GetControlKey())
100 this->StartDolly();
102 else
104 this->StartPan();
107 else
109 if (this->Interactor->GetControlKey())
111 this->StartSpin();
113 else
115 this->StartRotate();
120 //----------------------------------------------------------------------------
121 void egvtkInteractorStyle::OnLeftButtonUp()
123 switch (this->State)
125 case VTKIS_DOLLY:
126 this->EndDolly();
127 break;
129 case VTKIS_PAN:
130 this->EndPan();
131 break;
133 case VTKIS_SPIN:
134 this->EndSpin();
135 break;
137 case VTKIS_ROTATE:
138 this->EndRotate();
139 break;
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)
155 return;
158 this->GrabFocus(this->EventCallbackCommand);
159 this->StartPan();
162 //----------------------------------------------------------------------------
163 void egvtkInteractorStyle::OnMiddleButtonUp()
165 switch (this->State)
167 case VTKIS_PAN:
168 this->EndPan();
169 if ( this->Interactor )
171 this->ReleaseFocus();
173 break;
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)
188 return;
191 this->GrabFocus(this->EventCallbackCommand);
192 this->StartDolly();
195 //----------------------------------------------------------------------------
196 void egvtkInteractorStyle::OnRightButtonUp()
198 switch (this->State)
200 case VTKIS_DOLLY:
201 this->EndDolly();
203 if ( this->Interactor )
205 this->ReleaseFocus();
207 break;
211 //----------------------------------------------------------------------------
212 void egvtkInteractorStyle::OnMouseWheelForward()
214 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
215 this->Interactor->GetEventPosition()[1]);
216 if (this->CurrentRenderer == NULL)
218 return;
221 this->GrabFocus(this->EventCallbackCommand);
222 this->StartDolly();
223 double factor = this->MotionFactor * 0.2 * this->MouseWheelMotionFactor;
224 this->Dolly(pow(1.1, factor));
225 this->EndDolly();
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)
236 return;
239 this->GrabFocus(this->EventCallbackCommand);
240 this->StartDolly();
241 double factor = this->MotionFactor * -0.2 * this->MouseWheelMotionFactor;
242 this->Dolly(pow(1.1, factor));
243 this->EndDolly();
244 this->ReleaseFocus();
247 //----------------------------------------------------------------------------
248 void egvtkInteractorStyle::Rotate()
250 if (this->CurrentRenderer == NULL)
252 return;
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();
283 rwi->Render();
286 //----------------------------------------------------------------------------
287 void egvtkInteractorStyle::Spin()
289 if ( this->CurrentRenderer == NULL )
291 return;
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();
306 rwi->Render();
309 //----------------------------------------------------------------------------
310 void egvtkInteractorStyle::Pan()
312 if (this->CurrentRenderer == NULL)
314 return;
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],
327 viewFocus);
328 focalDepth = viewFocus[2];
330 this->ComputeDisplayToWorld(rwi->GetEventPosition()[0],
331 rwi->GetEventPosition()[1],
332 focalDepth,
333 newPickPoint);
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],
340 focalDepth,
341 oldPickPoint);
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();
364 rwi->Render();
367 //----------------------------------------------------------------------------
368 void egvtkInteractorStyle::Dolly()
370 if (this->CurrentRenderer == NULL)
372 return;
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)
387 return;
390 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
391 if (camera->GetParallelProjection())
393 camera->SetParallelScale(camera->GetParallelScale() / factor);
395 else
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();
427 if(key=='n') {
428 cout<<"pick node by mouse"<<endl;
430 /* else if(key=='N') {
431 cout<<"pick node by ID"<<endl;
434 else if(key=='c') {
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;
449 deletepickedpoint();
450 OPER *oper = new OPER(); \
451 (*oper)(); \
452 oper->del(); \
453 if(grid->GetNumberOfPoints()) updateBoundaryCodes(false);
454 updateActors();
458 else {
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);
467 else {
468 this->vtkInteractorStyle::OnChar();