KiCad PCB EDA Suite
trackball.cpp File Reference
#include <cmath>
#include <trackball.h>

Go to the source code of this file.

Macros

#define TRACKBALLSIZE   (0.8f)
 
#define RENORMCOUNT   97
 

Functions

static double tb_project_to_sphere (double, double, double)
 
static void normalize_quat (double[4])
 
void vzero (double *v)
 
void vset (double *v, double x, double y, double z)
 
void vsub (const double *src1, const double *src2, double *dst)
 
void vcopy (const double *v1, double *v2)
 
void vcross (const double *v1, const double *v2, double *cross)
 
double vlength (const double *v)
 
void vscale (double *v, double div)
 
void vnormal (double *v)
 
double vdot (const double *v1, const double *v2)
 
void vadd (const double *src1, const double *src2, double *dst)
 
void trackball (double q[4], double p1x, double p1y, double p2x, double p2y)
 
void axis_to_quat (double a[3], double phi, double q[4])
 
void add_quats (double q1[4], double q2[4], double dest[4])
 
void build_rotmatrix (float m[4][4], double q[4])
 

Macro Definition Documentation

#define RENORMCOUNT   97

Definition at line 247 of file trackball.cpp.

Referenced by add_quats().

#define TRACKBALLSIZE   (0.8f)

Definition at line 67 of file trackball.cpp.

Referenced by trackball().

Function Documentation

void add_quats ( double  q1[4],
double  q2[4],
double  dest[4] 
)

Definition at line 249 of file trackball.cpp.

References normalize_quat(), RENORMCOUNT, vadd(), vcopy(), vcross(), vdot(), and vscale().

Referenced by CTRACK_BALL::Drag().

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 }
void vscale(double *v, double div)
Definition: trackball.cpp:119
void vcross(const double *v1, const double *v2, double *cross)
Definition: trackball.cpp:104
double vdot(const double *v1, const double *v2)
Definition: trackball.cpp:131
void vcopy(const double *v1, double *v2)
Definition: trackball.cpp:96
void vadd(const double *src1, const double *src2, double *dst)
Definition: trackball.cpp:136
static void normalize_quat(double[4])
Definition: trackball.cpp:291
#define RENORMCOUNT
Definition: trackball.cpp:247
void axis_to_quat ( double  a[3],
double  phi,
double  q[4] 
)

Definition at line 205 of file trackball.cpp.

References vcopy(), vnormal(), and vscale().

Referenced by trackball().

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 }
void vscale(double *v, double div)
Definition: trackball.cpp:119
void vcopy(const double *v1, double *v2)
Definition: trackball.cpp:96
void vnormal(double *v)
Definition: trackball.cpp:126
void build_rotmatrix ( float  m[4][4],
double  q[4] 
)

Definition at line 306 of file trackball.cpp.

Referenced by CTRACK_BALL::Drag(), and CTRACK_BALL::Interpolate().

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 }
static void normalize_quat ( double  q[4])
static

Definition at line 291 of file trackball.cpp.

Referenced by add_quats().

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 }
static double tb_project_to_sphere ( double  r,
double  x,
double  y 
)
static

Definition at line 217 of file trackball.cpp.

References opti_s::t.

Referenced by trackball().

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 }
void trackball ( double  q[4],
double  p1x,
double  p1y,
double  p2x,
double  p2y 
)

Definition at line 155 of file trackball.cpp.

References axis_to_quat(), opti_s::t, tb_project_to_sphere(), TRACKBALLSIZE, vcross(), vlength(), vset(), vsub(), and vzero().

Referenced by CTRACK_BALL::CTRACK_BALL(), CTRACK_BALL::Drag(), CTRACK_BALL::Reset(), and CTRACK_BALL::Reset_T1().

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 }
static double tb_project_to_sphere(double, double, double)
Definition: trackball.cpp:217
void axis_to_quat(double a[3], double phi, double q[4])
Definition: trackball.cpp:205
void vset(double *v, double x, double y, double z)
Definition: trackball.cpp:82
void vcross(const double *v1, const double *v2, double *cross)
Definition: trackball.cpp:104
double vlength(const double *v)
Definition: trackball.cpp:114
#define TRACKBALLSIZE
Definition: trackball.cpp:67
void vsub(const double *src1, const double *src2, double *dst)
Definition: trackball.cpp:89
void vzero(double *v)
Definition: trackball.cpp:75
void vadd ( const double *  src1,
const double *  src2,
double *  dst 
)

Definition at line 136 of file trackball.cpp.

Referenced by add_quats().

137 {
138  dst[0] = src1[0] + src2[0];
139  dst[1] = src1[1] + src2[1];
140  dst[2] = src1[2] + src2[2];
141 }
void vcopy ( const double *  v1,
double *  v2 
)

Definition at line 96 of file trackball.cpp.

Referenced by add_quats(), axis_to_quat(), and vcross().

97 {
98  int i;
99 
100  for( i = 0 ; i < 3 ; i++ )
101  v2[i] = v1[i];
102 }
void vcross ( const double *  v1,
const double *  v2,
double *  cross 
)

Definition at line 104 of file trackball.cpp.

References vcopy().

Referenced by add_quats(), and trackball().

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 }
void vcopy(const double *v1, double *v2)
Definition: trackball.cpp:96
double vdot ( const double *  v1,
const double *  v2 
)

Definition at line 131 of file trackball.cpp.

Referenced by add_quats().

132 {
133  return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
134 }
double vlength ( const double *  v)

Definition at line 114 of file trackball.cpp.

Referenced by trackball(), and vnormal().

115 {
116  return (double) sqrt( v[0] * v[0] + v[1] * v[1] + v[2] * v[2] );
117 }
void vnormal ( double *  v)

Definition at line 126 of file trackball.cpp.

References vlength(), and vscale().

Referenced by axis_to_quat().

127 {
128  vscale( v, 1.0f / vlength( v ) );
129 }
void vscale(double *v, double div)
Definition: trackball.cpp:119
double vlength(const double *v)
Definition: trackball.cpp:114
void vscale ( double *  v,
double  div 
)

Definition at line 119 of file trackball.cpp.

Referenced by add_quats(), axis_to_quat(), and vnormal().

120 {
121  v[0] *= div;
122  v[1] *= div;
123  v[2] *= div;
124 }
void vset ( double *  v,
double  x,
double  y,
double  z 
)

Definition at line 82 of file trackball.cpp.

Referenced by CreateRoutesSection(), and trackball().

83 {
84  v[0] = x;
85  v[1] = y;
86  v[2] = z;
87 }
void vsub ( const double *  src1,
const double *  src2,
double *  dst 
)

Definition at line 89 of file trackball.cpp.

Referenced by trackball().

90 {
91  dst[0] = src1[0] - src2[0];
92  dst[1] = src1[1] - src2[1];
93  dst[2] = src1[2] - src2[2];
94 }
void vzero ( double *  v)

Definition at line 75 of file trackball.cpp.

Referenced by trackball().

76 {
77  v[0] = 0.0;
78  v[1] = 0.0;
79  v[2] = 0.0;
80 }