trackball.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. namespace igl
  2. {
  3. // Applies a trackball drag to a given rotation
  4. // Inputs:
  5. // w width of the trackball context
  6. // h height of the trackball context
  7. // speed_factor controls how fast the trackball feels, 1 is normal
  8. // down_quat rotation at mouse down, i.e. the rotation we're applying the
  9. // trackball motion to (as quaternion)
  10. // down_mouse_x x position of mouse down
  11. // down_mouse_y y position of mouse down
  12. // mouse_x current x position of mouse
  13. // mouse_y current y position of mouse
  14. // Outputs:
  15. // quat the resulting rotation (as quaternion)
  16. template <typename Q_type>
  17. inline void trackball(
  18. const int w,
  19. const int h,
  20. const Q_type speed_factor,
  21. const Q_type * down_quat,
  22. const int down_mouse_x,
  23. const int down_mouse_y,
  24. const int mouse_x,
  25. const int mouse_y,
  26. Q_type * quat);
  27. }
  28. // Implementation
  29. #include <dot.h>
  30. #include <cross.h>
  31. #include <axis_angle_to_quat.h>
  32. #include <quat_mult.h>
  33. #include <cmath>
  34. #include <cstdlib>
  35. #include <algorithm>
  36. // Utility inline functions
  37. template <typename Q_type>
  38. static inline Q_type _QuatD(int w, int h)
  39. {
  40. return (Q_type)std::min(abs(w), abs(h)) - 4;
  41. }
  42. template <typename Q_type>
  43. static inline Q_type _QuatIX(int x, int w, int h)
  44. {
  45. return (2.0f*(Q_type)x - (Q_type)w - 1.0f)/_QuatD<Q_type>(w, h);
  46. }
  47. template <typename Q_type>
  48. static inline Q_type _QuatIY(int y, int w, int h)
  49. {
  50. return (-2.0f*(Q_type)y + (Q_type)h - 1.0f)/_QuatD<Q_type>(w, h);
  51. }
  52. // This is largely the trackball as implemented in AntTweakbar. Much of the
  53. // code is straight from its source in TwMgr.cpp
  54. // http://www.antisphere.com/Wiki/tools:anttweakbar
  55. template <typename Q_type>
  56. inline void igl::trackball(
  57. const int w,
  58. const int h,
  59. const Q_type speed_factor,
  60. const Q_type * down_quat,
  61. const int down_mouse_x,
  62. const int down_mouse_y,
  63. const int mouse_x,
  64. const int mouse_y,
  65. Q_type * quat)
  66. {
  67. assert(speed_factor > 0);
  68. double original_x =
  69. _QuatIX<Q_type>(speed_factor*(down_mouse_x-w/2)+w/2, w, h);
  70. double original_y =
  71. _QuatIY<Q_type>(speed_factor*(down_mouse_y-h/2)+h/2, w, h);
  72. double x = _QuatIX<Q_type>(speed_factor*(mouse_x-w/2)+w/2, w, h);
  73. double y = _QuatIY<Q_type>(speed_factor*(mouse_y-h/2)+h/2, w, h);
  74. double z = 1;
  75. double n0 = sqrt(original_x*original_x + original_y*original_y + z*z);
  76. double n1 = sqrt(x*x + y*y + z*z);
  77. if(n0>DOUBLE_EPS && n1>DOUBLE_EPS)
  78. {
  79. double v0[] = { original_x/n0, original_y/n0, z/n0 };
  80. double v1[] = { x/n1, y/n1, z/n1 };
  81. double axis[3];
  82. cross(v0,v1,axis);
  83. double sa = sqrt(dot(axis, axis));
  84. double ca = dot(v0, v1);
  85. double angle = atan2(sa, ca);
  86. if( x*x+y*y>1.0 )
  87. {
  88. angle *= 1.0 + 0.2f*(sqrt(x*x+y*y)-1.0);
  89. }
  90. double qrot[4], qres[4], qorig[4];
  91. axis_angle_to_quat(axis,angle,qrot);
  92. double nqorig =
  93. sqrt(down_quat[0]*down_quat[0]+
  94. down_quat[1]*down_quat[1]+
  95. down_quat[2]*down_quat[2]+
  96. down_quat[3]*down_quat[3]);
  97. if( fabs(nqorig)>DOUBLE_EPS_SQ )
  98. {
  99. qorig[0] = down_quat[0]/nqorig;
  100. qorig[1] = down_quat[1]/nqorig;
  101. qorig[2] = down_quat[2]/nqorig;
  102. qorig[3] = down_quat[3]/nqorig;
  103. igl::quat_mult<double>(qrot,qorig,qres);
  104. quat[0] = qres[0];
  105. quat[1] = qres[1];
  106. quat[2] = qres[2];
  107. quat[3] = qres[3];
  108. }
  109. else
  110. {
  111. quat[0] = qrot[0];
  112. quat[1] = qrot[1];
  113. quat[2] = qrot[2];
  114. quat[3] = qrot[3];
  115. }
  116. }
  117. }