trackball.cpp 2.8 KB

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