trackball.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. /*
  38. * Trackball code:
  39. *
  40. * Implementation of a virtual trackball.
  41. * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
  42. * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
  43. *
  44. * Vector manip code:
  45. *
  46. * Original code from:
  47. * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
  48. *
  49. * Much mucking with by:
  50. * Gavin Bell
  51. */
  52. #include <math.h>
  53. #include "trackball.h"
  54. #ifdef WIN32
  55. //Disable warning in this file: conversion from 'double' to 'float', possible loss of data
  56. #pragma warning(disable:4244)
  57. #endif
  58. /*
  59. * This size should really be based on the distance from the center of
  60. * rotation to the point on the object underneath the mouse. That
  61. * point would then track the mouse as closely as possible. This is a
  62. * simple example, though, so that is left as an Exercise for the
  63. * Programmer.
  64. */
  65. #define TRACKBALLSIZE (0.8)
  66. /*
  67. * Local function prototypes (not defined in trackball.h)
  68. */
  69. static float tb_project_to_sphere(float, float, float);
  70. static void normalize_quat(float [4]);
  71. void
  72. vzero(float *v)
  73. {
  74. v[0] = 0.0;
  75. v[1] = 0.0;
  76. v[2] = 0.0;
  77. }
  78. void
  79. vset(float *v, float x, float y, float z)
  80. {
  81. v[0] = x;
  82. v[1] = y;
  83. v[2] = z;
  84. }
  85. void
  86. vsub(const float *src1, const float *src2, float *dst)
  87. {
  88. dst[0] = src1[0] - src2[0];
  89. dst[1] = src1[1] - src2[1];
  90. dst[2] = src1[2] - src2[2];
  91. }
  92. void
  93. vcopy(const float *v1, float *v2)
  94. {
  95. register int i;
  96. for (i = 0 ; i < 3 ; i++)
  97. v2[i] = v1[i];
  98. }
  99. void
  100. vcross(const float *v1, const float *v2, float *cross)
  101. {
  102. float temp[3];
  103. temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  104. temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  105. temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  106. vcopy(temp, cross);
  107. }
  108. float
  109. vlength(const float *v)
  110. {
  111. return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  112. }
  113. void
  114. vscale(float *v, float div)
  115. {
  116. v[0] *= div;
  117. v[1] *= div;
  118. v[2] *= div;
  119. }
  120. void
  121. vnormal(float *v)
  122. {
  123. vscale(v,1.0/vlength(v));
  124. }
  125. float
  126. vdot(const float *v1, const float *v2)
  127. {
  128. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  129. }
  130. void
  131. vadd(const float *src1, const float *src2, float *dst)
  132. {
  133. dst[0] = src1[0] + src2[0];
  134. dst[1] = src1[1] + src2[1];
  135. dst[2] = src1[2] + src2[2];
  136. }
  137. /*
  138. * Ok, simulate a track-ball. Project the points onto the virtual
  139. * trackball, then figure out the axis of rotation, which is the cross
  140. * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
  141. * Note: This is a deformed trackball-- is a trackball in the center,
  142. * but is deformed into a hyperbolic sheet of rotation away from the
  143. * center. This particular function was chosen after trying out
  144. * several variations.
  145. *
  146. * It is assumed that the arguments to this routine are in the range
  147. * (-1.0 ... 1.0)
  148. */
  149. void
  150. trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
  151. {
  152. float a[3]; /* Axis of rotation */
  153. float phi; /* how much to rotate about axis */
  154. float p1[3], p2[3], d[3];
  155. float t;
  156. if (p1x == p2x && p1y == p2y) {
  157. /* Zero rotation */
  158. vzero(q);
  159. q[3] = 1.0;
  160. return;
  161. }
  162. /*
  163. * First, figure out z-coordinates for igl::opengl2::projection of P1 and P2 to
  164. * deformed sphere
  165. */
  166. vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
  167. vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
  168. /*
  169. * Now, we want the cross product of P1 and P2
  170. */
  171. vcross(p2,p1,a);
  172. /*
  173. * Figure out how much to rotate around that axis.
  174. */
  175. vsub(p1,p2,d);
  176. t = vlength(d) / (2.0*TRACKBALLSIZE);
  177. /*
  178. * Avoid problems with out-of-control values...
  179. */
  180. if (t > 1.0) t = 1.0;
  181. if (t < -1.0) t = -1.0;
  182. phi = 2.0 * asin(t);
  183. axis_to_quat(a,phi,q);
  184. }
  185. /*
  186. * Given an axis and angle, compute quaternion.
  187. */
  188. void
  189. axis_to_quat(float a[3], float phi, float q[4])
  190. {
  191. vnormal(a);
  192. vcopy(a,q);
  193. vscale(q,sin(phi/2.0));
  194. q[3] = cos(phi/2.0);
  195. }
  196. void
  197. quat_to_axis(float q[4], float axis[3], float &phi) {
  198. if (q[3] > 1)
  199. normalize_quat(q); // if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
  200. phi = 2 * acos(q[3]);
  201. double s = sqrt(1-q[3]*q[3]); // assuming quaternion normalised then w is less than 1, so term always positive.
  202. if (s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
  203. // if s close to zero then direction of axis not important
  204. axis[0] = q[0]; // if it is important that axis is normalised then replace with x=1; y=z=0;
  205. axis[1] = q[1];
  206. axis[2] = q[2];
  207. } else {
  208. axis[0] = q[0] / s; // normalise axis
  209. axis[1] = q[1] / s;
  210. axis[2] = q[2] / s;
  211. }
  212. }
  213. /*
  214. * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
  215. * if we are away from the center of the sphere.
  216. */
  217. static float
  218. tb_project_to_sphere(float r, float x, float y)
  219. {
  220. float d, t, z;
  221. d = sqrt(x*x + y*y);
  222. if (d < r * 0.70710678118654752440) { /* Inside sphere */
  223. z = sqrt(r*r - d*d);
  224. } else { /* On hyperbola */
  225. t = r / 1.41421356237309504880;
  226. z = t*t / d;
  227. }
  228. return z;
  229. }
  230. /*
  231. * Given two rotations, e1 and e2, expressed as quaternion rotations,
  232. * figure out the equivalent single rotation and stuff it into dest.
  233. *
  234. * This routine also normalizes the result every RENORMCOUNT times it is
  235. * called, to keep error from creeping in.
  236. *
  237. * NOTE: This routine is written so that q1 or q2 may be the same
  238. * as dest (or each other).
  239. */
  240. #define RENORMCOUNT 1
  241. void
  242. add_quats(float q1[4], float q2[4], float dest[4])
  243. {
  244. static int count=0;
  245. float t1[4], t2[4], t3[4];
  246. float tf[4];
  247. vcopy(q1,t1);
  248. vscale(t1,q2[3]);
  249. vcopy(q2,t2);
  250. vscale(t2,q1[3]);
  251. vcross(q2,q1,t3);
  252. vadd(t1,t2,tf);
  253. vadd(t3,tf,tf);
  254. tf[3] = q1[3] * q2[3] - vdot(q1,q2);
  255. dest[0] = tf[0];
  256. dest[1] = tf[1];
  257. dest[2] = tf[2];
  258. dest[3] = tf[3];
  259. if (++count > RENORMCOUNT) {
  260. count = 0;
  261. normalize_quat(dest);
  262. }
  263. }
  264. /*
  265. * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
  266. * If they don't add up to 1.0, dividing by their magnitued will
  267. * renormalize them.
  268. *
  269. * Note: See the following for more information on quaternions:
  270. *
  271. * - Shoemake, K., Animating rotation with quaternion curves, Computer
  272. * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
  273. * - Pletinckx, D., Quaternion calculus as a basic tool in computer
  274. * graphics, The Visual Computer 5, 2-13, 1989.
  275. */
  276. static void
  277. normalize_quat(float q[4])
  278. {
  279. int i;
  280. float mag;
  281. mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
  282. for (i = 0; i < 4; i++) q[i] /= mag;
  283. }
  284. /*
  285. * Build a rotation matrix, given a quaternion rotation.
  286. *
  287. */
  288. void
  289. build_rotmatrix(float m[4][4], float q[4])
  290. {
  291. m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
  292. m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
  293. m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
  294. m[0][3] = 0.0;
  295. m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
  296. m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
  297. m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
  298. m[1][3] = 0.0;
  299. m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
  300. m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
  301. m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
  302. m[2][3] = 0.0;
  303. m[3][0] = 0.0;
  304. m[3][1] = 0.0;
  305. m[3][2] = 0.0;
  306. m[3][3] = 1.0;
  307. }
  308. void
  309. build_rotmatrix_v2(float m[16], float q[4])
  310. {
  311. m[0*4+0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
  312. m[0*4+1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
  313. m[0*4+2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
  314. m[0*4+3] = 0.0;
  315. m[1*4+0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
  316. m[1*4+1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
  317. m[1*4+2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
  318. m[1*4+3] = 0.0;
  319. m[2*4+0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
  320. m[2*4+1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
  321. m[2*4+2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
  322. m[2*4+3] = 0.0;
  323. m[3*4+0] = 0.0;
  324. m[3*4+1] = 0.0;
  325. m[3*4+2] = 0.0;
  326. m[3*4+3] = 1.0;
  327. }
  328. ////Olga
  329. void add_quats_no_renorm(float q1[4], float q2[4], float dest[4])
  330. {
  331. float t1[4], t2[4], t3[4];
  332. float tf[4];
  333. vcopy(q1,t1);
  334. vscale(t1,q2[3]);
  335. vcopy(q2,t2);
  336. vscale(t2,q1[3]);
  337. vcross(q2,q1,t3);
  338. vadd(t1,t2,tf);
  339. vadd(t3,tf,tf);
  340. tf[3] = q1[3] * q2[3] - vdot(q1,q2);
  341. dest[0] = tf[0];
  342. dest[1] = tf[1];
  343. dest[2] = tf[2];
  344. dest[3] = tf[3];
  345. }
  346. void quat_rotate_point(float q[4], const float p1[3], float p2[3])
  347. {
  348. float co[4];
  349. co[0] = -q[0];
  350. co[1] = -q[1];
  351. co[2] = -q[2];
  352. co[3] = q[3];
  353. float tmp[4];
  354. tmp[0] = p1[0];
  355. tmp[1] = p1[1];
  356. tmp[2] = p1[2];
  357. tmp[3] = 0;
  358. float tmp1[4];
  359. float tmp2[4];
  360. add_quats_no_renorm(tmp, co, tmp1);
  361. add_quats_no_renorm(q, tmp1, tmp2);
  362. p2[0] = tmp2[0];
  363. p2[1] = tmp2[1];
  364. p2[2] = tmp2[2];
  365. }
  366. ////Olga