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