KiCad PCB EDA Suite
Loading...
Searching...
No Matches
trackball.cpp
Go to the documentation of this file.
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 * Code in this file has been modified by the KiCad project.
39 * For modifications:
40 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
41 */
42/*
43 * Trackball code:
44 *
45 * Implementation of a virtual trackball.
46 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
47 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
48 *
49 * Vector manip code:
50 *
51 * Original code from:
52 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
53 *
54 * Much mucking with by:
55 * Gavin Bell
56 */
57#include <cmath>
58#include <trackball.h>
59
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 (0.8f)
68
69/*
70 * Local function prototypes (not defined in trackball.h)
71 */
72static double tb_project_to_sphere( double, double, double );
73static void normalize_quat( double [4] );
74
75
76void vzero( double *v )
77{
78 v[0] = 0.0;
79 v[1] = 0.0;
80 v[2] = 0.0;
81}
82
83
84void vset( double *v, double x, double y, double z )
85{
86 v[0] = x;
87 v[1] = y;
88 v[2] = z;
89}
90
91
92void vsub( const double *src1, const double *src2, double *dst )
93{
94 dst[0] = src1[0] - src2[0];
95 dst[1] = src1[1] - src2[1];
96 dst[2] = src1[2] - src2[2];
97}
98
99
100void vcopy( const double *v1, double *v2 )
101{
102 int i;
103
104 for( i = 0 ; i < 3 ; i++ )
105 v2[i] = v1[i];
106}
107
108
109void 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
120double vlength( const double *v )
121{
122 return (double) sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );
123}
124
125
126void vscale( double *v, double div )
127{
128 v[0] *= div;
129 v[1] *= div;
130 v[2] *= div;
131}
132
133
134void vnormal( double *v )
135{
136 vscale( v, 1.0f / vlength( v ) );
137}
138
139
140double 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
146void 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/*
155 * Ok, simulate a track-ball. Project the points onto the virtual
156 * trackball, then figure out the axis of rotation, which is the cross
157 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
158 * Note: This is a deformed trackball-- is a trackball in the center,
159 * but is deformed into a hyperbolic sheet of rotation away from the
160 * center. This particular function was chosen after trying out
161 * several variations.
162 *
163 * It is assumed that the arguments to this routine are in the range
164 * (-1.0 ... 1.0)
165 */
166void 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 {
175 /* Zero rotation */
176 vzero( q );
177 q[3] = 1.0;
178 return;
179 }
180
181 /*
182 * First, figure out z-coordinates for projection of P1 and P2 to
183 * deformed sphere
184 */
185 vset( p1, p1x, p1y, tb_project_to_sphere( TRACKBALLSIZE, p1x, p1y ) );
186 vset( p2, p2x, p2y, tb_project_to_sphere( TRACKBALLSIZE, p2x, p2y ) );
187
188 /*
189 * Now, we want the cross product of P1 and P2
190 */
191 vcross( p2, p1, a );
192
193 /*
194 * Figure out how much to rotate around that axis.
195 */
196 vsub( p1, p2, d );
197 t = vlength( d ) / ( 2.0f * TRACKBALLSIZE );
198
199 /*
200 * Avoid problems with out-of-control values...
201 */
202 if( t > 1.0 )
203 t = 1.0;
204
205 if( t < -1.0 )
206 t = -1.0;
207
208 phi = 2.0f * (double) asin( t );
209
210 axis_to_quat( a, phi, q );
211}
212
213
214/*
215 * Given an axis and angle, compute quaternion.
216 */
217void axis_to_quat( double a[3], double phi, double q[4] )
218{
219 vnormal( a );
220 vcopy( a, q );
221 vscale( q, (double) sin( phi / 2.0) );
222 q[3] = (double) cos( phi / 2.0 );
223}
224
225
226/*
227 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
228 * if we are away from the center of the sphere.
229 */
230static double tb_project_to_sphere( double r, double x, double y )
231{
232 double d, z;
233
234 d = (double) sqrt( x*x + y*y );
235
236 if( d < r * 0.70710678118654752440 )
237 { /* Inside sphere */
238 z = (double) sqrt( r*r - d*d );
239 }
240 else
241 { /* On hyperbola */
242 const double t = r / 1.41421356237309504880f;
243 z = t*t / d;
244 }
245
246 return z;
247}
248
249
250/*
251 * Given two rotations, e1 and e2, expressed as quaternion rotations,
252 * figure out the equivalent single rotation and stuff it into dest.
253 *
254 * This routine also normalizes the result every RENORMCOUNT times it is
255 * called, to keep error from creeping in.
256 *
257 * NOTE: This routine is written so that q1 or q2 may be the same
258 * as dest (or each other).
259 */
260
261#define RENORMCOUNT 97
262
263void add_quats( double q1[4], double q2[4], double dest[4] )
264{
265 static int count = 0;
266 double t1[4], t2[4], t3[4];
267 double tf[4];
268
269 vcopy( q1, t1 );
270 vscale( t1, q2[3] );
271
272 vcopy( q2, t2 );
273 vscale( t2, q1[3] );
274
275 vcross( q2, q1, t3 );
276 vadd( t1, t2, tf );
277 vadd( t3, tf, tf );
278
279 tf[3] = q1[3] * q2[3] - vdot( q1, q2 );
280
281 dest[0] = tf[0];
282 dest[1] = tf[1];
283 dest[2] = tf[2];
284 dest[3] = tf[3];
285
286 if( ++count > RENORMCOUNT )
287 {
288 count = 0;
289 normalize_quat( dest );
290 }
291}
292
293
294/*
295 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
296 * If they don't add up to 1.0, dividing by their magnitued will
297 * renormalize them.
298 *
299 * Note: See the following for more information on quaternions:
300 *
301 * - Shoemake, K., Animating rotation with quaternion curves, Computer
302 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
303 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
304 * graphics, The Visual Computer 5, 2-13, 1989.
305 */
306static void normalize_quat( double q[4] )
307{
308 int i;
309 double mag;
310
311 mag = ( q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3] );
312
313 for( i = 0; i < 4; i++ )
314 q[i] /= mag;
315}
316
317
318/*
319 * Build a rotation matrix, given a quaternion rotation.
320 */
321void build_rotmatrix( float m[4][4], double q[4] )
322{
323 m[0][0] = (float) ( 1.0 - 2.0 * ( q[1] * q[1] + q[2] * q[2] ) );
324 m[0][1] = (float) ( 2.0 * ( q[0] * q[1] - q[2] * q[3] ) );
325 m[0][2] = (float) ( 2.0 * ( q[2] * q[0] + q[1] * q[3] ) );
326 m[0][3] = 0.0f;
327
328 m[1][0] = (float) ( 2.0 * ( q[0] * q[1] + q[2] * q[3] ) );
329 m[1][1] = (float) ( 1.0 - 2.0f * ( q[2] * q[2] + q[0] * q[0] ) );
330 m[1][2] = (float) ( 2.0 * ( q[1] * q[2] - q[0] * q[3] ) );
331 m[1][3] = 0.0f;
332
333 m[2][0] = (float) ( 2.0 * ( q[2] * q[0] - q[1] * q[3] ) );
334 m[2][1] = (float) ( 2.0 * ( q[1] * q[2] + q[0] * q[3] ) );
335 m[2][2] = (float) ( 1.0 - 2.0 * ( q[1] * q[1] + q[0] * q[0] ) );
336 m[2][3] = 0.0f;
337
338 m[3][0] = 0.0f;
339 m[3][1] = 0.0f;
340 m[3][2] = 0.0f;
341 m[3][3] = 1.0f;
342}
343
VECTOR3I v1(5, 5, 5)
VECTOR2I v2(1, 0)
void vcross(const double *v1, const double *v2, double *cross)
Definition: trackball.cpp:109
void vscale(double *v, double div)
Definition: trackball.cpp:126
void vnormal(double *v)
Definition: trackball.cpp:134
double vdot(const double *v1, const double *v2)
Definition: trackball.cpp:140
void build_rotmatrix(float m[4][4], double q[4])
Definition: trackball.cpp:321
void vzero(double *v)
Definition: trackball.cpp:76
static void normalize_quat(double[4])
Definition: trackball.cpp:306
void trackball(double q[4], double p1x, double p1y, double p2x, double p2y)
Definition: trackball.cpp:166
void axis_to_quat(double a[3], double phi, double q[4])
Definition: trackball.cpp:217
#define TRACKBALLSIZE
Definition: trackball.cpp:67
#define RENORMCOUNT
Definition: trackball.cpp:261
void vset(double *v, double x, double y, double z)
Definition: trackball.cpp:84
double vlength(const double *v)
Definition: trackball.cpp:120
void vcopy(const double *v1, double *v2)
Definition: trackball.cpp:100
void vsub(const double *src1, const double *src2, double *dst)
Definition: trackball.cpp:92
void vadd(const double *src1, const double *src2, double *dst)
Definition: trackball.cpp:146
static double tb_project_to_sphere(double, double, double)
Definition: trackball.cpp:230
void add_quats(double q1[4], double q2[4], double dest[4])
Definition: trackball.cpp:263