You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
8.9 KiB

  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. * Code in this file has been modified by the KiCad project.
  39. * For modifications:
  40. * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
  41. */
  42. /*
  43. * Trackball code:
  44. *
  45. * Implementation of a virtual trackball.
  46. * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
  47. * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
  48. *
  49. * Vector manip code:
  50. *
  51. * Original code from:
  52. * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
  53. *
  54. * Much mucking with by:
  55. * Gavin Bell
  56. */
  57. #include <cmath>
  58. #include <trackball.h>
  59. /*
  60. * This size should really be based on the distance from the center of
  61. * rotation to the point on the object underneath the mouse. That
  62. * point would then track the mouse as closely as possible. This is a
  63. * simple example, though, so that is left as an Exercise for the
  64. * Programmer.
  65. */
  66. #define TRACKBALLSIZE (0.8f)
  67. /*
  68. * Local function prototypes (not defined in trackball.h)
  69. */
  70. static double tb_project_to_sphere( double, double, double );
  71. static void normalize_quat( double [4] );
  72. void vzero( double *v )
  73. {
  74. v[0] = 0.0;
  75. v[1] = 0.0;
  76. v[2] = 0.0;
  77. }
  78. void vset( double *v, double x, double y, double z )
  79. {
  80. v[0] = x;
  81. v[1] = y;
  82. v[2] = z;
  83. }
  84. void vsub( const double *src1, const double *src2, double *dst )
  85. {
  86. dst[0] = src1[0] - src2[0];
  87. dst[1] = src1[1] - src2[1];
  88. dst[2] = src1[2] - src2[2];
  89. }
  90. void vcopy( const double *v1, double *v2 )
  91. {
  92. int i;
  93. for( i = 0 ; i < 3 ; i++ )
  94. v2[i] = v1[i];
  95. }
  96. void vcross( const double *v1, const double *v2, double *cross )
  97. {
  98. double temp[3];
  99. temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  100. temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  101. temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  102. vcopy(temp, cross);
  103. }
  104. double vlength( const double *v )
  105. {
  106. return (double) sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );
  107. }
  108. void vscale( double *v, double div )
  109. {
  110. v[0] *= div;
  111. v[1] *= div;
  112. v[2] *= div;
  113. }
  114. void vnormal( double *v )
  115. {
  116. vscale( v, 1.0f / vlength( v ) );
  117. }
  118. double vdot( const double *v1, const double *v2 )
  119. {
  120. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  121. }
  122. void vadd( const double *src1, const double *src2, double *dst )
  123. {
  124. dst[0] = src1[0] + src2[0];
  125. dst[1] = src1[1] + src2[1];
  126. dst[2] = src1[2] + src2[2];
  127. }
  128. /*
  129. * Ok, simulate a track-ball. Project the points onto the virtual
  130. * trackball, then figure out the axis of rotation, which is the cross
  131. * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
  132. * Note: This is a deformed trackball-- is a trackball in the center,
  133. * but is deformed into a hyperbolic sheet of rotation away from the
  134. * center. This particular function was chosen after trying out
  135. * several variations.
  136. *
  137. * It is assumed that the arguments to this routine are in the range
  138. * (-1.0 ... 1.0)
  139. */
  140. void trackball( double q[4], double p1x, double p1y, double p2x, double p2y )
  141. {
  142. double a[3]; /* Axis of rotation */
  143. double phi; /* how much to rotate about axis */
  144. double p1[3], p2[3], d[3];
  145. double t;
  146. if( p1x == p2x && p1y == p2y )
  147. {
  148. /* Zero rotation */
  149. vzero( q );
  150. q[3] = 1.0;
  151. return;
  152. }
  153. /*
  154. * First, figure out z-coordinates for projection of P1 and P2 to
  155. * deformed sphere
  156. */
  157. vset( p1, p1x, p1y, tb_project_to_sphere( TRACKBALLSIZE, p1x, p1y ) );
  158. vset( p2, p2x, p2y, tb_project_to_sphere( TRACKBALLSIZE, p2x, p2y ) );
  159. /*
  160. * Now, we want the cross product of P1 and P2
  161. */
  162. vcross(p2,p1,a);
  163. /*
  164. * Figure out how much to rotate around that axis.
  165. */
  166. vsub( p1, p2, d );
  167. t = vlength( d ) / (2.0f * TRACKBALLSIZE);
  168. /*
  169. * Avoid problems with out-of-control values...
  170. */
  171. if( t > 1.0 )
  172. t = 1.0;
  173. if( t < -1.0 )
  174. t = -1.0;
  175. phi = 2.0f * (double) asin( t );
  176. axis_to_quat( a, phi, q );
  177. }
  178. /*
  179. * Given an axis and angle, compute quaternion.
  180. */
  181. void axis_to_quat( double a[3], double phi, double q[4] )
  182. {
  183. vnormal( a );
  184. vcopy( a, q );
  185. vscale( q, (double) sin( phi / 2.0) );
  186. q[3] = (double) cos( phi / 2.0 );
  187. }
  188. /*
  189. * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
  190. * if we are away from the center of the sphere.
  191. */
  192. static double tb_project_to_sphere( double r, double x, double y )
  193. {
  194. double d, z;
  195. d = (double) sqrt( x*x + y*y );
  196. if( d < r * 0.70710678118654752440 )
  197. { /* Inside sphere */
  198. z = (double) sqrt( r*r - d*d );
  199. }
  200. else
  201. { /* On hyperbola */
  202. const double t = r / 1.41421356237309504880f;
  203. z = t*t / d;
  204. }
  205. return z;
  206. }
  207. /*
  208. * Given two rotations, e1 and e2, expressed as quaternion rotations,
  209. * figure out the equivalent single rotation and stuff it into dest.
  210. *
  211. * This routine also normalizes the result every RENORMCOUNT times it is
  212. * called, to keep error from creeping in.
  213. *
  214. * NOTE: This routine is written so that q1 or q2 may be the same
  215. * as dest (or each other).
  216. */
  217. #define RENORMCOUNT 97
  218. void add_quats( double q1[4], double q2[4], double dest[4] )
  219. {
  220. static int count=0;
  221. double t1[4], t2[4], t3[4];
  222. double tf[4];
  223. vcopy( q1, t1 );
  224. vscale( t1, q2[3] );
  225. vcopy( q2, t2 );
  226. vscale( t2, q1[3] );
  227. vcross( q2, q1, t3 );
  228. vadd( t1, t2, tf );
  229. vadd( t3, tf, tf );
  230. tf[3] = q1[3] * q2[3] - vdot( q1, q2 );
  231. dest[0] = tf[0];
  232. dest[1] = tf[1];
  233. dest[2] = tf[2];
  234. dest[3] = tf[3];
  235. if( ++count > RENORMCOUNT )
  236. {
  237. count = 0;
  238. normalize_quat( dest );
  239. }
  240. }
  241. /*
  242. * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
  243. * If they don't add up to 1.0, dividing by their magnitued will
  244. * renormalize them.
  245. *
  246. * Note: See the following for more information on quaternions:
  247. *
  248. * - Shoemake, K., Animating rotation with quaternion curves, Computer
  249. * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
  250. * - Pletinckx, D., Quaternion calculus as a basic tool in computer
  251. * graphics, The Visual Computer 5, 2-13, 1989.
  252. */
  253. static void normalize_quat( double q[4] )
  254. {
  255. int i;
  256. double mag;
  257. mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
  258. for( i = 0; i < 4; i++ )
  259. q[i] /= mag;
  260. }
  261. /*
  262. * Build a rotation matrix, given a quaternion rotation.
  263. *
  264. */
  265. void build_rotmatrix( float m[4][4], double q[4] )
  266. {
  267. m[0][0] = (float)(1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]));
  268. m[0][1] = (float)(2.0 * (q[0] * q[1] - q[2] * q[3]));
  269. m[0][2] = (float)(2.0 * (q[2] * q[0] + q[1] * q[3]));
  270. m[0][3] = 0.0f;
  271. m[1][0] = (float)(2.0 * (q[0] * q[1] + q[2] * q[3]));
  272. m[1][1] = (float)(1.0 - 2.0f * (q[2] * q[2] + q[0] * q[0]));
  273. m[1][2] = (float)(2.0 * (q[1] * q[2] - q[0] * q[3]));
  274. m[1][3] = 0.0f;
  275. m[2][0] = (float)(2.0 * (q[2] * q[0] - q[1] * q[3]));
  276. m[2][1] = (float)(2.0 * (q[1] * q[2] + q[0] * q[3]));
  277. m[2][2] = (float)(1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]));
  278. m[2][3] = 0.0f;
  279. m[3][0] = 0.0f;
  280. m[3][1] = 0.0f;
  281. m[3][2] = 0.0f;
  282. m[3][3] = 1.0f;
  283. }