trackball.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include <iostream>
  19. // Utility IGL_INLINE functions
  20. template <typename Q_type>
  21. static IGL_INLINE Q_type _QuatD(double w, double h)
  22. {
  23. return (Q_type)(abs(w) < abs(h) ? abs(w) : abs(h)) - 4;
  24. }
  25. template <typename Q_type>
  26. static IGL_INLINE Q_type _QuatIX(double x, double w, double h)
  27. {
  28. return (2.0f*(Q_type)x - (Q_type)w - 1.0f)/_QuatD<Q_type>(w, h);
  29. }
  30. template <typename Q_type>
  31. static IGL_INLINE Q_type _QuatIY(double y, double w, double h)
  32. {
  33. return (-2.0f*(Q_type)y + (Q_type)h - 1.0f)/_QuatD<Q_type>(w, h);
  34. }
  35. // This is largely the trackball as implemented in AntTweakbar. Much of the
  36. // code is straight from its source in TwMgr.cpp
  37. // http://www.antisphere.com/Wiki/tools:anttweakbar
  38. template <typename Q_type>
  39. IGL_INLINE void igl::trackball(
  40. const double w,
  41. const double h,
  42. const Q_type speed_factor,
  43. const double down_mouse_x,
  44. const double down_mouse_y,
  45. const double mouse_x,
  46. const double mouse_y,
  47. Q_type * quat)
  48. {
  49. assert(speed_factor > 0);
  50. double original_x =
  51. _QuatIX<Q_type>(speed_factor*(down_mouse_x-w/2)+w/2, w, h);
  52. double original_y =
  53. _QuatIY<Q_type>(speed_factor*(down_mouse_y-h/2)+h/2, w, h);
  54. double x = _QuatIX<Q_type>(speed_factor*(mouse_x-w/2)+w/2, w, h);
  55. double y = _QuatIY<Q_type>(speed_factor*(mouse_y-h/2)+h/2, w, h);
  56. double z = 1;
  57. double n0 = sqrt(original_x*original_x + original_y*original_y + z*z);
  58. double n1 = sqrt(x*x + y*y + z*z);
  59. if(n0>igl::DOUBLE_EPS && n1>igl::DOUBLE_EPS)
  60. {
  61. double v0[] = { original_x/n0, original_y/n0, z/n0 };
  62. double v1[] = { x/n1, y/n1, z/n1 };
  63. double axis[3];
  64. cross(v0,v1,axis);
  65. double sa = sqrt(dot(axis, axis));
  66. double ca = dot(v0, v1);
  67. double angle = atan2(sa, ca);
  68. if( x*x+y*y>1.0 )
  69. {
  70. angle *= 1.0 + 0.2f*(sqrt(x*x+y*y)-1.0);
  71. }
  72. double qrot[4];
  73. axis_angle_to_quat(axis,angle,qrot);
  74. quat[0] = qrot[0];
  75. quat[1] = qrot[1];
  76. quat[2] = qrot[2];
  77. quat[3] = qrot[3];
  78. }
  79. }
  80. template <typename Q_type>
  81. IGL_INLINE void igl::trackball(
  82. const double w,
  83. const double h,
  84. const Q_type speed_factor,
  85. const Q_type * down_quat,
  86. const double down_mouse_x,
  87. const double down_mouse_y,
  88. const double mouse_x,
  89. const double mouse_y,
  90. Q_type * quat)
  91. {
  92. double qrot[4], qres[4], qorig[4];
  93. igl::trackball<double>(
  94. w,h,
  95. speed_factor,
  96. down_mouse_x,down_mouse_y,
  97. mouse_x,mouse_y,
  98. qrot);
  99. double nqorig =
  100. sqrt(down_quat[0]*down_quat[0]+
  101. down_quat[1]*down_quat[1]+
  102. down_quat[2]*down_quat[2]+
  103. down_quat[3]*down_quat[3]);
  104. if( fabs(nqorig)>igl::DOUBLE_EPS_SQ )
  105. {
  106. qorig[0] = down_quat[0]/nqorig;
  107. qorig[1] = down_quat[1]/nqorig;
  108. qorig[2] = down_quat[2]/nqorig;
  109. qorig[3] = down_quat[3]/nqorig;
  110. igl::quat_mult<double>(qrot,qorig,qres);
  111. quat[0] = qres[0];
  112. quat[1] = qres[1];
  113. quat[2] = qres[2];
  114. quat[3] = qres[3];
  115. }
  116. else
  117. {
  118. quat[0] = qrot[0];
  119. quat[1] = qrot[1];
  120. quat[2] = qrot[2];
  121. quat[3] = qrot[3];
  122. }
  123. }
  124. IGL_INLINE void igl::trackball(
  125. const double w,
  126. const double h,
  127. const double speed_factor,
  128. const Eigen::Quaterniond & down_quat,
  129. const double down_mouse_x,
  130. const double down_mouse_y,
  131. const double mouse_x,
  132. const double mouse_y,
  133. Eigen::Quaterniond & quat)
  134. {
  135. using namespace std;
  136. return trackball(
  137. w,
  138. h,
  139. speed_factor,
  140. down_quat.coeffs().data(),
  141. down_mouse_x,
  142. down_mouse_y,
  143. mouse_x,
  144. mouse_y,
  145. quat.coeffs().data());
  146. }
  147. #ifndef IGL_HEADER_ONLY
  148. // Explicit template specialization
  149. // generated by autoexplicit.sh
  150. template void igl::trackball<double>(double, double, double, double const*, double, double, double, double, double*);
  151. // generated by autoexplicit.sh
  152. template void igl::trackball<float>(double, double, float, float const*, double, double, double, double, float*);
  153. #endif