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