trackball.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "trackball.h"
  2. #include "EPS.h"
  3. #include "dot.h"
  4. #include "cross.h"
  5. #include "axis_angle_to_quat.h"
  6. #include "quat_mult.h"
  7. #include <cmath>
  8. #include <cstdlib>
  9. #include <algorithm>
  10. // Utility IGL_INLINE functions
  11. template <typename Q_type>
  12. static IGL_INLINE Q_type _QuatD(int w, int h)
  13. {
  14. return (Q_type)(abs(w) < abs(h) ? abs(w) : abs(h)) - 4;
  15. }
  16. template <typename Q_type>
  17. static IGL_INLINE Q_type _QuatIX(int x, int w, int h)
  18. {
  19. return (2.0f*(Q_type)x - (Q_type)w - 1.0f)/_QuatD<Q_type>(w, h);
  20. }
  21. template <typename Q_type>
  22. static IGL_INLINE Q_type _QuatIY(int y, int w, int h)
  23. {
  24. return (-2.0f*(Q_type)y + (Q_type)h - 1.0f)/_QuatD<Q_type>(w, h);
  25. }
  26. // This is largely the trackball as implemented in AntTweakbar. Much of the
  27. // code is straight from its source in TwMgr.cpp
  28. // http://www.antisphere.com/Wiki/tools:anttweakbar
  29. template <typename Q_type>
  30. IGL_INLINE void igl::trackball(
  31. const int w,
  32. const int h,
  33. const Q_type speed_factor,
  34. const Q_type * down_quat,
  35. const int down_mouse_x,
  36. const int down_mouse_y,
  37. const int mouse_x,
  38. const int mouse_y,
  39. Q_type * quat)
  40. {
  41. assert(speed_factor > 0);
  42. double original_x =
  43. _QuatIX<Q_type>(speed_factor*(down_mouse_x-w/2)+w/2, w, h);
  44. double original_y =
  45. _QuatIY<Q_type>(speed_factor*(down_mouse_y-h/2)+h/2, w, h);
  46. double x = _QuatIX<Q_type>(speed_factor*(mouse_x-w/2)+w/2, w, h);
  47. double y = _QuatIY<Q_type>(speed_factor*(mouse_y-h/2)+h/2, w, h);
  48. double z = 1;
  49. double n0 = sqrt(original_x*original_x + original_y*original_y + z*z);
  50. double n1 = sqrt(x*x + y*y + z*z);
  51. if(n0>igl::DOUBLE_EPS && n1>igl::DOUBLE_EPS)
  52. {
  53. double v0[] = { original_x/n0, original_y/n0, z/n0 };
  54. double v1[] = { x/n1, y/n1, z/n1 };
  55. double axis[3];
  56. cross(v0,v1,axis);
  57. double sa = sqrt(dot(axis, axis));
  58. double ca = dot(v0, v1);
  59. double angle = atan2(sa, ca);
  60. if( x*x+y*y>1.0 )
  61. {
  62. angle *= 1.0 + 0.2f*(sqrt(x*x+y*y)-1.0);
  63. }
  64. double qrot[4], qres[4], qorig[4];
  65. axis_angle_to_quat(axis,angle,qrot);
  66. double nqorig =
  67. sqrt(down_quat[0]*down_quat[0]+
  68. down_quat[1]*down_quat[1]+
  69. down_quat[2]*down_quat[2]+
  70. down_quat[3]*down_quat[3]);
  71. if( fabs(nqorig)>igl::DOUBLE_EPS_SQ )
  72. {
  73. qorig[0] = down_quat[0]/nqorig;
  74. qorig[1] = down_quat[1]/nqorig;
  75. qorig[2] = down_quat[2]/nqorig;
  76. qorig[3] = down_quat[3]/nqorig;
  77. igl::quat_mult<double>(qrot,qorig,qres);
  78. quat[0] = qres[0];
  79. quat[1] = qres[1];
  80. quat[2] = qres[2];
  81. quat[3] = qres[3];
  82. }
  83. else
  84. {
  85. quat[0] = qrot[0];
  86. quat[1] = qrot[1];
  87. quat[2] = qrot[2];
  88. quat[3] = qrot[3];
  89. }
  90. }
  91. }
  92. #ifndef IGL_HEADER_ONLY
  93. // Explicit template specialization
  94. #endif