/Mac/Classes/trackball.c

https://github.com/akosma/Lorenz · C · 165 lines · 90 code · 26 blank · 49 comment · 6 complexity · b90926e4a1d9ccf88edfb658ca9f769b MD5 · raw file

  1. #include "trackball.h"
  2. #include <math.h>
  3. static const float kTol = 0.001;
  4. static const float kRad2Deg = 180. / 3.1415927;
  5. static const float kDeg2Rad = 3.1415927 / 180.;
  6. float gRadiusTrackball;
  7. float gStartPtTrackball[3];
  8. float gEndPtTrackball[3];
  9. long gXCenterTrackball = 0, gYCenterTrackball = 0;
  10. // mouse positon and view size as inputs
  11. void startTrackball (long x, long y, long originX, long originY, long width, long height)
  12. {
  13. float xxyy;
  14. float nx, ny;
  15. /* Start up the trackball. The trackball works by pretending that a ball
  16. encloses the 3D view. You roll this pretend ball with the mouse. For
  17. example, if you click on the center of the ball and move the mouse straight
  18. to the right, you roll the ball around its Y-axis. This produces a Y-axis
  19. rotation. You can click on the "edge" of the ball and roll it around
  20. in a circle to get a Z-axis rotation.
  21. The math behind the trackball is simple: start with a vector from the first
  22. mouse-click on the ball to the center of the 3D view. At the same time, set the radius
  23. of the ball to be the smaller dimension of the 3D view. As you drag the mouse
  24. around in the 3D view, a second vector is computed from the surface of the ball
  25. to the center. The axis of rotation is the cross product of these two vectors,
  26. and the angle of rotation is the angle between the two vectors.
  27. */
  28. nx = width;
  29. ny = height;
  30. if (nx > ny)
  31. gRadiusTrackball = ny * 0.5;
  32. else
  33. gRadiusTrackball = nx * 0.5;
  34. // Figure the center of the view.
  35. gXCenterTrackball = originX + width * 0.5;
  36. gYCenterTrackball = originY + height * 0.5;
  37. // Compute the starting vector from the surface of the ball to its center.
  38. gStartPtTrackball [0] = x - gXCenterTrackball;
  39. gStartPtTrackball [1] = y - gYCenterTrackball;
  40. xxyy = gStartPtTrackball [0] * gStartPtTrackball[0] + gStartPtTrackball [1] * gStartPtTrackball [1];
  41. if (xxyy > gRadiusTrackball * gRadiusTrackball) {
  42. // Outside the sphere.
  43. gStartPtTrackball[2] = 0.;
  44. } else
  45. gStartPtTrackball[2] = sqrt (gRadiusTrackball * gRadiusTrackball - xxyy);
  46. }
  47. // update to new mouse position, output rotation angle
  48. void rollToTrackball (long x, long y, float rot [4]) // rot is output rotation angle
  49. {
  50. float xxyy;
  51. float cosAng, sinAng;
  52. float ls, le, lr;
  53. gEndPtTrackball[0] = x - gXCenterTrackball;
  54. gEndPtTrackball[1] = y - gYCenterTrackball;
  55. if (fabs (gEndPtTrackball [0] - gStartPtTrackball [0]) < kTol && fabs (gEndPtTrackball [1] - gStartPtTrackball [1]) < kTol)
  56. return; // Not enough change in the vectors to have an action.
  57. // Compute the ending vector from the surface of the ball to its center.
  58. xxyy = gEndPtTrackball [0] * gEndPtTrackball [0] + gEndPtTrackball [1] * gEndPtTrackball [1];
  59. if (xxyy > gRadiusTrackball * gRadiusTrackball) {
  60. // Outside the sphere.
  61. gEndPtTrackball [2] = 0.;
  62. } else
  63. gEndPtTrackball[ 2] = sqrt (gRadiusTrackball * gRadiusTrackball - xxyy);
  64. // Take the cross product of the two vectors. r = s X e
  65. rot[1] = gStartPtTrackball[1] * gEndPtTrackball[2] - gStartPtTrackball[2] * gEndPtTrackball[1];
  66. rot[2] = -gStartPtTrackball[0] * gEndPtTrackball[2] + gStartPtTrackball[2] * gEndPtTrackball[0];
  67. rot[3] = gStartPtTrackball[0] * gEndPtTrackball[1] - gStartPtTrackball[1] * gEndPtTrackball[0];
  68. // Use atan for a better angle. If you use only cos or sin, you only get
  69. // half the possible angles, and you can end up with rotations that flip around near
  70. // the poles.
  71. // cos(a) = (s . e) / (||s|| ||e||)
  72. cosAng = gStartPtTrackball[0] * gEndPtTrackball[0] + gStartPtTrackball[1] * gEndPtTrackball[1] + gStartPtTrackball[2] * gEndPtTrackball[2]; // (s . e)
  73. ls = sqrt(gStartPtTrackball[0] * gStartPtTrackball[0] + gStartPtTrackball[1] * gStartPtTrackball[1] + gStartPtTrackball[2] * gStartPtTrackball[2]);
  74. ls = 1. / ls; // 1 / ||s||
  75. le = sqrt(gEndPtTrackball[0] * gEndPtTrackball[0] + gEndPtTrackball[1] * gEndPtTrackball[1] + gEndPtTrackball[2] * gEndPtTrackball[2]);
  76. le = 1. / le; // 1 / ||e||
  77. cosAng = cosAng * ls * le;
  78. // sin(a) = ||(s X e)|| / (||s|| ||e||)
  79. sinAng = lr = sqrt(rot[1] * rot[1] + rot[2] * rot[2] + rot[3] * rot[3]); // ||(s X e)||;
  80. // keep this length in lr for normalizing the rotation vector later.
  81. sinAng = sinAng * ls * le;
  82. rot[0] = (float) atan2 (sinAng, cosAng) * kRad2Deg; // GL rotations are in degrees.
  83. // Normalize the rotation axis.
  84. lr = 1. / lr;
  85. rot[1] *= lr; rot[2] *= lr; rot[3] *= lr;
  86. // returns rotate
  87. }
  88. static void rotation2Quat (float *A, float *q)
  89. {
  90. float ang2; // The half-angle
  91. float sinAng2; // sin(half-angle)
  92. // Convert a GL-style rotation to a quaternion. The GL rotation looks like this:
  93. // {angle, x, y, z}, the corresponding quaternion looks like this:
  94. // {{v}, cos(angle/2)}, where {v} is {x, y, z} / sin(angle/2).
  95. ang2 = A[0] * kDeg2Rad * 0.5; // Convert from degrees ot radians, get the half-angle.
  96. sinAng2 = sin(ang2);
  97. q[0] = A[1] * sinAng2; q[1] = A[2] * sinAng2; q[2] = A[3] * sinAng2;
  98. q[3] = cos(ang2);
  99. }
  100. void addToRotationTrackball (float * dA, float * A)
  101. {
  102. float q0[4], q1[4], q2[4];
  103. float theta2, sinTheta2;
  104. // Figure out A' = A . dA
  105. // In quaternions: let q0 <- A, and q1 <- dA.
  106. // Figure out q2 = q1 + q0 (note the order reversal!).
  107. // A' <- q3.
  108. rotation2Quat(A, q0);
  109. rotation2Quat(dA, q1);
  110. // q2 = q1 + q0;
  111. q2[0] = q1[1]*q0[2] - q1[2]*q0[1] + q1[3]*q0[0] + q1[0]*q0[3];
  112. q2[1] = q1[2]*q0[0] - q1[0]*q0[2] + q1[3]*q0[1] + q1[1]*q0[3];
  113. q2[2] = q1[0]*q0[1] - q1[1]*q0[0] + q1[3]*q0[2] + q1[2]*q0[3];
  114. q2[3] = q1[3]*q0[3] - q1[0]*q0[0] - q1[1]*q0[1] - q1[2]*q0[2];
  115. // Here's an excersize for the reader: it's a good idea to re-normalize your quaternions
  116. // every so often. Experiment with different frequencies.
  117. // An identity rotation is expressed as rotation by 0 about any axis.
  118. // The "angle" term in a quaternion is really the cosine of the half-angle.
  119. // So, if the cosine of the half-angle is one (or, 1.0 within our tolerance),
  120. // then you have an identity rotation.
  121. if (fabs(fabs(q2[3] - 1.)) < 1.0e-7) {
  122. // Identity rotation.
  123. A[0] = 0.0f;
  124. A[1] = 1.0f;
  125. A[2] = A[3] = 0.0f;
  126. return;
  127. }
  128. // If you get here, then you have a non-identity rotation. In non-identity rotations,
  129. // the cosine of the half-angle is non-0, which means the sine of the angle is also
  130. // non-0. So we can safely divide by sin(theta2).
  131. // Turn the quaternion back into an {angle, {axis}} rotation.
  132. theta2 = (float) acos (q2[3]);
  133. sinTheta2 = (float) (1.0 / sin ((double) theta2));
  134. A[0] = theta2 * 2.0f * kRad2Deg;
  135. A[1] = q2[0] * sinTheta2;
  136. A[2] = q2[1] * sinTheta2;
  137. A[3] = q2[2] * sinTheta2;
  138. }