AnimatLab  2
Test
OsgTrackballManipulator.cpp
1 #include "StdAfx.h"
2 
3 #include <osgGA/TrackballManipulator>
4 #include <osg/Quat>
5 #include <osg/Notify>
6 #include <osg/BoundsChecking>
7 
8 #include "OsgTrackballManipulator.h"
9 
10 using namespace osg;
11 using namespace osgGA;
12 
13 namespace OsgAnimatSim
14 {
15  namespace Visualization
16  {
17 
18 OsgTrackballManipulator::OsgTrackballManipulator()
19 {
20  _modelScale = 0.01f;
21  _minimumZoomScale = 0.05f;
22  _thrown = false;
23 
24  _distance = 1.0f;
25  _trackballSize = 0.8f;
26 }
27 
28 
29 OsgTrackballManipulator::~OsgTrackballManipulator()
30 {
31 }
32 
33 
34 void OsgTrackballManipulator::setNode(osg::Node* node)
35 {
36  _node = node;
37  if (_node.get())
38  {
39  const osg::BoundingSphere& boundingSphere=_node->getBound();
40  _modelScale = boundingSphere._radius;
41  }
42  if (getAutoComputeHomePosition()) computeHomePosition();
43 }
44 
45 
46 const osg::Node* OsgTrackballManipulator::getNode() const
47 {
48  return _node.get();
49 }
50 
51 
52 osg::Node* OsgTrackballManipulator::getNode()
53 {
54  return _node.get();
55 }
56 
57 
58 void OsgTrackballManipulator::home(double /*currentTime*/)
59 {
60  //osg::Vec3d vOrigEye = _homeEye;
61  //osg::Vec3d vOrigCenter = _homeCenter;
62  //osg::Vec3d vOrigUp = _homeUp;
63 
64  if (getAutoComputeHomePosition()) computeHomePosition();
65  computePosition(_homeEye, _homeCenter, _homeEye);
66  _thrown = false;
67 }
68 
69 void OsgTrackballManipulator::home(const GUIEventAdapter& ea ,GUIActionAdapter& us)
70 {
71  home(ea.getTime());
72  us.requestRedraw();
73  us.requestContinuousUpdate(false);
74 }
75 
76 
77 void OsgTrackballManipulator::init(const GUIEventAdapter& ,GUIActionAdapter& )
78 {
79  flushMouseEventStack();
80 }
81 
82 
83 void OsgTrackballManipulator::getUsage(osg::ApplicationUsage& usage) const
84 {
85  usage.addKeyboardMouseBinding("Trackball: Space","Reset the viewing position to home");
86  usage.addKeyboardMouseBinding("Trackball: +","When in stereo, increase the fusion distance");
87  usage.addKeyboardMouseBinding("Trackball: -","When in stereo, reduce the fusion distance");
88 }
89 
90 bool OsgTrackballManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
91 {
92  switch(ea.getEventType())
93  {
94  case(GUIEventAdapter::FRAME):
95  if (_thrown)
96  {
97  if (calcMovement()) us.requestRedraw();
98  }
99  return false;
100  default:
101  break;
102  }
103 
104  if (ea.getHandled()) return false;
105 
106  switch(ea.getEventType())
107  {
108  case(GUIEventAdapter::PUSH):
109  {
110  flushMouseEventStack();
111  addMouseEvent(ea);
112  if (calcMovement()) us.requestRedraw();
113  us.requestContinuousUpdate(false);
114  _thrown = false;
115  return true;
116  }
117 
118  case(GUIEventAdapter::RELEASE):
119  {
120  if (ea.getButtonMask()==0)
121  {
122 
123  double timeSinceLastRecordEvent = _ga_t0.valid() ? (ea.getTime() - _ga_t0->getTime()) : DBL_MAX;
124  if (timeSinceLastRecordEvent>0.02) flushMouseEventStack();
125 
126  if (isMouseMoving())
127  {
128  if (calcMovement())
129  {
130  us.requestRedraw();
131  us.requestContinuousUpdate(true);
132  _thrown = true;
133  }
134  }
135  else
136  {
137  flushMouseEventStack();
138  addMouseEvent(ea);
139  if (calcMovement()) us.requestRedraw();
140  us.requestContinuousUpdate(false);
141  _thrown = false;
142  }
143 
144  }
145  else
146  {
147  flushMouseEventStack();
148  addMouseEvent(ea);
149  if (calcMovement()) us.requestRedraw();
150  us.requestContinuousUpdate(false);
151  _thrown = false;
152  }
153  return true;
154  }
155 
156  case(GUIEventAdapter::DRAG):
157  {
158  addMouseEvent(ea);
159  if (calcMovement()) us.requestRedraw();
160  us.requestContinuousUpdate(false);
161  _thrown = false;
162  return true;
163  }
164 
165  case(GUIEventAdapter::MOVE):
166  {
167  return false;
168  }
169 
170  case(GUIEventAdapter::KEYDOWN):
171  if (ea.getKey()== GUIEventAdapter::KEY_Space)
172  {
173  flushMouseEventStack();
174  _thrown = false;
175  home(ea,us);
176  return true;
177  }
178  return false;
179  case(GUIEventAdapter::FRAME):
180  if (_thrown)
181  {
182  if (calcMovement()) us.requestRedraw();
183  }
184  return false;
185  default:
186  return false;
187  }
188 }
189 
190 
191 bool OsgTrackballManipulator::isMouseMoving()
192 {
193  if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
194 
195  static const float velocity = 0.1f;
196 
197  float dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized();
198  float dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized();
199  float len = sqrtf(dx*dx+dy*dy);
200  float dt = _ga_t0->getTime()-_ga_t1->getTime();
201 
202  return (len>dt*velocity);
203 }
204 
205 
206 void OsgTrackballManipulator::flushMouseEventStack()
207 {
208  _ga_t1 = NULL;
209  _ga_t0 = NULL;
210 }
211 
212 
213 void OsgTrackballManipulator::addMouseEvent(const GUIEventAdapter& ea)
214 {
215  _ga_t1 = _ga_t0;
216  _ga_t0 = &ea;
217 }
218 
219 void OsgTrackballManipulator::setByMatrix(const osg::Matrixd& matrix)
220 {
221  _center = osg::Vec3(0.0f,0.0f,-_distance)*matrix;
222  _rotation = matrix.getRotate();
223 }
224 
225 osg::Matrixd OsgTrackballManipulator::getMatrix() const
226 {
227  return osg::Matrixd::translate(0.0,0.0,_distance)*osg::Matrixd::rotate(_rotation)*osg::Matrixd::translate(_center);
228 }
229 
230 osg::Matrixd OsgTrackballManipulator::getInverseMatrix() const
231 {
232  return osg::Matrixd::translate(-_center)*osg::Matrixd::rotate(_rotation.inverse())*osg::Matrixd::translate(0.0,0.0,-_distance);
233 }
234 
235 void OsgTrackballManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up)
236 {
237 
238  osg::Vec3 lv(center-eye);
239 
240  osg::Vec3 f(lv);
241  f.normalize();
242  osg::Vec3 s(f^up);
243  s.normalize();
244  osg::Vec3 u(s^f);
245  u.normalize();
246 
247  osg::Matrix rotation_matrix(s[0], u[0], -f[0], 0.0f,
248  s[1], u[1], -f[1], 0.0f,
249  s[2], u[2], -f[2], 0.0f,
250  0.0f, 0.0f, 0.0f, 1.0f);
251 
252  _center = center;
253  _distance = lv.length();
254  _rotation = rotation_matrix.getRotate().inverse();
255 }
256 
257 
258 bool OsgTrackballManipulator::calcMovement()
259 {
260  // return if less then two events have been added.
261  if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
262 
263  float dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized();
264  float dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized();
265 
266  float distance = sqrtf(dx*dx + dy*dy);
267  // return if movement is too fast, indicating an error in event values or change in screen.
268  if (distance>0.5)
269  {
270  return false;
271  }
272 
273  // return if there is no movement.
274  if (distance==0.0f)
275  {
276  return false;
277  }
278 
279  unsigned int buttonMask = _ga_t1->getButtonMask();
280  if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON)
281  {
282 
283  // rotate camera.
284 
285  osg::Vec3 axis;
286  float angle;
287 
288  float px0 = _ga_t0->getXnormalized();
289  float py0 = _ga_t0->getYnormalized();
290 
291  float px1 = _ga_t1->getXnormalized();
292  float py1 = _ga_t1->getYnormalized();
293 
294 
295  trackball(axis,angle,px1,py1,px0,py0);
296 
297  osg::Quat new_rotate;
298  new_rotate.makeRotate(angle,axis);
299 
300  _rotation = _rotation*new_rotate;
301 
302  return true;
303 
304  }
305  else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON ||
306  buttonMask==(GUIEventAdapter::LEFT_MOUSE_BUTTON|GUIEventAdapter::RIGHT_MOUSE_BUTTON))
307  {
308 
309  // pan model.
310 
311  float scale = -0.3f*_distance;
312 
313  osg::Matrix rotation_matrix;
314  rotation_matrix.makeRotate(_rotation);
315 
316  osg::Vec3 dv(dx*scale,dy*scale,0.0f);
317 
318  _center += dv*rotation_matrix;
319 
320  return true;
321 
322  }
323  else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON)
324  {
325 
326  // zoom model.
327 
328  float fd = _distance;
329  float scale = 1.0f+dy;
330  if (fd*scale>_modelScale*_minimumZoomScale)
331  {
332 
333  _distance *= scale;
334 
335  }
336  else
337  {
338 
339  // notify(DEBUG_INFO) << "Pushing forward"<<std::endl;
340  // push the camera forward.
341  float scale = -fd;
342 
343  osg::Matrix rotation_matrix(_rotation);
344 
345  osg::Vec3 dv = (osg::Vec3(0.0f,0.0f,-1.0f)*rotation_matrix)*(dy*scale);
346 
347  _center += dv;
348 
349  //char strDest[250];
350  //sprintf(strDest, "center: (%3.3f, %3.3f, %3.3f), dv: (%3.3f, %3.3f, %3.3f)\n", _center[0], _center[1], _center[2], dv[0], dv[1], dv[2]);
351  //OutputDebugString(strDest);
352 
353  }
354 
355  return true;
356 
357  }
358 
359  return false;
360 }
361 
362 
363 /*
364  * This size should really be based on the distance from the center of
365  * rotation to the point on the object underneath the mouse. That
366  * point would then track the mouse as closely as possible. This is a
367  * simple example, though, so that is left as an Exercise for the
368  * Programmer.
369  */
370 void OsgTrackballManipulator::setTrackballSize(float size)
371 {
372  _trackballSize = size;
373  osg::clampBetweenRange(_trackballSize,0.1f,1.0f,"OsgTrackballManipulator::setTrackballSize(float)");
374 }
375 
376 /*
377  * Ok, simulate a track-ball. Project the points onto the virtual
378  * trackball, then figure out the axis of rotation, which is the cross
379  * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
380  * Note: This is a deformed trackball-- is a trackball in the center,
381  * but is deformed into a hyperbolic sheet of rotation away from the
382  * center. This particular function was chosen after trying out
383  * several variations.
384  *
385  * It is assumed that the arguments to this routine are in the range
386  * (-1.0 ... 1.0)
387  */
388 void OsgTrackballManipulator::trackball(osg::Vec3& axis,float& angle, float p1x, float p1y, float p2x, float p2y)
389 {
390  /*
391  * First, figure out z-coordinates for projection of P1 and P2 to
392  * deformed sphere
393  */
394 
395  osg::Matrix rotation_matrix(_rotation);
396 
397 
398  osg::Vec3 uv = osg::Vec3(0.0f,1.0f,0.0f)*rotation_matrix;
399  osg::Vec3 sv = osg::Vec3(1.0f,0.0f,0.0f)*rotation_matrix;
400  osg::Vec3 lv = osg::Vec3(0.0f,0.0f,-1.0f)*rotation_matrix;
401 
402  osg::Vec3 p1 = sv * p1x + uv * p1y - lv * tb_project_to_sphere(_trackballSize, p1x, p1y);
403  osg::Vec3 p2 = sv * p2x + uv * p2y - lv * tb_project_to_sphere(_trackballSize, p2x, p2y);
404 
405  /*
406  * Now, we want the cross product of P1 and P2
407  */
408 
409 // Robert,
410 //
411 // This was the quick 'n' dirty fix to get the trackball doing the right
412 // thing after fixing the Quat rotations to be right-handed. You may want
413 // to do something more elegant.
414 // axis = p1^p2;
415 axis = p2^p1;
416  axis.normalize();
417 
418  /*
419  * Figure out how much to rotate around that axis.
420  */
421  float t = (p2 - p1).length() / (2.0 * _trackballSize);
422 
423  /*
424  * Avoid problems with out-of-control values...
425  */
426  if (t > 1.0) t = 1.0;
427  if (t < -1.0) t = -1.0;
428  angle = inRadians(asin(t));
429 
430 }
431 
432 
433 /*
434  * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
435  * if we are away from the center of the sphere.
436  */
437 float OsgTrackballManipulator::tb_project_to_sphere(float r, float x, float y)
438 {
439  float d, t, z;
440 
441  d = sqrt(x*x + y*y);
442  /* Inside sphere */
443  if (d < r * 0.70710678118654752440)
444  {
445  z = sqrt(r*r - d*d);
446  } /* On hyperbola */
447  else
448  {
449  t = r / 1.41421356237309504880;
450  z = t*t / d;
451  }
452  return z;
453 }
454 
455  } // Visualization
456 }
Classes for implementing the cm-labs vortex physics engine for AnimatLab.