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 | #if defined(_WIN32) |
---|
53 | #pragma warning (disable:4244) /* disable bogus conversion warnings */ |
---|
54 | #endif |
---|
55 | #include <math.h> |
---|
56 | #include "trackball.h" |
---|
57 | |
---|
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.8f) |
---|
66 | |
---|
67 | /* |
---|
68 | * Local function prototypes (not defined in trackball.h) |
---|
69 | */ |
---|
70 | static float tb_project_to_sphere(float, float, float); |
---|
71 | static void normalize_quat(float [4]); |
---|
72 | |
---|
73 | void vzero(float *v) |
---|
74 | { |
---|
75 | v[0] = 0.0; |
---|
76 | v[1] = 0.0; |
---|
77 | v[2] = 0.0; |
---|
78 | } |
---|
79 | |
---|
80 | void vset(float *v, float x, float y, float z) |
---|
81 | { |
---|
82 | v[0] = x; |
---|
83 | v[1] = y; |
---|
84 | v[2] = z; |
---|
85 | } |
---|
86 | |
---|
87 | void vsub(const float *src1, const float *src2, float *dst) |
---|
88 | { |
---|
89 | dst[0] = src1[0] - src2[0]; |
---|
90 | dst[1] = src1[1] - src2[1]; |
---|
91 | dst[2] = src1[2] - src2[2]; |
---|
92 | } |
---|
93 | |
---|
94 | void vcopy(const float *v1, float *v2) |
---|
95 | { |
---|
96 | register int i; |
---|
97 | for (i = 0 ; i < 3 ; i++) |
---|
98 | v2[i] = v1[i]; |
---|
99 | } |
---|
100 | |
---|
101 | void vcross(const float *v1, const float *v2, float *cross) |
---|
102 | { |
---|
103 | float temp[3]; |
---|
104 | |
---|
105 | temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]); |
---|
106 | temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]); |
---|
107 | temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]); |
---|
108 | vcopy(temp, cross); |
---|
109 | } |
---|
110 | |
---|
111 | float vlength(const float *v) |
---|
112 | { |
---|
113 | return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); |
---|
114 | } |
---|
115 | |
---|
116 | void vscale(float *v, float div) |
---|
117 | { |
---|
118 | v[0] *= div; |
---|
119 | v[1] *= div; |
---|
120 | v[2] *= div; |
---|
121 | } |
---|
122 | |
---|
123 | void vnormal(float *v) |
---|
124 | { |
---|
125 | vscale(v,1.0/vlength(v)); |
---|
126 | } |
---|
127 | |
---|
128 | float vdot(const float *v1, const float *v2) |
---|
129 | { |
---|
130 | return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]; |
---|
131 | } |
---|
132 | |
---|
133 | void vadd(const float *src1, const float *src2, float *dst) |
---|
134 | { |
---|
135 | dst[0] = src1[0] + src2[0]; |
---|
136 | dst[1] = src1[1] + src2[1]; |
---|
137 | dst[2] = src1[2] + src2[2]; |
---|
138 | } |
---|
139 | |
---|
140 | /* |
---|
141 | * Ok, simulate a track-ball. Project the points onto the virtual |
---|
142 | * trackball, then figure out the axis of rotation, which is the cross |
---|
143 | * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0) |
---|
144 | * Note: This is a deformed trackball-- is a trackball in the center, |
---|
145 | * but is deformed into a hyperbolic sheet of rotation away from the |
---|
146 | * center. This particular function was chosen after trying out |
---|
147 | * several variations. |
---|
148 | * |
---|
149 | * It is assumed that the arguments to this routine are in the range |
---|
150 | * (-1.0 ... 1.0) |
---|
151 | */ |
---|
152 | void trackball(float q[4], float p1x, float p1y, float p2x, float p2y) |
---|
153 | { |
---|
154 | float a[3]; /* Axis of rotation */ |
---|
155 | float phi; /* how much to rotate about axis */ |
---|
156 | float p1[3], p2[3], d[3]; |
---|
157 | float t; |
---|
158 | |
---|
159 | if (p1x == p2x && p1y == p2y) { |
---|
160 | /* Zero rotation */ |
---|
161 | vzero(q); |
---|
162 | q[3] = 1.0; |
---|
163 | return; |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * First, figure out z-coordinates for projection of P1 and P2 to |
---|
168 | * deformed sphere |
---|
169 | */ |
---|
170 | vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y)); |
---|
171 | vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y)); |
---|
172 | |
---|
173 | /* |
---|
174 | * Now, we want the cross product of P1 and P2 |
---|
175 | */ |
---|
176 | vcross(p2,p1,a); |
---|
177 | |
---|
178 | /* |
---|
179 | * Figure out how much to rotate around that axis. |
---|
180 | */ |
---|
181 | vsub(p1,p2,d); |
---|
182 | t = vlength(d) / (2.0*TRACKBALLSIZE); |
---|
183 | |
---|
184 | /* |
---|
185 | * Avoid problems with out-of-control values... |
---|
186 | */ |
---|
187 | if (t > 1.0) t = 1.0; |
---|
188 | if (t < -1.0) t = -1.0; |
---|
189 | phi = 2.0 * asin(t); |
---|
190 | |
---|
191 | axis_to_quat(a,phi,q); |
---|
192 | } |
---|
193 | |
---|
194 | /* |
---|
195 | * Given an axis and angle, compute quaternion. |
---|
196 | */ |
---|
197 | void axis_to_quat(float a[3], float phi, float q[4]) |
---|
198 | { |
---|
199 | vnormal(a); |
---|
200 | vcopy(a,q); |
---|
201 | vscale(q,sin(phi/2.0)); |
---|
202 | q[3] = cos(phi/2.0); |
---|
203 | } |
---|
204 | |
---|
205 | /* |
---|
206 | * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet |
---|
207 | * if we are away from the center of the sphere. |
---|
208 | */ |
---|
209 | static float tb_project_to_sphere(float r, float x, float y) |
---|
210 | { |
---|
211 | float d, t, z; |
---|
212 | |
---|
213 | d = sqrt(x*x + y*y); |
---|
214 | if (d < r * 0.70710678118654752440) { /* Inside sphere */ |
---|
215 | z = sqrt(r*r - d*d); |
---|
216 | } else { /* On hyperbola */ |
---|
217 | t = r / 1.41421356237309504880; |
---|
218 | z = t*t / d; |
---|
219 | } |
---|
220 | return z; |
---|
221 | } |
---|
222 | |
---|
223 | /* |
---|
224 | * Given two rotations, e1 and e2, expressed as quaternion rotations, |
---|
225 | * figure out the equivalent single rotation and stuff it into dest. |
---|
226 | * |
---|
227 | * This routine also normalizes the result every RENORMCOUNT times it is |
---|
228 | * called, to keep error from creeping in. |
---|
229 | * |
---|
230 | * NOTE: This routine is written so that q1 or q2 may be the same |
---|
231 | * as dest (or each other). |
---|
232 | */ |
---|
233 | |
---|
234 | #define RENORMCOUNT 97 |
---|
235 | |
---|
236 | void add_quats(float q1[4], float q2[4], float dest[4]) |
---|
237 | { |
---|
238 | static int count=0; |
---|
239 | float t1[4], t2[4], t3[4]; |
---|
240 | float tf[4]; |
---|
241 | |
---|
242 | #if 0 |
---|
243 | printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]); |
---|
244 | printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]); |
---|
245 | #endif |
---|
246 | |
---|
247 | vcopy(q1,t1); |
---|
248 | vscale(t1,q2[3]); |
---|
249 | |
---|
250 | vcopy(q2,t2); |
---|
251 | vscale(t2,q1[3]); |
---|
252 | |
---|
253 | vcross(q2,q1,t3); |
---|
254 | vadd(t1,t2,tf); |
---|
255 | vadd(t3,tf,tf); |
---|
256 | tf[3] = q1[3] * q2[3] - vdot(q1,q2); |
---|
257 | |
---|
258 | #if 0 |
---|
259 | printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]); |
---|
260 | #endif |
---|
261 | |
---|
262 | dest[0] = tf[0]; |
---|
263 | dest[1] = tf[1]; |
---|
264 | dest[2] = tf[2]; |
---|
265 | dest[3] = tf[3]; |
---|
266 | |
---|
267 | if (++count > RENORMCOUNT) { |
---|
268 | count = 0; |
---|
269 | normalize_quat(dest); |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | /* |
---|
274 | * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0 |
---|
275 | * If they don't add up to 1.0, dividing by their magnitued will |
---|
276 | * renormalize them. |
---|
277 | * |
---|
278 | * Note: See the following for more information on quaternions: |
---|
279 | * |
---|
280 | * - Shoemake, K., Animating rotation with quaternion curves, Computer |
---|
281 | * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985. |
---|
282 | * - Pletinckx, D., Quaternion calculus as a basic tool in computer |
---|
283 | * graphics, The Visual Computer 5, 2-13, 1989. |
---|
284 | */ |
---|
285 | static void normalize_quat(float q[4]) |
---|
286 | { |
---|
287 | int i; |
---|
288 | float mag; |
---|
289 | |
---|
290 | mag = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]); |
---|
291 | for (i = 0; i < 4; i++) q[i] /= mag; |
---|
292 | } |
---|
293 | |
---|
294 | /* |
---|
295 | * Build a rotation matrix, given a quaternion rotation. |
---|
296 | * |
---|
297 | */ |
---|
298 | void build_rotmatrix(float m[4][4], float q[4]) |
---|
299 | { |
---|
300 | m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]); |
---|
301 | m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]); |
---|
302 | m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]); |
---|
303 | m[0][3] = 0.0; |
---|
304 | |
---|
305 | m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]); |
---|
306 | m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]); |
---|
307 | m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]); |
---|
308 | m[1][3] = 0.0; |
---|
309 | |
---|
310 | m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]); |
---|
311 | m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]); |
---|
312 | m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]); |
---|
313 | m[2][3] = 0.0; |
---|
314 | |
---|
315 | m[3][0] = 0.0; |
---|
316 | m[3][1] = 0.0; |
---|
317 | m[3][2] = 0.0; |
---|
318 | m[3][3] = 1.0; |
---|
319 | } |
---|
320 | |
---|
321 | /* Fortran wrappers */ |
---|
322 | void trackball_(float *q, float *p1x, float *p1y, float *p2x, float *p2y) |
---|
323 | { |
---|
324 | trackball( q, *p1x, *p1y, *p2x, *p2y); |
---|
325 | } |
---|
326 | |
---|
327 | void add_quats__(float q1[4], float q2[4], float dest[4]) |
---|
328 | { |
---|
329 | add_quats(q1, q2, dest); |
---|
330 | } |
---|
331 | |
---|
332 | void build_rotmatrix__(float m[4][4], float q[4]) |
---|
333 | { |
---|
334 | /* matrix m is transposed twice. No need to do this here */ |
---|
335 | build_rotmatrix(m, q); |
---|
336 | } |
---|
337 | |
---|
338 | |
---|