Debian Codegraph @

common/maths/vecmat.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 /*
9 *
10 * C version of vecmat library
11 *
12 */
13
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <math.h> // for sqrt
17
18 #include "d_construct.h"
19 #include "maths.h"
20 #include "vecmat.h"
21 #include "dxxerror.h"
22
23 namespace dcx {
24
25 //#define USE_ISQRT 1
26
27 constexpr vms_matrix vmd_identity_matrix{IDENTITY_MATRIX};
28 constexpr vms_vector vmd_zero_vector{};
29
30 namespace {
31
32 [[nodiscard]]
33 vms_vector vm_vec_build_divide(const vms_vector &src, const fix m)
34 {
35 return vms_vector{
36 .x = fixdiv({src.x}, {m}),
37 .y = fixdiv({src.y}, {m}),
38 .z = fixdiv({src.z}, {m}),
39 };
40 }
41
42 static void vm_vector_to_matrix_f(vms_matrix &m)
43 {
44 if (m.fvec.x == 0 && m.fvec.z == 0) { //forward vec is straight up or down
45 m.rvec = vms_vector{
46 .x = F1_0,
47 .y = 0,
48 .z = 0
49 };
50 m.uvec = vms_vector{
51 .x = 0,
52 .y = 0,
53 .z = (m.fvec.y < 0) ? F1_0 : -F1_0
54 };
55 }
56 else { //not straight up or down
57 m.rvec = vms_vector{
58 .x = m.fvec.z,
59 .y = 0,
60 .z = -m.fvec.x
61 };
62 vm_vec_normalize(m.rvec);
63 reconstruct_at(m.uvec, vm_vec_cross, m.fvec, m.rvec);
64 }
65 }
66
67 static constexpr fix avg_fix(fix64 a, fix64 b)
68 {
69 return (a + b) / 2;
70 }
71
72 }
73
74 //adds two vectors, fills in dest, returns ptr to dest
75 //ok for dest to equal either source, but should use vm_vec_add2() if so
76 vms_vector &vm_vec_add(vms_vector &dest,const vms_vector &src0,const vms_vector &src1)
77 {
78 dest = vm_vec_build_add(src0, src1);
79 return dest;
80 }
81
82 vms_vector vm_vec_build_add(const vms_vector &src0, const vms_vector &src1)
83 {
84 return vms_vector{
85 .x = src0.x + src1.x,
86 .y = src0.y + src1.y,
87 .z = src0.z + src1.z,
88 };
89 }
90
91 //subs two vectors, fills in dest, returns ptr to dest
92 //ok for dest to equal either source, but should use vm_vec_sub2() if so
93 vms_vector &_vm_vec_sub(vms_vector &dest,const vms_vector &src0,const vms_vector &src1)
94 {
95 dest = vms_vector{
96 .x = src0.x - src1.x,
97 .y = src0.y - src1.y,
98 .z = src0.z - src1.z,
99 };
100 return dest;
101 }
102
103 //adds one vector to another. returns ptr to dest
104 //dest can equal source
105 void vm_vec_add2(vms_vector &dest,const vms_vector &src)
106 {
107 dest.x += src.x;
108 dest.y += src.y;
109 dest.z += src.z;
110 }
111
112 //subs one vector from another, returns ptr to dest
113 //dest can equal source
114 void vm_vec_sub2(vms_vector &dest,const vms_vector &src)
115 {
116 dest.x -= src.x;
117 dest.y -= src.y;
118 dest.z -= src.z;
119 }
120
121 //averages two vectors. returns ptr to dest
122 vms_vector vm_vec_build_avg(const vms_vector &src0, const vms_vector &src1)
123 {
124 return vms_vector{
125 .x = avg_fix({src0.x}, {src1.x}),
126 .y = avg_fix({src0.y}, {src1.y}),
127 .z = avg_fix({src0.z}, {src1.z}),
128 };
129 }
130
131 //scales a vector in place. returns ptr to vector
132 vms_vector &vm_vec_scale(vms_vector &dest, const fix s)
133 {
134 return dest = vm_vec_copy_scale(dest, {s});
135 }
136
137 //scales and copies a vector. returns scaled result
138 vms_vector vm_vec_copy_scale(const vms_vector src, const fix s)
139 {
140 return vms_vector{
141 .x = fixmul({src.x}, {s}),
142 .y = fixmul({src.y}, {s}),
143 .z = fixmul({src.z}, {s}),
144 };
145 }
146
147 //scales a vector, adds it to another, and returns it
148 //dest = src1 + k * src2
149 vms_vector vm_vec_scale_add(const vms_vector &src1, const vms_vector &src2, const fix k)
150 {
151 return vms_vector{
152 .x = src1.x + fixmul({src2.x}, {k}),
153 .y = src1.y + fixmul({src2.y}, {k}),
154 .z = src1.z + fixmul({src2.z}, {k}),
155 };
156 }
157
158 //scales a vector and adds it to another
159 //dest += k * src
160 void vm_vec_scale_add2(vms_vector &dest,const vms_vector &src,fix k)
161 {
162 dest.x += fixmul({src.x}, {k});
163 dest.y += fixmul({src.y}, {k});
164 dest.z += fixmul({src.z}, {k});
165 }
166
167 //scales a vector in place, taking n/d for scale. returns ptr to vector
168 //dest *= n/d
169 void vm_vec_scale2(vms_vector &dest,fix n,fix d)
170 {
171 #if 0 // DPH: Kludge: this was overflowing a lot, so I made it use the FPU.
172 float nd;
173 nd = f2fl(n) / f2fl(d);
174 dest.x = fl2f( f2fl(dest.x) * nd);
175 dest.y = fl2f( f2fl(dest.y) * nd);
176 dest.z = fl2f( f2fl(dest.z) * nd);
177 #endif
178 dest = vms_vector{
179 .x = fixmuldiv({dest.x}, {n}, {d}),
180 .y = fixmuldiv({dest.y}, {n}, {d}),
181 .z = fixmuldiv({dest.z}, {n}, {d}),
182 };
183 }
184
185 [[nodiscard]]
186 static fix vm_vec_dot3(fix x,fix y,fix z,const vms_vector &v)
187 {
188 const int64_t x0{x};
189 const int64_t x1{v.x};
190 const int64_t y0{y};
191 const int64_t y1{v.y};
192 const int64_t z0{z};
193 const int64_t z1{v.z};
194 const int64_t p{(x0 * x1) + (y0 * y1) + (z0 * z1)};
195 /* Convert back to fix and return. */
196 return p >> 16;
197 }
198
199 fix vm_vec_build_dot(const vms_vector &v0,const vms_vector &v1)
200 {
201 return vm_vec_dot3({v0.x}, {v0.y}, {v0.z}, v1);
202 }
203
204 vm_magnitude_squared vm_vec_mag2(const vms_vector &v)
205 {
206 const int64_t x{v.x};
207 const int64_t y{v.y};
208 const int64_t z{v.z};
209 return vm_magnitude_squared{static_cast<uint64_t>((x * x) + (y * y) + (z * z))};
210 }
211
212 vm_magnitude vm_vec_mag(const vms_vector &v)
213 {
214 return vm_magnitude{quad_sqrt(quadint{static_cast<int64_t>(vm_vec_mag2(v))})};
215 }
216
217 //computes the distance between two points. (does sub and mag)
218 vm_distance vm_vec_dist(const vms_vector &v0,const vms_vector &v1)
219 {
220 return vm_vec_mag(vm_vec_build_sub(v0, v1));
221 }
222
223 vm_distance_squared vm_vec_dist2(const vms_vector &v0,const vms_vector &v1)
224 {
225 return build_vm_distance_squared(vm_vec_mag2(vm_vec_build_sub(v0, v1)));
226 }
227
228 //computes an approximation of the magnitude of the vector
229 //uses dist = largest + next_largest*3/8 + smallest*3/16
230 vm_magnitude vm_vec_mag_quick(const vms_vector &v)
231 {
232 fix a{std::abs(v.x)};
233 fix b{std::abs(v.y)};
234
235 if (a < b) {
236 std::swap(a, b);
237 }
238
239 fix c{std::abs(v.z)};
240 if (b < c) {
241 std::swap(b, c);
242 if (a < b) {
243 std::swap(a, b);
244 }
245 }
246 const fix bc{(b >> 2) + (c >> 3)};
247 return vm_magnitude{static_cast<uint32_t>(a + bc + (bc>>1))};
248 }
249
250
251 //computes an approximation of the distance between two points.
252 //uses dist = largest + next_largest*3/8 + smallest*3/16
253 vm_distance vm_vec_dist_quick(const vms_vector &v0,const vms_vector &v1)
254 {
255 return vm_vec_mag_quick(vm_vec_build_sub(v0, v1));
256 }
257
258 //normalize a vector. returns mag of source vec
259 vm_magnitude vm_vec_copy_normalize(vms_vector &dest,const vms_vector &src)
260 {
261 const auto m{vm_vec_mag(src)};
262 if (likely(m)) {
263 dest = vm_vec_build_divide(src, m);
264 }
265 return m;
266 }
267
268 //normalize a vector. returns mag of source vec
269 vm_magnitude vm_vec_normalize(vms_vector &v)
270 {
271 return vm_vec_copy_normalize(v,v);
272 }
273
274 //normalize a vector. returns mag of source vec. uses approx mag
275 vm_magnitude vm_vec_copy_normalize_quick(vms_vector &dest,const vms_vector &src)
276 {
277 const auto m{vm_vec_mag_quick(src)};
278 if (likely(m)) {
279 dest = vm_vec_build_divide(src, m);
280 }
281 return m;
282 }
283
284 //normalize a vector. returns 1/mag of source vec. uses approx 1/mag
285 vm_magnitude vm_vec_normalize_quick(vms_vector &v)
286 {
287 return vm_vec_copy_normalize_quick(v,v);
288 }
289
290 //return the normalized direction vector between two points
291 //dest = normalized(end - start). Returns 1/mag of direction vector
292 //NOTE: the order of the parameters matches the vector subtraction
293 vm_magnitude vm_vec_normalized_dir_quick(vms_vector &dest,const vms_vector &end,const vms_vector &start)
294 {
295 return vm_vec_normalize_quick(vm_vec_sub(dest,end,start));
296 }
297
298 //return the normalized direction vector between two points
299 //dest = normalized(end - start). Returns mag of direction vector
300 //NOTE: the order of the parameters matches the vector subtraction
301 vm_magnitude vm_vec_normalized_dir(vms_vector &dest,const vms_vector &end,const vms_vector &start)
302 {
303 return vm_vec_normalize(vm_vec_sub(dest,end,start));
304 }
305
306 //computes surface normal from three points. result is normalized
307 vms_vector vm_vec_normal(const vms_vector &p0, const vms_vector &p1, const vms_vector &p2)
308 {
309 return vm_vec_normalized(vm_vec_perp(p0, p1, p2));
310 }
311
312 namespace {
313
314 //make sure a vector is reasonably sized to go into a cross product
315 [[nodiscard]]
316 static vms_vector check_vec(vms_vector v)
317 {
318 if (unlikely(v == vms_vector{}))
319 return v;
320 int cnt{0};
321
322 fix check = labs(v.x) | labs(v.y) | labs(v.z);
323 if (check & 0xfffc0000) { //too big
324
325 while (check & 0xfff00000) {
326 cnt += 4;
327 check >>= 4;
328 }
329
330 while (check & 0xfffc0000) {
331 cnt += 2;
332 check >>= 2;
333 }
334
335 v.x >>= cnt;
336 v.y >>= cnt;
337 v.z >>= cnt;
338 }
339 else //maybe too small
340 if ((check & 0xffff8000) == 0) { //yep, too small
341
342 while ((check & 0xfffff000) == 0) {
343 cnt += 4;
344 check <<= 4;
345 }
346
347 while ((check & 0xffff8000) == 0) {
348 cnt += 2;
349 check <<= 2;
350 }
351
352 v.x >>= cnt;
353 v.y >>= cnt;
354 v.z >>= cnt;
355 }
356 return v;
357 }
358
359 }
360
361 //computes cross product of two vectors.
362 //Note: this magnitude of the resultant vector is the
363 //product of the magnitudes of the two source vectors. This means it is
364 //quite easy for this routine to overflow and underflow. Be careful that
365 //your inputs are ok.
366 vms_vector vm_vec_cross(const vms_vector &src0, const vms_vector &src1)
367 {
368 const auto qx0{fixmulaccum({}, src0.y, src1.z)};
369 const auto qx1{fixmulaccum(qx0, -src0.z, src1.y)};
370
371 const auto qy0{fixmulaccum({}, src0.z, src1.x)};
372 const auto qy1{fixmulaccum(qy0, -src0.x, src1.z)};
373
374 const auto qz0{fixmulaccum({}, src0.x, src1.y)};
375 const auto qz1{fixmulaccum(qz0, -src0.y, src1.x)};
376 return vms_vector{
377 .x = fixquadadjust(qx1),
378 .y = fixquadadjust(qy1),
379 .z = fixquadadjust(qz1),
380 };
381 }
382
383 //computes non-normalized surface normal from three points.
384 vms_vector vm_vec_perp(const vms_vector &p0, const vms_vector &p1, const vms_vector &p2)
385 {
386 auto t0{check_vec(vm_vec_build_sub(p1, p0))};
387 auto t1{check_vec(vm_vec_build_sub(p2, p1))};
388 return vm_vec_cross(t0, t1);
389 }
390
391 //computes the delta angle between two vectors.
392 //vectors need not be normalized. if they are, call vm_vec_delta_ang_norm()
393 //the forward vector (third parameter) can be NULL, in which case the absolute
394 //value of the angle in returned. Otherwise the angle around that vector is
395 //returned.
396 fixang vm_vec_delta_ang(const vms_vector &v0,const vms_vector &v1,const vms_vector &fvec)
397 {
398 vms_vector t0,t1;
399
400 if (!vm_vec_copy_normalize(t0,v0) || !vm_vec_copy_normalize(t1,v1))
401 return 0;
402
403 return vm_vec_delta_ang_norm(t0,t1,fvec);
404 }
405
406 //computes the delta angle between two normalized vectors.
407 fixang vm_vec_delta_ang_norm(const vms_vector &v0,const vms_vector &v1,const vms_vector &fvec)
408 {
409 fixang a{fix_acos(vm_vec_build_dot(v0, v1))};
410 if (vm_vec_build_dot(vm_vec_cross(v0, v1), fvec) < 0)
411 a = -a;
412 return a;
413 }
414
415 namespace {
416
417 [[nodiscard]]
418 static vms_matrix vm_matrix_build_from_sincos(const fixang bank, const fix_sincos_result p, const fix_sincos_result h)
419 {
420 vms_matrix m;
421 #define DXX_S2M_DECL(V) \
422 const auto &sin##V = V.sin; \
423 const auto &cos##V = V.cos
424 DXX_S2M_DECL(p);
425 DXX_S2M_DECL(h);
426 m.fvec.y = -sinp; //m6
427 m.fvec.x = fixmul(sinh,cosp); //m3
428 m.fvec.z = fixmul(cosh,cosp); //m9
429 const auto &&b = fix_sincos(bank);
430 DXX_S2M_DECL(b);
431 #undef DXX_S2M_DECL
432 m.rvec.y = fixmul(sinb,cosp); //m4
433 m.uvec.y = fixmul(cosb,cosp); //m5
434
435 const auto cbch{fixmul(cosb, cosh)};
436 const auto sbsh{fixmul(sinb, sinh)};
437 m.rvec.x = cbch + fixmul(sinp,sbsh); //m1
438 m.uvec.z = sbsh + fixmul(sinp,cbch); //m8
439
440 const auto sbch{fixmul(sinb, cosh)};
441 const auto cbsh{fixmul(cosb, sinh)};
442 m.uvec.x = fixmul(sinp,cbsh) - sbch; //m2
443 m.rvec.z = fixmul(sinp,sbch) - cbsh; //m7
444 return m;
445 }
446
447 }
448
449 //computes a matrix from a set of three angles. returns ptr to matrix
450 vms_matrix vm_angles_2_matrix(const vms_angvec &a)
451 {
452 return vm_matrix_build_from_sincos(a.b, fix_sincos(a.p), fix_sincos(a.h));
453 }
454
455 #if DXX_USE_EDITOR
456 //computes a matrix from a forward vector and an angle
457 vms_matrix vm_vec_ang_2_matrix(const vms_vector &v, const fixang a)
458 {
459 const fix sinp{-v.y};
460 const fix cosp{fix_sqrt(F1_0 - fixmul(sinp, sinp))};
461 const fix sinh{fixdiv(v.x, cosp)};
462 const fix cosh{fixdiv(v.z, cosp)};
463 return vm_matrix_build_from_sincos(a, {sinp, cosp}, {sinh, cosh});
464 }
465 #endif
466
467 //computes a matrix from one or more vectors. The forward vector is required,
468 //with the other two being optional. If both up & right vectors are passed,
469 //the up vector is used. If only the forward vector is passed, a bank of
470 //zero is assumed
471 //returns ptr to matrix
472 vms_matrix vm_vector_to_matrix(const vms_vector &fvec)
473 {
474 vms_matrix m;
475 if (!vm_vec_copy_normalize(m.fvec, fvec))
476 {
477 Int3(); //forward vec should not be zero-length
478 }
479 else
480 vm_vector_to_matrix_f(m);
481 return m;
482 }
483
484 vms_matrix vm_vector_to_matrix_r(const vms_vector &fvec, const vms_vector &rvec)
485 {
486 vms_matrix m;
487 if (!vm_vec_copy_normalize(m.fvec, fvec))
488 {
489 Int3(); //forward vec should not be zero-length
490 }
491 else if (!vm_vec_copy_normalize(m.rvec, rvec) ||
492 //normalize new perpendicular vector
493 (reconstruct_at(m.uvec, vm_vec_cross, m.fvec, m.rvec), !vm_vec_normalize(m.uvec)))
494 {
495 vm_vector_to_matrix_f(m);
496 }
497 else
498 //now recompute right vector, in case it wasn't entirely perpendicular
499 reconstruct_at(m.rvec, vm_vec_cross, m.uvec, m.fvec);
500 return m;
501 }
502
503 vms_matrix vm_vector_to_matrix_u(const vms_vector &fvec, const vms_vector &uvec)
504 {
505 vms_matrix m;
506 if (!vm_vec_copy_normalize(m.fvec, fvec))
507 {
508 Int3(); //forward vec should not be zero-length
509 }
510 else if (!vm_vec_copy_normalize(m.uvec, uvec) ||
511 //normalize new perpendicular vector
512 (reconstruct_at(m.rvec, vm_vec_cross, m.uvec, m.fvec), !vm_vec_normalize(m.rvec)))
513 {
514 vm_vector_to_matrix_f(m);
515 }
516 else
517 //now recompute up vector, in case it wasn't entirely perpendicular
518 reconstruct_at(m.uvec, vm_vec_cross, m.fvec, m.rvec);
519 return m;
520 }
521
522 vms_vector vm_vec_build_rotated(const vms_vector &src, const vms_matrix &m)
523 {
524 return {
525 .x = vm_vec_build_dot(src, m.rvec),
526 .y = vm_vec_build_dot(src, m.uvec),
527 .z = vm_vec_build_dot(src, m.fvec)
528 };
529 }
530
531 //rotates a vector through a matrix.
532 void vm_vec_rotate(vms_vector &dest,const vms_vector &src,const vms_matrix &m)
533 {
534 dest = vm_vec_build_rotated(src, m);
535 }
536
537 //mulitply 2 matrices, fill in dest. returns ptr to dest
538 //dest CANNOT equal either source
539 void _vm_matrix_x_matrix(vms_matrix &dest,const vms_matrix &src0,const vms_matrix &src1)
540 {
541 dest.rvec.x = vm_vec_dot3(src0.rvec.x,src0.uvec.x,src0.fvec.x, src1.rvec);
542 dest.uvec.x = vm_vec_dot3(src0.rvec.x,src0.uvec.x,src0.fvec.x, src1.uvec);
543 dest.fvec.x = vm_vec_dot3(src0.rvec.x,src0.uvec.x,src0.fvec.x, src1.fvec);
544
545 dest.rvec.y = vm_vec_dot3(src0.rvec.y,src0.uvec.y,src0.fvec.y, src1.rvec);
546 dest.uvec.y = vm_vec_dot3(src0.rvec.y,src0.uvec.y,src0.fvec.y, src1.uvec);
547 dest.fvec.y = vm_vec_dot3(src0.rvec.y,src0.uvec.y,src0.fvec.y, src1.fvec);
548
549 dest.rvec.z = vm_vec_dot3(src0.rvec.z,src0.uvec.z,src0.fvec.z, src1.rvec);
550 dest.uvec.z = vm_vec_dot3(src0.rvec.z,src0.uvec.z,src0.fvec.z, src1.uvec);
551 dest.fvec.z = vm_vec_dot3(src0.rvec.z,src0.uvec.z,src0.fvec.z, src1.fvec);
552 }
553
554 //extract angles from a matrix
555 vms_angvec vm_extract_angles_matrix(const vms_matrix &m)
556 {
557 const fixang ah{
558 (m.fvec.x == 0 && m.fvec.z == 0)
559 //zero head
560 ? fixang{0}
561 : fix_atan2(m.fvec.z, m.fvec.x)
562 };
563
564 const auto &&[sinh, cosh] = fix_sincos(ah);
565 const auto cosp{
566 (abs(sinh) > abs(cosh))
567 //sine is larger, so use it
568 ? fixdiv(m.fvec.x, sinh)
569 //cosine is larger, so use it
570 : fixdiv(m.fvec.z, cosh)
571 };
572 return vms_angvec{
573 .p = (
574 (cosp == 0 && m.fvec.y == 0)
575 ? fixang{0}
576 : fix_atan2(cosp, -m.fvec.y)
577 ),
578 .b = (
579 (cosp == 0) //the cosine of pitch is zero. we're pitched straight up. say no bank
580 ? fixang{0}
581 : ({
582 const auto sinb{fixdiv(m.rvec.y, cosp)};
583 const auto cosb{fixdiv(m.uvec.y, cosp)};
584 (sinb == 0 && cosb == 0)
585 ? fixang{0}
586 : fix_atan2(cosb, sinb);
587 })
588 ),
589 .h = ah
590 };
591 }
592
593
594 //extract heading and pitch from a vector, assuming bank==0
595 static vms_angvec vm_extract_angles_vector_normalized(const vms_vector &v)
596 {
597 return vms_angvec{
598 .p = fix_asin(-v.y),
599 .b = 0, //always zero bank
600 .h = (v.x == 0 && v.z == 0)
601 ? fixang{0}
602 : fix_atan2(v.z, v.x)
603 };
604 }
605
606 //extract heading and pitch from a vector, assuming bank==0
607 vms_angvec vm_extract_angles_vector(const vms_vector &v)
608 {
609 vms_vector t;
610 if (vm_vec_copy_normalize(t,v))
611 return vm_extract_angles_vector_normalized(t);
612 else
613 return {};
614 }
615
616 //compute the distance from a point to a plane. takes the normalized normal
617 //of the plane (ebx), a point on the plane (edi), and the point to check (esi).
618 //returns distance in eax
619 //distance is signed, so negative dist is on the back of the plane
620 fix vm_dist_to_plane(const vms_vector &checkp,const vms_vector &norm,const vms_vector &planep)
621 {
622 return vm_vec_build_dot(vm_vec_build_sub(checkp, planep), norm);
623 }
624
625 // convert vms_matrix to vms_quaternion
626 vms_quaternion vms_quaternion_from_matrix(const vms_matrix &m)
627 {
628 vms_quaternion rq{};
629 const auto rx{m.rvec.x};
630 const auto ry{m.rvec.y};
631 const auto rz{m.rvec.z};
632 const auto ux{m.uvec.x};
633 const auto uy{m.uvec.y};
634 const auto uz{m.uvec.z};
635 const auto fx{m.fvec.x};
636 const auto fy{m.fvec.y};
637 const auto fz{m.fvec.z};
638 const fix tr{rx + uy + fz};
639 fix qw, qx, qy, qz;
640 if (tr > 0) {
641 const fix s{fixmul(fix_sqrt(tr + fl2f(1.0)), fl2f(2.0))};
642 qw = fixmul(fl2f(0.25), {s});
643 qx = fixdiv(fy - uz, {s});
644 qy = fixdiv(rz - fx, {s});
645 qz = fixdiv(ux - ry, {s});
646 } else if ((rx > uy) & (rx > fz)) {
647 const fix s{fixmul(fix_sqrt(fl2f(1.0) + rx - uy - fz), fl2f(2.0))};
648 qw = fixdiv(fy - uz, {s});
649 qx = fixmul(fl2f(0.25), {s});
650 qy = fixdiv(ry + ux, {s});
651 qz = fixdiv(rz + fx, {s});
652 } else if (uy > fz) {
653 const fix s{fixmul(fix_sqrt(fl2f(1.0) + uy - rx - fz), fl2f(2.0))};
654 qw = fixdiv(rz - fx, {s});
655 qx = fixdiv(ry + ux, {s});
656 qy = fixmul(fl2f(0.25), {s});
657 qz = fixdiv(uz + fy, {s});
658 } else {
659 const fix s{fixmul(fix_sqrt(fl2f(1.0) + fz - rx - uy), fl2f(2.0))};
660 qw = fixdiv(ux - ry, {s});
661 qx = fixdiv(rz + fx, {s});
662 qy = fixdiv(uz + fy, {s});
663 qz = fixmul(fl2f(0.25), {s});
664 }
665 rq.w = qw / 2;
666 rq.x = qx / 2;
667 rq.y = qy / 2;
668 rq.z = qz / 2;
669 return rq;
670 }
671
672 // convert vms_quaternion to vms_matrix
673 vms_matrix vms_matrix_from_quaternion(const vms_quaternion &q)
674 {
675 vms_matrix m;
676 const fix qw2{q.w * 2};
677 const fix qx2{q.x * 2};
678 const fix qy2{q.y * 2};
679 const fix qz2{q.z * 2};
680 const fix sqw{fixmul({qw2}, {qw2})};
681 const fix sqx{fixmul({qx2}, {qx2})};
682 const fix sqy{fixmul({qy2}, {qy2})};
683 const fix sqz{fixmul({qz2}, {qz2})};
684 const fix invs{fixdiv({fl2f(1.0)}, {sqw + sqx + sqy + sqz})};
685
686 m.rvec.x = fixmul(sqx - sqy - sqz + sqw, {invs});
687 m.uvec.y = fixmul(-sqx + sqy - sqz + sqw, {invs});
688 m.fvec.z = fixmul(-sqx - sqy + sqz + sqw, {invs});
689
690 const fix qxy{fixmul({qx2}, {qy2})};
691 const fix qzw{fixmul({qz2}, {qw2})};
692 m.uvec.x = fixmul(fixmul(fl2f(2.0), {qxy + qzw}), {invs});
693 m.rvec.y = fixmul(fixmul(fl2f(2.0), {qxy - qzw}), {invs});
694
695 const fix qxz{fixmul({qx2}, {qz2})};
696 const fix qyw{fixmul({qy2}, {qw2})};
697 m.fvec.x = fixmul(fixmul(fl2f(2.0), {qxz - qyw}), {invs});
698 m.rvec.z = fixmul(fixmul(fl2f(2.0), {qxz + qyw}), {invs});
699
700 const fix qyz{fixmul({qy2}, {qz2})};
701 const fix qxw{fixmul({qx2}, {qw2})};
702 m.fvec.y = fixmul(fixmul(fl2f(2.0), {qyz + qxw}), {invs});
703 m.uvec.z = fixmul(fixmul(fl2f(2.0), {qyz - qxw}), {invs});
704 return m;
705 }
706
707 }