trackball.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "EPS.h"
  30. #include "dot.h"
  31. #include "cross.h"
  32. #include "axis_angle_to_quat.h"
  33. #include "quat_mult.h"
  34. #include <cmath>
  35. #include <cstdlib>
  36. #include <algorithm>
  37. // Utility inline functions
  38. template <typename Q_type>
  39. static inline Q_type _QuatD(int w, int h)
  40. {
  41. return (Q_type)(abs(w) < abs(h) ? abs(w) : abs(h)) - 4;
  42. }
  43. template <typename Q_type>
  44. static inline Q_type _QuatIX(int x, int w, int h)
  45. {
  46. return (2.0f*(Q_type)x - (Q_type)w - 1.0f)/_QuatD<Q_type>(w, h);
  47. }
  48. template <typename Q_type>
  49. static inline Q_type _QuatIY(int y, int w, int h)
  50. {
  51. return (-2.0f*(Q_type)y + (Q_type)h - 1.0f)/_QuatD<Q_type>(w, h);
  52. }
  53. // This is largely the trackball as implemented in AntTweakbar. Much of the
  54. // code is straight from its source in TwMgr.cpp
  55. // http://www.antisphere.com/Wiki/tools:anttweakbar
  56. template <typename Q_type>
  57. inline void igl::trackball(
  58. const int w,
  59. const int h,
  60. const Q_type speed_factor,
  61. const Q_type * down_quat,
  62. const int down_mouse_x,
  63. const int down_mouse_y,
  64. const int mouse_x,
  65. const int mouse_y,
  66. Q_type * quat)
  67. {
  68. assert(speed_factor > 0);
  69. double original_x =
  70. _QuatIX<Q_type>(speed_factor*(down_mouse_x-w/2)+w/2, w, h);
  71. double original_y =
  72. _QuatIY<Q_type>(speed_factor*(down_mouse_y-h/2)+h/2, w, h);
  73. double x = _QuatIX<Q_type>(speed_factor*(mouse_x-w/2)+w/2, w, h);
  74. double y = _QuatIY<Q_type>(speed_factor*(mouse_y-h/2)+h/2, w, h);
  75. double z = 1;
  76. double n0 = sqrt(original_x*original_x + original_y*original_y + z*z);
  77. double n1 = sqrt(x*x + y*y + z*z);
  78. if(n0>igl::DOUBLE_EPS && n1>igl::DOUBLE_EPS)
  79. {
  80. double v0[] = { original_x/n0, original_y/n0, z/n0 };
  81. double v1[] = { x/n1, y/n1, z/n1 };
  82. double axis[3];
  83. cross(v0,v1,axis);
  84. double sa = sqrt(dot(axis, axis));
  85. double ca = dot(v0, v1);
  86. double angle = atan2(sa, ca);
  87. if( x*x+y*y>1.0 )
  88. {
  89. angle *= 1.0 + 0.2f*(sqrt(x*x+y*y)-1.0);
  90. }
  91. double qrot[4], qres[4], qorig[4];
  92. axis_angle_to_quat(axis,angle,qrot);
  93. double nqorig =
  94. sqrt(down_quat[0]*down_quat[0]+
  95. down_quat[1]*down_quat[1]+
  96. down_quat[2]*down_quat[2]+
  97. down_quat[3]*down_quat[3]);
  98. if( fabs(nqorig)>igl::DOUBLE_EPS_SQ )
  99. {
  100. qorig[0] = down_quat[0]/nqorig;
  101. qorig[1] = down_quat[1]/nqorig;
  102. qorig[2] = down_quat[2]/nqorig;
  103. qorig[3] = down_quat[3]/nqorig;
  104. igl::quat_mult<double>(qrot,qorig,qres);
  105. quat[0] = qres[0];
  106. quat[1] = qres[1];
  107. quat[2] = qres[2];
  108. quat[3] = qres[3];
  109. }
  110. else
  111. {
  112. quat[0] = qrot[0];
  113. quat[1] = qrot[1];
  114. quat[2] = qrot[2];
  115. quat[3] = qrot[3];
  116. }
  117. }
  118. }