Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
arcball.h
Go to the documentation of this file.
1
18/*************************************************************/
19
20/*************************************************************************************/
42/*************************************************************************************/
43
44#pragma once
45
46// 8<--Snip here if you have your own math types/funcs-->8
47
48# include <assert.h>
49
50//Math types derived from the KempoApi tMath library
51typedef union {
52 struct {
53 float X, Y;
54 } s;
55
56 float T[2];
57} Tuple2fT; //A generic 2-element tuple that is represented by single-precision floating point x,y coordinates.
58
59typedef union {
60 struct {
61 float X, Y, Z;
62 } s;
63
64 float T[3];
65} Tuple3fT; //A generic 3-element tuple that is represented by single precision-floating point x,y,z coordinates.
66
67typedef union {
68 struct {
69 float X, Y, Z, W;
70 } s;
71
72 float T[4];
73} Tuple4fT; //A 4-element tuple represented by single-precision floating point x,y,z,w coordinates.
74
75typedef union {
76 float M[9];
77 struct {
78 //column major
79 union {
80 float M00;
81 float XX;
82 float SX;
83 }; //XAxis.X and Scale X
84 union {
85 float M10;
86 float XY;
87 }; //XAxis.Y
88 union {
89 float M20;
90 float XZ;
91 }; //XAxis.Z
92 union {
93 float M01;
94 float YX;
95 }; //YAxis.X
96 union {
97 float M11;
98 float YY;
99 float SY;
100 }; //YAxis.Y and Scale Y
101 union {
102 float M21;
103 float YZ;
104 }; //YAxis.Z
105 union {
106 float M02;
107 float ZX;
108 }; //ZAxis.X
109 union {
110 float M12;
111 float ZY;
112 }; //ZAxis.Y
113 union {
114 float M22;
115 float ZZ;
116 float SZ;
117 }; //ZAxis.Z and Scale Z
118 } s;
119} Matrix3fT; //A single precision floating point 3 by 3 matrix.
120
121typedef union {
122 float M[16];
123 struct {
124 //column major
125 union {
126 float M00;
127 float XX;
128 float SX;
129 }; //XAxis.X and Scale X
130 union {
131 float M10;
132 float XY;
133 }; //XAxis.Y
134 union {
135 float M20;
136 float XZ;
137 }; //XAxis.Z
138 union {
139 float M30;
140 float XW;
141 }; //XAxis.W
142 union {
143 float M01;
144 float YX;
145 }; //YAxis.X
146 union {
147 float M11;
148 float YY;
149 float SY;
150 }; //YAxis.Y and Scale Y
151 union {
152 float M21;
153 float YZ;
154 }; //YAxis.Z
155 union {
156 float M31;
157 float YW;
158 }; //YAxis.W
159 union {
160 float M02;
161 float ZX;
162 }; //ZAxis.X
163 union {
164 float M12;
165 float ZY;
166 }; //ZAxis.Y
167 union {
168 float M22;
169 float ZZ;
170 float SZ;
171 }; //ZAxis.Z and Scale Z
172 union {
173 float M32;
174 float ZW;
175 }; //ZAxis.W
176 union {
177 float M03;
178 float TX;
179 }; //Trans.X
180 union {
181 float M13;
182 float TY;
183 }; //Trans.Y
184 union {
185 float M23;
186 float TZ;
187 }; //Trans.Z
188 union {
189 float M33;
190 float TW;
191 float SW;
192 }; //Trans.W and Scale W
193 } s;
194} Matrix4fT; //A single precision floating point 4 by 4 matrix.
195
196
197//"Inherited" types
198#define Point2fT Tuple2fT //A 2 element point that is represented by single precision floating point x,y coordinates.
199
200#define Quat4fT Tuple4fT //A 4 element unit quaternion represented by single precision floating point x,y,z,w coordinates.
201
202#define Vector2fT Tuple2fT //A 2-element vector that is represented by single-precision floating point x,y coordinates.
203#define Vector3fT Tuple3fT //A 3-element vector that is represented by single-precision floating point x,y,z coordinates.
204
205//Custom math, or speed overrides
206#define FuncSqrt sqrtf
207
208//utility macros
209//assuming IEEE-754(float), which i believe has max precision of 7 bits
210# define Epsilon 1.0e-5
211
212//Math functions
213
214#ifdef ARCBALL_C
221static void Vector3fCross(Vector3fT * NewObj, const Vector3fT * v1,
222 const Vector3fT * v2)
223{
224 Vector3fT Result; //safe not to initialize
225
226 assert(NewObj && v1 && v2);
227
228 // store on stack once for aliasing-safty
229 // i.e. safe when a.cross(a, b)
230
231 Result.s.X = (v1->s.Y * v2->s.Z) - (v1->s.Z * v2->s.Y);
232 Result.s.Y = (v1->s.Z * v2->s.X) - (v1->s.X * v2->s.Z);
233 Result.s.Z = (v1->s.X * v2->s.Y) - (v1->s.Y * v2->s.X);
234
235 //copy result back
236 *NewObj = Result;
237}
238
244static float Vector3fDot(const Vector3fT * NewObj, const Vector3fT * v1)
245{
246 assert(NewObj && v1);
247
248 return (NewObj->s.X * v1->s.X) +
249 (NewObj->s.Y * v1->s.Y) + (NewObj->s.Z * v1->s.Z);
250}
251
257static float Vector3fLengthSquared(const Vector3fT * NewObj)
258{
259 assert(NewObj);
260
261 return (NewObj->s.X * NewObj->s.X) +
262 (NewObj->s.Y * NewObj->s.Y) + (NewObj->s.Z * NewObj->s.Z);
263}
264
270static float Vector3fLength(const Vector3fT * NewObj)
271{
272 assert(NewObj);
273
274 return FuncSqrt(Vector3fLengthSquared(NewObj));
275}
276
282 //$hack this can be optimized some(if s == 0)
283
284static void Matrix3fSetRotationFromQuat4f(Matrix3fT * NewObj,
285 const Quat4fT * q1)
286{
287 float n, s;
288 float xs, ys, zs;
289 float wx, wy, wz;
290 float xx, xy, xz;
291 float yy, yz, zz;
292
293 assert(NewObj && q1);
294
295 n = (q1->s.X * q1->s.X) + (q1->s.Y * q1->s.Y) + (q1->s.Z * q1->s.Z) +
296 (q1->s.W * q1->s.W);
297 s = (n > 0.0f) ? (2.0f / n) : 0.0f;
298
299 xs = q1->s.X * s;
300 ys = q1->s.Y * s;
301 zs = q1->s.Z * s;
302 wx = q1->s.W * xs;
303 wy = q1->s.W * ys;
304 wz = q1->s.W * zs;
305 xx = q1->s.X * xs;
306 xy = q1->s.X * ys;
307 xz = q1->s.X * zs;
308 yy = q1->s.Y * ys;
309 yz = q1->s.Y * zs;
310 zz = q1->s.Z * zs;
311
312 NewObj->s.XX = 1.0f - (yy + zz);
313 NewObj->s.YX = xy - wz;
314 NewObj->s.ZX = xz + wy;
315 NewObj->s.XY = xy + wz;
316 NewObj->s.YY = 1.0f - (xx + zz);
317 NewObj->s.ZY = yz - wx;
318 NewObj->s.XZ = xz - wy;
319 NewObj->s.YZ = yz + wx;
320 NewObj->s.ZZ = 1.0f - (xx + yy);
321}
322
329static void Matrix3fMulMatrix3f(Matrix3fT * NewObj, const Matrix3fT * m1)
330{
331 Matrix3fT Result; //safe not to initialize
332
333 assert(NewObj && m1);
334
335 // alias-safe way.
336 Result.s.M00 =
337 (NewObj->s.M00 * m1->s.M00) + (NewObj->s.M01 * m1->s.M10) +
338 (NewObj->s.M02 * m1->s.M20);
339 Result.s.M01 =
340 (NewObj->s.M00 * m1->s.M01) + (NewObj->s.M01 * m1->s.M11) +
341 (NewObj->s.M02 * m1->s.M21);
342 Result.s.M02 =
343 (NewObj->s.M00 * m1->s.M02) + (NewObj->s.M01 * m1->s.M12) +
344 (NewObj->s.M02 * m1->s.M22);
345
346 Result.s.M10 =
347 (NewObj->s.M10 * m1->s.M00) + (NewObj->s.M11 * m1->s.M10) +
348 (NewObj->s.M12 * m1->s.M20);
349 Result.s.M11 =
350 (NewObj->s.M10 * m1->s.M01) + (NewObj->s.M11 * m1->s.M11) +
351 (NewObj->s.M12 * m1->s.M21);
352 Result.s.M12 =
353 (NewObj->s.M10 * m1->s.M02) + (NewObj->s.M11 * m1->s.M12) +
354 (NewObj->s.M12 * m1->s.M22);
355
356 Result.s.M20 =
357 (NewObj->s.M20 * m1->s.M00) + (NewObj->s.M21 * m1->s.M10) +
358 (NewObj->s.M22 * m1->s.M20);
359 Result.s.M21 =
360 (NewObj->s.M20 * m1->s.M01) + (NewObj->s.M21 * m1->s.M11) +
361 (NewObj->s.M22 * m1->s.M21);
362 Result.s.M22 =
363 (NewObj->s.M20 * m1->s.M02) + (NewObj->s.M21 * m1->s.M12) +
364 (NewObj->s.M22 * m1->s.M22);
365
366 //copy result back to this
367 *NewObj = Result;
368}
369
370
371static void Matrix4fSetRotationScaleFromMatrix4f(Matrix4fT * NewObj,
372 const Matrix4fT * m1)
373{
374 assert(NewObj && m1);
375
376 NewObj->s.XX = m1->s.XX;
377 NewObj->s.YX = m1->s.YX;
378 NewObj->s.ZX = m1->s.ZX;
379 NewObj->s.XY = m1->s.XY;
380 NewObj->s.YY = m1->s.YY;
381 NewObj->s.ZY = m1->s.ZY;
382 NewObj->s.XZ = m1->s.XZ;
383 NewObj->s.YZ = m1->s.YZ;
384 NewObj->s.ZZ = m1->s.ZZ;
385}
386
395static float Matrix4fSVD(const Matrix4fT * NewObj, Matrix3fT * rot3,
396 Matrix4fT * rot4)
397{
398 float s, n;
399
400 assert(NewObj);
401
402 // this is a simple svd.
403 // Not complete but fast and reasonable.
404 // See comment in Matrix3d.
405
406 s = FuncSqrt(((NewObj->s.XX * NewObj->s.XX) +
407 (NewObj->s.XY * NewObj->s.XY) +
408 (NewObj->s.XZ * NewObj->s.XZ) +
409 (NewObj->s.YX * NewObj->s.YX) +
410 (NewObj->s.YY * NewObj->s.YY) +
411 (NewObj->s.YZ * NewObj->s.YZ) +
412 (NewObj->s.ZX * NewObj->s.ZX) +
413 (NewObj->s.ZY * NewObj->s.ZY) +
414 (NewObj->s.ZZ * NewObj->s.ZZ)) / 3.0f);
415
416 if (rot3) //if pointer not null
417 {
418 rot3->s.XX = NewObj->s.XX;
419 rot3->s.XY = NewObj->s.XY;
420 rot3->s.XZ = NewObj->s.XZ;
421 rot3->s.YX = NewObj->s.YX;
422 rot3->s.YY = NewObj->s.YY;
423 rot3->s.YZ = NewObj->s.YZ;
424 rot3->s.ZX = NewObj->s.ZX;
425 rot3->s.ZY = NewObj->s.ZY;
426 rot3->s.ZZ = NewObj->s.ZZ;
427
428 // zero-div may occur.
429
430 n = 1.0f / FuncSqrt((NewObj->s.XX * NewObj->s.XX) +
431 (NewObj->s.XY * NewObj->s.XY) +
432 (NewObj->s.XZ * NewObj->s.XZ));
433 rot3->s.XX *= n;
434 rot3->s.XY *= n;
435 rot3->s.XZ *= n;
436
437 n = 1.0f / FuncSqrt((NewObj->s.YX * NewObj->s.YX) +
438 (NewObj->s.YY * NewObj->s.YY) +
439 (NewObj->s.YZ * NewObj->s.YZ));
440 rot3->s.YX *= n;
441 rot3->s.YY *= n;
442 rot3->s.YZ *= n;
443
444 n = 1.0f / FuncSqrt((NewObj->s.ZX * NewObj->s.ZX) +
445 (NewObj->s.ZY * NewObj->s.ZY) +
446 (NewObj->s.ZZ * NewObj->s.ZZ));
447 rot3->s.ZX *= n;
448 rot3->s.ZY *= n;
449 rot3->s.ZZ *= n;
450 }
451
452 if (rot4) //if pointer not null
453 {
454 if (rot4 != NewObj) {
455 Matrix4fSetRotationScaleFromMatrix4f(rot4, NewObj); // private method
456 }
457 // zero-div may occur.
458
459 n = 1.0f / FuncSqrt((NewObj->s.XX * NewObj->s.XX) +
460 (NewObj->s.XY * NewObj->s.XY) +
461 (NewObj->s.XZ * NewObj->s.XZ));
462 rot4->s.XX *= n;
463 rot4->s.XY *= n;
464 rot4->s.XZ *= n;
465
466 n = 1.0f / FuncSqrt((NewObj->s.YX * NewObj->s.YX) +
467 (NewObj->s.YY * NewObj->s.YY) +
468 (NewObj->s.YZ * NewObj->s.YZ));
469 rot4->s.YX *= n;
470 rot4->s.YY *= n;
471 rot4->s.YZ *= n;
472
473 n = 1.0f / FuncSqrt((NewObj->s.ZX * NewObj->s.ZX) +
474 (NewObj->s.ZY * NewObj->s.ZY) +
475 (NewObj->s.ZZ * NewObj->s.ZZ));
476 rot4->s.ZX *= n;
477 rot4->s.ZY *= n;
478 rot4->s.ZZ *= n;
479 }
480
481 return s;
482}
483
484
485static void Matrix4fSetRotationScaleFromMatrix3f(Matrix4fT * NewObj,
486 const Matrix3fT * m1)
487{
488 assert(NewObj && m1);
489
490 NewObj->s.XX = m1->s.XX;
491 NewObj->s.YX = m1->s.YX;
492 NewObj->s.ZX = m1->s.ZX;
493 NewObj->s.XY = m1->s.XY;
494 NewObj->s.YY = m1->s.YY;
495 NewObj->s.ZY = m1->s.ZY;
496 NewObj->s.XZ = m1->s.XZ;
497 NewObj->s.YZ = m1->s.YZ;
498 NewObj->s.ZZ = m1->s.ZZ;
499}
500
501
502static void Matrix4fMulRotationScale(Matrix4fT * NewObj, float scale)
503{
504 assert(NewObj);
505
506 NewObj->s.XX *= scale;
507 NewObj->s.YX *= scale;
508 NewObj->s.ZX *= scale;
509 NewObj->s.XY *= scale;
510 NewObj->s.YY *= scale;
511 NewObj->s.ZY *= scale;
512 NewObj->s.XZ *= scale;
513 NewObj->s.YZ *= scale;
514 NewObj->s.ZZ *= scale;
515}
516
528static void Matrix4fSetRotationFromMatrix3f(Matrix4fT * NewObj,
529 const Matrix3fT * m1)
530{
531 float scale;
532
533 assert(NewObj && m1);
534
535 scale = Matrix4fSVD(NewObj, NULL, NULL);
536
537 Matrix4fSetRotationScaleFromMatrix3f(NewObj, m1);
538 Matrix4fMulRotationScale(NewObj, scale);
539}
540#endif
541
542// 8<--Snip here if you have your own math types/funcs-->8
556
557
558void init_arcBall(ArcBall_t * a, float NewWidth, float NewHeight);
559void arcmouseClick(void);
560void arcmouseDrag(void);
#define Quat4fT
Definition arcball.h:200
void arcmouseClick(void)
Definition arcball.c:142
void arcmouseDrag(void)
Definition arcball.c:150
void init_arcBall(ArcBall_t *a, float NewWidth, float NewHeight)
Definition arcball.c:88
#define FuncSqrt
Definition arcball.h:206
#define Vector3fT
Definition arcball.h:203
#define Point2fT
Definition arcball.h:198
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
static pointf scale(double c, pointf p)
Definition geomprocs.h:130
node NULL
Definition grammar.y:163
#define M
Definition randomkit.c:90
Vector3fT EnVec
Definition arcball.h:545
Point2fT MousePt
Definition arcball.h:551
Matrix3fT LastRot
Definition arcball.h:549
Matrix3fT ThisRot
Definition arcball.h:550
Matrix4fT Transform
Definition arcball.h:548
float AdjustWidth
Definition arcball.h:546
int isDragging
Definition arcball.h:554
int isClicked
Definition arcball.h:552
float AdjustHeight
Definition arcball.h:547
int isRClicked
Definition arcball.h:553
Vector3fT StVec
Definition arcball.h:544
static mytime_t T
Definition timing.c:41
float YY
Definition arcball.h:98
float M02
Definition arcball.h:106
float YZ
Definition arcball.h:103
struct Matrix3fT::@3 s
float M01
Definition arcball.h:93
float SX
Definition arcball.h:82
float XX
Definition arcball.h:81
float M10
Definition arcball.h:85
float SY
Definition arcball.h:99
float M21
Definition arcball.h:102
float M00
Definition arcball.h:80
float ZY
Definition arcball.h:111
float M22
Definition arcball.h:114
float M20
Definition arcball.h:89
float XY
Definition arcball.h:86
float M12
Definition arcball.h:110
float SZ
Definition arcball.h:116
float XZ
Definition arcball.h:90
float ZZ
Definition arcball.h:115
float M11
Definition arcball.h:97
float ZX
Definition arcball.h:107
float YX
Definition arcball.h:94
float ZX
Definition arcball.h:161
float M00
Definition arcball.h:126
float XX
Definition arcball.h:127
float M02
Definition arcball.h:160
float M13
Definition arcball.h:181
float M01
Definition arcball.h:143
float XW
Definition arcball.h:140
struct Matrix4fT::@22 s
float SY
Definition arcball.h:149
float ZY
Definition arcball.h:165
float M32
Definition arcball.h:173
float TZ
Definition arcball.h:186
float TX
Definition arcball.h:178
float XY
Definition arcball.h:132
float SX
Definition arcball.h:128
float YW
Definition arcball.h:157
float SW
Definition arcball.h:191
float M12
Definition arcball.h:164
float M03
Definition arcball.h:177
float M33
Definition arcball.h:189
float M10
Definition arcball.h:131
float YX
Definition arcball.h:144
float M21
Definition arcball.h:152
float ZW
Definition arcball.h:174
float M31
Definition arcball.h:156
float M11
Definition arcball.h:147
float M30
Definition arcball.h:139
float M22
Definition arcball.h:168
float SZ
Definition arcball.h:170
float XZ
Definition arcball.h:136
float TW
Definition arcball.h:190
float M20
Definition arcball.h:135
float TY
Definition arcball.h:182
float YY
Definition arcball.h:148
float ZZ
Definition arcball.h:169
float YZ
Definition arcball.h:153
float M23
Definition arcball.h:185
float X
Definition arcball.h:53
float X
Definition arcball.h:61
float W
Definition arcball.h:69
Definition grammar.c:93