src/qvgui/trackball.c

00001 /*
00002  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
00003  * ALL RIGHTS RESERVED
00004  * Permission to use, copy, modify, and distribute this software for
00005  * any purpose and without fee is hereby granted, provided that the above
00006  * copyright notice appear in all copies and that both the copyright notice
00007  * and this permission notice appear in supporting documentation, and that
00008  * the name of Silicon Graphics, Inc. not be used in advertising
00009  * or publicity pertaining to distribution of the software without specific,
00010  * written prior permission.
00011  *
00012  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
00013  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
00014  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
00015  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
00016  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
00017  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
00018  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
00019  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
00020  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
00021  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
00022  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
00023  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
00024  *
00025  * US Government Users Restricted Rights
00026  * Use, duplication, or disclosure by the Government is subject to
00027  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
00028  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
00029  * clause at DFARS 252.227-7013 and/or in similar or successor
00030  * clauses in the FAR or the DOD or NASA FAR Supplement.
00031  * Unpublished-- rights reserved under the copyright laws of the
00032  * United States.  Contractor/manufacturer is Silicon Graphics,
00033  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
00034  *
00035  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
00036  */
00037 /*
00038  * Trackball code:
00039  *
00040  * Implementation of a virtual trackball.
00041  * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
00042  *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
00043  *
00044  * Vector manip code:
00045  *
00046  * Original code from:
00047  * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
00048  *
00049  * Much mucking with by:
00050  * Gavin Bell
00051  */
00052 
00053 #include <math.h>
00054 #include "trackball.h"
00055 
00056 /*
00057  * This size should really be based on the distance from the center of
00058  * rotation to the point on the object underneath the mouse.  That
00059  * point would then track the mouse as closely as possible.  This is a
00060  * simple example, though, so that is left as an Exercise for the
00061  * Programmer.
00062  */
00063 #define TRACKBALLSIZE  (0.8)
00064 
00065 /*
00066  * Local function prototypes (not defined in trackball.h)
00067  */
00068 static float tb_project_to_sphere(float, float, float);
00069 static void normalize_quat(float [4]);
00070 
00071 void
00072 vzero(float *v)
00073 {
00074     v[0] = 0.0;
00075     v[1] = 0.0;
00076     v[2] = 0.0;
00077 }
00078 
00079 void
00080 vset(float *v, float x, float y, float z)
00081 {
00082     v[0] = x;
00083     v[1] = y;
00084     v[2] = z;
00085 }
00086 
00087 void
00088 vsub(const float *src1, const float *src2, float *dst)
00089 {
00090     dst[0] = src1[0] - src2[0];
00091     dst[1] = src1[1] - src2[1];
00092     dst[2] = src1[2] - src2[2];
00093 }
00094 
00095 void
00096 vcopy(const float *v1, float *v2)
00097 {
00098     register int i;
00099     for (i = 0 ; i < 3 ; i++)
00100         v2[i] = v1[i];
00101 }
00102 
00103 void
00104 vcross(const float *v1, const float *v2, float *cross)
00105 {
00106     float temp[3];
00107 
00108     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
00109     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
00110     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
00111     vcopy(temp, cross);
00112 }
00113 
00114 float
00115 vlength(const float *v)
00116 {
00117     return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
00118 }
00119 
00120 void
00121 vscale(float *v, float div)
00122 {
00123     v[0] *= div;
00124     v[1] *= div;
00125     v[2] *= div;
00126 }
00127 
00128 void
00129 vnormal(float *v)
00130 {
00131     vscale(v,1.0/vlength(v));
00132 }
00133 
00134 float
00135 vdot(const float *v1, const float *v2)
00136 {
00137     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
00138 }
00139 
00140 void
00141 vadd(const float *src1, const float *src2, float *dst)
00142 {
00143     dst[0] = src1[0] + src2[0];
00144     dst[1] = src1[1] + src2[1];
00145     dst[2] = src1[2] + src2[2];
00146 }
00147 
00148 /*
00149  * Ok, simulate a track-ball.  Project the points onto the virtual
00150  * trackball, then figure out the axis of rotation, which is the cross
00151  * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
00152  * Note:  This is a deformed trackball-- is a trackball in the center,
00153  * but is deformed into a hyperbolic sheet of rotation away from the
00154  * center.  This particular function was chosen after trying out
00155  * several variations.
00156  *
00157  * It is assumed that the arguments to this routine are in the range
00158  * (-1.0 ... 1.0)
00159  */
00160 void
00161 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
00162 {
00163     float a[3]; /* Axis of rotation */
00164     float phi;  /* how much to rotate about axis */
00165     float p1[3], p2[3], d[3];
00166     float t;
00167 
00168     if (p1x == p2x && p1y == p2y) {
00169         /* Zero rotation */
00170         vzero(q);
00171         q[3] = 1.0;
00172         return;
00173     }
00174 
00175     /*
00176      * First, figure out z-coordinates for projection of P1 and P2 to
00177      * deformed sphere
00178      */
00179     vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
00180     vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
00181 
00182     /*
00183      *  Now, we want the cross product of P1 and P2
00184      */
00185     vcross(p2,p1,a);
00186 
00187     /*
00188      *  Figure out how much to rotate around that axis.
00189      */
00190     vsub(p1,p2,d);
00191     t = vlength(d) / (2.0*TRACKBALLSIZE);
00192 
00193     /*
00194      * Avoid problems with out-of-control values...
00195      */
00196     if (t > 1.0) t = 1.0;
00197     if (t < -1.0) t = -1.0;
00198     phi = 2.0 * asin(t);
00199 
00200     axis_to_quat(a,phi,q);
00201 }
00202 
00203 /*
00204  *  Given an axis and angle, compute quaternion.
00205  */
00206 void
00207 axis_to_quat(float a[3], float phi, float q[4])
00208 {
00209     vnormal(a);
00210     vcopy(a,q);
00211     vscale(q,sin(phi/2.0));
00212     q[3] = cos(phi/2.0);
00213 }
00214 
00215 /*
00216  * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
00217  * if we are away from the center of the sphere.
00218  */
00219 static float
00220 tb_project_to_sphere(float r, float x, float y)
00221 {
00222     float d, t, z;
00223 
00224     d = sqrt(x*x + y*y);
00225     if (d < r * 0.70710678118654752440) {    /* Inside sphere */
00226         z = sqrt(r*r - d*d);
00227     } else {           /* On hyperbola */
00228         t = r / 1.41421356237309504880;
00229         z = t*t / d;
00230     }
00231     return z;
00232 }
00233 
00234 /*
00235  * Given two rotations, e1 and e2, expressed as quaternion rotations,
00236  * figure out the equivalent single rotation and stuff it into dest.
00237  *
00238  * This routine also normalizes the result every RENORMCOUNT times it is
00239  * called, to keep error from creeping in.
00240  *
00241  * NOTE: This routine is written so that q1 or q2 may be the same
00242  * as dest (or each other).
00243  */
00244 
00245 #define RENORMCOUNT 97
00246 
00247 void
00248 add_quats(float q1[4], float q2[4], float dest[4])
00249 {
00250     static int count=0;
00251     float t1[4], t2[4], t3[4];
00252     float tf[4];
00253 
00254     vcopy(q1,t1);
00255     vscale(t1,q2[3]);
00256 
00257     vcopy(q2,t2);
00258     vscale(t2,q1[3]);
00259 
00260     vcross(q2,q1,t3);
00261     vadd(t1,t2,tf);
00262     vadd(t3,tf,tf);
00263     tf[3] = q1[3] * q2[3] - vdot(q1,q2);
00264 
00265     dest[0] = tf[0];
00266     dest[1] = tf[1];
00267     dest[2] = tf[2];
00268     dest[3] = tf[3];
00269 
00270     if (++count > RENORMCOUNT) {
00271         count = 0;
00272         normalize_quat(dest);
00273     }
00274 }
00275 
00276 /*
00277  * Quaternions always obey:  a^2 + b^2 + c^2 + d^2 = 1.0
00278  * If they don't add up to 1.0, dividing by their magnitued will
00279  * renormalize them.
00280  *
00281  * Note: See the following for more information on quaternions:
00282  *
00283  * - Shoemake, K., Animating rotation with quaternion curves, Computer
00284  *   Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
00285  * - Pletinckx, D., Quaternion calculus as a basic tool in computer
00286  *   graphics, The Visual Computer 5, 2-13, 1989.
00287  */
00288 static void
00289 normalize_quat(float q[4])
00290 {
00291     int i;
00292     float mag;
00293 
00294     mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
00295     for (i = 0; i < 4; i++) q[i] /= mag;
00296 }
00297 
00298 /*
00299  * Build a rotation matrix, given a quaternion rotation.
00300  *
00301  */
00302 void
00303 build_rotmatrix(float m[4][4], float q[4])
00304 {
00305     m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
00306     m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
00307     m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
00308     m[0][3] = 0.0;
00309 
00310     m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
00311     m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
00312     m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
00313     m[1][3] = 0.0;
00314 
00315     m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
00316     m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
00317     m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
00318     m[2][3] = 0.0;
00319 
00320     m[3][0] = 0.0;
00321     m[3][1] = 0.0;
00322     m[3][2] = 0.0;
00323     m[3][3] = 1.0;
00324 }

Generated on Thu Mar 13 19:18:16 2008 for QVision by  doxygen 1.5.3