The open source OpenXR runtime

m/mat4: Add Matrix 4x4 f64 functions

+216
+92
src/xrt/auxiliary/math/m_base.cpp
··· 40 40 return copy(*q); 41 41 } 42 42 43 + XRT_MAYBE_UNUSED static inline Eigen::Quaterniond 44 + copyd(const struct xrt_quat &q) 45 + { 46 + // Eigen constructor order is different from XRT, OpenHMD and OpenXR! 47 + // Eigen: `float w, x, y, z`. 48 + // OpenXR: `float x, y, z, w`. 49 + return Eigen::Quaterniond(q.w, q.x, q.y, q.z); 50 + } 51 + 52 + XRT_MAYBE_UNUSED static inline Eigen::Quaterniond 53 + copyd(const struct xrt_quat *q) 54 + { 55 + return copyd(*q); 56 + } 57 + 43 58 static inline Eigen::Vector3f 44 59 copy(const struct xrt_vec3 &v) 45 60 { ··· 50 65 copy(const struct xrt_vec3 *v) 51 66 { 52 67 return copy(*v); 68 + } 69 + 70 + XRT_MAYBE_UNUSED static inline Eigen::Vector3d 71 + copyd(const struct xrt_vec3 &v) 72 + { 73 + return Eigen::Vector3d(v.x, v.y, v.z); 74 + } 75 + 76 + XRT_MAYBE_UNUSED static inline Eigen::Vector3d 77 + copyd(const struct xrt_vec3 *v) 78 + { 79 + return copyd(*v); 53 80 } 54 81 55 82 static inline Eigen::Matrix3f ··· 76 103 // clang-format on 77 104 return res; 78 105 } 106 + 79 107 80 108 /* 81 109 * ··· 416 444 Eigen::Matrix4f vp = copy(projection) * v3; 417 445 map_matrix_4x4(*result) = vp.inverse(); 418 446 } 447 + 448 + 449 + /* 450 + * 451 + * Exported Matrix 4x4 functions. 452 + * 453 + */ 454 + 455 + extern "C" void 456 + m_mat4_f64_identity(struct xrt_matrix_4x4_f64 *result) 457 + { 458 + map_matrix_4x4_f64(*result) = Eigen::Matrix4d::Identity(); 459 + } 460 + 461 + extern "C" void 462 + m_mat4_f64_invert(const struct xrt_matrix_4x4_f64 *matrix, struct xrt_matrix_4x4_f64 *result) 463 + { 464 + Eigen::Matrix4d m = map_matrix_4x4_f64(*matrix); 465 + map_matrix_4x4_f64(*result) = m.inverse(); 466 + } 467 + 468 + extern "C" void 469 + m_mat4_f64_multiply(const struct xrt_matrix_4x4_f64 *left, 470 + const struct xrt_matrix_4x4_f64 *right, 471 + struct xrt_matrix_4x4_f64 *result) 472 + { 473 + Eigen::Matrix4d l = map_matrix_4x4_f64(*left); 474 + Eigen::Matrix4d r = map_matrix_4x4_f64(*right); 475 + 476 + map_matrix_4x4_f64(*result) = l * r; 477 + } 478 + 479 + extern "C" void 480 + m_mat4_f64_orientation(const struct xrt_quat *quat, struct xrt_matrix_4x4_f64 *result) 481 + { 482 + map_matrix_4x4_f64(*result) = Eigen::Affine3d(copyd(*quat)).matrix(); 483 + } 484 + 485 + extern "C" void 486 + m_mat4_f64_model(const struct xrt_pose *pose, const struct xrt_vec3 *size, struct xrt_matrix_4x4_f64 *result) 487 + { 488 + Eigen::Vector3d position = copyd(pose->position); 489 + Eigen::Quaterniond orientation = copyd(pose->orientation); 490 + 491 + auto scale = Eigen::Scaling(copyd(size)); 492 + 493 + Eigen::Translation3d translation(position); 494 + Eigen::Affine3d transformation = translation * orientation * scale; 495 + 496 + map_matrix_4x4_f64(*result) = transformation.matrix(); 497 + } 498 + 499 + extern "C" void 500 + m_mat4_f64_view(const struct xrt_pose *pose, struct xrt_matrix_4x4_f64 *result) 501 + { 502 + Eigen::Vector3d position = copyd(pose->position); 503 + Eigen::Quaterniond orientation = copyd(pose->orientation); 504 + 505 + Eigen::Translation3d translation(position); 506 + Eigen::Affine3d transformation = translation * orientation; 507 + 508 + map_matrix_4x4_f64(*result) = transformation.matrix().inverse(); 509 + } 510 + 419 511 420 512 /* 421 513 *
+24
src/xrt/auxiliary/math/m_eigen_interop.hpp
··· 97 97 return Eigen::Map<Eigen::Matrix4f>(m.v); 98 98 } 99 99 100 + /*! 101 + * @brief Wrap an internal 4x4 matrix f64 struct in an Eigen type, const overload. 102 + * 103 + * Permits zero-overhead manipulation of `const xrt_matrix_4x4_f64&` by Eigen routines as if it were a 104 + * `const Eigen::Matrix4d&`. 105 + */ 106 + static inline Eigen::Map<const Eigen::Matrix4d> 107 + map_matrix_4x4_f64(const struct xrt_matrix_4x4_f64 &m) 108 + { 109 + return Eigen::Map<const Eigen::Matrix4d>(m.v); 110 + } 111 + 112 + /*! 113 + * @brief Wrap an internal 4x4 matrix struct in an Eigen type, non-const overload. 114 + * 115 + * Permits zero-overhead manipulation of `xrt_matrix_4x4_f64&` by Eigen routines as if it were a `Eigen::Matrix4d&`. 116 + */ 117 + static inline Eigen::Map<Eigen::Matrix4d> 118 + map_matrix_4x4_f64(struct xrt_matrix_4x4_f64 &m) 119 + { 120 + return Eigen::Map<Eigen::Matrix4d>(m.v); 121 + } 122 + 123 + 100 124 /* 101 125 * 102 126 * Pose deconstruction helpers.
+100
src/xrt/auxiliary/math/m_matrix_4x4_f64.h
··· 1 + // Copyright 2019-2021, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief C matrix 4x4 f64 math library. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * 8 + * @see xrt_matrix_4x4_f64 9 + * @ingroup aux_math 10 + */ 11 + 12 + #pragma once 13 + 14 + #include "xrt/xrt_defines.h" 15 + 16 + #include "m_mathinclude.h" 17 + 18 + 19 + #ifdef __cplusplus 20 + extern "C" { 21 + #endif 22 + 23 + 24 + /*! 25 + * Initialize Matrix4x4 F64 with identity. 26 + * 27 + * @relates xrt_matrix_4x4_f64 28 + * @ingroup aux_math 29 + */ 30 + void 31 + m_mat4_f64_identity(struct xrt_matrix_4x4_f64 *result); 32 + 33 + /*! 34 + * Invert a Matrix4x4 F64. 35 + * 36 + * @relates xrt_matrix_4x4_f64 37 + * @ingroup aux_math 38 + */ 39 + void 40 + m_mat4_f64_invert(const struct xrt_matrix_4x4_f64 *matrix, struct xrt_matrix_4x4_f64 *result); 41 + 42 + /*! 43 + * Multiply Matrix4x4 F64. 44 + * 45 + * @relates xrt_matrix_4x4_f64 46 + * @ingroup aux_math 47 + */ 48 + void 49 + m_mat4_f64_multiply(const struct xrt_matrix_4x4_f64 *left, 50 + const struct xrt_matrix_4x4_f64 *right, 51 + struct xrt_matrix_4x4_f64 *result); 52 + 53 + /*! 54 + * Initialize Matrix4x4 F64 with a orientation. 55 + * 56 + * @relates xrt_matrix_4x4_f64 57 + * @ingroup aux_math 58 + */ 59 + void 60 + m_mat4_f64_orientation(const struct xrt_quat *quat, struct xrt_matrix_4x4_f64 *result); 61 + 62 + /*! 63 + * Initialize Matrix4x4 F64 with a pose and size that can be used as a model matrix. 64 + * 65 + * @relates xrt_matrix_4x4_f64 66 + * @ingroup aux_math 67 + */ 68 + void 69 + m_mat4_f64_model(const struct xrt_pose *pose, const struct xrt_vec3 *size, struct xrt_matrix_4x4_f64 *result); 70 + 71 + /*! 72 + * Initialize Matrix4x4 F64 with a pose that can be used as a view martix. 73 + * 74 + * @relates xrt_matrix_4x4_f64 75 + * @ingroup aux_math 76 + */ 77 + void 78 + m_mat4_f64_view(const struct xrt_pose *pose, const struct xrt_vec3 *size, struct xrt_matrix_4x4_f64 *result); 79 + 80 + 81 + #ifdef __cplusplus 82 + } 83 + 84 + 85 + static inline struct xrt_matrix_4x4_f64 // Until clang-format-11 is on the CI. 86 + operator*(const struct xrt_matrix_4x4_f64 &a, const struct xrt_matrix_4x4_f64 &b) 87 + { 88 + struct xrt_matrix_4x4_f64 ret = {{0}}; 89 + m_mat4_f64_multiply(&l, &r, &ret); 90 + return ret; 91 + } 92 + 93 + static inline void 94 + operator*=(struct xrt_matrix_4x4_f64 &a, const struct xrt_matrix_4x4_f64 &b) 95 + { 96 + a = a * b; 97 + } 98 + 99 + 100 + #endif