Graphviz 14.1.4~dev.20260320.0055
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 struct {
52 double X, Y;
53} Tuple2fT; //A generic 2-element tuple that is represented by single-precision floating point x,y coordinates.
54
55typedef struct {
56 double X, Y, Z;
57} Tuple3fT; //A generic 3-element tuple that is represented by single precision-floating point x,y,z coordinates.
58
59typedef struct {
60 double X, Y, Z, W;
61} Tuple4fT; //A 4-element tuple represented by single-precision floating point x,y,z,w coordinates.
62
63typedef union {
64 double M[9];
65 struct {
66 //column major
67 union {
68 double M00;
69 double XX;
70 double SX;
71 }; //XAxis.X and Scale X
72 union {
73 double M10;
74 double XY;
75 }; //XAxis.Y
76 union {
77 double M20;
78 double XZ;
79 }; //XAxis.Z
80 union {
81 double M01;
82 double YX;
83 }; //YAxis.X
84 union {
85 double M11;
86 double YY;
87 double SY;
88 }; //YAxis.Y and Scale Y
89 union {
90 double M21;
91 double YZ;
92 }; //YAxis.Z
93 union {
94 double M02;
95 double ZX;
96 }; //ZAxis.X
97 union {
98 double M12;
99 double ZY;
100 }; //ZAxis.Y
101 union {
102 double M22;
103 double ZZ;
104 double SZ;
105 }; //ZAxis.Z and Scale Z
106 } s;
107} Matrix3fT; //A single precision floating point 3 by 3 matrix.
108
109typedef union {
110 double M[16];
111 struct {
112 double XX;
113 double XY;
114 double XZ;
115 double XW;
116 double YX;
117 double YY;
118 double YZ;
119 double YW;
120 double ZX;
121 double ZY;
122 double ZZ;
123 double ZW;
124 };
125} Matrix4fT; //A single precision floating point 4 by 4 matrix.
126
127
128//"Inherited" types
129#define Point2fT Tuple2fT //A 2 element point that is represented by single precision floating point x,y coordinates.
130
131#define Quat4fT Tuple4fT //A 4 element unit quaternion represented by single precision floating point x,y,z,w coordinates.
132
133#define Vector3fT Tuple3fT //A 3-element vector that is represented by single-precision floating point x,y,z coordinates.
134
135//utility macros
136//assuming IEEE-754(float), which i believe has max precision of 7 bits
137# define Epsilon 1.0e-5
138
139//Math functions
140
141#ifdef ARCBALL_C
148static Vector3fT Vector3fCross(Vector3fT v1, Vector3fT v2) {
149 return (Vector3fT){.X = v1.Y * v2.Z - v1.Z * v2.Y,
150 .Y = v1.Z * v2.X - v1.X * v2.Z,
151 .Z = v1.X * v2.Y - v1.Y * v2.X};
152}
153
159static double Vector3fDot(Vector3fT NewObj, Vector3fT v1) {
160 return NewObj.X * v1.X + NewObj.Y * v1.Y + NewObj.Z * v1.Z;
161}
162
168static double Vector3fLengthSquared(Vector3fT NewObj) {
169 return NewObj.X * NewObj.X + NewObj.Y * NewObj.Y + NewObj.Z * NewObj.Z;
170}
171
177static double Vector3fLength(Vector3fT NewObj) {
178 return sqrt(Vector3fLengthSquared(NewObj));
179}
180
186 //$hack this can be optimized some(if s == 0)
187
188static Matrix3fT Matrix3fSetRotationFromQuat4f(Quat4fT q1) {
189 double n, s;
190 double xs, ys, zs;
191 double wx, wy, wz;
192 double xx, xy, xz;
193 double yy, yz, zz;
194 Matrix3fT NewObj = {0};
195
196 n = q1.X * q1.X + q1.Y * q1.Y + q1.Z * q1.Z + q1.W * q1.W;
197 s = (n > 0) ? (2.0 / n) : 0;
198
199 xs = q1.X * s;
200 ys = q1.Y * s;
201 zs = q1.Z * s;
202 wx = q1.W * xs;
203 wy = q1.W * ys;
204 wz = q1.W * zs;
205 xx = q1.X * xs;
206 xy = q1.X * ys;
207 xz = q1.X * zs;
208 yy = q1.Y * ys;
209 yz = q1.Y * zs;
210 zz = q1.Z * zs;
211
212 NewObj.s.XX = 1.0 - (yy + zz);
213 NewObj.s.YX = xy - wz;
214 NewObj.s.ZX = xz + wy;
215 NewObj.s.XY = xy + wz;
216 NewObj.s.YY = 1.0 - (xx + zz);
217 NewObj.s.ZY = yz - wx;
218 NewObj.s.XZ = xz - wy;
219 NewObj.s.YZ = yz + wx;
220 NewObj.s.ZZ = 1.0 - (xx + yy);
221 return NewObj;
222}
223
230static void Matrix3fMulMatrix3f(Matrix3fT *NewObj, Matrix3fT m1) {
231 Matrix3fT Result; //safe not to initialize
232
233 assert(NewObj);
234
235 // alias-safe way.
236 Result.s.M00 = NewObj->s.M00 * m1.s.M00 + NewObj->s.M01 * m1.s.M10 +
237 NewObj->s.M02 * m1.s.M20;
238 Result.s.M01 = NewObj->s.M00 * m1.s.M01 + NewObj->s.M01 * m1.s.M11 +
239 NewObj->s.M02 * m1.s.M21;
240 Result.s.M02 = NewObj->s.M00 * m1.s.M02 + NewObj->s.M01 * m1.s.M12 +
241 NewObj->s.M02 * m1.s.M22;
242
243 Result.s.M10 = NewObj->s.M10 * m1.s.M00 + NewObj->s.M11 * m1.s.M10 +
244 NewObj->s.M12 * m1.s.M20;
245 Result.s.M11 = NewObj->s.M10 * m1.s.M01 + NewObj->s.M11 * m1.s.M11 +
246 NewObj->s.M12 * m1.s.M21;
247 Result.s.M12 = NewObj->s.M10 * m1.s.M02 + NewObj->s.M11 * m1.s.M12 +
248 NewObj->s.M12 * m1.s.M22;
249
250 Result.s.M20 = NewObj->s.M20 * m1.s.M00 + NewObj->s.M21 * m1.s.M10 +
251 NewObj->s.M22 * m1.s.M20;
252 Result.s.M21 = NewObj->s.M20 * m1.s.M01 + NewObj->s.M21 * m1.s.M11 +
253 NewObj->s.M22 * m1.s.M21;
254 Result.s.M22 = NewObj->s.M20 * m1.s.M02 + NewObj->s.M21 * m1.s.M12 +
255 NewObj->s.M22 * m1.s.M22;
256
257 //copy result back to this
258 *NewObj = Result;
259}
260
269static double Matrix4fSVD(const Matrix4fT * NewObj, Matrix3fT * rot3,
270 Matrix4fT * rot4)
271{
272 double s, n;
273
274 assert(NewObj);
275
276 // this is a simple svd.
277 // Not complete but fast and reasonable.
278 // See comment in Matrix3d.
279
280 s = sqrt((NewObj->XX * NewObj->XX +
281 NewObj->XY * NewObj->XY +
282 NewObj->XZ * NewObj->XZ +
283 NewObj->YX * NewObj->YX +
284 NewObj->YY * NewObj->YY +
285 NewObj->YZ * NewObj->YZ +
286 NewObj->ZX * NewObj->ZX +
287 NewObj->ZY * NewObj->ZY +
288 NewObj->ZZ * NewObj->ZZ) / 3.0);
289
290 if (rot3) //if pointer not null
291 {
292 rot3->s.XX = NewObj->XX;
293 rot3->s.XY = NewObj->XY;
294 rot3->s.XZ = NewObj->XZ;
295 rot3->s.YX = NewObj->YX;
296 rot3->s.YY = NewObj->YY;
297 rot3->s.YZ = NewObj->YZ;
298 rot3->s.ZX = NewObj->ZX;
299 rot3->s.ZY = NewObj->ZY;
300 rot3->s.ZZ = NewObj->ZZ;
301
302 // zero-div may occur.
303
304 n = 1.0 / sqrt(NewObj->XX * NewObj->XX +
305 NewObj->XY * NewObj->XY +
306 NewObj->XZ * NewObj->XZ);
307 rot3->s.XX *= n;
308 rot3->s.XY *= n;
309 rot3->s.XZ *= n;
310
311 n = 1.0 / sqrt(NewObj->YX * NewObj->YX +
312 NewObj->YY * NewObj->YY +
313 NewObj->YZ * NewObj->YZ);
314 rot3->s.YX *= n;
315 rot3->s.YY *= n;
316 rot3->s.YZ *= n;
317
318 n = 1.0 / sqrt(NewObj->ZX * NewObj->ZX +
319 NewObj->ZY * NewObj->ZY +
320 NewObj->ZZ * NewObj->ZZ);
321 rot3->s.ZX *= n;
322 rot3->s.ZY *= n;
323 rot3->s.ZZ *= n;
324 }
325
326 if (rot4) //if pointer not null
327 {
328 *rot4 = *NewObj;
329 // zero-div may occur.
330
331 n = 1.0 / sqrt(NewObj->XX * NewObj->XX +
332 NewObj->XY * NewObj->XY +
333 NewObj->XZ * NewObj->XZ);
334 rot4->XX *= n;
335 rot4->XY *= n;
336 rot4->XZ *= n;
337
338 n = 1.0 / sqrt(NewObj->YX * NewObj->YX +
339 NewObj->YY * NewObj->YY +
340 NewObj->YZ * NewObj->YZ);
341 rot4->YX *= n;
342 rot4->YY *= n;
343 rot4->YZ *= n;
344
345 n = 1.0 / sqrt(NewObj->ZX * NewObj->ZX +
346 NewObj->ZY * NewObj->ZY +
347 NewObj->ZZ * NewObj->ZZ);
348 rot4->ZX *= n;
349 rot4->ZY *= n;
350 rot4->ZZ *= n;
351 }
352
353 return s;
354}
355
356
357static void Matrix4fSetRotationScaleFromMatrix3f(Matrix4fT * NewObj,
358 const Matrix3fT * m1)
359{
360 assert(NewObj && m1);
361
362 NewObj->XX = m1->s.XX;
363 NewObj->YX = m1->s.YX;
364 NewObj->ZX = m1->s.ZX;
365 NewObj->XY = m1->s.XY;
366 NewObj->YY = m1->s.YY;
367 NewObj->ZY = m1->s.ZY;
368 NewObj->XZ = m1->s.XZ;
369 NewObj->YZ = m1->s.YZ;
370 NewObj->ZZ = m1->s.ZZ;
371}
372
373
374static void Matrix4fMulRotationScale(Matrix4fT *NewObj, double scale) {
375 assert(NewObj);
376
377 NewObj->XX *= scale;
378 NewObj->YX *= scale;
379 NewObj->ZX *= scale;
380 NewObj->XY *= scale;
381 NewObj->YY *= scale;
382 NewObj->ZY *= scale;
383 NewObj->XZ *= scale;
384 NewObj->YZ *= scale;
385 NewObj->ZZ *= scale;
386}
387
399static void Matrix4fSetRotationFromMatrix3f(Matrix4fT * NewObj,
400 const Matrix3fT * m1)
401{
402 double scale;
403
404 assert(NewObj && m1);
405
406 scale = Matrix4fSVD(NewObj, NULL, NULL);
407
408 Matrix4fSetRotationScaleFromMatrix3f(NewObj, m1);
409 Matrix4fMulRotationScale(NewObj, scale);
410}
411#endif
412
413// 8<--Snip here if you have your own math types/funcs-->8
425
426ArcBall_t init_arcBall(double NewWidth, double NewHeight);
427void arcmouseClick(void);
428void arcmouseDrag(void);
#define Quat4fT
Definition arcball.h:131
void arcmouseClick(void)
Definition arcball.c:126
void arcmouseDrag(void)
Definition arcball.c:134
ArcBall_t init_arcBall(double NewWidth, double NewHeight)
Definition arcball.c:85
#define Vector3fT
Definition arcball.h:133
#define Point2fT
Definition arcball.h:129
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
static WUR pointf scale(double c, pointf p)
Definition geomprocs.h:148
node NULL
Definition grammar.y:181
#define M
Definition randomkit.c:92
double X
Definition arcball.h:52
double X
Definition arcball.h:56
double W
Definition arcball.h:60
double AdjustHeight
Definition arcball.h:418
Vector3fT EnVec
Definition arcball.h:416
Point2fT MousePt
Definition arcball.h:422
Matrix3fT LastRot
Definition arcball.h:420
double AdjustWidth
Definition arcball.h:417
Matrix3fT ThisRot
Definition arcball.h:421
Matrix4fT Transform
Definition arcball.h:419
int isDragging
Definition arcball.h:423
Vector3fT StVec
Definition arcball.h:415
double ZZ
Definition arcball.h:103
double XX
Definition arcball.h:69
double M00
Definition arcball.h:68
double M01
Definition arcball.h:81
double SZ
Definition arcball.h:104
double ZX
Definition arcball.h:95
double M22
Definition arcball.h:102
double M20
Definition arcball.h:77
struct Matrix3fT::@0 s
double YZ
Definition arcball.h:91
double YY
Definition arcball.h:86
double M10
Definition arcball.h:73
double XY
Definition arcball.h:74
double ZY
Definition arcball.h:99
double YX
Definition arcball.h:82
double SY
Definition arcball.h:87
double M02
Definition arcball.h:94
double SX
Definition arcball.h:70
double M11
Definition arcball.h:85
double XZ
Definition arcball.h:78
double M21
Definition arcball.h:90
double M12
Definition arcball.h:98
double ZX
Definition arcball.h:120
double YY
Definition arcball.h:117
double YX
Definition arcball.h:116
double XW
Definition arcball.h:115
double YZ
Definition arcball.h:118
double ZW
Definition arcball.h:123
double XX
Definition arcball.h:112
double ZZ
Definition arcball.h:122
double XZ
Definition arcball.h:114
double YW
Definition arcball.h:119
double ZY
Definition arcball.h:121
double XY
Definition arcball.h:113
Definition grammar.c:90