add .gitignore
[xgalaxy.git] / trackball.c
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 <string.h>
53 #include <math.h>
54 #include "trackball.h"
55
56 /*
57  * This size should really be based on the distance from the center of
58  * rotation to the point on the object underneath the mouse.  That
59  * point would then track the mouse as closely as possible.  This is a
60  * simple example, though, so that is left as an Exercise for the
61  * Programmer.
62  */
63 #define TRACKBALLSIZE  (0.8)
64
65 /*
66  * Local function prototypes (not defined in trackball.h)
67  */
68 static double tb_project_to_sphere(double, double, double);
69
70
71 /*
72  * Ok, simulate a track-ball.  Project the points onto the virtual
73  * trackball, then figure out the axis of rotation, which is the cross
74  * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
75  * Note:  This is a deformed trackball-- is a trackball in the center,
76  * but is deformed into a hyperbolic sheet of rotation away from the
77  * center.  This particular function was chosen after trying out
78  * several variations.
79  *
80  * It is assumed that the arguments to this routine are in the range
81  * (-1.0 ... 1.0)
82  */
83 void
84 trackball(Quaternion *q, double p1x, double p1y, double p2x, double p2y)
85 {
86     Vector a; /* Axis of rotation */
87     double phi;  /* how much to rotate about axis */
88     Vector p1, p2, d;
89     double t;
90
91     if (p1x == p2x && p1y == p2y) {
92         /* Zero rotation */
93         memset(&(q->d), 0, sizeof(Vector));
94         q->w = 1.0;
95         return;
96     }
97
98     /*
99      * First, figure out z-coordinates for projection of P1 and P2 to
100      * deformed sphere
101      */
102         p1.x = p1x; p1.y = p1y; p1.z = tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y);
103         p2.x = p2x; p2.y = p2y; p2.z = tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y);
104
105     /*
106      *  Now, we want the cross product of P1 and P2
107      */
108     vector_vmul(&a,&p2,&p1);
109
110     /*
111      *  Figure out how much to rotate around that axis.
112      */
113     vector_sub(&d,&p1,&p2);
114     t = vector_length(&d) / (2.0*TRACKBALLSIZE);
115
116     /*
117      * Avoid problems with out-of-control values...
118      */
119     if (t > 1.0) t = 1.0;
120     if (t < -1.0) t = -1.0;
121     phi = 2.0 * asin(t);
122
123     axis_to_quat(&a,phi,q);
124 }
125
126 /*
127  *  Given an axis and angle, compute quaternion.
128  */
129 void
130 axis_to_quat(Vector *a, double phi, Quaternion *q) {
131     vector_normal(a);
132     memcpy( &(q->d), a, sizeof(Vector) );
133     vector_mul( &(q->d), &(q->d), -sin(phi/2.0) );
134     q->w = cos(phi/2.0);
135 }
136
137 /*
138  * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
139  * if we are away from the center of the sphere.
140  */
141 static double
142 tb_project_to_sphere(double r, double x, double y)
143 {
144     double d, t, z;
145
146     d = sqrt(x*x + y*y);
147     if (d < r * 0.70710678118654752440) {    /* Inside sphere */
148         z = sqrt(r*r - d*d);
149     } else {           /* On hyperbola */
150         t = r / 1.41421356237309504880;
151         z = t*t / d;
152     }
153     return z;
154 }
155
156