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