|
| 1 | +/* |
| 2 | + * smath - Single-file linear algebra math library for C++23. |
| 3 | + * |
| 4 | + * Copyright 2025 Slendi <slendi@socopon.com> |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <Eigen/Core> |
| 22 | +#include <Eigen/Geometry> |
| 23 | + |
| 24 | +#include <smath/smath.hpp> |
| 25 | + |
| 26 | +namespace smath |
| 27 | +{ |
| 28 | + |
| 29 | +template<class Scalar, int N, int Options, int MaxRows, int MaxCols> |
| 30 | +requires(std::is_arithmetic_v<Scalar> && (N > 0)) |
| 31 | +struct interop_adapter<Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols>> |
| 32 | +{ |
| 33 | + using smath_type = Vec<static_cast<std::size_t>(N), Scalar>; |
| 34 | + |
| 35 | + static constexpr auto to_smath( |
| 36 | + Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols> const &v) |
| 37 | + -> smath_type |
| 38 | + { |
| 39 | + smath_type out {}; |
| 40 | + for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i) |
| 41 | + out[i] = v(static_cast<int>(i)); |
| 42 | + return out; |
| 43 | + } |
| 44 | + |
| 45 | + static constexpr auto from_smath(smath_type const &v) |
| 46 | + -> Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols> |
| 47 | + { |
| 48 | + Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols> out; |
| 49 | + for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i) |
| 50 | + out(static_cast<int>(i)) = v[i]; |
| 51 | + return out; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +template<class Scalar, int N, int Options, int MaxRows, int MaxCols> |
| 56 | +requires(std::is_arithmetic_v<Scalar> && (N > 0)) |
| 57 | +struct interop_adapter<Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols>> |
| 58 | +{ |
| 59 | + using smath_type = Vec<static_cast<std::size_t>(N), Scalar>; |
| 60 | + |
| 61 | + static constexpr auto to_smath( |
| 62 | + Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols> const &v) |
| 63 | + -> smath_type |
| 64 | + { |
| 65 | + smath_type out {}; |
| 66 | + for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i) |
| 67 | + out[i] = v(static_cast<int>(i)); |
| 68 | + return out; |
| 69 | + } |
| 70 | + |
| 71 | + static constexpr auto from_smath(smath_type const &v) |
| 72 | + -> Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols> |
| 73 | + { |
| 74 | + Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols> out; |
| 75 | + for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i) |
| 76 | + out(static_cast<int>(i)) = v[i]; |
| 77 | + return out; |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +template<class Scalar, int R, int C, int Options, int MaxRows, int MaxCols> |
| 82 | +requires(std::is_arithmetic_v<Scalar> && (R > 1) && (C > 1)) |
| 83 | +struct interop_adapter<Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols>> |
| 84 | +{ |
| 85 | + using smath_type |
| 86 | + = Mat<static_cast<std::size_t>(R), static_cast<std::size_t>(C), Scalar>; |
| 87 | + |
| 88 | + static constexpr auto to_smath( |
| 89 | + Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols> const &m) |
| 90 | + -> smath_type |
| 91 | + { |
| 92 | + smath_type out {}; |
| 93 | + for (std::size_t c = 0; c < static_cast<std::size_t>(C); ++c) { |
| 94 | + for (std::size_t r = 0; r < static_cast<std::size_t>(R); ++r) |
| 95 | + out[r, c] = m(static_cast<int>(r), static_cast<int>(c)); |
| 96 | + } |
| 97 | + return out; |
| 98 | + } |
| 99 | + |
| 100 | + static constexpr auto from_smath(smath_type const &m) |
| 101 | + -> Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols> |
| 102 | + { |
| 103 | + Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols> out; |
| 104 | + for (std::size_t c = 0; c < static_cast<std::size_t>(C); ++c) { |
| 105 | + for (std::size_t r = 0; r < static_cast<std::size_t>(R); ++r) |
| 106 | + out(static_cast<int>(r), static_cast<int>(c)) = m[r, c]; |
| 107 | + } |
| 108 | + return out; |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +template<class Scalar, int Options> |
| 113 | +requires std::is_arithmetic_v<Scalar> |
| 114 | +struct interop_adapter<Eigen::Quaternion<Scalar, Options>> |
| 115 | +{ |
| 116 | + using smath_type = Quaternion<Scalar>; |
| 117 | + |
| 118 | + static constexpr auto to_smath(Eigen::Quaternion<Scalar, Options> const &q) |
| 119 | + -> smath_type |
| 120 | + { |
| 121 | + return { q.x(), q.y(), q.z(), q.w() }; |
| 122 | + } |
| 123 | + |
| 124 | + static constexpr auto from_smath(smath_type const &q) |
| 125 | + -> Eigen::Quaternion<Scalar, Options> |
| 126 | + { |
| 127 | + return { q.w(), q.x(), q.y(), q.z() }; |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace smath |
0 commit comments