trackball.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "trackball.h"
  9. #include "EPS.h"
  10. #include "dot.h"
  11. #include "cross.h"
  12. #include "axis_angle_to_quat.h"
  13. #include "quat_mult.h"
  14. #include <cmath>
  15. #include <cstdlib>
  16. #include <cassert>
  17. #include <algorithm>
  18. // Utility IGL_INLINE functions
  19. template <typename Q_type>
  20. static IGL_INLINE Q_type _QuatD(int w, int h)
  21. {
  22. return (Q_type)(abs(w) < abs(h) ? abs(w) : abs(h)) - 4;
  23. }
  24. template <typename Q_type>
  25. static IGL_INLINE Q_type _QuatIX(int x, int w, int h)
  26. {
  27. return (2.0f*(Q_type)x - (Q_type)w - 1.0f)/_QuatD<Q_type>(w, h);
  28. }
  29. template <typename Q_type>
  30. static IGL_INLINE Q_type _QuatIY(int y, int w, int h)
  31. {
  32. return (-2.0f*(Q_type)y + (Q_type)h - 1.0f)/_QuatD<Q_type>(w, h);
  33. }
  34. // This is largely the trackball as implemented in AntTweakbar. Much of the
  35. // code is straight from its source in TwMgr.cpp
  36. // http://www.antisphere.com/Wiki/tools:anttweakbar
  37. template <typename Q_type>
  38. IGL_INLINE void igl::trackball(
  39. const int w,
  40. const int h,
  41. const Q_type speed_factor,
  42. const int down_mouse_x,
  43. const int down_mouse_y,
  44. const int mouse_x,
  45. const int mouse_y,
  46. Q_type * quat)
  47. {
  48. assert(speed_factor > 0);
  49. double original_x =
  50. _QuatIX<Q_type>(speed_factor*(down_mouse_x-w/2)+w/2, w, h);
  51. double original_y =
  52. _QuatIY<Q_type>(speed_factor*(down_mouse_y-h/2)+h/2, w, h);
  53. double x = _QuatIX<Q_type>(speed_factor*(mouse_x-w/2)+w/2, w, h);
  54. double y = _QuatIY<Q_type>(speed_factor*(mouse_y-h/2)+h/2, w, h);
  55. double z = 1;
  56. double n0 = sqrt(original_x*original_x + original_y*original_y + z*z);
  57. double n1 = sqrt(x*x + y*y + z*z);
  58. if(n0>igl::DOUBLE_EPS && n1>igl::DOUBLE_EPS)
  59. {
  60. double v0[] = { original_x/n0, original_y/n0, z/n0 };
  61. double v1[] = { x/n1, y/n1, z/n1 };
  62. double axis[3];
  63. cross(v0,v1,axis);
  64. double sa = sqrt(dot(axis, axis));
  65. double ca = dot(v0, v1);
  66. double angle = atan2(sa, ca);
  67. if( x*x+y*y>1.0 )
  68. {
  69. angle *= 1.0 + 0.2f*(sqrt(x*x+y*y)-1.0);
  70. }
  71. double qrot[4];
  72. axis_angle_to_quat(axis,angle,qrot);
  73. quat[0] = qrot[0];
  74. quat[1] = qrot[1];
  75. quat[2] = qrot[2];
  76. quat[3] = qrot[3];
  77. }
  78. }
  79. template <typename Q_type>
  80. IGL_INLINE void igl::trackball(
  81. const int w,
  82. const int h,
  83. const Q_type speed_factor,
  84. const Q_type * down_quat,
  85. const int down_mouse_x,
  86. const int down_mouse_y,
  87. const int mouse_x,
  88. const int mouse_y,
  89. Q_type * quat)
  90. {
  91. double qrot[4], qres[4], qorig[4];
  92. igl::trackball<double>(
  93. w,h,
  94. speed_factor,
  95. down_mouse_x,down_mouse_y,
  96. mouse_x,mouse_y,
  97. qrot);
  98. double nqorig =
  99. sqrt(down_quat[0]*down_quat[0]+
  100. down_quat[1]*down_quat[1]+
  101. down_quat[2]*down_quat[2]+
  102. down_quat[3]*down_quat[3]);
  103. if( fabs(nqorig)>igl::DOUBLE_EPS_SQ )
  104. {
  105. qorig[0] = down_quat[0]/nqorig;
  106. qorig[1] = down_quat[1]/nqorig;
  107. qorig[2] = down_quat[2]/nqorig;
  108. qorig[3] = down_quat[3]/nqorig;
  109. igl::quat_mult<double>(qrot,qorig,qres);
  110. quat[0] = qres[0];
  111. quat[1] = qres[1];
  112. quat[2] = qres[2];
  113. quat[3] = qres[3];
  114. }
  115. else
  116. {
  117. quat[0] = qrot[0];
  118. quat[1] = qrot[1];
  119. quat[2] = qrot[2];
  120. quat[3] = qrot[3];
  121. }
  122. }
  123. IGL_INLINE void igl::trackball(
  124. const int w,
  125. const int h,
  126. const double speed_factor,
  127. const Eigen::Quaterniond & down_quat,
  128. const int down_mouse_x,
  129. const int down_mouse_y,
  130. const int mouse_x,
  131. const int mouse_y,
  132. Eigen::Quaterniond & quat)
  133. {
  134. return trackball(
  135. w,
  136. h,
  137. speed_factor,
  138. down_quat.coeffs().data(),
  139. down_mouse_x,
  140. down_mouse_y,
  141. mouse_x,
  142. mouse_y,
  143. quat.coeffs().data());
  144. }
  145. #ifndef IGL_HEADER_ONLY
  146. // Explicit template specialization
  147. // generated by autoexplicit.sh
  148. template void igl::trackball<double>(int, int, double, double const*, int, int, int, int, double*);
  149. // generated by autoexplicit.sh
  150. template void igl::trackball<float>(int, int, float, float const*, int, int, int, int, float*);
  151. #endif