Debian Codegraph @

cmd/smyrna/arcball.h

raw
1 /** KempoApi: The Turloc Toolkit *****************************/
2 /** * * **/
3 /** ** ** Filename: ArcBall.h **/
4 /** ** Version: Common **/
5 /** ** **/
6 /** **/
7 /** Arcball class for mouse manipulation. **/
8 /** **/
9 /** **/
10 /** **/
11 /** **/
12 /** (C) 1999-2003 Tatewake.com **/
13 /** History: **/
14 /** 08/17/2003 - (TJG) - Creation **/
15 /** 09/23/2003 - (TJG) - Bug fix and optimization **/
16 /** 09/25/2003 - (TJG) - Version for NeHe Basecode users **/
17 /** **/
18 /*************************************************************/
19
20 /*************************************************************************************/
21 /** **/
22 /** Copyright (c) 1999-2009 Tatewake.com **/
23 /** **/
24 /** Permission is hereby granted, free of charge, to any person obtaining a copy **/
25 /** of this software and associated documentation files (the "Software"), to deal **/
26 /** in the Software without restriction, including without limitation the rights **/
27 /** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell **/
28 /** copies of the Software, and to permit persons to whom the Software is **/
29 /** furnished to do so, subject to the following conditions: **/
30 /** **/
31 /** The above copyright notice and this permission notice shall be included in **/
32 /** all copies or substantial portions of the Software. **/
33 /** **/
34 /** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR **/
35 /** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, **/
36 /** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE **/
37 /** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER **/
38 /** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, **/
39 /** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN **/
40 /** THE SOFTWARE. **/
41 /** **/
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
51 typedef struct {
52 float X, Y;
53 } Tuple2fT; //A generic 2-element tuple that is represented by single-precision floating point x,y coordinates.
54
55 typedef struct {
56 float X, Y, Z;
57 } Tuple3fT; //A generic 3-element tuple that is represented by single precision-floating point x,y,z coordinates.
58
59 typedef struct {
60 float X, Y, Z, W;
61 } Tuple4fT; //A 4-element tuple represented by single-precision floating point x,y,z,w coordinates.
62
63 typedef union {
64 float M[9];
65 struct {
66 //column major
67 union {
68 float M00;
69 float XX;
70 float SX;
71 }; //XAxis.X and Scale X
72 union {
73 float M10;
74 float XY;
75 }; //XAxis.Y
76 union {
77 float M20;
78 float XZ;
79 }; //XAxis.Z
80 union {
81 float M01;
82 float YX;
83 }; //YAxis.X
84 union {
85 float M11;
86 float YY;
87 float SY;
88 }; //YAxis.Y and Scale Y
89 union {
90 float M21;
91 float YZ;
92 }; //YAxis.Z
93 union {
94 float M02;
95 float ZX;
96 }; //ZAxis.X
97 union {
98 float M12;
99 float ZY;
100 }; //ZAxis.Y
101 union {
102 float M22;
103 float ZZ;
104 float SZ;
105 }; //ZAxis.Z and Scale Z
106 } s;
107 } Matrix3fT; //A single precision floating point 3 by 3 matrix.
108
109 typedef union {
110 float M[16];
111 struct {
112 float XX;
113 float XY;
114 float XZ;
115 float XW;
116 float YX;
117 float YY;
118 float YZ;
119 float YW;
120 float ZX;
121 float ZY;
122 float ZZ;
123 float 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 //Custom math, or speed overrides
136 #define FuncSqrt sqrtf
137
138 //utility macros
139 //assuming IEEE-754(float), which i believe has max precision of 7 bits
140 # define Epsilon 1.0e-5
141
142 //Math functions
143
144 #ifdef ARCBALL_C
145 /**
146 * Returns a vector that is the vector cross product of vectors v1 and v2.
147 * @param v1 the first vector
148 * @param v2 the second vector
149 */
150
151 static Vector3fT Vector3fCross(Vector3fT v1, Vector3fT v2) {
152 return (Vector3fT){.X = v1.Y * v2.Z - v1.Z * v2.Y,
153 .Y = v1.Z * v2.X - v1.X * v2.Z,
154 .Z = v1.X * v2.Y - v1.Y * v2.X};
155 }
156
157 /**
158 * Computes the dot product of the this vector and vector v1.
159 * @param v1 the other vector
160 */
161
162 static float Vector3fDot(Vector3fT NewObj, Vector3fT v1) {
163 return NewObj.X * v1.X + NewObj.Y * v1.Y + NewObj.Z * v1.Z;
164 }
165
166 /**
167 * Returns the squared length of this vector.
168 * @return the squared length of this vector
169 */
170
171 static float Vector3fLengthSquared(Vector3fT NewObj) {
172 return NewObj.X * NewObj.X + NewObj.Y * NewObj.Y + NewObj.Z * NewObj.Z;
173 }
174
175 /**
176 * Returns the length of this vector.
177 * @return the length of this vector
178 */
179
180 static float Vector3fLength(Vector3fT NewObj) {
181 return FuncSqrt(Vector3fLengthSquared(NewObj));
182 }
183
184 /**
185 * Sets the value of this matrix to the matrix conversion of the
186 * quaternion argument.
187 * @param q1 the quaternion to be converted
188 */
189 //$hack this can be optimized some(if s == 0)
190
191 static Matrix3fT Matrix3fSetRotationFromQuat4f(Quat4fT q1) {
192 float n, s;
193 float xs, ys, zs;
194 float wx, wy, wz;
195 float xx, xy, xz;
196 float yy, yz, zz;
197 Matrix3fT NewObj = {0};
198
199 n = q1.X * q1.X + q1.Y * q1.Y + q1.Z * q1.Z + q1.W * q1.W;
200 s = (n > 0.0f) ? (2.0f / n) : 0.0f;
201
202 xs = q1.X * s;
203 ys = q1.Y * s;
204 zs = q1.Z * s;
205 wx = q1.W * xs;
206 wy = q1.W * ys;
207 wz = q1.W * zs;
208 xx = q1.X * xs;
209 xy = q1.X * ys;
210 xz = q1.X * zs;
211 yy = q1.Y * ys;
212 yz = q1.Y * zs;
213 zz = q1.Z * zs;
214
215 NewObj.s.XX = 1.0f - (yy + zz);
216 NewObj.s.YX = xy - wz;
217 NewObj.s.ZX = xz + wy;
218 NewObj.s.XY = xy + wz;
219 NewObj.s.YY = 1.0f - (xx + zz);
220 NewObj.s.ZY = yz - wx;
221 NewObj.s.XZ = xz - wy;
222 NewObj.s.YZ = yz + wx;
223 NewObj.s.ZZ = 1.0f - (xx + yy);
224 return NewObj;
225 }
226
227 /**
228 * Sets the value of this matrix to the result of multiplying itself
229 * with matrix m1.
230 * @param m1 the other matrix
231 */
232
233 static void Matrix3fMulMatrix3f(Matrix3fT *NewObj, Matrix3fT m1) {
234 Matrix3fT Result; //safe not to initialize
235
236 assert(NewObj);
237
238 // alias-safe way.
239 Result.s.M00 = NewObj->s.M00 * m1.s.M00 + NewObj->s.M01 * m1.s.M10 +
240 NewObj->s.M02 * m1.s.M20;
241 Result.s.M01 = NewObj->s.M00 * m1.s.M01 + NewObj->s.M01 * m1.s.M11 +
242 NewObj->s.M02 * m1.s.M21;
243 Result.s.M02 = NewObj->s.M00 * m1.s.M02 + NewObj->s.M01 * m1.s.M12 +
244 NewObj->s.M02 * m1.s.M22;
245
246 Result.s.M10 = NewObj->s.M10 * m1.s.M00 + NewObj->s.M11 * m1.s.M10 +
247 NewObj->s.M12 * m1.s.M20;
248 Result.s.M11 = NewObj->s.M10 * m1.s.M01 + NewObj->s.M11 * m1.s.M11 +
249 NewObj->s.M12 * m1.s.M21;
250 Result.s.M12 = NewObj->s.M10 * m1.s.M02 + NewObj->s.M11 * m1.s.M12 +
251 NewObj->s.M12 * m1.s.M22;
252
253 Result.s.M20 = NewObj->s.M20 * m1.s.M00 + NewObj->s.M21 * m1.s.M10 +
254 NewObj->s.M22 * m1.s.M20;
255 Result.s.M21 = NewObj->s.M20 * m1.s.M01 + NewObj->s.M21 * m1.s.M11 +
256 NewObj->s.M22 * m1.s.M21;
257 Result.s.M22 = NewObj->s.M20 * m1.s.M02 + NewObj->s.M21 * m1.s.M12 +
258 NewObj->s.M22 * m1.s.M22;
259
260 //copy result back to this
261 *NewObj = Result;
262 }
263
264 /**
265 * Performs SVD on this matrix and gets scale and rotation.
266 * Rotation is placed into rot3, and rot4.
267 * @param rot3 the rotation factor(Matrix3d). if null, ignored
268 * @param rot4 the rotation factor(Matrix4) only upper 3x3 elements are changed. if null, ignored
269 * @return scale factor
270 */
271
272 static float Matrix4fSVD(const Matrix4fT * NewObj, Matrix3fT * rot3,
273 Matrix4fT * rot4)
274 {
275 float s, n;
276
277 assert(NewObj);
278
279 // this is a simple svd.
280 // Not complete but fast and reasonable.
281 // See comment in Matrix3d.
282
283 s = FuncSqrt((NewObj->XX * NewObj->XX +
284 NewObj->XY * NewObj->XY +
285 NewObj->XZ * NewObj->XZ +
286 NewObj->YX * NewObj->YX +
287 NewObj->YY * NewObj->YY +
288 NewObj->YZ * NewObj->YZ +
289 NewObj->ZX * NewObj->ZX +
290 NewObj->ZY * NewObj->ZY +
291 NewObj->ZZ * NewObj->ZZ) / 3.0f);
292
293 if (rot3) //if pointer not null
294 {
295 rot3->s.XX = NewObj->XX;
296 rot3->s.XY = NewObj->XY;
297 rot3->s.XZ = NewObj->XZ;
298 rot3->s.YX = NewObj->YX;
299 rot3->s.YY = NewObj->YY;
300 rot3->s.YZ = NewObj->YZ;
301 rot3->s.ZX = NewObj->ZX;
302 rot3->s.ZY = NewObj->ZY;
303 rot3->s.ZZ = NewObj->ZZ;
304
305 // zero-div may occur.
306
307 n = 1.0f / FuncSqrt(NewObj->XX * NewObj->XX +
308 NewObj->XY * NewObj->XY +
309 NewObj->XZ * NewObj->XZ);
310 rot3->s.XX *= n;
311 rot3->s.XY *= n;
312 rot3->s.XZ *= n;
313
314 n = 1.0f / FuncSqrt(NewObj->YX * NewObj->YX +
315 NewObj->YY * NewObj->YY +
316 NewObj->YZ * NewObj->YZ);
317 rot3->s.YX *= n;
318 rot3->s.YY *= n;
319 rot3->s.YZ *= n;
320
321 n = 1.0f / FuncSqrt(NewObj->ZX * NewObj->ZX +
322 NewObj->ZY * NewObj->ZY +
323 NewObj->ZZ * NewObj->ZZ);
324 rot3->s.ZX *= n;
325 rot3->s.ZY *= n;
326 rot3->s.ZZ *= n;
327 }
328
329 if (rot4) //if pointer not null
330 {
331 *rot4 = *NewObj;
332 // zero-div may occur.
333
334 n = 1.0f / FuncSqrt(NewObj->XX * NewObj->XX +
335 NewObj->XY * NewObj->XY +
336 NewObj->XZ * NewObj->XZ);
337 rot4->XX *= n;
338 rot4->XY *= n;
339 rot4->XZ *= n;
340
341 n = 1.0f / FuncSqrt(NewObj->YX * NewObj->YX +
342 NewObj->YY * NewObj->YY +
343 NewObj->YZ * NewObj->YZ);
344 rot4->YX *= n;
345 rot4->YY *= n;
346 rot4->YZ *= n;
347
348 n = 1.0f / FuncSqrt(NewObj->ZX * NewObj->ZX +
349 NewObj->ZY * NewObj->ZY +
350 NewObj->ZZ * NewObj->ZZ);
351 rot4->ZX *= n;
352 rot4->ZY *= n;
353 rot4->ZZ *= n;
354 }
355
356 return s;
357 }
358
359
360 static void Matrix4fSetRotationScaleFromMatrix3f(Matrix4fT * NewObj,
361 const Matrix3fT * m1)
362 {
363 assert(NewObj && m1);
364
365 NewObj->XX = m1->s.XX;
366 NewObj->YX = m1->s.YX;
367 NewObj->ZX = m1->s.ZX;
368 NewObj->XY = m1->s.XY;
369 NewObj->YY = m1->s.YY;
370 NewObj->ZY = m1->s.ZY;
371 NewObj->XZ = m1->s.XZ;
372 NewObj->YZ = m1->s.YZ;
373 NewObj->ZZ = m1->s.ZZ;
374 }
375
376
377 static void Matrix4fMulRotationScale(Matrix4fT * NewObj, float scale)
378 {
379 assert(NewObj);
380
381 NewObj->XX *= scale;
382 NewObj->YX *= scale;
383 NewObj->ZX *= scale;
384 NewObj->XY *= scale;
385 NewObj->YY *= scale;
386 NewObj->ZY *= scale;
387 NewObj->XZ *= scale;
388 NewObj->YZ *= scale;
389 NewObj->ZZ *= scale;
390 }
391
392 /**
393 * Sets the rotational component (upper 3x3) of this matrix to the matrix
394 * values in the T precision Matrix3d argument; the other elements of
395 * this matrix are unchanged; a singular value decomposition is performed
396 * on this object's upper 3x3 matrix to factor out the scale, then this
397 * object's upper 3x3 matrix components are replaced by the passed rotation
398 * components, and then the scale is reapplied to the rotational
399 * components.
400 * @param m1 T precision 3x3 matrix
401 */
402
403 static void Matrix4fSetRotationFromMatrix3f(Matrix4fT * NewObj,
404 const Matrix3fT * m1)
405 {
406 float scale;
407
408 assert(NewObj && m1);
409
410 scale = Matrix4fSVD(NewObj, NULL, NULL);
411
412 Matrix4fSetRotationScaleFromMatrix3f(NewObj, m1);
413 Matrix4fMulRotationScale(NewObj, scale);
414 }
415 #endif
416
417 // 8<--Snip here if you have your own math types/funcs-->8
418 struct _ArcBall_t {
419 Vector3fT StVec;
420 Vector3fT EnVec;
421 float AdjustWidth;
422 float AdjustHeight;
423 Matrix4fT Transform;
424 Matrix3fT LastRot;
425 Matrix3fT ThisRot;
426 Point2fT MousePt;
427 int isDragging;
428 };
429
430 ArcBall_t init_arcBall(float NewWidth, float NewHeight);
431 void arcmouseClick(void);
432 void arcmouseDrag(void);